How to Join with child process?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Join with child process?
# 1  
Old 10-14-2005
How to Join with child process?

Java and perl have a join feature that allows the main thread to block until a child thread is finished.

Is there a similar feature in (1) bash and (2) perl that will allow the main process to block until the child process is complete?

Thanks,
Siegfried
# 2  
Old 10-14-2005
in bash at least it is "wait"
You can wait for jobs, pids or all children.

Perl no doubt has something similar
# 3  
Old 10-14-2005
How to wait on multiple child processes?

Let us say I want to create five child processes while the parent process waits until they are all done.
(1) Normally I create a child process with the "&". How do I get the pid? Do I have to parse the "ps" command with perl or awk?
(2) How do I specify multiple child processes? I tried "info wait" and "man wait" with no luck. I also tried "info bash" and "man bash" and still no luck. However, I see that my distro does implement wait. Apparently it takes a single integer.

I tried "sleep 40&" and "sleep 30&" and "ps" and "wait 6088 5966" and that almost worked. It came back and said "pid 5956 is not a child of this shell after waiting for 6088". I'm checking my work. wait wants the first column of ps labeled "PID", correct?

Sieg
# 4  
Old 10-14-2005
Quote:
Originally Posted by siegfried
Let us say I want to create five child processes while the parent process waits until they are all done.
(1) Normally I create a child process with the "&". How do I get the pid? Do I have to parse the "ps" command with perl or awk?
foo &
fooPID="${!}"
echo "foo's pid -> [${fooPID}]"

Quote:
Originally Posted by siegfried
(2) How do I specify multiple child processes? I tried "info wait" and "man wait" with no luck. I also tried "info bash" and "man bash" and still no luck. However, I see that my distro does implement wait. Apparently it takes a single integer.

I tried "sleep 40&" and "sleep 30&" and "ps" and "wait 6088 5966" and that almost worked. It came back and said "pid 5956 is not a child of this shell after waiting for 6088". I'm checking my work. wait wants the first column of ps labeled "PID", correct?

Sieg
correct. Get the pid's of your background processes as outlined above and pass them to 'wait'.
# 5  
Old 10-15-2005
How to wait for logical "OR"

Since my info/man pages are incomplete, I am assuming wait implements a logical "AND" allowing my parent process to sleep until all the children have died.

(1) How would I implement an "OR" wait where the parent waits for the first child to die, examine the return status code of that child, and possibly spawn a new child to take its place?

The children will all be running perl.

(2a) I've tried writing synchronous perl children use various integer values for exit function, but I cannot figure out how to detect the exit values in bash. How is this done for a synchronous child?

(2b) How do I detect the return status code for an asynchronous child when the parent is blocking on multiple children wait for the first to die?

Thanks much!
Siegfried
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting child process id for a given PID

HI Am trying to get child process id for a PID using ksh.. ps -ef | grep xntpd root 3342472 2228308 0 12:17:40 - 0:00 /usr/sbin/xntpd root 4522024 6488316 0 12:18:56 pts/0 0:00 grep xntpd root 6291614 3342472 0 12:17:40 - 0:00 /usr/sbin/xntpd Here now i... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

5. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

6. Shell Programming and Scripting

Child Process Name

Hi , I want to find out the child process name given its PID. I have used the ps command but it displays the parent process name against child PID. Is there any way to find out name of child program executing under any parent program? (1 Reply)
Discussion started by: sneha_heda
1 Replies

7. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

8. UNIX for Dummies Questions & Answers

about child process

hello every one, i want to know more about creation of child process. UNDER WHAT CIRCUMSTANCES child process is created? WHAT ARE THE PREREQUISITES for a child process to be created? let us say we have a prog.c, prog.obj(compiled.c),.a\.out files. is any child PROCESS CREATED... (12 Replies)
Discussion started by: compbug
12 Replies

9. Programming

creating child process

i want to create 3 child processes from the same parent using folk. I know how to use folk but my child processes did not come from the same parent. Any suggestion what i did wrong ? (12 Replies)
Discussion started by: Confuse
12 Replies

10. UNIX for Dummies Questions & Answers

KDM child process

Hello all, I got this little problem. I don't know what happen, but its not stopping work but is more of an FYI. I have this funny process running when I do ps -aef (on RedHat AS3 ) server I get this funny child process. root 2345 1 .... /usr/bin/kdm -nodaemon root... (6 Replies)
Discussion started by: larryase
6 Replies
Login or Register to Ask a Question