⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pipe.c

📁 进程管道通信
💻 C
字号:
#include<unistd.h>

#include<sys/types.h>

#include<sys/wait.h>

#include<stdio.h>

#include<errno.h>

#define INPUT 0

#define OUTPUT 1


int main(){


  int file1[2];

  pid_t child1,child2;

  char buf1[256];

  char buf2[256];

  int returned_count;

  pipe(file1);

 
  if((child1=fork())==0){

     close(file1[INPUT]);

     write(file1[OUTPUT],"child1",6);

	      exit(0);
  }
 
  read(file1[INPUT],buf1,sizeof(buf1));

  open(file1[OUTPUT]);

  if((child2=fork())==0){
 
     close(file1[INPUT]);

      write(file1[OUTPUT],"child2",6);

      exit(0);

      }

  close(file1[OUTPUT]);

  read(file1[INPUT],buf2,sizeof(buf2));


  printf("received from spawned process:\n%s\n%s\n",buf1,buf2);

  return 1;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -