进程间的通信IPC(无名管道和命名管道)

在这里插入图片描述进程间的通信IPC介绍
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。

IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。
一、管道
管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。
(1)特点:

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

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

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

(2)原型:

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

当一个管道建立时,它会创建两个文件描述符:fd[0]为读而打开,fd[1]为写而打开。要关闭管道只需将这两个文件描述符关闭即可。

#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
        //int pipe(int pipefd[2]);
        int fd[2];//两个文件描述符
        pid_t pid;
        char *readbuf=NULL;
        readbuf=(char*)malloc(128);
        if(pipe(fd)==-1){//创建管道
                printf("creat pipe fail\n");
        }
        pid=fork();
        if(pid<0){
                printf("creat child fail\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],readbuf,128);
                printf("read from father is %s\n",readbuf);
                exit(-1);
}

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

特点:

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

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

原型:

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

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

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

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

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

写端代码

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <fcntl.h>
#include<string.h>
int main()
{
        //int mkfifo(char*pathname,mode_t mode);
        int fd;
        int cnt=0;
        char*str="message from file";
        fd=open("./file",O_WRONLY);
        while(1){
                write(fd,str,strlen(str));
                sleep(1);
                if(cnt==5){
                        break;
                }
         }
        close(fd);
        printf("write open successful\n");
        return 0;
}

读端代码

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <fcntl.h>
int main()
{
        //int mkfifo(char*pathname,mode_t mode);
        int fd;
        int nread=0;
        char buf[30]={0};
        if(mkfifo("./file",0600)==-1&& errno!=EEXIST){
                printf("mkfifo fail\n");
                perror("why");
        }
        fd=open("./file",O_RDONLY);
        //fd=open("./file",O_RDONLY|O_NONBLOCK);非堵塞方式打开
        printf("open successful\n");
        while(1){
                nread=read(fd,buf,20);//若没有写端将会堵塞在这里
                printf("read %d byte from file,context is%s\n",nread,buf);
        }
        close(fd);
        return 0;
}

详细介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值