📄 p11-2.c
字号:
#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include "err_exit.h"/* 从管道读字符然后输出到stdout */void read_from_pipe (int fd0){ FILE *stream; int c; stream = fdopen (fd0, "r"); /* 建立描述字与标准I/O流的连接 */ printf("read from pipe:"); while ((c = fgetc (stream)) != EOF) putchar (c); fclose (stream);}/* 写出一些字符至管道*/void write_to_pipe (int fd1){ FILE *stream; stream = fdopen (fd1, "w"); /* 建立描述字与标准I/O流的连接 */ fprintf (stream, "hello, world!\n"); fclose (stream);}int main (void){ pid_t pid; int mypipe[2]; /* 创建管道 */ 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]); read_from_pipe (mypipe[0]); } else { /* 这是父进程 */ close(mypipe[0]); write_to_pipe (mypipe[1]); } exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -