进程间通信——管道

本文介绍了进程间通信的两种方式:无名管道和FIFO(命名管道)。无名管道是半双工的,用于具有亲缘关系的进程,如父子进程之间。FIFO则可以在无关进程间交换数据,且具有路径名,可以被多个进程共享。文章通过代码示例展示了如何创建和使用这两种管道进行数据传输。
摘要由CSDN通过智能技术生成

参考来源:(1条消息) 【转】进程间的五种通信方式_进程间的5种通信方式_哈市雪花的博客-CSDN博客icon-default.png?t=N176https://blog.csdn.net/baidu_38621657/article/details/105724822?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167860274416800222885633%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167860274416800222885633&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-105724822-null-null.142^v73^control,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=%E8%BF%9B%E7%A8%8B%E9%97%B4%E7%9A%84%E4%BA%94%E7%A7%8D%E9%80%9A%E4%BF%A1%E6%96%B9%E5%BC%8F&spm=1018.2226.3001.4187

无名管道

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

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

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

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

2、原型:
1 #include <unistd.h>
2 int pipe(int fd[2]);    // 返回值:若成功返回0,失败返回-1
当一个管道建立时,它会创建两个文件描述符:fd[0]为读而打开,fd[1]为写而打开。要关闭管道只

3、例子
单个进程中的管道几乎没有任何用处。所以,通常调用 pipe 的进程接着调用 fork,这样就创建了父进程与子进程之间的 IPC 通道。
若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。

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

int main()
{
        //int pipe(int pipefd[2]);
        // 返回值:若成功返回0,失败返回-1
        //fd[0]为读而打开,fd[1]为写而打开
        
        int fd[2];

        int pid;
        char buf[128];
        if(pipe(fd)==-1){
                printf("creat pipe failed\n");
        }

        pid = fork();
        if(pid<0){
                printf("creat child failed\n");
        }

        else if(pid>0){
                sleep(3);
                printf("this is farther\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、特点
FIFO可以在无关的进程之间交换数据,与无名管道不同。
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。

实例

创建有名管道

方式1:

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

        //有名管道
        //int mkfifo(const char *pathname, mode_t mode);
        // 返回值:成功返回0,出错返回-1        

int main()
{
       int ret = mkfifo("./file",0600);
        if(ret == 0){
                printf("mkfifo success\n");     
        }
        if(ret == -1){
                printf("mkfifo failuer\n");
                perror("why");
        }




        return 0;
}

方式二:

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

        //有名管道
        //int mkfifo(const char *pathname, mode_t mode);
        // 返回值:成功返回0,出错返回-1        

int main()
{

        if((mkfifo("./file",0600)==-1)&&errno==EEXIST){
                printf("mkfifo failuer \n");
                perror("why");
        }else{
                printf("mkfifo success\n");
        }


        return 0;
}

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

GuanDao2_read.c

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


        //有名管道
        //int mkfifo(const char *pathname, mode_t mode);
        // 返回值:成功返回0,出错返回-1        

int main()
{
        int nread = 0;

        char buf[30] = {0};

        if((mkfifo("./file",0600)==-1)&&errno!=EEXIST){
                printf("mkfifo failed\n");
                perror("why");
        }
        int fd = open("./file",O_RDONLY);
        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;
}

 GuanDao2_write.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 mkfifo(const char *pathname, mode_t mode);
        // 返回值:成功返回0,出错返回-1        

int main()
{
        int cnt = 0;
        char *str = "message from fifo";

        int fd = open("./file",O_WRONLY);
        printf("write open success\n");

        while(1){
                write(fd,str,strlen(str));

                sleep(1);
                if(cnt==5){
                        break;
                }
        }
        close(fd);

        return 0;
}

两终端运行情况

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值