我想要打印管道内容

时间:2017-02-12 14:37:21

标签: c unix operating-system

我想将管道内容打印到屏幕。特别是我希望父进程在屏幕上打印发送子进程的管道内容。我写了一个代码,但这不能很好。我有一个分段错误错误为什么?谢谢大家!

这是我的代码:

enter code here
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>

int main(int argc,char* argv[]){

int fd[2]; //File descriptor for the pipe;
pid_t P11; //Process child PID;
int status;
pipe(fd); //Make a pipe;

int buff[10000000000],i;

P11=fork();

if(P11<0){
printf("\nError\n");
exit(-1);
}

if(P11==0){
//This is the child process;
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("ls","ls","-l",NULL);
          }
else{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);

read(0,&buff,sizeof(buff)); //Read the pipe content;
waitpid(P11,&status,0); //Wait the child process;

//Print the pipe content;
for(i=0;i<10000000000;i++)
printf("\nPrint: %c\n",buff[i]);

//Father's exec;
execlp("cd","cd"," ..",NULL);
     }

return 0;
 }

0 个答案:

没有答案