📄 p11-1.c
字号:
#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include "err_exit.h"int main (void){ pid_t pid; int n, mypipe[2]; char buffer[BUFSIZ+1]; char some_data[] = "Hello, world!"; /* 创建管道 */ if (pipe (mypipe)) err_exit("Pipe failed.\n"); /* 派生子进程 */ pid = fork (); if (pid < (pid_t) 0) err_exit ("Fork failed.\n"); /* fork 失败 */ else if (pid == (pid_t) 0) { /* 这是子进程 */ close(mypipe[1]); n = read(mypipe[0],buffer,BUFSIZ); printf("child %d: read %d bytes: %s\n",getpid(),n,buffer); } else { /* 这是父进程 */ close(mypipe[0]); n = write(mypipe[1], some_data, strlen(some_data)); printf("parent %d: write %d bytes\n", getpid(),n); } exit (EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -