当前位置:   article > 正文

linux管道_linux 管道

linux 管道

管道
管道的概念:
管道是一种最基本的IPC机制,作用于有血缘关系的进程之间,完成数据传递。调用pipe系统函数即可创建一个管道。有如下特质:

  1. 其本质是一个伪文件(实为内核缓冲区)
  2. 由两个文件描述符引用,一个表示读端fd[0],一个表示写端发的fd[1]。
  3. 规定数据从管道的写端流入管道,从读端流出。
    管道的原理: 管道实为内核使用环形队列机制,借助内核缓冲区(4k)实现。
    管道的局限性:
    ① 数据自己读不能自己写。
    ② 数据一旦被读走,便不在管道中存在,不可反复读取。
    ③ 由于管道采用半双工通信方式。因此,数据只能在一个方向上流动。
    ④ 只能在有公共祖先的进程间使用管道。
    常见的通信方式有,单工通信、半双工通信、全双工通信。
    一、linux系统提供的创建无名管道的API,pipe函数
    pipe的原型是
int pipe(int pipefd[2]);
返回值为-1则创建管道失败
  • 1
  • 2

示例1:创建一个父子进程,父进程向管道写入一个字符串,子进程再从管道中将字符串读出来并打印。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{

        int fd[2];
        char buff[20];
        pid_t pid;
        if(pipe(fd) < 0){ //creat pipe
                printf("create pipe error\n");
        }
        pid = fork();创建父子进程
        if(pid < 0){ // 父子进程创建失败
                printf("fork error\n");
        }else if(pid > 0){//父进程
                sleep(3);
                printf("this is father\n");
                close(fd[0]);关闭读端
                write(fd[1], "lizehao henshuai", strlen("lizehao henshuai"));
                wait();
        }else{//pid等于0创建子进程
                printf("this is child\n");
                close(fd[1]);关闭写端

                int n_read = read(fd[0], buff, 20);
                printf("read %d byte buf:%s\n",n_read, buff);
                exit(0);
        }
        return 0;
}
  • 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

二、创建有名管道mkfifo
创建有名管道一个进程将字符串写入管道中在创建一个文件读出写入的字符串

mkfifo函数原型:int mkfifo(const char *pathname, mode_t mode);
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
        int fd;
        char buf[30];

        if(mkfifo("./file", 0600) == -1 && errno != EEXIST){//创建有名管道当返回值为-1 errno的不等于eexist的时候不等于EEXIST
                printf("creat fifo error\n");
                perror("why");
        }
        fd = open("./file", O_RDONLY);打开创建的管道
        int n_read = 0;
        while(1){
                n_read = read(fd, buf, 30);
                printf("read %d byte, context:%s\n", n_read, buf);
        }
        close(fd);
        return 0;
 }
  • 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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main()
{
        int fd;
        int cnt =0;
        char *str = "from wite fifo";
        fd = open("./file",O_WRONLY);
        printf("write open succese\n");
        while(1){
                write(fd, str, strlen(str));
                sleep(1);
                if(cnt == 3){
                        break;
                }
                cnt++;
        }
        close(fd);
        return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/212039
推荐阅读
相关标签
  

闽ICP备14008679号