p10.3.c

来自「《linux编程技术详解》 linux编程入门的好书」· C语言 代码 · 共 51 行

C
51
字号
#include <stdio.h>#include <unistd.h>int main(int argc,char* argv[]){	int f_des[2];	int pid;		if(argc!=3){		printf("Usage: %s comand1 comand2\n",argv[0]);		return 1;	}	if(pipe(f_des)==-1){		perror("cannot create the IPC pipe");		return 1;	}	pid=fork();	if(pid==-1){		perror("cannot create new process");		return 1;	}else if(pid==0){		dup2(f_des[1],STDOUT_FILENO);				close(f_des[0]);		close(f_des[1]);		if(execlp(argv[1],argv[1],NULL)==-1){			perror("in child process,cannot execute the command");			return 1;		}				return 1;	}else {		dup2(f_des[0],STDIN_FILENO);		close(f_des[0]);		close(f_des[1]);			if(execlp(argv[2],argv[2],NULL)==-1){			perror("in parent process,cannot execute the command");			return 1;		}					return 1;	}	return 0;}

⌨️ 快捷键说明

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