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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:16:38+00:00 2026-06-09T05:16:38+00:00

Possible Duplicate: How is it possible for fork() to return two values? I’m new

  • 0

Possible Duplicate:
How is it possible for fork() to return two values?

I’m new to C, and I’m confused about the return value structure of the fork() function.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
  pid_t childPid;
  childPid = fork();
  printf("%d\n",childPid);
  return EXIT_SUCCESS;
}

The output is:

28501
0

Since pid_t is an int type, how does the childPid have 2 values?

  • 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-09T05:16:39+00:00Added an answer on June 9, 2026 at 5:16 am

    You’re actually seeing the output of two copies of the executable. When you call fork(), the process clones itself, giving two processes. The parent gets the child process’s PID as the return value, and the child gets 0 as a return value.

    The trick is, the clones share STDIN, STDOUT, and STDERR. When the parent reaches the printf, it prints the value it had, as does the child, so you see both PIDs, with both processes sharing the same STDOUT — there’s no obvious way to tell them apart in your program.

    Try rewriting it as:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(){
      pid_t childPid;
      childPid = fork();
      if(childPid != 0)
          printf("I'm the parent and my child PID is %d\n",childPid);
      else
          printf("\tI'm the child, so I received  %d\n",childPid);
    
      return EXIT_SUCCESS;
    }
    

    and you’ll see this more clearly.

    For extra credit, look at the wait() syscall, and use it to make the parent terminate after the child.

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

Sidebar

Related Questions

Possible Duplicate: Working of fork() in linux gcc #include <stdio.h> void main () {
Possible Duplicate: Does PHP have threading? I found this: http://php.net/manual/en/function.pcntl-fork.php But I can't tell
Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: How does this bash fork bomb work? Today one of my friend
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: Opening url in new tab while i am doing window.open(), my page

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.