Pergunta de entrevista da empresa Riverbed Technology

what happens when you call fork() fork () fork()

Respostas da entrevista

Sigiloso

11 de jul. de 2012

A correction to Raj's formula is the following: Assuming M initial threads and N total forks, this will create a total of M * (2^N) threads. The fork() operation creates a copy of the process that called it. Step 1) That means that starting with 1 initial thread, you call the first fork() operation and you have two identical processes. [Total of 2 processes so far] Step 2) Those two identical processes then go on to call the 2nd fork(), which create identical processes of the two processes calling the operation. [Total of 4 processes so far] Step 3) Those four identical processes then go on to call the last remaining fork(), which create identical processes of the four processes calling the operation. [Total of 8 processes] I encourage anyone to correct any potential mistakes I might have made. The last thing I'd want is to wrongly inform someone going into an interview.

4

Sigiloso

22 de jun. de 2011

@raj: Your formula doesn't quite work if there's only one thread running.

1

Sigiloso

27 de jan. de 2010

8 processes(2^n) will be created

3

Sigiloso

16 de set. de 2010

Assuming M initial threads and N total forks, this will create a total of M * (M^N - 1) threads.