tags:

views:

710

answers:

4
A: 

Your problem is proably here:-

// Wait for child to terminate int status; wait(&status);

As the child process is a deamon it wont terminate anytime soon.

Also your "read()" is likely to hang. You are going to have to decide how long you wait before abandoning any attempt to display output.

James Anderson
A: 

As the child process is a deamon it wont terminate anytime soon.

Are you sure? Of course I would agree that a daemon won't terminate anytime soon - but when a daemon starts up it forks so the child can disassociate itself with the terminal, and then the parent exits. Since the wait() system call is waiting on the parent daemon process, it should exit.

Regardless, the same problem occurs without the call to wait().

Also, why doesn't the read() get an EOF? The read() is reading from an open pipe that is connected with the parent daemon process. So when the parent daemon process exits, the read() should return immediately with an EOF.

A simple way to resolve this is just to attach gdb to the program and step through the program to see if it's hanging on `wait`.
Hao Lian
A: 

I think you should receive the SIGPIPE signal when waiting on the read to complete, since the other end of the pipe is closed. Did you make anything unusual with the signal ? I suggest you run your code with the strace command.

shodanex
You will never get a SIGPIPE for a read() operation. If the other end of the pipe you are read()ing is closed, read() will eventually return 0 indicating EOF. If the other end of the pipe you are write()ing is closed, you'll get a SIGPIPE unless your process ignores SIGPIPE (e.g. with signal(SIGPIPE, SIG_IGN)). In this case, write() would return -1, and errno would become EPIPE.
pts
+1  A: 

The most probable solution is adding close(fd[1]); above the execl().

The reason why your program hangs is that the read() function waits for the daemon to write something to its stdout/stderr. If the daemon (including the child process of your program, and also the child process' forked children who keep their stdout/stderr) doesn't write anything and there is at least one process holding the writable end of the pipe open, read() will never return. But which is that process, which is holding the writable end of the pipe open? It is most probably the child of your program's child, the long-running daemon process. Although it may have called close(0); and close(1); when daemonizing itself, most probably it hasn't called close(fd[1]);, so the writable end of the pipe is still open.

pts