功能:
父进程创建一个子进程父进程负责读用户终端输入,并写入管道
子进程从管道接收字符流写入另一个文件
代码:
#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <fcntl.h> #define MAX 100 int main() { int n,c = 0; int fd[2]; char ch; pid_t pid; char buffer[MAX+10]; if(pipe(fd) < 0) { //创建管道 puts("管道创建失败"); exit(1); } pid = fork(); //创建子进程 if(pid < 0) puts("子进程创建失败"); else if(pid > 0) { //父 close(fd[0]); //关闭读端口 puts("please input what you want to say:"); while((ch = getchar()) != ' ') { //读字符串(可空格)到buffer if(c == MAX) { puts("buffer is fulled!"); break; } buffer[c++] = ch; } buffer[c] = '