打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C语言进程间通信(一)
userphoto

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. }  
 代码都很简单,只是需要记住在创建新进程后记得分别关闭读写描述符。如果想让子进程给父进程传递数据,则关闭父进程的写描述符和子进程的读描述符即可。利用管道可以做简单的进程同步工作,因为读端会一直阻塞到写端发出数据后再运行后面的代码,可以利用这个特点保证让指定的进程先运行。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux进程间通信
进程管道通信 - 多端写入lockf加锁
Linux系统管道和有名管道的通信机制
MPlayer从模式说明
Linux的进程通讯之匿名管道
有名管道和无名管道
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服