Amazon Interview Question
Software Engineer / DevelopersCountry: India
This is how I got 20
A: fork();
B:fork() && fork() || fork()
C:fork();
A: fork() 2 processes (1 parent and 1 child)
B: fork() && fork() || fork() 4(both fork) + 4(both fork) + 2(only child do this fork) = 10 since this is a single execution statement we need add them
C: fork() 20
When a process execute fork() then we have two processes in total. If two processes fork() then we have 4 in total. Before B execution starts there are two processes.
I think it's 20 including the program's original process. Here's a few things that might help when solving it:
- Labeling the fork calls and drawing a process spawn tree helps a lot
- fork() returns zero in the child and returns the child's PID in the parent. This has an effect on which of the three middle fork() calls get executed in each process.
- logical AND has precedence over logical OR
First of all , please explain ur answer how did u get it?
my answer is 12
A: fork();
B:fork() && fork() || fork()
C:fork();
From A two process are there Parent and child
now both execute from B
so for both total process will be same
we need not count for both but one
lets count for parent
after statement B; parent executes first fork of and operation and get a non zero value
so now two process in the system
p1 and c1
p1 executes second fork of and operation and gets non zero value and c2
so no need to execute OR operation
so due to p1 ,c1 and c2 gets spawned
now both c1 and c2 should execute from next statement i suppose
for each 1 one process (p1,c1 , c2) fork() of C gets one more process
so total = 2 *[ (p1 + 1)+(c1+1) + (c2+1)] = 2*(6) = 12
any Comments ??/
f#x : fork no. "x" as appearing in program text
0 : return value of child process of the fork that created the branching
1 : return value of parent process of the fork that created the branching
control branches
/ \
/ \ f#1
0 1 this subtree has equal #leaves as its sister
/ \
/ \ f#2
0 1
/ \ / \
f#4 / \ / \ f#3
0 1 0 1
/\ /\ /\ |\
f#5 / \ f#5 / \ / \ f#4 | \ f#5
0 1 0 1 0 1 0 1
/\ /\
f#5 / \ / \ f#5
0 1 0 1
total no. of leaves = 2* (10) = 20
24.
I agree with the anonymous guys explanation
@ Russel: in the 'fork() && fork() || fork()' expression, since '&&' takes precedence over '||' first the 2 processes from (a) will each produce 4 processes each giving a total of 8 processes (4 parents and 4 children).... now the '|| fork()' process will operate on either of the two options:
a) the child process from the first fork()
b) the child processes (4 of them) from the 'fork() && fork()' operation.
Which one is correct? I believe its the latter.
So the '|| fork()' operates on the 4 children giving a total of 4+8 = 12 processes.
This when passed through the final fork, gives out 24.
Please correct me if Im wrong here.
Will it be 12? I got 12 in the following manner:
Let's start with a process, say A
Process A
1. Fork()
A(PID =1) and B(PID=0)
2. Fork()&&fork()||fork()
IN the fork() && fork() || fork()
A executes 1st and 2nd forks. It doesn’t execute the 3rd because 1&&1 (since fork() returns 1 for parent process and 0 for child process) results in 1 and hence the 2nd statement of || is not executed. So now we have 2 more child processes of A.
B also executes only two fork() processes as its PID=0 the && will not check the second condition as the 1st fork return 0. Hence it creates 2 more child processes
3. Fork()
So all the 3+3=6 processes execute the 3rd line which results in 12 processes.
Here the difficult part is to figure out how many processes fork() && fork() || fork(); created
A: fork();
// at this stage above fork() created 2 processes
B: fork() && fork() || fork();
p --- 1st fork()
/ \
p c --- fork() 0 for child, 0 && fork() || fork(), child fork() after && and go straight to fork() behind || which created 2 processes
/ \ / \
| c | | --- for parent fork() in 1 && fork() returns 1, it will skip fork() after ||, no process is created for parent.
| / \ | | --- for child fork() in 1&& fork() returns 0, it goes to fork() after || which created 2 processes.
| | | | | --- now we have 5 processes
C: fork();
// above fork() created 2 processes.
Total number of processes becomes 2 x 5 x 2 = 20.
Here the difficult part is to figure out how many processes fork() && fork() || fork(); created
A: fork();
// at this stage above fork() created 2 processes
B: fork() && fork() || fork();
p --- 1st fork()
/ \
p c --- fork() 0 for child, 0 && fork() || fork(), child fork() after && and go straight to fork() behind || which created 2 processes
/ \ / \
| c | | --- for parent fork() in 1 && fork() returns 1, it will skip fork() after ||, no process is created for parent.
| / \ | | --- for child fork() in 1&& fork() returns 0, it goes to fork() after || which created 2 processes.
| | | | | --- now we have 5 processes
C: fork();
// above fork() created 2 processes.
Total number of processes becomes 2 x 5 x 2 = 20.
Assume A=B=C=D=E=fork();
Question is
A(); // Will be called twice means (A()=0 and A()>0). Iterations 2
B()&&C()||D(); // When B() = 0 , D() will be called means (D()=0 and D()>0). Iterations 2
// When B() >0 and
// When C()=0 , D() will be called means (D()=0 and D()>0). Iterations 2
// When C()>0 , C() will get called means (C()>0). Iterations 1
E(); // E() will be called twice means (A()=0 and A()>0). Iterations 2
Total Iterations = Iterations_of(A()) * Iterations_of( B()&&C()||D() ) * Iterations_of(E())
= 2 * (2+2+1) * 2
= 20.
Total processes to be spawned = 20.
The answer is 20.
Initially there is only one parent process P.
The first fork() results in two processes overall namely, P (parent) and C (child). P has return value of some positive number which is the pid of child while child has return number 0 because of its successful creation.
Now the first fork() on the second line will execute. This will result in two more child processes C1 and C2 spawning from two parent processes P and C. P and C will have return +ve return values while C1 and C2 will have return value 0.
Now the '&& fork()', ie the second fork on the second line will only execute if the first fork of this line returned a +ve integer. So, only the processes P and C will execute this '&& fork()' statement and result in one child process each, C3 and C4. Now, P and C will have +ve values while C1, C2, C3, and C4 will have 0 value. The '|| fork()' is only executed if 'fork() && fork()' returned 0. Hence, C1 upto C4 will all execute this fork statement and result in one children each, ie 4 children. Hence the total number of processes becomes 10 (P, C, C1, C2, ....C7, C8). Now the last fork() statement is executed and each of these 10 processes will spawn a child process, making the total count as 20.
20
- J March 14, 2012