The answer is 19, but it is compiler dependent, ill explain that at
the very end.

L1 - Fork(1);
L2 - Fork(2) && Fork (3) || Fork (4);
L3 - Fork (5);

Suppose our initial process was P0.
After L1, we have P0 & its child P1. This is pretty clear.
At L2, after first fork(), P0 creates P2, P2's work for L2 is
finished, since it returns a 0(fork gives a 0 to child & child's pid
to parent), and due to C's short circuiting technique, nothing after
&& executes.(since 0 && ..., C does not evaluate any further).
At L2, after 2nd fork(), P0 creates P3, P0's work for L2 is finished,
since (true && true) becomes true & does not evaluate any further the
|| part.
At L2, after 3rd fork(), P3 creates P4.
Work at L2 done.
After L3, P0 to P4, create children from P5 to P9,i.e., 10 processes
in total.
Similarly for the first child of P0, P1, 10 processes are created,
totalling 20 processes, out of which if we remove the original
process, we get 19 processes.

Now, the compiler dependent part.
L2 - Fork(2) && Fork (3) || Fork (4);

if we look at this statement, we'll find that this can be interpreted
in two ways.
(fork() && fork()) || fork() -- if this is how it is interpreted by
the compiler, answer will be different.
or
fork() && fork() || fork() -- this is the interpreted expr for our
solution, the way we have reached it -- if first fork() evaluates to
false, nothing after that executes on the line.

But what I feel is that the interpreted expression should be first &
not the second, and ans should not be 19. But again I think it is the
way complier interprets it, hence compiler dependent. I hope some of
you would have got my point. If still you feel that it is not complier
dependent, please post here with some facts.(I still feel it should
not be compiler dependent but first expr, but b'coz diff people are
getting diff ans, this supports the fact that it is compiler
dependent).


On Sep 6, 11:27 am, jayesh <[email protected]> wrote:
> and on running this code the process is printd 17 times, how is the
> answer 19??????im getting 31(including the initial process).....plz
> explain.....
>
> On Sep 6, 7:07 am, PremShankar Kumar <[email protected]> wrote:
>
>
>
> > @Sachin, @Mohit:-
>
> > You are right 19 new processes will get created. Thanks guys.
>
> > #include <unistd.h>
> > #include<iostream>
>
> > using namespace std;
>
> > int Fork(int i)
> > {
> >           //cout<<endl<<"Fork"<<i<<" executed."<<endl;
> >           return fork();}
>
> > int main()
> > {
> >            Fork(1);
> >            Fork(2) && Fork (3) || Fork (4);
> >            Fork (5);
> >            cout<<endl<<"Process\n";}
>
> > Regards,
> > Prem
>
> > On Sun, Sep 5, 2010 at 9:35 PM, sachin <[email protected]> wrote:
> > > Yes, you are right mohit, the no of processes is indeed 20.
> > > but the question asks for the no of new processes created, not the
> > > total no of processes.
> > > Hence, we subtract the initial process from the final ans, we get 19.,
> > > which is the required answer...:-) :-)
>
> > > On Sep 4, 11:10 pm, "MOHIT ...." <[email protected]> wrote:
> > > > Fork() && fork () || fork ();
> > > > Fork return 0 in child process and non-zero in parent
> > > > so in child process Fork()&& only executed not fork()||fork();(&& stop
> > > > working if get 0 as previous input);
>
> > > > if we get parent after parent only fork of (&& ) and initial fork of ||
> > > get
> > > > executed;(|| stops if one input is 1 next one not evaluated).
>
> > > > if we get child after parent whole fork && fork()|| fork() get executed.
>
> > > > but answer comes 20.
> > > > plz correct me if i am wrong
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Algorithm Geeks" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<algogeeks%2bunsubscr...@googlegroups
> > >  .com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/algogeeks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to