Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
There will be maximum of 16 processes active. Each parent when ending will print one and each child two. A parent is any process that spawned another and a child is a process that did not call "fork"
So in theory you would get any series of 8 times "one" and 8 times "two" and we can never have more two's then one's in the series.
That being said no father process waits for the children to end so the process might (and it probably will) end without all the processes being executed.
So the correct answer is we will get a series of up to 8 "one" and up to 8 "two" where the only sure thing is there will not be more twos than ones if we look from 1st row to last
yup....as we know that fork is used to create the child process
(which p.t to parent next step instruction ) duplicate of parent process plus we can't which process while get first Machine it is depends on CPU .
if progm content 1 fork then there is 2 process.
if progm content 2 fork then there is 4 process.
if progm content 3 fork then there is 8 process.
if progrm content 4 fork then there is 16 process.
total 2^n processes means 16 processes
out of which 1 is main rest all are child
for parent fork return greater than 0 negation in if means two will be printed once
and all rest child means that fork will return 0 for them means one will be printed
o/p two...............15 times one..................
Could someone explain why it should be a binary tree?
I think it will be a general tree , because with every fork(), the previous running process should get a child process.. so 'main' process(root of our tree) should have 4 children and so on...which is the only way we get a total of 16 nodes-> 16 processes. And we cannot decide the order of 1s and 2s.. it will depend on the CPU scheduling..
but on ideone.com the following codes gives output as
two
one
two
two
two
just the 5 not 16 prints why can anybody suggest.
The output will contain total 8 one and total 8 two printed.
But the order in which they would be printed would depend highly whether parent or child process get scheduled first.
In my above solution i have considered that child process will get executed first always which is not the case always
In fact, could the "one"s and "two"s be garbled? The child processes could be operating on separate cores and the prints could be happening simultaneously, right? (or even if they were on just one core, can a process be preempted in the middle of a printf?)
The answer is 8 times two and 8 times one.. How ..? see below..!
- f2003062 July 17, 2012after 1 st call to fork() -> there are 2 processes [printf not executed till now]
after 2 nd call to fork() -> there are 4 processes [printf not executed till now]
after 3 rd call to fork() -> there are 8 processes [printf not executed till now]
Till the time we have called fork() is called thrice which resulted into creating 8 processes. Now we are going to call 4 th fork().
after 4 th call to fork() -> now there are 16 processes...
Now we are going to print. This print will be executed for all 16 processes out of them 8 processes will print one and 8 processes will print two..
The order of printing ones ant twos is not guaranteed. it depends on CPU scheduling.