C语言笔记-21-Linux基础-通信管道

C语言笔记-21-Linux基础-通信管道



前言

自学笔记,没有历史知识铺垫(省略百度部分)C语言笔记-21-Linux基础-通信管道


一、管道概述

  1. 匿名管道:应用于关联进程的场景通讯,如父子进程、兄弟进程。
  2. 命名管道:是一种文件的类型,这种类型的文件没有内容,只是起到一个接口的作用.应用于任意的进程间通讯.

二、管道函数

匿名管道

  1. pipe() 创建匿名管道
  2. read() 如果管道中没有内容,阻塞
  3. write() 如果管道满,阻塞

pipe匿名管道示例

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

int main(int argc, char *argv[])
{
    char *msg = "main msg !!\n";
    char buf[128];
    int fds[2];
    int pp = pipe(fds);
    if (pp == -1)
    {
        perror("open pipe failed\n");
        return -1;
    }

    pid_t p = fork();
    if (p == -1)
    {
        perror("open child failed\n");
        return -1;
    }

    if (p == 0)
    {
        printf("start child process\n");
        close(fds[1]);
        // 如果管道内没有内容,进程回阻塞在此处
        int r = read(fds[0], buf, 128);
        write(1, buf, r);
        close(fds[0]);
    }
    else
    {
        printf("this is main process\n");
        close(fds[0]);
        // wait(NULL); wait在此处时,管道write无法触发,导致子进程管道读取阻塞无法结束,进而导致整个程序阻塞
        write(fds[1], msg, strlen(msg));
        close(fds[1]);
    }

    return 0;
}

// 执行结果
this is main process
main msg !!
start child process

命名管道

  1. mkfifo() 创建命名管道,创建后可使用open,write,read等底层io方式向有名管道文件读写数据,实现多进程通信的效果。

命名管道示例

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

int main(int argc, char *argv[])
{
    char *msg = "main msg22 !!\n";
    char buf[128];
    int fifoWritePipe;
    // 后续打开使用命名管道
    if (!access("tempFifo", F_OK))
    {
    	// 写入端打开管道
        fifoWritePipe = open("tempFifo", O_RDWR);
        if (fifoWritePipe == -1)
        {
            perror("open mkfifo failed\n");
            return -1;
        }
    }
    else
    {
    	// 首次创建管道
        fifoWritePipe = mkfifo("tempFifo", 0644);

        if (fifoWritePipe == -1)
        {
            perror("create mkfifo failed\n");
            return -1;
        }
    }

    pid_t p = fork();
    if (p == -1)
    {
        perror("open child failed\n");
        return -1;
    }

    if (p == 0)
    {
        printf("start child process\n");
        // 读取端打开管道
        int fifoReadPipe = open("tempFifo", O_RDONLY);
        if (fifoReadPipe == -1)
        {
            perror("open mkfifo failed\n");
            return -1;
        }
        // 子进程读取管道内容
        int r = read(fifoReadPipe, buf, 128);
        write(1, buf, r);
        close(fifoReadPipe);
    }
    else
    {
        printf("this is main process\n");
        // 主进程写入管道内容
        write(fifoWritePipe, msg, strlen(msg));
        close(fifoWritePipe);
    }

    return 0;
}

// 执行结果
this is main process
main msg22 !!
start child process

总结

本章主要为C语言笔记-21-Linux基础-通信管道

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值