【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; }

 

posted @ 2022-08-13 13:38  老年新手工程师  阅读(72)  评论(0编辑  收藏  举报