码迷,mamicode.com
首页 > 编程语言 > 详细

C语言实现管道通讯

时间:2017-10-14 17:04:28      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:led   return   code   技术   nbsp   types.h   es2017   while   eof   

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,效果如下图:技术分享 

C语言实现管道通讯

标签:led   return   code   技术   nbsp   types.h   es2017   while   eof   

原文地址:http://www.cnblogs.com/ysjd/p/7667126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!