C : Store PID in global variable











up vote
0
down vote

favorite












I am currently working on a school project and I wanted to store the PID of my process in the global array id, so that I can use it in another function :



int id[3];

int main(int agrc,const char* agrv) {
for(int i = 0; i < 3; i++) {
if((fork() == 0)) {
id[i] = (int)getpid();
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
}
}
sleep(2);

printf("ID[0] = %dn",id[0]);
printf("ID[1] = %dn",id[1]);
printf("ID[2] = %dn",id[2]);

}


But when I run this the output of the last 3 prints is 0, where it should be the PID of each process. Why does this happen?










share|improve this question






















  • Try int id[3] = { -2, -3, -4 }; and post the output.
    – chux
    Nov 11 at 0:42












  • that's cause every child process that sets the id variable, exits before those last 3 prints.
    – nlsdkd
    Nov 11 at 0:51










  • regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
    – user3629249
    Nov 11 at 1:04










  • OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
    – user3629249
    Nov 11 at 1:09










  • the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
    – user3629249
    Nov 11 at 1:11















up vote
0
down vote

favorite












I am currently working on a school project and I wanted to store the PID of my process in the global array id, so that I can use it in another function :



int id[3];

int main(int agrc,const char* agrv) {
for(int i = 0; i < 3; i++) {
if((fork() == 0)) {
id[i] = (int)getpid();
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
}
}
sleep(2);

printf("ID[0] = %dn",id[0]);
printf("ID[1] = %dn",id[1]);
printf("ID[2] = %dn",id[2]);

}


But when I run this the output of the last 3 prints is 0, where it should be the PID of each process. Why does this happen?










share|improve this question






















  • Try int id[3] = { -2, -3, -4 }; and post the output.
    – chux
    Nov 11 at 0:42












  • that's cause every child process that sets the id variable, exits before those last 3 prints.
    – nlsdkd
    Nov 11 at 0:51










  • regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
    – user3629249
    Nov 11 at 1:04










  • OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
    – user3629249
    Nov 11 at 1:09










  • the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
    – user3629249
    Nov 11 at 1:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently working on a school project and I wanted to store the PID of my process in the global array id, so that I can use it in another function :



int id[3];

int main(int agrc,const char* agrv) {
for(int i = 0; i < 3; i++) {
if((fork() == 0)) {
id[i] = (int)getpid();
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
}
}
sleep(2);

printf("ID[0] = %dn",id[0]);
printf("ID[1] = %dn",id[1]);
printf("ID[2] = %dn",id[2]);

}


But when I run this the output of the last 3 prints is 0, where it should be the PID of each process. Why does this happen?










share|improve this question













I am currently working on a school project and I wanted to store the PID of my process in the global array id, so that I can use it in another function :



int id[3];

int main(int agrc,const char* agrv) {
for(int i = 0; i < 3; i++) {
if((fork() == 0)) {
id[i] = (int)getpid();
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
}
}
sleep(2);

printf("ID[0] = %dn",id[0]);
printf("ID[1] = %dn",id[1]);
printf("ID[2] = %dn",id[2]);

}


But when I run this the output of the last 3 prints is 0, where it should be the PID of each process. Why does this happen?







c process operating-system global-variables fork






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:27









Alexandre Serra

31




31












  • Try int id[3] = { -2, -3, -4 }; and post the output.
    – chux
    Nov 11 at 0:42












  • that's cause every child process that sets the id variable, exits before those last 3 prints.
    – nlsdkd
    Nov 11 at 0:51










  • regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
    – user3629249
    Nov 11 at 1:04










  • OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
    – user3629249
    Nov 11 at 1:09










  • the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
    – user3629249
    Nov 11 at 1:11


















  • Try int id[3] = { -2, -3, -4 }; and post the output.
    – chux
    Nov 11 at 0:42












  • that's cause every child process that sets the id variable, exits before those last 3 prints.
    – nlsdkd
    Nov 11 at 0:51










  • regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
    – user3629249
    Nov 11 at 1:04










  • OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
    – user3629249
    Nov 11 at 1:09










  • the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
    – user3629249
    Nov 11 at 1:11
















Try int id[3] = { -2, -3, -4 }; and post the output.
– chux
Nov 11 at 0:42






Try int id[3] = { -2, -3, -4 }; and post the output.
– chux
Nov 11 at 0:42














that's cause every child process that sets the id variable, exits before those last 3 prints.
– nlsdkd
Nov 11 at 0:51




that's cause every child process that sets the id variable, exits before those last 3 prints.
– nlsdkd
Nov 11 at 0:51












regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
– user3629249
Nov 11 at 1:04




regarding: int main(int agrc,const char* agrv) when the parameters to main() are not going to be used, then the signature ; int main( void ) should be used. Otherwise the compiler will output 2 warnings about unused function parameters
– user3629249
Nov 11 at 1:04












OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
– user3629249
Nov 11 at 1:09




OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks for if else while do...while switch case default via a single blank line
– user3629249
Nov 11 at 1:09












the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
– user3629249
Nov 11 at 1:11




the posted code does not compile! Amongst other things, it is missing the needed #include statements for the needed header files. Do you expect us to guess as to which header files your code actually includes? Please post a Minimal, Complete, and Verifiable example when asking about a run time problem.
– user3629249
Nov 11 at 1:11












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.



If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:



pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}


By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.






share|improve this answer



















  • 1




    "print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
    – chux
    Nov 11 at 1:01










  • Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
    – MadKarel
    Nov 11 at 14:28











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',
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%2f53244746%2fc-store-pid-in-global-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.



If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:



pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}


By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.






share|improve this answer



















  • 1




    "print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
    – chux
    Nov 11 at 1:01










  • Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
    – MadKarel
    Nov 11 at 14:28















up vote
2
down vote



accepted










On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.



If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:



pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}


By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.






share|improve this answer



















  • 1




    "print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
    – chux
    Nov 11 at 1:01










  • Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
    – MadKarel
    Nov 11 at 14:28













up vote
2
down vote



accepted







up vote
2
down vote



accepted






On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.



If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:



pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}


By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.






share|improve this answer














On the call to fork(), new process is created with separate virtual memory space. This child process will return from the call to fork with 0, so in your code, this child will go inside the if branch and assign to id[i] it's pid. But this assignment is happening in a separate process, with separate virtual memory, so it has no effect on the parents virtual memory space and the parent will not see any change in its array. That is why your code prints the zeroes.



If you want to print the pids of the children by the parent, use the return value of fork(), which in parent is the pid of the child process. Inside the for, use code such as this:



pid_t child_id;
switch (child_id = fork()) {
case -1:
//Fork failed, exit with error or something
break;
case 0:
//Child code
printf("ID[%d] = %dn",i,id[i]);
if(i != 3) {
printf("I am a wharehouse. PID = [%d] PPID = [%d]n",getpid(),getppid());
whcode();
exit(0);
}
else if(i == 3) {
printf("I am the central. PID = [%d] PPID = [%d]n",getpid(),getppid());
central_code();
exit(0);
}
break;
default:
id[i] = child_id;
break;
}


By the way, you should really declare the id array as pid_t id[3], and when printing, print it as long. That for now is probably the most portable way to handle these things.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 9:18









qiAlex

2,0201523




2,0201523










answered Nov 11 at 0:54









MadKarel

812




812








  • 1




    "print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
    – chux
    Nov 11 at 1:01










  • Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
    – MadKarel
    Nov 11 at 14:28














  • 1




    "print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
    – chux
    Nov 11 at 1:01










  • Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
    – MadKarel
    Nov 11 at 14:28








1




1




"print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
– chux
Nov 11 at 1:01




"print it as long" is simply a guess as to pid_t type and can fail when pid_t is an int . Print to a casted wide type like printf("%ldn", (long)getpid());, printf("%jdn", (intmax_t)getpid());.
– chux
Nov 11 at 1:01












Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
– MadKarel
Nov 11 at 14:28




Yeah that is what i wanted to say, to cast it to long, longlong or if there is a type alias for the biggest integer type possible and then print it. It was two in the morning so i cut it little short :D. Thank you for the correction.
– MadKarel
Nov 11 at 14:28


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244746%2fc-store-pid-in-global-variable%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

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation