pipe.c
来自「操作系统Interprocess Communication(IPC)实验源码。」· C语言 代码 · 共 44 行
C
44 行
#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <errno.h>#define FD_READ 0#define FD_WRITE 1#define BUF_LEN 100int main(){ pid_t pid; int fildes[2]; char buf[BUF_LEN]; if (pipe(fildes)<0) { perror("pipe"); exit(1); } if ((pid=fork())<0) { perror("fork"); exit(1); } /* create a pipe from father to child */ if (pid>0) { /*in child process*/ printf("In child process\n"); close(fildes[FD_WRITE]); /* close write fd */ if (read(fildes[FD_READ],buf,sizeof(buf))>0) { printf("Read String:%s\n",buf); } } else { printf("In father process\n"); close(fildes[FD_READ]); /* close read fd */ if (write(fildes[FD_WRITE],"Hello World",sizeof("Hello World"))<0) { perror("write"); } } return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?