Linux进程间通信 - - -管道


前言

本文为笔者学习笔记,若有不妥之处,欢迎斧正。


一、进程间通信的概述

进程间通信也称为IPC技术。作用是:使进程之间相互通信的技术。

进程间通信的目的:

  1. 数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几兆字节之间。
  2. 共享数据:多个进程想要操作共享数据,一个进程对共享数据的修改,别的进程应该立刻看到。
  3. 通知事件:一个进程需要向另一个或一组进程发送消息,通知它(它们)发生了某种事件(如进程终止时要通知父进程)。
  4. 资源共享:多个进程之间共享同样的资源。为了做到这一点,需要内核提供锁和同步机制。
  5. 进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望能够拦截另一个进程的所有陷入和异常,并能够及时知道它的状态改变。

进程间通信的方式有:管道(pipe或FIFO)、消息队列、信号量、共享内存、信号和网络套接字。

二、管道概述

1.无名管道(pipe)

管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。进程的亲缘关系通常是指父子进程关系。

注意:通常我们说的管道就是无名管道,是UNIX系统IPC最古老的进程通信方式。

无名管道的特点:

  1. 半双工(数据只能单向流动),具有固定的读端和写端。
  2. 只能使用在具有血缘关系的进程之间通信。(父子进程或者兄弟进程)
  3. 可以看成一种特殊的文件,对于它的读写也可以使用read、write等函数操作。但是它不是普通的文件,不属于任何文件系统,存在于内存当中。

函数原型:

#include <unistd.h>

int pipe(int pipefd[2]);

函数作用:创建一个无名管道。进行两个进程间的通信。

参数说明:在建立管道时,会创建两个文件描述符

  1. fd[0]为读端
  2. fd[1]为写端

返回值:成功返回0,失败返回-1

注意事项:千万注意不以文件的形式存在,而是存放在内存中。

管道机制需要提供一下几点的协调能力
1.互斥,即当一个进程正在对pipe执行读/写操作时,其它进程必须等待
2.同步,当一个进程将一定数量的数据写入,然后就去睡眠等待,直到读进程将数据取走,再去唤醒。读进程与之类似
3.确定对方是否存在

代码演示:父进程给子进程发消息

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
    
    
        pid_t pid;
        int fd[2];
        char buf[64]={
    
    0};


        if(pipe(fd) == -1)
        {
    
    
                perror("pipe failed:");
                exit(-1);
        }

        if((pid=fork()) == -1)
        {
    
    
                perror("fork failed:");
                exit(-1);
        }

        if(pid >0)
        {
    
    
                printf("this is father\n");
                close(fd[0]);
                write(fd[1],"hello my son",strlen("hello my son"));
                wait(NULL);
        }
        else if(pid == 0)
        {
    
    
                printf("this is son\n");
                close(fd[1]);
                read(fd[0],buf,64);
                printf("from father context:%s\n",buf);
                exit(0);
        }

        return 0;
}

运行结果:

this is father
this is son
from father context:hello my son

代码演示:子进程给父进程发消息

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
    
    
        pid_t pid;
        int fd[2];
        char buf[64];
        memset(buf,'\0',64);


        if(pipe(fd) == -1)
        {
    
    
                perror("pipe failed:");
                exit(-1);
        }

        if((pid=fork()) == -1)
        {
    
    
                perror("fork failed:");
                exit(-1);
        }

        if(pid >0)
        {
    
    
                wait(NULL);
                printf("this is father\n");
                close(fd[1]);
                read(fd[0],buf,64);
                printf("form son context:%s\n",buf);
        }
        else if(pid == 0)
        {
    
    
                printf("this is son\n");
                close(fd[0]);
                write(fd[1],"hello my father",strlen("hello my father"));
                exit(1);
        }

        return 0;
}

运行结果如下:

this is son
this is father
form son context:hello my father

2.命名管道(FIFO)

命名管道 (FIFO) : 命名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信。

FIFO的通信方式类似于在进程中使用文件来传输数据,只不过FIFO类型文件同时具有管道的特性。在数据读出时,FIFO管道中同时清除数据,并且“先进先出”。

FIFO是一种文件类型。

FIFO特点:

  1. FIFO可以在无关进程之间进行通信。
  2. FIFO有路径与之相关联,它是一种特殊设备以文件的形式存在于文件系统中。
  3. 和无名管道一样也是半双工通信

函数原型:

#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);

函数作用:创建一个命名管道,进行进程间通信。

参数说明:

  1. pathname:路径名,指定我们使用的命名管道在哪里
  2. mode:该参数和open函数中mode是一样的,但是有需要注意,打开一个FIFO时需要关注是否设置非阻塞标志。mode详见
  • 若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open
    要阻塞到某个其他进程为读而打开它。
  • 若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该
    FIFO,其errno置ENXIO。

返回值:成功返回0,失败返回-1.

代码演示:实现两个无关进程之间的通信

read端:

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    
    
        char buf[64];
        memset(buf,'\0',64);

        if(mkfifo("./file",0600)  == -1 && errno != EEXIST)
        {
    
    
                perror("mkfifo failed:");
                exit(-1);
        }

        int fd = open("./file",O_RDONLY);
        if(fd == -1)
        {
    
    
                perror("open failed:");
                exit(-1);
        }

        int n_read = 0;
        while(1)
        {
    
    
                n_read = read(fd,buf,64);
                printf("read byte:%d,read context:%s\n",n_read,buf);
        }

        close(fd);


        return 0;
}

write端:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>

int main(void)
{
    
    
        char buf[64] = "hello I am is fifoWrite";

        if(mkfifo("./file",0600) == -1 && errno != EEXIST)
        {
    
    
                perror("mkfifo failed or file exist:");
                exit(-1);
        }

        int fd = open("./file",O_WRONLY);
        if(fd == -1)
        {
    
    
                perror("open failed:");
                exit(-1);
        }

        while(1)
        {
    
    
                write(fd,buf,strlen(buf));
                sleep(1);
        }

        return 0;
}

运行结果如下:

read byte:23,read context:hello I am is fifoWrite
read byte:23,read context:hello I am is fifoWrite
read byte:23,read context:hello I am is fifoWrite
read byte:23,read context:hello I am is fifoWrite
^C

三、无名管道和命名管道的异同

共同点:

  1. 都可以实现两个进程之间的通信。
  2. 都是半双工的通信方式(数据单向流动)
  3. 都与文件编程类似
  4. 都可以使用open、read、write、close进行操作

不同点:

  1. pipe只能在具有亲缘关系的进程中通信,而FIFO可以再没有任何联系的进程之间相互通信。
  2. pipe不是以文件的形式存在,而是在内存中,而FIFO是以文件的形式存在于文件系统中。

参考书籍:
《UNIX环境高级编程》第三版

Guess you like

Origin blog.csdn.net/weixin_51363326/article/details/116887015