Linux进程通信系列文章将详细介绍各种通信方式的机制和区别
1.进程间通信
每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程A把数据从用户空间拷到内核缓冲区,进程B再从内核缓冲区把数据读走,内核提供的这种机制称为进程间通信。
2、进程间通信方式
一、进程间通信-管道 https://www.linuxidc.com/Linux/2018-04/151680.htm
二、进程间通信-命名管道 https://www.linuxidc.com/Linux/2018-04/151681.htm
三、进程间通信-消息队列 https://www.linuxidc.com/Linux/2018-04/151682.htm
四、进程间通信-共享内存 https://www.linuxidc.com/Linux/2018-04/151683.htm
3、进程间通信-管道(pipe)
3.1 管道是如何通信的
(1)父进程创建管道,得到两个⽂件描述符指向管道的两端
(2)父进程fork出子进程,⼦进程也有两个⽂件描述符指向同⼀管道。
(3)父进程关闭fd[0],子进程关闭fd[1],即⽗进程关闭管道读端,⼦进程关闭管道写端(因为管道只支持单向通信)。⽗进程可以往管道⾥写,⼦进程可以从管道⾥读,管道是⽤环形队列实现的,数据从写端流⼊从读端流出,这样就实现了进程间通信。
3.2 利用管道实现通信
#include “stdio.h”
#include “unistd.h”
#include “string.h”
int main(int argc, char* argv[])
{
int fd[2];
int ret = pipe(fd);
if (ret == -1)
{
perror(“pipe errorn”);
return 1;
}
pid_t pid = fork();
if (pid == 0) // child
{
close(fd[0]);
int i = 0;
char* msg = “i am childn”;
while (i < 5)
{
write(fd[1],msg,strlen(msg));
sleep(2);
i++;
}
}
else if(pid > 0)
{
close(fd[1]);
char buf[256] = {0};
int j = 0;
while (j < 5)
{
int n = read(fd[0],buf,256);
if (n>0)
{
buf[n] = ‘