write_fifo.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <syspes.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 int main(int argc, const char *argv[])
10 {
11 int fd;
12 char buf[1024] = "";
13 if(argc != 2)
14 {
15 fprintf(stderr,"usage:%s\n",argv[0]);
16 return -1;
17 }
18 fd = open(argv[1],O_WRONLY);
19 if(fd < 0)
20 {
21
22 perror("fail to open");
23 return -1;
24 }
25 while(1)
26 {
27
28 fgets(buf,sizeof(buf),stdin);
29 write(fd,buf,strlen(buf));
30 if(!strncmp(buf,"quit",4))
31 {
32 exit(0);
33 }
34 }
35
36 return 0;
37 }
read_fifo.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <syspes.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10
11 int main(int argc, const char *argv[])
12 {
13 char buf[1024];
14 int fd;
15 if(argc != 2)
16 {
17 fprintf(stderr,"usage:%s fifo",argv[0]);
18 return -1;
19
20 }
21 fd = open(argv[1],O_RDONLY);
22 if(fd < 0)
23 {
24 perror("fail to open");
25 return -1;
26 }
27 while(1)
28 {
29 memset(buf,0,sizeof(buf));
30 read(fd,buf,sizeof(buf));
31 printf("read>>%s",buf);
32 if(!strncmp(buf,"quit",4))
33 {
34 exit(0);
35 }
36 }
37 return 0;
38 }