Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Forks Purpose?

Tags:

linux

fork

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

like image 725
Rage91 Avatar asked Apr 12 '26 05:04

Rage91


1 Answers

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

like image 82
David Tonhofer Avatar answered Apr 14 '26 02:04

David Tonhofer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!