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
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:

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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With