I coded a basic shell in C for executing basic commands like ls , pwd , ...
I have a feature that can make a process run in the background instead of waiting until its done for example : ls & instead of ls (I have a list that contains the processes that run in the background now)
The whole thing is that I want to know when a process is done or not , so I can remove it from the list .
thanks
Asked
Active
Viewed 3,380 times
-2

Rawhi
- 6,155
- 8
- 36
- 57
-
There must be dozens, if not more freely available examples of shells that do this… perhaps you should search for just one of them and read its source? – mah Nov 06 '13 at 21:49
-
do you have a specific one ? – Rawhi Nov 06 '13 at 21:51
-
http://stackoverflow.com/questions/642775/where-to-find-example-source-code-for-a-very-simple-linux-shell seems to have promise. Edit: They might actually be too simple for what you need, but finding any simple shell that supports background tasks is likely to have what you need. – mah Nov 06 '13 at 21:53
1 Answers
1
When a child process terminates, SIGCHLD
signal is sent to parent. waitpid
function with WNOHANG
flag can be used to check if a child process has terminated or is still running. Probably, you'll want to combine those both methods.

el.pescado - нет войне
- 18,889
- 4
- 46
- 89
-
Thanks a lot, it works now I created a function called signalHandler to handle the SIGCHLD signal , that when it comes, the parent process waits for the child using : pid = waitpid(-1, &cstatus, WNOHANG); – Rawhi Nov 07 '13 at 16:14
-
What I didn't understand is that how we can wait for a process that just sent a signal which indicates the it has already finished ! – Rawhi Nov 07 '13 at 16:16
-
1It's not the process that sends signal, it's kernel that sends it on behalf of terminated process. – el.pescado - нет войне Nov 07 '13 at 16:46