Why is the return code of child required by parent ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why is the return code of child required by parent ?
# 1  
Old 11-27-2012
Question Why is the return code of child required by parent ?

Hello everyone,
I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent process, meaning that the child has finished executing. Then the parent process calls wait() to get the return code of the child process and once it has that it removes the entry of the child process from the process table.
Now when the child process finishes execution before the parent and if the parent fails to retrieve the return code sent by the child the entry of the of the process won't be removed from the table, this is what is known as a zombie process.

Now my questions are-:
1) How will parent process get the return code from the child process? Is it the same value that is returned by the fork() system call in the parent process?
2) Why does the parent need the return code return by the child ? Why is it so necessary to get the code?
3) Why does the parent process need to execute wait() to get the return code? I mean how does wait() or halting execution get it the return code ? And does it execute wait() on itself or on the child process??

Thank You.
# 2  
Old 11-27-2012
Quote:
Originally Posted by sreyan32
Hello everyone,
I am a complete newbie to UNIX. I am using Debian LXDE 64-bit. I have a question regarding the child and parent process communication. According to wikipedia.org and various other sources, when a child process exits it sends the SIGCHLD signal to its parent process, meaning that the child has finished executing. Then the parent process calls wait() to get the return code of the child process and once it has that it removes the entry of the child process from the process table.
Now when the child process finishes execution before the parent and if the parent fails to retrieve the return code sent by the child the entry of the of the process won't be removed from the table, this is what is known as a zombie process.

Now my questions are-:
1) How will parent process get the return code from the child process? Is it the same value that is returned by the fork() system call in the parent process?
2) Why does the parent need the return code return by the child ? Why is it so necessary to get the code?
3) Why does the parent process need to execute wait() to get the return code? I mean how does wait() or halting execution get it the return code ? And does it execute wait() on itself or on the child process??

Thank You.
A process becomes a zombie when it exits. The zombie remains in the process table until another process collects its exit status. As long as the process that started the zombie (its parent) is still running, only it can wait() for for the zombie to collect its exit status.

If a parent exits without collecting the exit status of its children (zombies or still running), a system process (usually known as init) will collect the status of those zombie children when the parent dies and will collect the status of the orphaned children that were still running when they die.

In response to your other questions:
  1. The exit status of the dead child is collected by calling wait(), waitid(), or waitpid() (all of which are defined by the POSIX standards and Single UNIX Specification) or a similar system specific system call. The return code from fork to the parent is the process ID of the child. The return code from one of the functions above will be the process ID of the child that exited or stopped. The exit status will be stored in the location pointed to by one of the arguments passed to those routines
  2. The exit status of the child tells the parent if the child exited normally (and if so what value was passed to exit() when the child terminated itself), was terminated by the receipt of a signal, or was stopped by a tracing event. (You don't need to worry about tracing events for now.)
  3. Look at the man page for the wait() system call on your system. The parent calls wait to get the status of one of its children. The return code from wait() will tell you which child's status change caused wait to return.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

2. Shell Programming and Scripting

Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue: I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh ... (3 Replies)
Discussion started by: gvolpini
3 Replies

3. 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

4. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

5. Programming

getting the return from forked child process to parent in C++

This needs to work on HPUX and Linux. I do a fork and create a child process. During execution of the child process, it is possible child become lost or get killed. That is the reason why I create the child process. However if the child process doesnt get killed, I do want to know the return... (2 Replies)
Discussion started by: usustarr
2 Replies

6. Programming

To share fd between parent and child

i used function fork(). so i made two process. parent process accepted socket fd and writing to shared memory. then now. how can child process share parent's socket fd? is this possible? Thanks in advance (1 Reply)
Discussion started by: andrew.paul
1 Replies

7. Shell Programming and Scripting

How to return control from the child script to the parent one?

I have two shell scripts : A.sh and B.sh A.sh echo "In A" exec B.sh echo "After B" B.sh echo "In B" The output is : In A In B I want the output : In A In B After B (4 Replies)
Discussion started by: suchismitasuchi
4 Replies

8. Programming

getting the return code of forked child process (ftp)

Hi, From within my C++ program, I fork a child process and execl an ftp session (solaris), like this : std::string szStartCmd = "ftp -i -n -v 192.168.149.31"; int nExecRes = execl("/bin/sh", "sh", "-c", szStartCmd.c_str(), (char *)0); I use 2 pipes to communicate between my... (7 Replies)
Discussion started by: KittyJ
7 Replies

9. Shell Programming and Scripting

return valuse from child script to parent script

Hi, I am trying to return a value from child script to a parent script just as a function does. The child script will look for a file and if exists will return 1 else 0. I need to capture the status 1 from child script in the parent script and proceed further. if 0, i need not do... (1 Reply)
Discussion started by: borncrazy
1 Replies

10. Filesystems, Disks and Memory

How hard can it be? ps child/parent

:( Since I'm fairly new to the scene and don't have much experience in shell programming, I decided to check out the net for a useful script or two. What I'm looking for is a script that would let me enter a PID and then show the process tree associated with it. So it would display the (grand-)... (2 Replies)
Discussion started by: velde046
2 Replies
Login or Register to Ask a Question