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 7998305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:10:57+00:00 2026-06-04T15:10:57+00:00

int main() { if(Fork() == 0) { printf(a); } else { printf(b); waitpid(-1, NULL,

  • 0
int main()
{
    if(Fork() == 0)
    {
       printf("a");
    }
    else
    {
       printf("b");
       waitpid(-1, NULL, 0);
    }

    printf("c");
    exit(0);
}

The above code asks what are the possible outputs?

I find that, acbc, abcc, bacc but answer includes bcac. How can bcac be an answer? Is parent process exits before child process created or sth. else? I confused, any explanations are welcome.

Thnx.

  • 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-04T15:10:58+00:00Added an answer on June 4, 2026 at 3:10 pm

    The only guarantee is that waitpid won’t return (unless it’s interrupted) until the child’s status information is available. As soon as you call exit(0), the 0 is available. So waitpid can return before the child’s output is flushed.

    POSIX says: “The wait() function shall suspend execution of the calling thread until status information for one of the terminated child processes of the calling process is available, or until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process.” (It also says waitpid is identical in this respect.)

    I don’t know of any actual platform where this happens and can only imagine it happening on an OS that emulates POSIX in user space and thus has some user space code to signal the parent that the child is terminating rather than using its actual termination to signal the parent. But it’s permitted by the standards.

    This is really obscure and I wonder if this is the expected rationale for that sequence. I can think of no other.

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

Sidebar

Related Questions

int main(void) { std::string foo(foo); } My understanding is that the above code uses
Considering the below code : int main() { int pid; pid=vfork(); if(pid==0) printf(child\n); else
The code: int main(void) { printf(pid: %d\n, getpid()); pid = fork(); if (pid <
Its obvious that we have a code block like int main() { pid_t pid;
That is my code. #include <stdio.h> #include <stdlib.h> int main ( int argc, char
... int main(void) { int i; for(i=0;i<2;i++){ switch(fork()){ case 0: sleep(1); break; default: sleep(1);
int main() { int *d=0; printf(%d\n,*d); return 0; } this works fine. >cc legal.c
void child(int pid){ printf(Child PID:%d\n,pid); exit(0); } void parent(int pid){ printf(Parent PID:%d\n,pid); exit(0); }
I have studied that during a fork, the data and code segment of the
Based on this: http://man7.org/tlpi/code/online/book/procexec/multi_wait.c.html int main(int argc, char *argv[]) { int numDead; /* Number

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.