Skip to content

Instantly share code, notes, and snippets.

@b0bu
Last active March 25, 2022 17:10
Show Gist options
  • Save b0bu/6c384f679efb0fbe2ba6a23950b7424d to your computer and use it in GitHub Desktop.
Save b0bu/6c384f679efb0fbe2ba6a23950b7424d to your computer and use it in GitHub Desktop.
How fork() works

fork() causes the current process to reun itself again and these lines of execution run concurrently from the point at which fork() is called.

A process is a program in execution. The main process has a line of execution. For each call to fork() that line of execution splits. The main process continues but there is now a child process which continues from the same point. If fork() is called once, then a new branch of exection spins off and each branch exectes the same code from the same point forward, even another call to fork()

For every subsequent fork() each current line of execution must branch. For 1 call it's 2 branches, for 2 it's 4, for 3 it's 8 and so on. total_pids = 2^number_of_forks.

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment