my_pipe.c

来自「Unix进程通讯及同步互斥机制」· C语言 代码 · 共 32 行

C
32
字号
#include <stdio.h>#include <unistd.h>int main(int argc, char* argv[]){	int pid;	int fd[2];	if(argc<3)	{		exit(0);	}	if (pipe(fd) <0 )		printf("Pipe Error");	if ((pid= fork())<0)		printf("Fork Error");	else if (pid>0)	{		close(fd[0]);		/*close the read port*/		if(dup2(fd[1],STDOUT_FILENO)!=STDOUT_FILENO)			printf("Dup Error");		execl("/bin/sh","sh","-c",argv[1],(char*) 0);	/* use execl to use sh mode execute the cat command */	}	else	{		close(fd[1]);		/*close the write port*/		if(dup2(fd[0],STDIN_FILENO)!=STDIN_FILENO)			printf("Dup Error");		execl("/bin/sh","sh","-c",argv[2],(char*) 0);	}	exit(0);}

⌨️ 快捷键说明

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