Question

in fork() , when a child process is created , usually parent waits for child to finish , so I was wondering what is the reason child process is created if it has to wait idle for it to finish anyways instead of doing the job itself? I tried reading some stuff about it but instead it for me more confused

Was it helpful?

Solution

You are confused: The parent may "wait" for the child but does not necessarily mean it does nothing before waiting. The parent does its thing, then calls wait. If the child has already finished, wait returns immediately, otherwise the parent may be idle (i.e. not not be scheduled for execution by the Operating System) for some time, until the child is actually done.

An example: the parent in green forks the child in yellow. The child may finish before or after the parent waits for it:

processes

Some situations demand that the parent do nothing, e.g. it spawns a number of worker processes, then waits for them all to finish, thus acting simply as a manager...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top