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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3144
  • Last Modified:

Questions About fork() & waitpid() .... C/C++ Socket Programming in Linux and Unix

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

>>quote "The parent process then resumes execution."

1. What does it resume exactly? What kind of execuion?

"every process is created when another process executes the fork system call. The process that invoked fork is the parent process and the newly-created process is the child process. Every process has one parent process, but can have many child processes. The kernel identifies each process by its process identifier (PID). Process 0 is a special process that is created when the system boots; after forking a child process (process 1), process 0 becomes the swapper process & process 1, known as init, is the ancestor of every other process in the system."

>>quote "The process that invoked fork is the parent process and the newly-created process is the child process. Every process has one parent process, but can have many child processes. "

2. The process that invoked fork is the parent process. Does this mean
a child process and can create a new process?

Every process has one parent process. Parent process always has many child processes. and every child process can create another new child process which makes the first child process becomes a parent to the new child process, and in the same time it is considered a child process to its parent process. It is like a parent creates a child and child creates a new child! so the new child process has a parent of a child process who got another parent. It goes like a chain. Thats how i understand it. Is this right?

If yes, then

a. when the parent process will get a chance to create several child process who do not have another child process under them?

If no,
b. can you fix it for me please?




0
F-J-K
Asked:
F-J-K
  • 3
  • 3
1 Solution
 
sunnycoderCommented:
>1. What does it resume exactly? What kind of execuion?

parent()
{
    ...
    waitpid(...);
    next_statement;  <---- this is where it resumes. Right after the waitpid
}

>2. The process that invoked fork is the parent process. Does this mean
>a child process and can create a new process?

Yes

Parent process always has many child processes. and every child process can create another new child process which makes the first child process becomes a parent to the new child process, and in the same time it is considered a child process to its parent process. It is like a parent creates a child and child creates a new child! so the new child process has a parent of a child process who got another parent. It goes like a chain. Thats how i understand it. Is this right?

Thats correct .. except parent process does not *always* have many child process ... only when it forks them

>a. when the parent process will get a chance to create several child process who do not have another child
>process under them?
The child creation is not automatic ... you need to call fork() to create a child process ... child in turn calls fork() to create its child process. If child does not call fork() there would be no grandchildren of the parent process.

If no,
>b. can you fix it for me please?
Answered above.
0
 
F-J-KAuthor Commented:
>>If child does not call fork() there would be no grandchildren of the parent process.

 if a child got a parent who is actually a child of another parent and then that parent (who is a child of another parent process) died, how this grandchild is going to react? Is its grandparent will take care of it, or init() will come and pick that child whose parent (who is a child of another parent) has died?

What i know is, init() will only take place of the died parent, the parent who is created after init() immediately, since init() considers the rest are just children, only the first process that came after init() is the parent of the rest of other children....That how i understand it. Am i right?

I hope my question is clear.....

Thanks for you reply
0
 
sunnycoderCommented:

If a parent dies before child dies, init (not a grandparent or grandgrandparent) acts as parent of the orphaned process. Above links have a lot of details for all scenarios.

Also see
http://www.ibm.com/developerworks/aix/library/au-unixprocess.html
http://www-cdf.fnal.gov/offline/UNIX_Concepts/concepts.zombies.txt
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
F-J-KAuthor Commented:
GREAT...

Ok, last question...

lets say X parent process got a child Y & this child Y got another child Z.

Does X considers Z as one of its childs?


THANKS! I appreciate your help...
0
 
sunnycoderCommented:
>Does X considers Z as one of its childs?
No
0
 
F-J-KAuthor Commented:
Well Done
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now