Skip to content
Advertisement

Father-child process use pipe to talk, hangs after “execlp”, why?

I’ve got a simple text file called “tmp” under current directory, I wish to “cat” this file and then “sort” it, I want to use a c program to act like pipe “|” so I tried to use a father/child talk to do this.

Unexpectedly, the program hangs after “cat”, like below:

JavaScript

g++ to compile and run this file, after “cat” tihis file, I don’t even see “child cat ends”, it just hangs.

Where’s the problem, how to fix it? Thanks

Advertisement

Answer

1) The order of arguments in dup2 is incorrect. Look at dup2

2) parameters (stdin/stdout) to dup2 are incorrect.

3) The exec() family of functions replace the process image with a new one. So the code after that call does not get to run (unless the exec() fails), so I removed those.

Here is the code:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement