pipe1.c

来自「本文件介绍了unix内核的c源代码」· C语言 代码 · 共 32 行

C
32
字号
#include <unistd.h>#include <stdio.h>void main(){	int fildes[2];	pid_t pid;	int i, j;	char buf[256];	if (pipe(fildes) < 0)	/* 创建管道 */	{		fprintf(stderr, "pipe error!\n");		return;	}	if ((pid = fork()) < 0)	/* 创建子进程 */	{		fprintf(stderr, "fork error!\n");		return;	}	if (pid == 0)		/* 子进程 */	{		close(fildes[1]);		memset(buf, 0, sizeof(buf));		j = read(fildes[0], buf, sizeof(buf));		fprintf(stderr, "[child] buf=[%s]len[%d]\n", buf, j);		return;	}	/* 父进程 */	close(fildes[0]);	write(fildes[1], "hello!", strlen("hello!"));	write(fildes[1], "world!", strlen("world!"));}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?