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

  • SEARCH
  • Home
  • 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 8970525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:44:18+00:00 2026-06-15T17:44:18+00:00

The following code: B() { pid_t pid; if ((pid=fork())!= 0) waitpid(pid,NULL,0); printf(2 ); if

  • 0

The following code:

B() { 
   pid_t pid; 
   if ((pid=fork())!= 0) 
       waitpid(pid,NULL,0); 
   printf("2 "); 
   if (fork() == 0) 
      { printf("3 "); exit(0); } 
   printf("5 "); 
   exit(0); 
}

could have one of the outputs: and im not sure which one is the right output.

232553
235325
232355
235253
252533

these 2 lines mean if the pid is the parent, then wait for what?

if ((pid=fork())!= 0) 
           waitpid(pid,NULL,0); 

then if it is the child process (fork = 0), then print 3.. correct?

 if (fork() == 0) 
              { printf("3 "); exit(0); }
  • 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-15T17:44:19+00:00Added an answer on June 15, 2026 at 5:44 pm
    B() { 
       pid_t pid; 
    
       /* On success, fork() returns 0 to the child. It returns the child's process 
        * ID to the parent. So this block of code, executes a fork(), and has the
        * parent wait for the child.
        * Amusing sidenote: if the fork() fails, the parent will wait for *any* 
        * child process, since the return will be -1.
        */
       if ((pid=fork())!= 0) 
           waitpid(pid,NULL,0); 
    
       /* Both the parent and the child will print 2. The parent, will, of course,
        * only print 2 after the child exits, because of the waitpid above.
        */
       printf("2 "); 
    
       /* Now we fork again. Both the parent and the child from the previous fork will
        * fork again, although the parent will do it *after* the child exit. The resulting 
        * child process will print a single 3 and then exit.
        */
       if (fork() == 0) 
          { printf("3 "); exit(0); } 
    
       /* Now both the parent and the child print a 5 and exit */
       printf("5 "); 
       exit(0); 
    }
    

    As David Schwartz said, the output of this program will consists of some permutation of the digits 2, 3 and 5. No one output is correct, because the output depends on the order in which the processes execute, which is arbitrary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code. i want the elseif part to check if $pId
I have the following code, which I'm trying to only allow a maximum of
I have a problem with the following javascript code. Basically I want to run
I have the following code (I'm working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I'm
If I have the following code (from Silberschatz, Operating Systems), why becomes value =
I have the following source code, which I am sending signals from parent to
So I have a couple of questions about fork(): 1) I'm pretty sure that
Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {
I have the following code which forks two new processes to take the contents
I have the following code draft. #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>

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.