linux进程间通信之管道(无名管道pipe)实现全双工双向通信

管道是什么:
1. 管道只能用于具有亲缘关系的进程之间通信。
2.管道是一种单工或者说半双工的通信方式,传递信息的方向是固定的,只能由一端传递到另一端。

头文件及函数原型:
#include <unistd.h>
int pipe(int fd[2]);

当用pipe 创建管道后,两个文件描述符fd[0],fd[1]就可以使用了,其中fd[0]用于读取,fd[1]用于写入。调用管道pipe返回值0表示成功,返回值-1表示失败。


pipe函数创建管道后,接着fork出子进程,子进程继承父进程管道。

代码举例来看:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #define MAX_DATA_LEN 256
  7. #define DELAY_TIME 1
  8. int main() {
  9.     pid_t pid;
  10.     char buf[MAX_DATA_LEN];
  11.     const char *data="Pipe Test";
  12.     int real_read,real_write;
  13.     int pipe_fd[2];
  14.     memset((void*)buf,0,sizeof(buf));
  15.     if(pipe(pipe_fd)<0){
  16.         perror("Pipe create error!\n");
  17.         exit(1);
  18.     }
  19.         
  20.     if ((pid=fork())<0) {
  21.         perror("Fork error!\n");
  22.         exit(1);
  23.     } else if (pid==0) {
  24.         close(pipe_fd[1]);
  25.         sleep(DELAY_TIME*3);
  26.         if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
  27.             printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
  28.         }
  29.         close(pipe_fd[0]);
  30.         exit(0);
  31.     } else {
  32.         close(pipe_fd[0]);
  33.         sleep(DELAY_TIME);
  34.         if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
  35.             printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
  36.         }
  37.         close(pipe_fd[1]);
  38.         waitpid(pid,NULL,0);
  39.         exit(0);
  40.     }
  41. }
复制代码

运行输出:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.

上述代码只能从父进程向子进程传递,如何从子进程向父进程传递呢?我们看到父进程关闭了管道读取端“close(pipe_fd[0]);”,子进程关闭了管道写入端“close(pipe_fd[1]);”,如果取消关闭这两个端,是否能够实现子进程向父进程传递呢。
父进程先发送,子进程接收,然后子进程再发送,父进程再接收,实现全双工互相通信,期望运行结果如下:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.
Child write 9 bytes from pipe: 'Pipe Test'.
Parent receive 9 bytes from pipe: 'Pipe Test'.

修改代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #define MAX_DATA_LEN 256
  7. #define DELAY_TIME 1
  8. int main() {
  9.     pid_t pid;
  10.     char buf[MAX_DATA_LEN];
  11.     const char *data="Pipe Test";
  12.     int real_read,real_write;
  13.     int pipe_fd[2];
  14.     memset((void*)buf,0,sizeof(buf));
  15.     if(pipe(pipe_fd)<0){
  16.         perror("Pipe create error!\n");
  17.         exit(1);
  18.     }
  19.         
  20.     if ((pid=fork())<0) {
  21.         perror("Fork error!\n");
  22.         exit(1);
  23.     } else if (pid==0) {
  24.         //close(pipe_fd[1]);
  25.         sleep(DELAY_TIME*3);
  26.         if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
  27.             printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
  28.         }
  29.                 
  30.                 if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
  31.             printf("Child write %d bytes into pipe: '%s'.\n",real_write,data);
  32.         }
  33.         close(pipe_fd[0]);
  34.         exit(0);
  35.     } else {
  36.         //close(pipe_fd[0]);
  37.         sleep(DELAY_TIME);
  38.         if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
  39.             printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
  40.         }
  41.                 
  42.         if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
  43.             printf("Parent receive %d bytes from pipe: '%s'.\n",real_read,buf);
  44.         }
  45.         close(pipe_fd[1]);
  46.         waitpid(pid,NULL,0);
  47.         exit(0);
  48.     }
  49. }
复制代码


但是实际运行如下:
Parent write 9 bytes into pipe: 'Pipe Test'.
Parent receive 9 bytes from pipe: 'Pipe Test'.


可以看到,父进程发送的数据被父进程自己接收了,子进程读不到数据被阻塞了。显然这种方法不行。

因为管道是单工的,只能固定从一个方向传递到另一个方向。
要实现互相通信,一个管道是不行的,可以创建两个管道,一个管道是父写子读,另一个是子写父读。
代码如下:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #define MAX_DATA_LEN 256
  7. #define DELAY_TIME 1
  8. int main() {
  9.     pid_t pid;
  10.     char buf[MAX_DATA_LEN];
  11.     const char *data="Pipe Test";
  12.     int real_read,real_write;
  13.     int pipe_fd[2],pipe_fd2[2];
  14.     memset((void*)buf,0,sizeof(buf));
  15.     if(pipe(pipe_fd)<0){
  16.         perror("Pipe create error!\n");
  17.         exit(1);
  18.     }
  19.         
  20.         if(pipe(pipe_fd2)<0){
  21.         perror("Pipe create error!\n");
  22.         exit(1);
  23.     }
  24.     if ((pid=fork())<0) {
  25.         perror("Fork error!\n");
  26.         exit(1);
  27.     } else if (pid==0) {
  28.         close(pipe_fd[1]);
  29.                 close(pipe_fd2[0]);
  30.         sleep(DELAY_TIME*3);
  31.         if ((real_read=read(pipe_fd[0],buf,MAX_DATA_LEN))>0) {
  32.             printf("Child receive %d bytes from pipe: '%s'.\n",real_read,buf);
  33.         }
  34.                 if ((real_write=write(pipe_fd2[1],data,strlen(data)))>0) {
  35.             printf("Child write %d bytes into pipe: '%s'.\n",real_write,data);
  36.         }
  37.         close(pipe_fd[0]);
  38.                 close(pipe_fd2[1]);
  39.         exit(0);
  40.     } else {
  41.         close(pipe_fd[0]);
  42.                 close(pipe_fd2[1]);
  43.         sleep(DELAY_TIME);
  44.         if ((real_write=write(pipe_fd[1],data,strlen(data)))>0) {
  45.             printf("Parent write %d bytes into pipe: '%s'.\n",real_write,data);
  46.         }
  47.                 if ((real_read=read(pipe_fd2[0],buf,MAX_DATA_LEN))>0) {
  48.             printf("Parent  receive %d bytes from pipe: '%s'.\n",real_read,buf);
  49.         }
  50.         close(pipe_fd[1]);
  51.                 close(pipe_fd2[0]);
  52.         waitpid(pid,NULL,0);
  53.         exit(0);
  54.     }
  55. }
复制代码

运行结果:
Parent write 9 bytes into pipe: 'Pipe Test'.
Child receive 9 bytes from pipe: 'Pipe Test'.
Child write 9 bytes into pipe: 'Pipe Test'.
Parent  receive 9 bytes from pipe: 'Pipe Test'.


可以看到,创建了两个管道 “int pipe_fd[2],pipe_fd2[2];”,
pipe_fd 是父写子读,pipe_fd2是子写父读,通过两个管道,实现了进程的全双工互相通信。

猜你喜欢

转载自www.cnblogs.com/wudymand/p/9226417.html