OS Assignment1

OS Assignment1 - ! fork() )] next fork is executed only on...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
main() printf(“AA\n”); else if(! fork()) printf(“BB\n”); else printf(“CC\n”);} Brief explanation for the above code: When the program executes the if statement, first fork() is called on the current process(in this case P1 with pid=1). Child process P2 is generated as a result of fork(). This child process returns 0; the parent process returns 2 (which is the pid of the child process). The if statement evaluates to true when fork () returns a positive value >0 (if ( fork() || rest is not evaluated by the program) and “AA” is printed. The child process P2 returns a 0; [if ( fork() || ( fork() && ! fork() )], the next fork is executed generating a new child process P3 (pid = 3). Now P2 returns 3 and P3 returns 0. 1. [if(fork() || (fork() &&
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ! fork() )] next fork is executed only on P2 which returns positive value >0. Hence a new child process is generated P4(pid=4, returns 0) . At this point if statement evaluates to: 0 || (1 && !0) which is true, hence AA is printed. 2. At P3 (else if), fork () is called again. A new child process P5(returns 0, pid=5) is created and else if evaluates to : !0 which is true, hence BB is printed. Else CC is printed. 3. At P2 (else if), fork () is called again. A new child process P6(returns 0, pid=6) is created and else if evaluates to : !0 which is true, hence BB is printed. Else CC is printed. Therefore 1) AA is printed 2 times 2) BB is printed 2 times 3) CC is printed 2 times...
View Full Document

This document was uploaded on 12/13/2010.

Page1 / 2

OS Assignment1 - ! fork() )] next fork is executed only on...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online