【C语言】无名管道
作者:互联网
#include <unistd.h> #include <sys/types.h> #include <stdio.h> int main(void) { int fd[2]; int pid; if(pipe(fd) == -1) perror("pipe");
//创建子进程 pid = fork();
//判断如果是父进程 if(pid > 0) {
//关闭管道读端 close(fd[0]); sleep(5);
//写入ab两个字母 write(fd[1],"ab",2); while(1); }
//判断子进程 else if(pid == 0) { char ch[2]; printf("Child process is waiting for data: \n");
//关闭管道写端 close(fd[1]);
//读取管道两个字母 read(fd[0],ch,2); printf("Read from pipe: %s\n",ch); } return 0; }
标签:ch,int,pid,无名,pipe,include,管道,fd,C语言 来源: https://www.cnblogs.com/powercool/p/16582897.html