当前位置:   article > 正文

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

c语言 管道分类

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

命名管道

  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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/837672
推荐阅读
相关标签
  

闽ICP备14008679号