linux 进程和线程或线程和线程之间通过管道通信(pipe)

 

linux 进程和线程或线程和线程之间通过管道通信(pipe)

转自:http://blog.csdn.net/robertkun/article/details/8095331

线程间通信:

[cpp]  view plain  copy
  1. #include <stdio.h>  // printf  
  2. #include <stdlib.h> // exit  
  3. #include <unistd.h> // pipe  
  4. #include <string.h> // strlen  
  5. #include <pthread.h> // pthread_create  
  6.   
  7. using namespace std;  
  8.   
  9. void *func(void * fd)  
  10. {  
  11.         printf("write fd = %d\n", *(int*)fd);  
  12.         char str[] = "hello everyone!";  
  13.         write( *(int*)fd, str, strlen(str) );  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.         int fd[2];  
  19.         char readbuf[1024];  
  20.   
  21.         if(pipe(fd) < 0)  
  22.         {  
  23.                 printf("pipe error!\n");  
  24.         }  
  25.   
  26.         // create a new thread  
  27.         pthread_t tid = 0;  
  28.         pthread_create(&tid, NULL, func, &fd[1]);  
  29.         pthread_join(tid, NULL);  
  30.   
  31.         sleep(3);  
  32.   
  33.         // read buf from child thread  
  34.         read( fd[0], readbuf, sizeof(readbuf) );  
  35.         printf("read buf = %s\n", readbuf);  
  36.   
  37.         return 0;  
  38. }  
[cpp]  view plain  copy
  1. // 输出结果  
[cpp]  view plain  copy
  1. write fd = 4  
  2. read buf = hello everyone!�  


进程间通信:

[cpp]  view plain  copy
  1. #include <stdio.h>  // printf  
  2. #include <stdlib.h> // exit  
  3. #include <unistd.h> // pipe  
  4. #include <string.h> // strlen  
  5. #include <pthread.h> // pthread_create  
  6.   
  7. using namespace std;  
  8.   
  9. int main()  
  10. {  
  11.         int fd[2];  
  12.         int pid = 0;  
  13.         char str[] = "hello";  
  14.         char readbuf[1024];  
  15.   
  16.         if(pipe(fd) < 0)  
  17.         {  
  18.                 printf("pipe error!\n");  
  19.         }  
  20.   
  21.         if((pid = fork()) < 0)  
  22.         {  
  23.                 printf("fork error!\n");  
  24.         }  
  25.         else if(pid == 0)  
  26.         {  
  27.                 printf("child process!\n");  
  28.   
  29.                 // close read channel  
  30.                 close(fd[0]);  
  31.                 write(fd[1], str, strlen(str));  
  32.         }  
  33.         else  
  34.         {  
  35.                 printf("father process!\n");  
  36.   
  37.                 // close write channel  
  38.                 close(fd[1]);  
  39.                 read(fd[0], readbuf, sizeof(readbuf));  
  40.                 printf("readbuf = %s\n", readbuf);  
  41.         }  
  42.   
  43.         return 0;  
  44. }  
[cpp]  view plain  copy
  1. // 输出结果:  
[cpp]  view plain  copy
  1. father process!  
  2. child process!  
  3. readbuf = hello  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值