Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:36:57+00:00 2026-06-14T05:36:57+00:00

What would be the output of following fork() calls ? func(){ fork(); fork(); fork()

  • 0

What would be the output of following fork() calls ?

func(){
fork();
fork();
fork() && fork() || fork();
fork();
print("Saikacollection\n");
}

Can anyone help me in getting the answer to this code as well as some explanations as I am new to OS ? I have found several questions on fork() on SO, but couldn’t figure out much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:36:59+00:00Added an answer on June 14, 2026 at 5:36 am

    Saikacollection will be printed 40 times as output to the code. This can be explained as follows :-

    To understand the output, we need to know following :-

    • On successful execution of a fork() call, new child is created. The process creating the child is called parent process.
    • Fork() call returns pid (process identifier) of the child to parent
    • Fork() returns 0 to the child process.

    enter image description here

    Consider the image shown :-

    Convention : All the parents(callers) of the process are written to left and marked with a star.

    fork()
    
    • At the beginning, we have just 1 process, so a fork() call creates a child. Considering the root of the tree as level 1, we can see at level 2 , we have two processes, parent(left) and child(right) .

      fork()

    • fork() again further creates 4 such processes, marked as 1, 2, 3, 4. Since all of the four processes will go through similar code structure further, we can say the total number of processes will be 4 times a single process produces

      fork()&&fork()||fork()

    • Understanding this statement involves, realizing the fact that in C, && operator has more precedence than ||

    • Also, if first of two operands joined by && goes wrong, we don't check the second. Similarly, if first of the two operands of || is true, we don't check the second operand.
    • Consider, the fork() call at 1(marked) node, two processes are created. While the parent gets a positive number(pid) as return, child gets a 0. So, parent executes the second operator, while the child jumps to fork() after || as marked in the figure.
    • The execution of &&fork() for the parent at 4th level, returns a pid for one process, which terminates, while the child of that step, gets a 0. So, it goes for execution of ||fork()
    • the final ||fork() call for child of level 5, further produces a process as a result
    • So, at the end of the step, we have 5 leaves(processes) as marked by underlines in the figure.
    • Had we done the same for all the three nodes, we could have got 5*4 = 20 processes.

      fork()

    • Final fork() just doubles the number of process available at that step.

    • So, total number of processes = 2*20 = 40.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any idea why the following code would output no match? boost::regex
In python, the following instruction: print 'a'*5 would output aaaaa . How would one
Would it be possible to hide the following output produced by Jetty? 2012-04-08 10:52:35.467:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT
Given the following schema / data / output how would I format a SQL
I would like to output in this format: e.EE_id e.FNAME e.LNAME SUPer_id s.FNAME s.LNAME
I would like to output the following table from my database DateTime | Measure
I would like to output the following using django templates: Manhattan, NY, USA with
I would like to iterate. @some_value outputs the following result: { Meta => {
Is there a pretty printing function in Clojure that would output data-structures like lists
An example of the process/output would be: File1: hello world File2: foo bar Resulting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.