C语言实现管道通讯

mkfifo.c文件

 1 #include<sys/types.h>
 2 #include<sys/stat.h>
 3 #include<stdio.h>
 4 #include<errno.h>
 5 
 6 int main()
 7 {
 8     //int mkfifo(const char *pathname, mode_t mode);
 9 
10     int ret=mkfifo("./test",0777);
11     if(ret<0)
12     {
13         if(errno==EEXIST)
14         {
15             printf("create error errno=%d\n",errno);
16             return -1;
17         }
18     }
19 }

link.h文件

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<unistd.h>
 5 typedef struct student
 6 {
 7     int id;
 8     char name[20];
 9     struct student *next;
10 }STU,*Pstu;
11 
12 Pstu add_node(Pstu head);
13 void show_link(Pstu head);
14 void write_link(Pstu head,int fd);
15 Pstu read_link(int fd);

link.c文件

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include"link.h"
 5 
 6 Pstu add_node(Pstu head)
 7 {
 8     Pstu temp=(Pstu)malloc(sizeof(STU));
 9     printf("please input id:");
10     scanf("%d",&temp->id);
11     printf("please input name:");
12     scanf("%s",temp->name);
13     temp->next=head;
14     return temp;
15 }
16 
17 void show_link(Pstu head)
18 {
19     printf("id\tname\n");
20     while(head!=NULL)
21     {
22         printf("%d\t%s\n",head->id,head->name);
23         head=head->next;
24     }
25 }
26 Pstu read_link(int fd)
27 {
28     int ret;
29     Pstu temp,head=NULL;
30     while(1)
31     {
32         temp=(Pstu)malloc(sizeof(STU));
33         ret=read(fd,temp,sizeof(STU));
34         if(ret<0)
35         {
36             printf("read error\n");
37             return NULL;
38         }
39         else if(ret==0)
40             return head;
41         else
42         {
43             temp->next=head;
44             head=temp;
45         }
46     }
47 }
48 void write_link(Pstu head,int fd)
49 {
50     while(head!=NULL)
51     {
52         write(fd,head,sizeof(STU));
53         head=head->next;
54     }
55 }

write.c文件

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<sys/types.h>
 4 #include<sys/stat.h>
 5 #include<fcntl.h>
 6 #include<string.h>
 7 #include"link.h"
 8 int main()
 9 {
10     int fd;
11     fd=open("./test",O_WRONLY);
12     if(fd<0)
13     {
14         printf("open filed\n");
15         return -1;
16     }
17     Pstu head=NULL;
18     for(int i=0;i<5;i++)
19     {
20         head=add_node(head);
21     }
22     show_link(head);
23 
24     write_link(head,fd);
25     close(fd);
26 }

read.c文件

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<sys/types.h>
 4 #include<sys/stat.h>
 5 #include<fcntl.h>
 6 #include"link.h"
 7 
 8 int main()
 9 {
10     int fd;
11     fd=open("./test",O_RDONLY);
12     if(fd<0)
13     {
14         printf("open failed\n");
15         return -1;
16     }
17 
18     Pstu head=read_link(fd);
19     show_link(head);
20 
21     close(fd);
22 }

gcc mkfifo.c

./aou.t 生成 test

gcc link.c write.c -o write

gcc link.c read.c -o read

运行read和write,效果如下图: 

posted @ 2017-10-14 13:56  时光淡了旧人心  阅读(1580)  评论(0编辑  收藏  举报