Papipone
Papipone

Reputation: 1123

Fork : concurrency or parallelism

I was recently doing some experiments with the fork function and I was concerned by a "simple" (short) question:

Thanks for your answer.

nb: Damn I fork bombed myself again!

Edit:

  1. Concurrency: Each operation run on one core. Interrupts are received in order to switch from one process to another (Sequential computation).
  2. Parallelism: Each operation run on two cores (or more).

Upvotes: 2

Views: 2168

Answers (4)

lu5er
lu5er

Reputation: 3564

Well, you can do something. Write a complex program say, O(n^3), so that it takes a good amount of time to compute. fork() four times (if you have quad-core). Now open any graphical CPU monitor. Anything cool?

Upvotes: -1

user6555228
user6555228

Reputation:

In fact, fork() is a system call (aka. system service) which creates a new process from the current process (read the return code to see who you are, the parent or the child).

In the UNIX work, processes shares the CPU computing time. This works like that :

  1. a process is running
  2. the clock generates an interrupt, calling the kernel and pausing the process
  3. the kernel takes the list of available processes, and decide to resume one (this is called scheduling)
  4. go to point 1)

When there is multiples processor cores, kernels are able to dispatch processes on them.

Upvotes: 1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137487

fork() duplicates the current process, creating another independent process. End of story.

How the kernel chooses to schedule these processes is a different, very broad question. In general the kernel will try to use all available resources (cores) to run as many tasks as possible. When there are more runnable tasks than cores, it has to start making decisions about who gets to run, and for how long.

Upvotes: 4

Martin Nyolt
Martin Nyolt

Reputation: 4650

The fork function creates a separate process. It is up to the operating system how it handles different processes.

Of course, if only once core is available, the OS has no other choice but running all processes interleaved.

If more cores are available, every sane OS will distribute the processes to the different cores, so every core runs at least one process. However, even then, more processes can be active than there are cores. So even then, it is up to the OS to decide which processes can be run parallel (by distributing to cores) and which have to be run interleaved (on a single core).

Upvotes: 2

Related Questions