分享

C语言进程间通信(一)

 yggzs2002 2017-06-20

 

进程间通信(IPC)是指在不同进程之间传递信息。linux的进程通信方式有管道,消息队列,信号量,共享内存,套接口等方式,下面一一整理。

首先是管道(PIPE),管道是Unix系统IPC最古老的方式,所有的Unix系统都提供这种通信机制。它的优点在于简单易用,缺点在于有限制,详细见下面几点:

 

  • 只能用于父子进程或兄弟进程之间通信
  • 大多数系统中都是半双工的,数据信息只能单向流动,如果需要双向通信则需要建立两个管道
  • 传输的是无格式字节流,需要双方约定格式
  • 管道缓冲区是有限的,等等
首先来看父子进程之间通信的例子。
C代码  收藏代码
  1. #include<stdio.h>  
  2. #include<limits.h>  
  3. #include<sys/types.h>  
  4. #include<string.h>  
  5. #include<stdlib.h>  
  6.   
  7. #define BUFSIZE PIPE_BUF  //管道默认一次性读取的数据长度  
  8.   
  9. void err_quit(char *err) {  
  10.         printf("%s\n",err);  
  11.         exit(-1);  
  12. }  
  13.   
  14. int main()  
  15. {  
  16.         int fd[2];  
  17.         char buf[BUFSIZE] = "hello my son";  
  18.         pid_t pid;  
  19.   
  20.         if (pipe(fd) < 0) {  
  21.                 err_quit("pipe error");  
  22.         }  
  23.         if ((pid = fork()) < 0) {  
  24.                 err_quit("fork error");  
  25.         }  
  26.         else if(pid > 0) { //父进程  
  27.                 close(fd[0]);  
  28.                 write(fd[1], buf, strlen(buf));  
  29.         }  
  30.         else {  
  31.                 close(fd[1]);  
  32.                 int len = read(fd[0], buf, BUFSIZE);  
  33.                 if (len < 0) {  
  34.                         err_quit("read error");  
  35.                 }  
  36.                 printf("Get : %s\n",buf);  
  37.         }  
  38.         return 0;  
  39. }  
 接下来是兄弟进程之间通信的例子。
C代码  收藏代码
  1. #include<stdio.h>  
  2. #include<limits.h>  
  3. #include<sys/types.h>  
  4. #include<string.h>  
  5. #include<stdlib.h>  
  6.   
  7. #define BUFSIZE PIPE_BUF  //管道默认一次性读取的数据长度  
  8.   
  9. void err_quit(char *err) {  
  10.     printf("%s\n",err);  
  11.     exit(-1);  
  12. }  
  13.   
  14. int main()  
  15. {  
  16.     int fd[2];  
  17.     char buf[BUFSIZE] = "hello my brother";  
  18.     pid_t pid;  
  19.   
  20.     if (pipe(fd) < 0) {  
  21.         err_quit("pipe error");  
  22.     }  
  23.     if ((pid = fork()) < 0) {  
  24.         err_quit("fork error");  
  25.     }  
  26.     else if(pid > 0) { //父进程  
  27.   
  28.         if ((pid = fork()) < 0) {  
  29.             err_quit("fork error");  
  30.         }  
  31.         else if (pid > 0) { //父进程  
  32.   
  33.         }  
  34.         else {  
  35.             close(fd[0]);  
  36.             write(fd[1], buf, strlen(buf));  
  37.         }  
  38.     }  
  39.     else {  
  40.         close(fd[1]);  
  41.         int len = read(fd[0], buf, BUFSIZE);  
  42.         if (len < 0) {  
  43.             err_quit("read error");  
  44.         }  
  45.         printf("Get : %s\n",buf);  
  46.     }  
  47.     return 0;  
  48. }  
 代码都很简单,只是需要记住在创建新进程后记得分别关闭读写描述符。如果想让子进程给父进程传递数据,则关闭父进程的写描述符和子进程的读描述符即可。利用管道可以做简单的进程同步工作,因为读端会一直阻塞到写端发出数据后再运行后面的代码,可以利用这个特点保证让指定的进程先运行。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多