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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:47:19+00:00 2026-06-13T05:47:19+00:00

I’m studying computer systems and I’ve made this very simple function which uses fork()

  • 0

I’m studying computer systems and I’ve made this very simple function which uses fork() to create a child process. fork() returns a pid_t that is 0 if it’s a child process. But calling the getpid() function within this child process returns a different, nonzero pid. In the code I have below, is newPid only meaningful in the context of the program, and not to the operating system? Is it possibly only a relative value, measured against the pid of the parent?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

void unixError(char* msg)
{
    printf("%s: %s\n", msg, strerror(errno));
    exit(0);
}

pid_t Fork()
{
    pid_t pid;
    if ((pid = fork()) < 0)
        unixError("Fork error");
    return pid;
}


int main(int argc, const char * argv[])
{
    pid_t thisPid, parentPid, newPid;
    int count = 0;
    thisPid = getpid();
    parentPid = getppid();

    printf("thisPid = %d, parent pid = %d\n", thisPid, parentPid);

    if ((newPid = Fork()) == 0) {
        count++;
        printf("I am the child. My pid is %d, my other pid is %d\n", getpid(), newPid);
        exit(0);
    }
    printf("I am the parent. My pid is %d\n", thisPid);
    return 0;
}

Output:

thisPid = 30050, parent pid = 30049
I am the parent. My pid is 30050
I am the child. My pid is 30052, my other pid is 0

Lastly, why is the child’s pid 2 higher than the parent’s, and not 1? The difference between the main function’s pid and its parent is 1, but when we create a child it increments the pid by 2. Why is that?

  • 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-13T05:47:20+00:00Added an answer on June 13, 2026 at 5:47 am

    From fork man page :

    Return Value

    On success, the PID of the child process is returned in the parent,
    and 0 is returned in the child. On failure, -1 is returned in the
    parent, no child process is created, and errno is set appropriately.

    Fork does not returns the pid of the child, only in the parent. Therefore, the child process does not have two pids.

    Try this

    int main(int argc, const char * argv[])
    {
        pid_t thisPid, parentPid, newPid;
        int count = 0;
        thisPid = getpid();
        parentPid = getppid();
    
        printf("thisPid = %d, parent pid = %d\n", thisPid, parentPid);
    
        if ((newPid = Fork()) == 0) {
            count++;
            printf("I am teh child. My pid is %d\n", getpid());
            exit(0);
        }
        else
           printf("I am the parent. My pid is %d, my child pid is %d\n", thisPid, newPid);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.