进程通信--管道

进程通信–管道

IPC: InterProcess Communication 进程间通信, 通过内核提供的缓冲区进行数据交换机制.

IPC通信的方式有

  • pipe 管道 – 最简单的. — 有血缘关系的
  • fifo 有名管道.
  • mmap 文件映射IO (文件共享IO) – 速度最快
  • 本地socket 最稳定的
  • 信号 — 携带信息量最小的
  • 共享内存 —
  • 消息队列

管道 -半双工通信

常见的通信方式 :

  • 单工(数据流向只能是一个方向 – 广播 接听只能接听不能有任何反馈)
  • 半双工 (同一个时刻,数据只能往一个方向流 – 对讲机)
  • 全双工 (两方同时 – 打电话)

pipe

#include <unistd.h>

int pipe(int pipefd[2]);

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

int pipe2(int pipefd[2], int flags);

实例

#include<unistd.h>
#include<stdio.h>

int main()
{
    int fd[2];
    pipe(fd);

    pid_t pid = fork();
    
    // 子进程
    if(pid == 0){
        sleep(2);
        write(fd[1],"hello",5);
    }
    
    // 父进程
    if(pid > 0){
        char buf[1024];
        read(fd[0],buf,sizeof(buf));
        printf("父进读到的内容:%s\n",buf);
        wait(NULL);
    }

    return 0;
}

为什么子进程等待,父进程还能读到文件?

读设备的时候,read默认情况下是堵塞的 只要对方打开水管(管道)他就认为某个时刻会出来水(数据).

父子进程实现pipe通信,实现ps aux| grep bash 功能

#include<stdio.h>
#include<unistd.h>

int main()
{
    int fd[2];
    pipe(fd);

    pid_t pid = fork();
    if(pid == 0)
    {
        // 1. 先重定向
        // 2. 执行execl
        clse(fd[0]);
        dup2(fd[1],STDOUT_FILENO);  //  标准输出重定向到管道的写端.
        execlp("ps","ps","aux",NULL);
    
    }
    if(pid > 0)
    {
        close(fd[1]);
        // 1. 重定向
        dup2(fd[0],STDIN_FILENO);

        execlp("grep","grep","bash",NULL);
    }
}

注意要使管道流向单一化,读端关闭写端, 写端关闭读端 .

读管道

  • 写端全部关闭 – read读0 相当于读到文件末尾
  • 写端端没有关闭,
    • 有数据 – read 读数据
    • 没有数据 – read 阻塞 fcntl函数可以更改非阻塞

写管道

  • 读端全部关闭 – 产生一个信号 SIGPIPE, 程序异常终止
  • 读端没全部关闭 –
    • 管道以满 – write 堵塞
    • 管道未满 – write正常写入

管道的优劣

优点:

简单, 相比信号, 套接字, 实现的进程间通信,简单很多.

缺点:
  1. 只能单向通信, 双向通信需要建立两个管道.
  2. 只能用于父子 兄弟进程(有共同祖先)键通信. 该问题后面使用fifo有名管道解决.

FIFO 通信 – 全双工

FIFO 命名管道, 实现无血缘关系的进程通信.

  • 创建就一个管道的伪文件 :mkfifo
  • 也可以用函数创建 :int mkfifo(const char *pathname, mode_t mode);
  • 内核会根据fifo文件开辟缓冲区, 操作fifo文件, 可以操作缓冲区, 实现进程间通信.
  • 实际操作就是文件读写
#include <sys/types.h>
#include <sys/stat.h>

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

写入

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

int main(int argc, char * argv[])
{
    if(argc != 2)
    {
        printf("请加上文件\n"); 
        return -1;
    }
    // 当前目录有一个 myfifo 文件
    // 打开fifo文件
    int fd = open(argv[1],O_WRONLY);
    if(fd == -1){
        printf("文件打开失败\n");
        return -1;
    }
    // 写
    char buf[1024]={0};
    int wcount=0,i=0;
    while(1)
    {
        memset(buf,0x00,sizeof(buf));
        sprintf(buf,"代登辉 %4d\n",++i);
        wcount = write(fd,buf,strlen(buf));
        printf("写入%d个字符\n",wcount);
        sleep(1);
        if(i>100) break;
    }

    // 关闭
    close(fd);
    printf("管道关闭\n");


    return 0;
}

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

int main(int argc, char* argv[])
{
    if(argc != 2){
        printf("请加入文件\n");
        return -1;
    }

    int fd = open(argv[1],O_RDONLY);
    if(fd ==-1){
        printf("文件打开失败\n");
        return -1;
    }
    
    char* buf[1024];
    int rcount;
    while(1)
    {
        memset(buf,0x00,sizeof(buf));
        rcount = read(fd,buf,sizeof(buf));
        if(rcount == 0){
            printf("读取完成\n");
            break;
        }
        printf("读取到的文件是:%s\n",buf);
    }
    
    close(fd);

    return 0;
}

注意:

  1. 只打开写进程,不打开读进程, 写进程会发生阻塞. (只有读进程或者只有写进程都会阻塞)
  2. 可同时打开多个读进程, 任何一个进程读取后,管道内的内容消失. 两个进程读取内容加一起就是发送进程发送的全部内容.
  3. 可以开多个读多个写.
  4. 运行后 读端关闭 写端也会关闭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

去留意

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

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

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

打赏作者

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

抵扣说明:

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

余额充值