why does the parent pid return a different value than getpid()?
According to the man page
getpid() returns the process ID (PID) of the calling process.
- In the following code why does the
parent pid
return a different value thangetpid()
? - Isn't the main process the same as the parent process?
- 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
add a comment |
According to the man page
getpid() returns the process ID (PID) of the calling process.
- In the following code why does the
parent pid
return a different value thangetpid()
? - Isn't the main process the same as the parent process?
- 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
1
You must callgetpid
after thefork
becausefork
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 ofpid
when youswitch
on it, land incase 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
add a comment |
According to the man page
getpid() returns the process ID (PID) of the calling process.
- In the following code why does the
parent pid
return a different value thangetpid()
? - Isn't the main process the same as the parent process?
- 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
According to the man page
getpid() returns the process ID (PID) of the calling process.
- In the following code why does the
parent pid
return a different value thangetpid()
? - Isn't the main process the same as the parent process?
- 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
c unix fork pid
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 callgetpid
after thefork
becausefork
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 ofpid
when youswitch
on it, land incase 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
add a comment |
1
You must callgetpid
after thefork
becausefork
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 ofpid
when youswitch
on it, land incase 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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 16:52
Steven HilderSteven Hilder
1625
1625
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Nov 25 '18 at 17:13
answered Nov 25 '18 at 16:52
Paul OgilviePaul Ogilvie
18.5k21235
18.5k21235
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
You must call
getpid
after thefork
becausefork
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 youswitch
on it, land incase 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