Assuming that the system call fork () never fails, consider the following C programs P1 and P2 executed on a UNIX / Linux system:

/*P1*/

Int main() {

fork ():

fork () ;

fork () ;

Printf(“Happy\n”);

}

/*P2*/

Int main() {

fork ();

Printf(“Happy\n”);

fork ()

Printf(“Happy\n”);

fork () ;

Printf(“Happy\n”);

}

 

Statement I: P1 displays "Happy" 8 times.

Statement II: P2 displays "Happy" 12 times.

In the light of the above statements, choose the correct answer from the options given below

This question was previously asked in
UGC NET Computer Science (Paper 2) 2020 Official Paper
View all UGC NET Papers >
  1. Both Statement I and Statement II are true
  2. Both Statement I and Statement II are false
  3. Statement I is correct but Statement II is false
  4. Statement I is incorrect but Statement II is true

Answer (Detailed Solution Below)

Option 3 : Statement I is correct but Statement II is false
Free
UGC NET Paper 1: Held on 21st August 2024 Shift 1
10.8 K Users
50 Questions 100 Marks 60 Mins

Detailed Solution

Download Solution PDF

The correct answer is option 3

Explanation:

fork() is a system call used to create a child process.

Once a child process is created, both the parent process and the child process run the next line following the fork() command simultaneously.

'n' number of successive for() calls create 2n - 1 child processes.

in P1:

Each child process prints one "Happy".

Parent process also prints one "Happy".

So, number of times P1 displays "Happy" = (23 - 1) + 1 = 8

In P2:

At first fork(), a new child process with two fork() and printf statements is created.

Similarly, more child processes are created at each fork() command.

So, P2 shows "Happy" 14 (2 + 4 + 8 )times.

Latest UGC NET Updates

Last updated on Jun 12, 2025

-> The UGC NET June 2025 exam will be conducted from 25th to 29th June 2025.

-> The UGC-NET exam takes place for 85 subjects, to determine the eligibility for 'Junior Research Fellowship’ and ‘Assistant Professor’ posts, as well as for PhD. admissions.

-> The exam is conducted bi-annually - in June and December cycles.

-> The exam comprises two papers - Paper I and Paper II. Paper I consists of 50 questions and Paper II consists of 100 questions. 

-> The candidates who are preparing for the exam can check the UGC NET Previous Year Papers and UGC NET Test Series to boost their preparations.

More Process and Thread Questions

Get Free Access Now
Hot Links: teen patti classic teen patti 51 bonus lotus teen patti teen patti master gold download