Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c - Pipe implementation

I am trying to implement a linux shell that supports piping. I have already done simple commands, commands running in background, redirections, but piping is still missing.

I have already read about it and seen some snippets of code, but still haven't been able to sort out a working solution.

What I have so far:

int fd[2];

pipe(fd);

pid_t pid = fork();

if (pid == -1)  
   return -1;  

if (pid == 0)  
{  
   close(fd[1]); //close write to pipe, in child
   execlp("cat", "cat", "names.txt", NULL);
}

else
{
   close(fd[0]); //close read from pipe, in parent
   execlp("sort", "sort", NULL);
}  

I am a novice programmer, as you can probably tell, and when I am programming something I don't know much about, this being obviously the case, I like to start with something really easy and concrete and then build from there.

So, before being able to implement three and more different commands in pipeline, I would like to be able to compute "ls names.txt | sort" or something similiar, in which names.txt is a file of names alfabetically unordered.

Updated code, but still doesn't work.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to replace one child's stdout with the writing end of the pipe, and the other child's stdin with the reading end:

if (pid == 0)  
{  
   close(fd[0]); //close read from pipe, in parent
   dup2(fd[1], STDOUT_FILENO); // Replace stdout with the write end of the pipe
   close(fd[1]); // Don't need another copy of the pipe write end hanging about
   execlp("cat", "cat", "names.txt", NULL);
}
else
{
   close(fd[1]); //close write to pipe, in child
   dup2(fd[0], STDIN_FILENO); // Replace stdin with the read end of the pipe
   close(fd[0]); // Don't need another copy of the pipe read end hanging about
   execlp("sort", "sort", NULL);
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...