why does the parent pid return a different value than getpid()?












0















According to the man page




getpid() returns the process ID (PID) of the calling process.





  1. In the following code why does the parent pid return a different value than getpid() ?

  2. Isn't the main process the same as the parent process?

  3. And why do I get different outputs when run on a different system?




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

int main(int argc, char const *argv)
{

printf("getpid = %d n", (int)getpid());

pid_t pid = fork();
printf("fork returned %d n", (int)pid);

switch (pid)
{
case -1:
perror("fork failed");
break;

case 0:
printf("I am a child with pid = %dn", (int)pid);
break;

default:
printf("I am a parent with pid = %dn", (int)pid);

break;

}
return 0;
}


output when i run :



getpid = 8208 
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0


output when run on a different pc:



getpid = 2522 
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523









share|improve this question




















  • 1





    You must call getpid after the fork because fork creates a new process with a new pid.

    – Paul Ogilvie
    Nov 25 '18 at 16:42






  • 4





    Either your other PC is utterly broken, or you are not running what you think you are running.

    – n.m.
    Nov 25 '18 at 16:43













  • Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

    – Swordfish
    Nov 25 '18 at 16:45






  • 1





    Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

    – Jonathan Leffler
    Nov 25 '18 at 16:49
















0















According to the man page




getpid() returns the process ID (PID) of the calling process.





  1. In the following code why does the parent pid return a different value than getpid() ?

  2. Isn't the main process the same as the parent process?

  3. And why do I get different outputs when run on a different system?




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

int main(int argc, char const *argv)
{

printf("getpid = %d n", (int)getpid());

pid_t pid = fork();
printf("fork returned %d n", (int)pid);

switch (pid)
{
case -1:
perror("fork failed");
break;

case 0:
printf("I am a child with pid = %dn", (int)pid);
break;

default:
printf("I am a parent with pid = %dn", (int)pid);

break;

}
return 0;
}


output when i run :



getpid = 8208 
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0


output when run on a different pc:



getpid = 2522 
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523









share|improve this question




















  • 1





    You must call getpid after the fork because fork creates a new process with a new pid.

    – Paul Ogilvie
    Nov 25 '18 at 16:42






  • 4





    Either your other PC is utterly broken, or you are not running what you think you are running.

    – n.m.
    Nov 25 '18 at 16:43













  • Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

    – Swordfish
    Nov 25 '18 at 16:45






  • 1





    Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

    – Jonathan Leffler
    Nov 25 '18 at 16:49














0












0








0


1






According to the man page




getpid() returns the process ID (PID) of the calling process.





  1. In the following code why does the parent pid return a different value than getpid() ?

  2. Isn't the main process the same as the parent process?

  3. And why do I get different outputs when run on a different system?




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

int main(int argc, char const *argv)
{

printf("getpid = %d n", (int)getpid());

pid_t pid = fork();
printf("fork returned %d n", (int)pid);

switch (pid)
{
case -1:
perror("fork failed");
break;

case 0:
printf("I am a child with pid = %dn", (int)pid);
break;

default:
printf("I am a parent with pid = %dn", (int)pid);

break;

}
return 0;
}


output when i run :



getpid = 8208 
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0


output when run on a different pc:



getpid = 2522 
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523









share|improve this question
















According to the man page




getpid() returns the process ID (PID) of the calling process.





  1. In the following code why does the parent pid return a different value than getpid() ?

  2. Isn't the main process the same as the parent process?

  3. And why do I get different outputs when run on a different system?




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

int main(int argc, char const *argv)
{

printf("getpid = %d n", (int)getpid());

pid_t pid = fork();
printf("fork returned %d n", (int)pid);

switch (pid)
{
case -1:
perror("fork failed");
break;

case 0:
printf("I am a child with pid = %dn", (int)pid);
break;

default:
printf("I am a parent with pid = %dn", (int)pid);

break;

}
return 0;
}


output when i run :



getpid = 8208 
fork returned 8209
I am a parent with pid = 8209
fork returned 0
I am a child with pid = 0


output when run on a different pc:



getpid = 2522 
fork returned 2523
I am a parent with pid = 2522
fork returned 0
I am a child with pid = 2523






c unix fork pid






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:40









Swordfish

1




1










asked Nov 25 '18 at 16:35









afsara_benafsara_ben

337




337








  • 1





    You must call getpid after the fork because fork creates a new process with a new pid.

    – Paul Ogilvie
    Nov 25 '18 at 16:42






  • 4





    Either your other PC is utterly broken, or you are not running what you think you are running.

    – n.m.
    Nov 25 '18 at 16:43













  • Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

    – Swordfish
    Nov 25 '18 at 16:45






  • 1





    Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

    – Jonathan Leffler
    Nov 25 '18 at 16:49














  • 1





    You must call getpid after the fork because fork creates a new process with a new pid.

    – Paul Ogilvie
    Nov 25 '18 at 16:42






  • 4





    Either your other PC is utterly broken, or you are not running what you think you are running.

    – n.m.
    Nov 25 '18 at 16:43













  • Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

    – Swordfish
    Nov 25 '18 at 16:45






  • 1





    Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

    – Jonathan Leffler
    Nov 25 '18 at 16:49








1




1





You must call getpid after the fork because fork creates a new process with a new pid.

– Paul Ogilvie
Nov 25 '18 at 16:42





You must call getpid after the fork because fork creates a new process with a new pid.

– Paul Ogilvie
Nov 25 '18 at 16:42




4




4





Either your other PC is utterly broken, or you are not running what you think you are running.

– n.m.
Nov 25 '18 at 16:43







Either your other PC is utterly broken, or you are not running what you think you are running.

– n.m.
Nov 25 '18 at 16:43















Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

– Swordfish
Nov 25 '18 at 16:45





Whink about the value of pid when you switch on it, land in case 0 and wheter that can really be a pid.

– Swordfish
Nov 25 '18 at 16:45




1




1





Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

– Jonathan Leffler
Nov 25 '18 at 16:49





Your other PC does not produce the output you claim from the code you claim to be running on it. Recopy the code from the machine where you get correct output to the machine where you don't, recompile the program on the second machine, and run it (but don't lose the first program — make backup copies, etc). It is very improbable that the recompiled code produces the answer you claim the unrecompiled code produces.

– Jonathan Leffler
Nov 25 '18 at 16:49












2 Answers
2






active

oldest

votes


















2














Yes, the parent process and main process are the same thing.



This snippet of your code should give you a clue about the solution:



switch (pid) {
/* ... */
case 0:
printf("I am a child with pid = %dn", (int)pid);
break;


This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.



The man page states:




Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.




So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.



In the child process, you would need to call getpid() after fork() to get the child's own pid.






share|improve this answer































    1














    fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.



    So after the fork, you must call getpid() for both parent and child, in your code that would be:



        pid_t thispid= getpid(); 
    switch (pid)
    {
    case -1:
    perror("fork failed");
    break;

    case 0:
    printf("I am a child with pid = %dn", (int)thispid);
    break;

    default:
    printf("I am a parent with pid = %dn", (int)thispid);
    break;

    }





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469592%2fwhy-does-the-parent-pid-return-a-different-value-than-getpid%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Yes, the parent process and main process are the same thing.



      This snippet of your code should give you a clue about the solution:



      switch (pid) {
      /* ... */
      case 0:
      printf("I am a child with pid = %dn", (int)pid);
      break;


      This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.



      The man page states:




      Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.




      So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.



      In the child process, you would need to call getpid() after fork() to get the child's own pid.






      share|improve this answer




























        2














        Yes, the parent process and main process are the same thing.



        This snippet of your code should give you a clue about the solution:



        switch (pid) {
        /* ... */
        case 0:
        printf("I am a child with pid = %dn", (int)pid);
        break;


        This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.



        The man page states:




        Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.




        So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.



        In the child process, you would need to call getpid() after fork() to get the child's own pid.






        share|improve this answer


























          2












          2








          2







          Yes, the parent process and main process are the same thing.



          This snippet of your code should give you a clue about the solution:



          switch (pid) {
          /* ... */
          case 0:
          printf("I am a child with pid = %dn", (int)pid);
          break;


          This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.



          The man page states:




          Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.




          So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.



          In the child process, you would need to call getpid() after fork() to get the child's own pid.






          share|improve this answer













          Yes, the parent process and main process are the same thing.



          This snippet of your code should give you a clue about the solution:



          switch (pid) {
          /* ... */
          case 0:
          printf("I am a child with pid = %dn", (int)pid);
          break;


          This effectively says "if pid is zero then the child pid is always zero". This obviously cannot be true, so it is your interpretation of fork()'s return value that is incorrect.



          The man page states:




          Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process.




          So the variable pid in the parent process is the pid of the child, NOT the parent's own pid.



          In the child process, you would need to call getpid() after fork() to get the child's own pid.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 16:52









          Steven HilderSteven Hilder

          1625




          1625

























              1














              fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.



              So after the fork, you must call getpid() for both parent and child, in your code that would be:



                  pid_t thispid= getpid(); 
              switch (pid)
              {
              case -1:
              perror("fork failed");
              break;

              case 0:
              printf("I am a child with pid = %dn", (int)thispid);
              break;

              default:
              printf("I am a parent with pid = %dn", (int)thispid);
              break;

              }





              share|improve this answer






























                1














                fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.



                So after the fork, you must call getpid() for both parent and child, in your code that would be:



                    pid_t thispid= getpid(); 
                switch (pid)
                {
                case -1:
                perror("fork failed");
                break;

                case 0:
                printf("I am a child with pid = %dn", (int)thispid);
                break;

                default:
                printf("I am a parent with pid = %dn", (int)thispid);
                break;

                }





                share|improve this answer




























                  1












                  1








                  1







                  fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.



                  So after the fork, you must call getpid() for both parent and child, in your code that would be:



                      pid_t thispid= getpid(); 
                  switch (pid)
                  {
                  case -1:
                  perror("fork failed");
                  break;

                  case 0:
                  printf("I am a child with pid = %dn", (int)thispid);
                  break;

                  default:
                  printf("I am a parent with pid = %dn", (int)thispid);
                  break;

                  }





                  share|improve this answer















                  fork duplicates a process and after the fork, they both run in parallel. To tell the program whether it is a child or the parent, it returns 0 for the child and the PID of the child process is returned in the parent.



                  So after the fork, you must call getpid() for both parent and child, in your code that would be:



                      pid_t thispid= getpid(); 
                  switch (pid)
                  {
                  case -1:
                  perror("fork failed");
                  break;

                  case 0:
                  printf("I am a child with pid = %dn", (int)thispid);
                  break;

                  default:
                  printf("I am a parent with pid = %dn", (int)thispid);
                  break;

                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 17:13

























                  answered Nov 25 '18 at 16:52









                  Paul OgilviePaul Ogilvie

                  18.5k21235




                  18.5k21235






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469592%2fwhy-does-the-parent-pid-return-a-different-value-than-getpid%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin