进程间的通信方式有多种,最典型的无名管道,有名管道,二者的区别在于,无名管道适用于小规模,具有血缘关系的进程间通信,有名管道适用于小规模,无血缘和有血缘的进程间通信,管道的实质是:内核开辟的一端缓存区,一端读,一端写。两端默认都是阻塞状态想要实现双向通信就得创建两个管道,因为管道是半双工,写的时候就只能写,读的时候就只读,数据的传输是单向的,如果要实现双向,就必须增加一根管道。
下面直接上demo:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#define FILENAME "./file1"
void print_err(char* str)
{
perror(str);
exit(-1);
}
char buf[1024] = {0};
int main()
{
//[0]: 读端
//[1]: 写端
int pipe1[2]; //管道1
int pipe2[2]; //管道2
int ret;
ret = pipe(pipe1);
if(ret == -1) print_err("pipe1 fail:");
ret = pipe(pipe2);
if(ret == -1) print_err("pipe2 fail:");
pid_t pid = fork();
if(pid > 0)
{
//父进程往管道1中写数据,关闭管道1的读端
close(pipe1[0]);
//父进程从管道2中读数据,关闭管道2的写端
close(pipe2[1]);
while(1)
{
int fd = open(FILENAME,O_RDWR|O_CREAT|O_APPEND,0664);
if( fd == -1) print_err("open fail:");
//写之前清空一下buf
bzero(buf,sizeof(buf));
//记录是谁发的消息
strcpy(buf,"parent:");
printf("parent write:\n");
//从标准输入读取消息放到buf中
read(0,buf+strlen("parent:"),sizeof(buf));
//把消息写进文件做记录,后面可以直接打开文件查看各自发了哪些消息
write(fd,buf,strlen(buf));
//父进程往管道1里写数据,阻塞
int ret = write(pipe1[1],buf,sizeof(buf));
if(ret != -1) printf("send success!\n");
//父进程从管道2里读数据,阻塞
ret = read(pipe2[0],buf,sizeof(buf));
//睡眠一段时间,场景需要
usleep(10);
if(ret == -1) print_err("parent process read fail:");
else printf("parent receive success!\n");
}
}
else if(pid == 0)
{
//子进程从管道1中读数据,关闭管道1的写端
close(pipe1[1]);
//子进程往管道2中写数据,关闭管道2的读端
close(pipe2[0]);
while(1)
{
//打开文件,以追加的方式
int fd = open(FILENAME,O_RDWR|O_CREAT|O_APPEND,0664);
if(fd == -1) print_err("open fail:");
//子进程从管道1中读数据,阻塞
int ret = read(pipe1[0],buf,sizeof(buf));
if(ret == -1) print_err("child process read fail:");
else printf("child receive success!\n");
//读之后清空一下buf
bzero(buf,sizeof(buf));
//记录是谁发的消息
strcpy(buf,"child:");
printf("child write:\n");
//从标准输入读取消息放到buf中
read(0,buf+strlen("child:"),sizeof(buf));
//把buf中的消息写进文件做记录
write(fd,buf,strlen(buf));
//子进程往管道2中写数据,阻塞
ret = write(pipe2[1],buf,sizeof(buf));
if(ret != -1) printf("send success!\n");
}
}
return 0;
}