首页 > 系统相关 >进程间通信之匿名管道

进程间通信之匿名管道

时间:2022-08-31 16:35:43浏览次数:58  
标签:pipe read pid pipefd 管道 间通信 匿名 include buff

Linux手册中对匿名管道的描述如下:

DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For further details, see pipe(7).

调用pipe()接口后会创建一个匿名管道并返回管道读写两端的描述符,其中pipefd[0]为读端,pipefd[1]为写端。通常用于父子进程间的通信。
对于pipe()返回的描述符可以直接进行read/write操作,无需调用open()

测试代码如下。

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

#define BUFFSIZE    128

int main(int argc, char* argv[])
{
    int pipefd[2] = {0, 0};
    pid_t pid = -1;
    unsigned char buff[BUFFSIZE] = {0};
    int count = 0;

    if(0 != pipe(pipefd))
    {
        perror("create pipe failed!");
        return -1;
    }

    pid = fork();
    if(-1 == pid)
    {
        perror("fork error");
        return -1;
    }
    
    if(pid == 0)/* child process */
    {
        close(pipefd[1]);
        while(1)
        {
            memset(buff, 0, BUFFSIZE);
            if(0 < read(pipefd[0], buff, sizeof(buff)))
            {
                printf("[child rec]:%s\n", buff);
            }
            sleep(5);
        }
    }
    else /* parent process */
    {
        close(pipefd[0]);
        while(1)
        {
            memset(buff, 0, sizeof(buff));
            sprintf(buff, "Hello-%d", ++count);
            if(-1 != write(pipefd[1], buff, BUFFSIZE))
            {
                printf("[parent send]:%s\n", buff); 
            }
            sleep(1);
        }
    }
    return 0;
}

标签:pipe,read,pid,pipefd,管道,间通信,匿名,include,buff
From: https://www.cnblogs.com/lucky-glc/p/16643530.html

相关文章

  • 嵌套类匿名类与封装类
    嵌套类在C#中可以将一个类定义在另一个类的内部;外面的类叫“外部类”,内部的类叫“嵌套类”;嵌套类和普通类相似,只是声明的位置比较特殊。classPerson{//外部类......
  • Linux中重定向及管道
    1重定向1.1重定向符号>输出重定向到一个文件或设备覆盖原来的文件>!输出重定向到一个文件或设备强制覆盖原来的文件>>......
  • 系统架构设计风格之二---管道-过滤器风格
    一、管道-过滤风格 过滤器:功能组件管道:数据流之间的通路1.2特点管道/过滤器结构将数据流处理分为几个顺序的步骤来实现,一个步骤的输出是另一个步骤的输入,每个步骤......
  • BTC笔记-10-匿名性
    BTC-匿名性B站视频链接比特币的匿名性比特币的匿名性弱于现金,也弱于无需实名的银行,强于实名制的银行一个人可以拥有很多个账户,但这些账户可能会被关联起来(账户与账户......
  • linux-grep管道命令
    grep命令文件过滤分割与合并grep(globalsearchregularexpression(RE)andprintouttheline,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正......
  • NetCore 入门 (八) : 管道
    1.入门ASP.NETCore是一个Web开发平台,而不是一个单纯的开发框架。这是因为它具有一个极具扩展性的请求处理管道,我们可以通过对这个管道的定制来满足各种场景下的HTTP处理......
  • 匿名函数lambda
    Golang//https://blog.csdn.net/yyclassmvp/article/details/124942527sum:=func(xint,yint)int{returnx*y}Nodejsconstsum=(x,y)=>x*y;Python......
  • js-forEach和匿名函数
    foreach[].foreach(function(item,index,array){ //item:[]中的每一个元素对象 //index:[]中每一个元素对象的索引 //array:[]本身 //循环体})匿名函数arr......
  • JavaScript知识-函数基础知识、匿名函数、闭包函数、箭头函数、js内置对象和方法
    目录JavaScript函数1.函数的语法格式2.无参函数3.有参函数4.关键字arguments5.函数返回值关键字return6.匿名函数(没有函数名)7.箭头函数8.函数的全局变量与局部变量9.闭包......
  • 匿名函数
    定义时不取名字的函数,我们称之为匿名函数,匿名函数通常整体传递给其他函数,或者从其他函数的返回有了匿名函数我们可以给标准库里的内置函数(标准函数)制定特殊规则例如: ......