Amazon Interview Question
Testing / Quality AssurancesThe first critical piece is that Fork() returns an integer - it returns 0 to the child process (created by the fork) and a positive number (the process ID number of the created child process) to the parent. The second critical piece is that && and || are evaluated lazily in C - that is, the program only evaluates the second piece if the result is still ambiguous. (ie 'true || foo()' always evaluates true, so why run foo()?) The third critical piece is that && is evaluated before || in C.
In the first line, the original process (call it P0) forks and makes no use of the returned value, so we have 2 essentially identical processes entering line 2 (now P0 and P1).
In the second line, each process runs the first Fork() and spawns a child (P2 and P3). P2 and P3 both return 0 from that fork, while P0 and P1 return a positive integer. For P2 and P3, that 0 is evaluated as false, and so they don't bother to run the second fork. P0 and P1 do run it, creating P4 and P5. At this point, P0 and P1 have returned true to the first and second forks of line 2, and so have evaluated the && as true - no need to evaluate the other side of the ||, so they skip to line 3. P2-5 have each returned false to one of the forks, so they return false to the && and proceed to evaluate the final Fork() of line 2, creating processes P6-9.
On line 3, every process (and there are 10 so far) runs the fork, for a total of 20 processes.
I got 20
basically I got 10 processes for the parent after the first fork.
That will be similar for the child after first fork.
so total 20 including the original one.
I guess 15 cannot be answer for sure as it will be some even number
fork return 0 for child process, and non-zero for parent.
L1: fork
L2: fork && fork || fork
L3: fork.
P0, after L1, got P0, P1. Say P0 is parent, the origin
handle P0 first
P0, L2, after first fork, got P0, P2. P2's L2 work done.
P0, L2, after 2nd fork, got P0, P3. P0's L2 work done.
P3, L2, after 3rd fork, got P3, P4.
For P0, P1, P2, P3, P4, got P5, P6, P7, P8, and P9 respectively after L3 fork.
Similar for P1 from L2.
Total got 20 processes, minus P0, create 19.
the fork() in OR part will get executed only when the first part gives FALSE, right? And we don't know the value returned by the two forks in this part, it may return 0 or 1. so depending on the return value it'll be decided that the second/ORed fork() will get executed or not. In short you can't predict the process count as asked in the question.
if there are n forks in series, then overall, 2 power n processes will be created, i.e one less than 2 power n
New process will be created,
So If we take the above process as a series of processes.
there are 4 fork statements in series, so there exist at most 16 procesess.
please correct me, if i am wrong.
if there are n forks in series, then overall, 2 power n processes will be created, i.e one less than 2 power n
New process will be created,
So If we take the above process as a series of processes.
there are 4 fork statements in series, so there exist at most 16 procesess.
please correct me, if i am wrong.
Actually it depends on whether the compiler implements short-circuit evaluation. If it does, then the second statement will evaluate as follows
fork
/\
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
/ \
&& fork && fork
/\ /\
/ \ / \
/ \ / \
/ \ / \
/ \ / \
&& fork && fork || fork || fork
/ /\
/ / \
/ / \
/ / \
/ / \
SC || fork || fork
where SC is the result of short-circuit evaluation. So in the presence of short-circuit evaluation the answer is 20 threads.
&& implies "short-circuit evaluation", i think it is dependent on the operator. & definitely does not have this.
fork return 0 for child process, and non-zero for parent.
L1: fork
L2: fork && fork || fork
L3: fork.
P0, after L1, got P0, P1. Say P0 is parent, the origin
handle P0 first
P0, L2, after first fork, got P0, P2. P2's L2 work done.
P0, L2, after 2nd fork, got P0, P3. P0's L2 work done.
P3, L2, after 3rd fork, got P3, P4.
For P0, P1, P2, P3, P4, got P5, P6, P7, P8, and P9 respectively after L3 fork.
Similar for P1 from L2.
Total got 20 processes, minus P0, create 19.
i have a doubt at l2 whether compiler interprets l2 as (fork() && fork()) || fork()
or in some other way??
if it interpret it as (fork() && fork()) || fork() then p2 job is not done with the first fork it self it will execute the fork after || so it will create one extra process and that extra process will create one more extra process on line 3 so total no of process is 21.correct me if i am wrong.
main()
{
if(fork()||(fork()&&!fork()))
printf("AA\n");
else if(!fork())
printf("BB\n");
else
printf("CC\n");
}
I tried solving this, got AA 2 times, BB 2 times and CC 2 times but not 100% sure
can someone try solving this and see what could be the output... thanks in advance
can someone explain clearly how come the answer is 20, i'm not able to follow EZ's response. help me out plese
- Anonymous September 17, 2009