进程(四)(理解):传统进程间通信:管道

一、无名管道

1、介绍

  • 无名管道的通讯原理是在内核中完成的,无名管道只能在亲缘进程间通信
  • 无名管道的大小为64K,它是一个半双工的通信方式,无名管道在使用的时候不能使用 lseek 函数(因为原理和队列相似,先进先出)。
  • 1

2、函数API

 #include <unistd.h>
函数原型:
       int pipe(int pipefd[2]);
参数:
    @pipefd[2] : 是一个数组
        pipefd[0] : 代表读端
        pipefd[1] : 代表写端
返回值:
    成功返回0 失败返回-1

3、示例

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
int main(){
    pid_t pid;
    char buf[128] = {0};

    //1. 创建管道
    int pipefd[2];
    if ( pipe(pipefd) == -1){
        printf("pipe error\n");return -1;
    }
    
    //2. 创建父子进程
    pid = fork();    //进程要在管道创建之后创建,要保证进程创建的时候已经有了读端和写端
                    //因为进程一旦创建就要立即拿到管道的读端和写端
    if(pid == -1){
        printf("fork error\n");return -1;
    }else if(pid == 0){
        //子进程
        //子进程进行写操作            所以要关闭读端
        close(pipefd[0]);
        
        //进行写操作
        while(1){
            fgets(buf, sizeof(buf), stdin);
            buf[strlen(buf)-1] = '\0';
            write(pipefd[1], buf, strlen(buf));

            if(strcmp(buf, "quit") == 0)
            break;
        }
        exit(0);
    }else{
        //父进程
        //父进程进行读操作              关闭写端
        close(pipefd[1]);
        while(1){
            memset(buf, 0, sizeof(buf));
            read(pipefd[0], buf, sizeof(buf));
            if(strcmp(buf, "quit") == 0)
            break;
            
            printf("buf = %s\n", buf);            
        }
        wait(NULL);
    }

    return 0;
}

4、无名管道读写规则

  1. 读端存在,对管道写,有多少写多少,直到写满为止(64K),再写会阻塞。
    如果读写端都存在,如果只写不读,当管道写满是,写操作也会阻塞,默认无名管道64K字节。
  2. 写端存在,读(v.动词)管道,有多少读多少,如果管道数据为空,读阻塞。
    如果读写端都存在,如果只读不写,如果管道有数据则正常读取,如果没有数据会一直阻塞。
  3. 读端不存在,写(v.动词)管道,管道破裂,进程会收到一个信号SIGPIPE
    如果关闭读端,只有写端,当执行到write函数时,就会产生SIGPIPE(管道破裂)信号,信号默认的处理方式是退出整个进程。
  4. 写端不存在,读(v.动词)管道,有多少读多少,如果管道数据为空,立即返回。
    如果关闭写端,只有读端,如果管道有数据则正常读取,如果没有数据则read会返回0

二、有名管道

1、介绍

  • 可以在任意进程间通信,有名管道会在用户空间创建一个管道文件,管道文件是在内存存放的。
  • 如果想要通过有名管道实现进程间通信,就在当前进程中打开一个管道文件,产生一个文件描述符 fd 并对其操作即可。

2、函数API

#include <sys/types.h>
#include <sys/stat.h>
函数原型:
       int mkfifo(const char *pathname, mode_t mode)       
参数:
    @ pathname : 创建的管道的路径以及管道名称
    @ mode     : 管道的权限
返回值:
    成功返回0 失败返回-1

3、示例

3.1、creat.c

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



int main(){
    //1. 创建有名管道
    if( mkfifo("/fifo", 0666) == -1){
        printf("mkfifo error");return -1;
    }
    
    //2. 等待用户输入  
    getchar();
    
    //3. 删除管道
    system("sudo rm fifo");
    
    return 0;        
}

3.2、write.c

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

#include <fcntl.h>

int main(){
    int fd;
    char buf[128] = {0};
    
    //用只写的方式打开管道文件
    if ((fd = open("/fifo", O_WRONLY)) == -1){
        printf("open error\n");return -1;
    }
    
    //往管道写内容
    while(1){
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf)-1] = '\0';
        write(fd, buf, strlen(buf));
        
        if(strcmp(buf, "quit") == 0)
        break;
    }
    close(fd);
    return 0;
}

3.3、read.c

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

#include <fcntl.h>

int main(){
    int fd;
    char buf[128] = {0};
    
    //用只读的方式打开管道文件
    if ((fd = open("/fifo", O_RDONLY)) == -1){
        printf("open error\n");return -1;
    }
    
    //从管道读内容
    while(1){
        memset(buf, 0, sizeof(buf));
        read(fd, buf, sizeof(buf));
        
        if(strcmp(buf, "quit") == 0)
        break;
        printf("buf = %s\n", buf);
    }
    close(fd);
    return 0;
}

4、有名管道读写规则

  1. 读端存在,写管道,有多少写多少,直到写满阻塞。
  2. 写端存在,读管道,有多少读多少,如果管道数据为空,读阻塞。
  3. 如果读端不存在,写管道: 读端没有打开过:写端在open的位置阻塞 读端打开又关闭了:写管道,管道破裂
  4. 如果写端不存在,读管道: 写端没有打开过:读端在open位置阻塞 写端打开有关闭了:管道中有多少数据就读多少,如果数据为空,立即返回。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好睡觉好好吃饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值