LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2008, 11:15 AM   #1
Curtor
Member
 
Registered: Feb 2008
Posts: 65

Rep: Reputation: 16
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.

Last edited by Curtor; 12-05-2008 at 11:15 AM. Reason: Missing close brackets on code
 
Old 12-06-2008, 03:52 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Curtor View Post
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 View Post
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 View Post
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

Last edited by ta0kira; 12-06-2008 at 03:56 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Forking a Project posto Programming 3 08-29-2005 06:39 PM
Linux Forking? linuxmania Linux - Distributions 3 04-07-2005 01:59 PM
Forking in Unix Rv5 Linux - General 5 11-04-2003 04:27 PM
Forking and multiprocessors sniff Programming 4 02-18-2003 06:28 PM
Starting a forking program in terminal Ztyx Linux - General 2 11-09-2002 06:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration