写:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[1024] = "";
if(argc != 2)
{
fprintf(stderr,"usage:%s fifo\n",argv[0]);
return -1;
}
// if(mkfifo(argv[1],0666) < 0 && errno == EEXIST)
// {
// perror("Fail to mkfifo");
// return -1;
// }
fd = open(argv[1],O_WRONLY);
while(1)
{
fgets(buf, sizeof(buf), stdin);
write(fd, buf, sizeof(buf));
if(!strncmp(buf, "quit", 4))
{
exit(0);
}
}
return 0;
}
读:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[1024] = "";
if(argc != 2)
{
fprintf(stderr,"usage:%s fifo\n",argv[0]);
return -1;
}
// if(mkfifo(argv[1],0666) < 0 && errno == EEXIST)
// {
// perror("Fail to mkfifo");
// return -1;
// }
fd = open(argv[1],O_RDONLY);
while(1)
{
read(fd, buf, sizeof(buf));
if(!strncmp(buf, "quit", 4))
{
exit(0);
}
printf("read:%s\n",buf);
}
return 0;
}
结果: