[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parent Process and Child Process - fork() in C/C++ Confuses Me! Can You End It For Me Please?

Posted on 2008-11-11
2
Medium Priority
?
5,135 Views
Last Modified: 2013-11-13
I'm new to multi-threading concepts. It confuses me alot.

I got several questions on this quote:

"Waitpid() - In modern computer operating systems, a process (or task) may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent(its own) process while the child executes. When the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution. The exit status returned by a child process typically indicates whether the process terminated normally or abnormally"

>> In modern computer operating systems, a process (or task) may wait on another process to complete its execution.

1. the idea of multi-threading is not implemented here since a process (parent process) is keep waiting on another process (child process) to complete its execution. right?

2. as it is known that a parent can have a child and this child can have new child and regarding question 1,  that every parent process is keep waiting its child. Eventually, we will have alot of processes waiting for the last appeared process to finish its execution. Does not make sense to me! can you clarify?

>> In most systems, a parent process can create an independently executing child process. The parent process may then issue a wait system call, which suspends the execution of the parent(its own) process while the child executes.

3. Isn't parent process creates a newly independent child process. So how come the parent process get suspended while the child executes?! I believe parent process can get suspended temporarily only when a child process exits. The goal of parent suspension is to grab the exit status. After that, parent should go back to its activity...That's how i know it! Please clarify if i'm wrong

4. Are parent process only responsible of creating child process and grab its exit status?
If no, then what kind of other activities parent process does?
If yes, then this kind of a problem because every child process can become a parent process! so we will end up having alot of parent processes who does nothing except grabbing exit status of its X child process & since that X child process can have another Y child, then X child is considered a parent who does nothing  grabbing exit status of its Y child process, and so on...It goes like a chain...please fix me!

>>When the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution. ***The parent process then resumes execution.***

5. This does not make sense, since its going to cost long time until the first parent process get executed, as i said it goes like a chain:

init -> process 2 -> process 3 -> process 4 -> process 5

3, 4, and 5 are all childs but 3 is a parent of 4 and 4 is a parent 5. The child that is executing right now is 5. 4 will have to wait until 5 finishes & then 4 does its job of execution. At this time 2, and 3 are still waiting & so on ....

Can you please explain it in a plain English language, because i always get confused in concepts of threads, childs, parents, etc....

I'm looking forward to hearing from you!
0
Comment
Question by:F-J-K
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 22936532
>>> In modern computer operating systems, a process (or task) may wait on another process to complete its execution.

>1. the idea of multi-threading is not implemented here since a process (parent process) is keep waiting on another process (child process) to complete its execution. right?

Multi-threading is a different concept altogether ... A thread is not the same as process. A thread is a light weight process.
http://en.wikipedia.org/wiki/Thread_(computer_science)

>2. as it is known that a parent can have a child and this child can have new child and regarding question 1,  that
>every parent process is keep waiting its child. Eventually, we will have alot of processes waiting for the last
>appeared process to finish its execution. Does not make sense to me! can you clarify?

Parent does not necessarily have to wait ... It *may* choose to wait by explicitly calling wait() or waitpid(). There are circumstances where you may want the child process to finish before parent proceeds. e.g. If child process creates a list of file on which the parent does processing. Parent cannot continue until child has finished execution. Hence parent will wait on child.

>3. Isn't parent process creates a newly independent child process.
Thats correct

>So how come the parent process get suspended while the child executes?!
It doesnt ... it may *choose* to wait.

>I believe parent process can get suspended temporarily only when a child process exits.
In the event of child exiting, SIGCHLD is sent to parent process. Parent may choose to ignore this signal or it may handle it. It is free to collect the exit status of child whenever it wishes to. Parent does not suspend.

>The goal of parent suspension is to grab the exit status. After that, parent should go back to its activity...That's
>how i know it! Please clarify if i'm wrong
I would not call it the goal of parent process but this is one of the things it should do. Your understanding is correct.

>4. Are parent process only responsible of creating child process and grab its exit status?
>If no, then what kind of other activities parent process does?
A parent process is a normal process .. it is free to do normal processing like all other processes

>If yes, then this kind of a problem because every child process can become a parent process! so we will end up
>having alot of parent processes who does nothing except grabbing exit status of its X child process & since that X
>child process can have another Y child, then X child is considered a parent who does nothing  grabbing exit status
>of its Y child process, and so on...It goes like a chain...please fix me!
Draw an analogy with humans ... A process (human) may or may not have children ... may have any number of them ... irrespective of number of children, every human (process) has a normal life (execution and processing) ... If a process has children, it is expected to collect their exit status in addition to normal execution.

>>When the child process terminates, it returns an exit status to the operating system, which is then returned to the waiting parent process. The parent process then resumes execution. ***The parent process then resumes execution.***

>5. This does not make sense, since its going to cost long time until the first parent process get executed, as i said
>it goes like a chain:

>init -> process 2 -> process 3 -> process 4 -> process 5

>3, 4, and 5 are all childs but 3 is a parent of 4 and 4 is a parent 5. The child that is executing right now is 5. 4 will
>have to wait until 5 finishes & then 4 does its job of execution. At this time 2, and 3 are still waiting & so on ....

Should be answered by now.
0
 
LVL 1

Author Closing Comment

by:F-J-K
ID: 31515784
Well Done! I appreciate your help alot & you deserve the full points
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Six Sigma Control Plans

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question