代码示例_IPC_有名管道

有名管道

 


 

1.client

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <strings.h>
 6 #include <sys/stat.h>
 7 #include <sys/types.h>
 8 #include <fcntl.h>
 9 
10 int main(void)
11 {
12     if(mkfifo("./fifo",0666)<0){       // 创建有名管道
13         perror("mkfifo");
14         exit(1);
15     }
16 
17     int fd = open("./fifo",O_WRONLY);  // 打开有名管道
18     if(fd<0){
19         perror("open failed");
20         exit(1);
21     }
22 
23     char buf[1024];
24 
25     while(1){            //
26     bzero (buf,1024);
27     fgets(buf,1024,stdin);
28     write(fd,buf,1024);
29         if(strncmp("quit",buf,4)==0)
30             break;
31     }
32 
33     close(fd);
34 
35     return 0 ;
36 }

 

 

 2.server

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <strings.h>
 6 #include <sys/stat.h>
 7 #include <sys/types.h>
 8 #include <fcntl.h>
 9 
10 int main(void)
11 {
12 
13     int fd = open("./fifo",O_RDWR);    // 打开有名管道
14     if(fd<0){
15         perror("open failed");
16         exit(1);
17     }
18 
19     char buf[1024];
20 
21     while(1){            //
22        bzero (buf,1024);     
23     read(fd,buf,1024);
24     printf("read :%s\n",buf);
25     if(strncmp(buf,"quit",4)==0)
26          break;
27     }
28 
29     close(fd);
30 
31     return 0;
32 }

 

 

 

 

 


 

 

 

success !

 

转载于:https://www.cnblogs.com/panda-w/p/11049430.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值