进程间通信(一)管道的pipe函数 FIFO的mkfifo函数

进程间通信

进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。

IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。

以Linux中的C语言编程为例。

一、无名管道

一、管道
管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。

1、特点:
它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。

它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。

它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。

2、原型:

1.#include <unistd.h>
2.int pipe(int fd[2]);    // 返回值:若成功返回0,失败返回-1

1. 父进程调用pipe函数创建管道,得到两个文件描述符fd[0]、fd[1]指向管道的读端和写端。

2. 父进程调用fork创建子进程,那么子进程也有两个文件描述符指向同一管道。

3. 父进程关闭管道读端,子进程关闭管道写端。父进程可以向管道中写入数据,子进程将管道中的数据读出。由于管道是利用环形队列实现的,数据从写端流入管道,从读端流出,这样就实现了进程间通信。

若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。

无名管道:管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道;只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程)

该函数创建的管道的两端处于一个进程中间,在实际应用中没有太大意义,因此,一个进程在由 pipe()创建管道后,一般再fork一个子进程,然后通过管道实现父子进程间的通信(因此也不难推出,只要两个进程中存在亲缘关系,这里的亲缘关系指的是具有共同的祖先,都可以采用管道方式来进行通信)。

向管道中写入数据时,linux将不保证写入的原子性,管道缓冲区一有空闲区域,写进程就会试图向管道写入数据。如果读进程不读走管道缓冲区中的数据,那么写操作将一直阻塞。

注:只有在管道的读端存在时,向管道中写入数据才有意义。否则,向管道中写入数据的进程将收到内核传来的SIFPIPE信号,应用程序可以处理该信号,也可以忽略(默认动作则是应用程序终止)。

pipetest2.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
        int fd[2];
        int pid;
        char buf[128];
//      int pipe(int pipefd[2]);
        if(pipe(fd) == -1)
        {
                printf("creat pipe filed\n");
        }
        pid = fork();

        if(pid < 0){
                printf("creat child failed\n");
        }
        else if(pid > 0){
                sleep(3);
                printf("this is father\n");

                close(fd[0]);
                write(fd[1],"hello from father",strlen("hello from father"));
                wait(NULL);
        }
        else{
                printf("this is child\n");
                close(fd[1]);
                read(fd[0],buf,128);
                printf("read from father :%s\n",buf);
                exit(0);
        }
        return 0;
}

 运行结果为:

二、有名管道

FIFO

FIFO,也称为命名管道,它是一种文件类型。

1、特点

1.FIFO可以在无关的进程之间交换数据,与无名管道不同。

2.FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。

 2、原型

1 #include <sys/stat.h>
2 // 返回值:成功返回0,出错返回-1
3 int mkfifo(const char *pathname, mode_t mode);

其中的 mode 参数与open函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。

当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:

  • 若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它。

  • 若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。

mkfifotest4.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main()
{
//      int mkfifo(const char *pathname, mode_t mode);
        if(mkfifo("./file",0600) == -1 && errno == EEXIST)//can read can write
        {
                printf("mkfifo failed\n");
                perror("why");
        }else{
                if(errno == EEXIST){
                        printf("file have\n");
                }else{
                        printf("mkfifo success\n");
                }
        }
        return 0;
}
~     

运行结果为:

mkfifotest5.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main()
{
//      int mkfifo(const char *pathname, mode_t mode);
        if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write
        {
                printf("mkfifo failed\n");
                perror("why");
        }
        return 0;
}

运行结果为: 

===============================================================================================================================================

 read write 配合管道的使用

usefifotest6.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
//      int mkfifo(const char *pathname, mode_t mode);
        if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write
        {
                printf("mkfifo failed\n");
                perror("why");
        }

        int fd = open("./file",O_RDONLY);//only read
        printf("open success\n");


        return 0;
}
writefifo6.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
int main()
{
        int fd = open("./file",O_WRONLY);//only read
        printf("write open success\n");


        return 0;
}

运行结果为: 

=================================================================================================================================================== 

一直读取 一直写

fiforead7.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
//      int mkfifo(const char *pathname, mode_t mode);
        char buf[30] = {0};
        int nread = 0;
        if(mkfifo("./file",0600) == -1 && errno != EEXIST)//can read can write
        {
                printf("mkfifo failed\n");
                perror("why");
        }
        int fd = open("./file",O_RDONLY);//only read
        printf("read open success\n");

        while(1){
                nread = read(fd,buf,30);

                printf("read %d byte from fifo,context: %s\n",nread,buf);
        }
        close(fd);
        return 0;
}
fifowrite7.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main()
{
        int cnt = 0;
        char *str = "message from fifo";
        int fd = open("./file",O_WRONLY);//only read
        printf("write open success\n");
        while(1){

                write(fd,str,strlen(str));
                sleep(1);
                if(cnt == 5){
                        break;
                }
        }
        close(fd);
        return 0;
}

运行结果为:

上述例子可以扩展成 客户进程—服务器进程 通信的实例,write_fifo的作用类似于客户端,可以打开多个客户端向一个服务器发送请求信息,read_fifo类似于服务器,它适时监控着FIFO的读端,当有数据时,读出并进行处理,但是有一个关键的问题是,每一个客户端必须预先知道服务器提供的FIFO接口,下图显示了这种安排:

猜你喜欢

转载自blog.csdn.net/weixin_46016743/article/details/115413118