C語言實現管道通訊

來源:互聯網
上載者:User

標籤: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         else42         {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語言實現管道通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.