LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   terminal forking and interrupts (https://www.linuxquestions.org/questions/programming-9/terminal-forking-and-interrupts-688555/)

Curtor 12-05-2008 10:15 AM

terminal forking and interrupts
 
I'm working on an interesting problem here, that is simi-related to the previous post I made 'Finding all your children'.

Language: C++
System: Linux

So, if you exec something like...
Code:

execlp( "/bin/sh", "/bin/sh", "-c", "./doNothing", (char*)NULL );
The current process 'becomes' a shell command that runs 'doNothing' and happily waits for in and out streams. If you kill that shell process, the 'doNothing' sticks around, happily processing.

On the other hand, if you run something like...
Code:

execlp( "/bin/sh", "/bin/sh", "-c", "./doNothing &> /dev/null", (char*)NULL );
The current process 'becomes' a shell command that runs 'doNothing' and exits, as there is no in stream for the program and the out stream has been taken care of. Meanwhile, the 'doNothing' process is still around.

Now, here is my question. What does the terminal do?
When I run ...
Code:

> /bin/sh -c ./doNothing &> /dev/null
At my terminal, I don't get the terminal back. Why not? What is it doing differently. The terminal doesn't 'exec' into the shell command, it has to fork and then execute it. In addition to that, any interrupt (^C, closing the terminal, etc.) to the terminal will cause the shell command AND the processes to exit.

ta0kira 12-06-2008 02:52 PM

Quote:

Originally Posted by Curtor (Post 3365571)
Code:

execlp( "/bin/sh", "/bin/sh", "-c", "./doNothing", (char*)NULL );
The current process 'becomes' a shell command that runs 'doNothing' and happily waits for in and out streams. If you kill that shell process, the 'doNothing' sticks around, happily processing.

What makes you think that? The exec-ed shell just happens to be the new program replacing the current process, and when it forks for ./doNothing it will waitpid (or equivalent,) while ./doNothing will have inherited standard I/O/E. Because the shell isn't a session leader and it might not change the process group of ./doNothing, the shell's exit probably doesn't prompt the exit of ./doNothing and the original terminal owner sees that something is still active in the current foreground group.
Quote:

Originally Posted by Curtor (Post 3365571)
Code:

execlp( "/bin/sh", "/bin/sh", "-c", "./doNothing &> /dev/null", (char*)NULL );

This no doubt forces the creation of a new process group because it's necessary to control the two processes as one; therefore, if the shell exits then the session leader sees that nothing is left in the foreground process group and it takes back control of the terminal, but that doesn't mean that ./doNothing isn't still processing.
Quote:

Originally Posted by Curtor (Post 3365571)
Code:

> /bin/sh -c ./doNothing &> /dev/null

Code:

> /bin/sh -c './doNothing &> /dev/null'
The redirection takes place in the session-leader shell in your third line, whereas in the second it takes place within the subshell.
ta0kira


All times are GMT -5. The time now is 06:53 AM.