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?
c process operating-system global-variables fork
|
show 1 more comment
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?
c process operating-system global-variables fork
Tryint 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 tomain()
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 blocksfor
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
|
show 1 more comment
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?
c process operating-system global-variables fork
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
c process operating-system global-variables fork
asked Nov 11 at 0:27
Alexandre Serra
31
31
Tryint 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 tomain()
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 blocksfor
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
|
show 1 more comment
Tryint 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 tomain()
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 blocksfor
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
|
show 1 more comment
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.
1
"print it as long" is simply a guess as topid_t
type and can fail whenpid_t
is anint
. Print to a casted wide type likeprintf("%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
add a comment |
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.
1
"print it as long" is simply a guess as topid_t
type and can fail whenpid_t
is anint
. Print to a casted wide type likeprintf("%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
add a comment |
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.
1
"print it as long" is simply a guess as topid_t
type and can fail whenpid_t
is anint
. Print to a casted wide type likeprintf("%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
add a comment |
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.
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.
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 topid_t
type and can fail whenpid_t
is anint
. Print to a casted wide type likeprintf("%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
add a comment |
1
"print it as long" is simply a guess as topid_t
type and can fail whenpid_t
is anint
. Print to a casted wide type likeprintf("%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
add a comment |
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%2f53244746%2fc-store-pid-in-global-variable%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
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 tomain()
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