资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

[Linux管道和IPC]在父子进程中使用管道

#include 
#include 
#include 
#include 
#include 

int main(int argc,char *argv[])
{
  int n, fd[2];            
  pid_t pid;
  char buffer[25];  //缓冲区
  if(pipe(fd)<0)    //创建一个管道,两个文件描述符在fd数组中
  {
    printf("创建管道失败!\n ");
    exit(0);
  }
  if((pid=fork())<0)    //创建一个子进程
  {
    printf("创建子进程失败!\n ");
    exit(0);
  }
  else if (pid>0)   //父进程
  {
    close(fd[0]);
    write(fd[1],"This is a pipe test!\n",22);  //向管道写入数据,注意回车换行符
  }
  else  //子进程
  {
    close(fd[1]); //关闭
    n = read(fd[0],buffer,25);  //从通道中读出数据
    printf("%s",buffer);  //将数据写到标准输出设备
  }
  exit(0);
}

分享标题:[Linux管道和IPC]在父子进程中使用管道
文章起源:http://cdkjz.cn/article/gjephp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220