网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

[Linux管道和IPC]在兄弟进程中使用管道

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define BUFSIZE 4096  //定义一个最大的读写空间
int main(void)
{
    int fd[2];
    char buf[BUFSIZE] = "hello!I am your  brother!\n";        // 缓冲区
    pid_t pid;
    int len;
    if ( (pipe(fd)) < 0 )           //创建管道
     {
        perror("pipe failed\n");
     }
    if ( (pid = fork()) < 0 )        //创建第一个子进程
     {
        perror("fork failed\n");
     }
    else if ( pid == 0 )            //子进程
    {
        close ( fd[0] );                //关闭不使用的文件描述符
        write(fd[1], buf, strlen(buf));   //发送字符串
        exit(0);
     }
    if ( (pid = fork()) < 0 )             //创建第二个子进程
     {
        perror("fork failed\n");
     }
    else if ( pid > 0 )             //父进程
    {
        close ( fd[0] );
        close ( fd[1] );
        exit ( 0 );
     }
    else                       //第二个子进程中
    {
        close ( fd[1] );                  //关闭管道文件描述符
        len = read (fd[0], buf, BUFSIZE);  //读取消息
        write(STDOUT_FILENO, buf, len); //将消息输出到标准输出
        exit(0);
     }
    return 0;
}

本文题目:[Linux管道和IPC]在兄弟进程中使用管道
网站URL:http://bjjierui.cn/article/iihpsi.html

其他资讯