Problem to understand how it works theses processes
up vote
0
down vote
favorite
Good evening,
I'm programming and testing some things about processes in C with fork() and waitpid() system calls. I understand the behavior with the global variable, but I don't understand why when the second process is finished and back in the first process the variable "i" has the same value as the second process.
And also why when the program go back to the root process, the variable "i" has the value 2.
Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main(int argc, char **argv) {
int i, pid;
for(i = 1; i < 3; ++i) {
pid = fork();
if(pid == 0) {
total = total + i;
printf("n");
printf("Child process %dn", getpid());
printf("Parent process %dn", getppid());
printf("i = %dn", i);
} else {
waitpid(pid, NULL, 0);
}
}
printf("Child process (end) %dn", getpid());
printf("Parent process (end) %dn", getppid());
printf("total = %dn", total);
printf("i = %dn", i);
exit(0);
}
And here is the result of the execution
Child process 9191
Parent process 9190
i = 1
Child process 9192
Parent process 9191
i = 2
Child process (end) 9192
Parent process (end) 9191
total = 3
i = 3
Child process (end) 9191
Parent process (end) 9190
total = 1
i = 3
Child process 9193
Parent process 9190
i = 2
Child process (end) 9193
Parent process (end) 9190
total = 2
i = 3
Child process (end) 9190
Parent process (end) 2876
total = 0
i = 3
I have a suggestion : it is that the function waitpid() take the resources to the child process, but it cannot explain the value of the variable "i" in the root process.
I hope that I was clear about my problems and sorry for my little bad english.
Thank you for your answers.
c linux fork waitpid
add a comment |
up vote
0
down vote
favorite
Good evening,
I'm programming and testing some things about processes in C with fork() and waitpid() system calls. I understand the behavior with the global variable, but I don't understand why when the second process is finished and back in the first process the variable "i" has the same value as the second process.
And also why when the program go back to the root process, the variable "i" has the value 2.
Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main(int argc, char **argv) {
int i, pid;
for(i = 1; i < 3; ++i) {
pid = fork();
if(pid == 0) {
total = total + i;
printf("n");
printf("Child process %dn", getpid());
printf("Parent process %dn", getppid());
printf("i = %dn", i);
} else {
waitpid(pid, NULL, 0);
}
}
printf("Child process (end) %dn", getpid());
printf("Parent process (end) %dn", getppid());
printf("total = %dn", total);
printf("i = %dn", i);
exit(0);
}
And here is the result of the execution
Child process 9191
Parent process 9190
i = 1
Child process 9192
Parent process 9191
i = 2
Child process (end) 9192
Parent process (end) 9191
total = 3
i = 3
Child process (end) 9191
Parent process (end) 9190
total = 1
i = 3
Child process 9193
Parent process 9190
i = 2
Child process (end) 9193
Parent process (end) 9190
total = 2
i = 3
Child process (end) 9190
Parent process (end) 2876
total = 0
i = 3
I have a suggestion : it is that the function waitpid() take the resources to the child process, but it cannot explain the value of the variable "i" in the root process.
I hope that I was clear about my problems and sorry for my little bad english.
Thank you for your answers.
c linux fork waitpid
4
When youfork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.
– John Bollinger
Nov 9 at 21:47
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
1
it cannot explain the value of the variable "i" in the root process
. You actually only printi
of the root process one time at the end. In the loop you only print whenfork()
returns0
and that only applies for child processes.
– Osiris
Nov 9 at 21:52
OT: When the parameters tomain()
argc
andargv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature:int main( void )
– user3629249
Nov 10 at 0:23
regarding:if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have anexit( 0 );
statement, otherwise the execution of the child will continue around the loop
– user3629249
Nov 10 at 0:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Good evening,
I'm programming and testing some things about processes in C with fork() and waitpid() system calls. I understand the behavior with the global variable, but I don't understand why when the second process is finished and back in the first process the variable "i" has the same value as the second process.
And also why when the program go back to the root process, the variable "i" has the value 2.
Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main(int argc, char **argv) {
int i, pid;
for(i = 1; i < 3; ++i) {
pid = fork();
if(pid == 0) {
total = total + i;
printf("n");
printf("Child process %dn", getpid());
printf("Parent process %dn", getppid());
printf("i = %dn", i);
} else {
waitpid(pid, NULL, 0);
}
}
printf("Child process (end) %dn", getpid());
printf("Parent process (end) %dn", getppid());
printf("total = %dn", total);
printf("i = %dn", i);
exit(0);
}
And here is the result of the execution
Child process 9191
Parent process 9190
i = 1
Child process 9192
Parent process 9191
i = 2
Child process (end) 9192
Parent process (end) 9191
total = 3
i = 3
Child process (end) 9191
Parent process (end) 9190
total = 1
i = 3
Child process 9193
Parent process 9190
i = 2
Child process (end) 9193
Parent process (end) 9190
total = 2
i = 3
Child process (end) 9190
Parent process (end) 2876
total = 0
i = 3
I have a suggestion : it is that the function waitpid() take the resources to the child process, but it cannot explain the value of the variable "i" in the root process.
I hope that I was clear about my problems and sorry for my little bad english.
Thank you for your answers.
c linux fork waitpid
Good evening,
I'm programming and testing some things about processes in C with fork() and waitpid() system calls. I understand the behavior with the global variable, but I don't understand why when the second process is finished and back in the first process the variable "i" has the same value as the second process.
And also why when the program go back to the root process, the variable "i" has the value 2.
Here is the code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main(int argc, char **argv) {
int i, pid;
for(i = 1; i < 3; ++i) {
pid = fork();
if(pid == 0) {
total = total + i;
printf("n");
printf("Child process %dn", getpid());
printf("Parent process %dn", getppid());
printf("i = %dn", i);
} else {
waitpid(pid, NULL, 0);
}
}
printf("Child process (end) %dn", getpid());
printf("Parent process (end) %dn", getppid());
printf("total = %dn", total);
printf("i = %dn", i);
exit(0);
}
And here is the result of the execution
Child process 9191
Parent process 9190
i = 1
Child process 9192
Parent process 9191
i = 2
Child process (end) 9192
Parent process (end) 9191
total = 3
i = 3
Child process (end) 9191
Parent process (end) 9190
total = 1
i = 3
Child process 9193
Parent process 9190
i = 2
Child process (end) 9193
Parent process (end) 9190
total = 2
i = 3
Child process (end) 9190
Parent process (end) 2876
total = 0
i = 3
I have a suggestion : it is that the function waitpid() take the resources to the child process, but it cannot explain the value of the variable "i" in the root process.
I hope that I was clear about my problems and sorry for my little bad english.
Thank you for your answers.
c linux fork waitpid
c linux fork waitpid
asked Nov 9 at 21:30
ZeRedDiamond
204
204
4
When youfork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.
– John Bollinger
Nov 9 at 21:47
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
1
it cannot explain the value of the variable "i" in the root process
. You actually only printi
of the root process one time at the end. In the loop you only print whenfork()
returns0
and that only applies for child processes.
– Osiris
Nov 9 at 21:52
OT: When the parameters tomain()
argc
andargv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature:int main( void )
– user3629249
Nov 10 at 0:23
regarding:if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have anexit( 0 );
statement, otherwise the execution of the child will continue around the loop
– user3629249
Nov 10 at 0:26
add a comment |
4
When youfork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.
– John Bollinger
Nov 9 at 21:47
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
1
it cannot explain the value of the variable "i" in the root process
. You actually only printi
of the root process one time at the end. In the loop you only print whenfork()
returns0
and that only applies for child processes.
– Osiris
Nov 9 at 21:52
OT: When the parameters tomain()
argc
andargv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature:int main( void )
– user3629249
Nov 10 at 0:23
regarding:if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have anexit( 0 );
statement, otherwise the execution of the child will continue around the loop
– user3629249
Nov 10 at 0:26
4
4
When you
fork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.– John Bollinger
Nov 9 at 21:47
When you
fork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.– John Bollinger
Nov 9 at 21:47
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
1
1
it cannot explain the value of the variable "i" in the root process
. You actually only print i
of the root process one time at the end. In the loop you only print when fork()
returns 0
and that only applies for child processes.– Osiris
Nov 9 at 21:52
it cannot explain the value of the variable "i" in the root process
. You actually only print i
of the root process one time at the end. In the loop you only print when fork()
returns 0
and that only applies for child processes.– Osiris
Nov 9 at 21:52
OT: When the parameters to
main()
argc
and argv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature: int main( void )
– user3629249
Nov 10 at 0:23
OT: When the parameters to
main()
argc
and argv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature: int main( void )
– user3629249
Nov 10 at 0:23
regarding:
if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have an exit( 0 );
statement, otherwise the execution of the child will continue around the loop– user3629249
Nov 10 at 0:26
regarding:
if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have an exit( 0 );
statement, otherwise the execution of the child will continue around the loop– user3629249
Nov 10 at 0:26
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
the following corrected code:
- cleanly compiles
- performs the desired functionality
- clearly shows that
total
in the parent is no changed by the child processes
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main( void ) // <<-- corrected statement
{
int i, pid;
for(i = 1; i < 3; ++i)
{
pid = fork();
if(pid == 0)
{
total = total + i;
printf( "n") ;
printf( "Child process %dn", getpid() );
printf( "Parent process %dn", getppid() );
printf( "i = %dn", i );
exit( 0 ); // <<-- added statement
}
else
{
waitpid( pid, NULL, 0 );
}
}
printf( "Child process (end) %dn", getpid() );
printf( "Parent process (end) %dn", getppid() );
printf( "total = %dn", total );
printf( "i = %dn", i );
exit( 0 );
}
the output of the above code is:
Child process 10378
Parent process 10377
i = 1
Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3
however, the code fails to check for the error case from the function: fork()
so will be open to a major failure if the call to fork()
fails. I.E. the code should be (also) checking for a returned value of -1 from the call to fork()
and calling
if( pid < 0 )
{
perror( "fork failed");
exit( 1 );`
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
the following corrected code:
- cleanly compiles
- performs the desired functionality
- clearly shows that
total
in the parent is no changed by the child processes
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main( void ) // <<-- corrected statement
{
int i, pid;
for(i = 1; i < 3; ++i)
{
pid = fork();
if(pid == 0)
{
total = total + i;
printf( "n") ;
printf( "Child process %dn", getpid() );
printf( "Parent process %dn", getppid() );
printf( "i = %dn", i );
exit( 0 ); // <<-- added statement
}
else
{
waitpid( pid, NULL, 0 );
}
}
printf( "Child process (end) %dn", getpid() );
printf( "Parent process (end) %dn", getppid() );
printf( "total = %dn", total );
printf( "i = %dn", i );
exit( 0 );
}
the output of the above code is:
Child process 10378
Parent process 10377
i = 1
Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3
however, the code fails to check for the error case from the function: fork()
so will be open to a major failure if the call to fork()
fails. I.E. the code should be (also) checking for a returned value of -1 from the call to fork()
and calling
if( pid < 0 )
{
perror( "fork failed");
exit( 1 );`
}
add a comment |
up vote
1
down vote
accepted
the following corrected code:
- cleanly compiles
- performs the desired functionality
- clearly shows that
total
in the parent is no changed by the child processes
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main( void ) // <<-- corrected statement
{
int i, pid;
for(i = 1; i < 3; ++i)
{
pid = fork();
if(pid == 0)
{
total = total + i;
printf( "n") ;
printf( "Child process %dn", getpid() );
printf( "Parent process %dn", getppid() );
printf( "i = %dn", i );
exit( 0 ); // <<-- added statement
}
else
{
waitpid( pid, NULL, 0 );
}
}
printf( "Child process (end) %dn", getpid() );
printf( "Parent process (end) %dn", getppid() );
printf( "total = %dn", total );
printf( "i = %dn", i );
exit( 0 );
}
the output of the above code is:
Child process 10378
Parent process 10377
i = 1
Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3
however, the code fails to check for the error case from the function: fork()
so will be open to a major failure if the call to fork()
fails. I.E. the code should be (also) checking for a returned value of -1 from the call to fork()
and calling
if( pid < 0 )
{
perror( "fork failed");
exit( 1 );`
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
the following corrected code:
- cleanly compiles
- performs the desired functionality
- clearly shows that
total
in the parent is no changed by the child processes
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main( void ) // <<-- corrected statement
{
int i, pid;
for(i = 1; i < 3; ++i)
{
pid = fork();
if(pid == 0)
{
total = total + i;
printf( "n") ;
printf( "Child process %dn", getpid() );
printf( "Parent process %dn", getppid() );
printf( "i = %dn", i );
exit( 0 ); // <<-- added statement
}
else
{
waitpid( pid, NULL, 0 );
}
}
printf( "Child process (end) %dn", getpid() );
printf( "Parent process (end) %dn", getppid() );
printf( "total = %dn", total );
printf( "i = %dn", i );
exit( 0 );
}
the output of the above code is:
Child process 10378
Parent process 10377
i = 1
Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3
however, the code fails to check for the error case from the function: fork()
so will be open to a major failure if the call to fork()
fails. I.E. the code should be (also) checking for a returned value of -1 from the call to fork()
and calling
if( pid < 0 )
{
perror( "fork failed");
exit( 1 );`
}
the following corrected code:
- cleanly compiles
- performs the desired functionality
- clearly shows that
total
in the parent is no changed by the child processes
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int total = 0;
int main( void ) // <<-- corrected statement
{
int i, pid;
for(i = 1; i < 3; ++i)
{
pid = fork();
if(pid == 0)
{
total = total + i;
printf( "n") ;
printf( "Child process %dn", getpid() );
printf( "Parent process %dn", getppid() );
printf( "i = %dn", i );
exit( 0 ); // <<-- added statement
}
else
{
waitpid( pid, NULL, 0 );
}
}
printf( "Child process (end) %dn", getpid() );
printf( "Parent process (end) %dn", getppid() );
printf( "total = %dn", total );
printf( "i = %dn", i );
exit( 0 );
}
the output of the above code is:
Child process 10378
Parent process 10377
i = 1
Child process 10379
Parent process 10377
i = 2
Child process (end) 10377
Parent process (end) 10375
total = 0
i = 3
however, the code fails to check for the error case from the function: fork()
so will be open to a major failure if the call to fork()
fails. I.E. the code should be (also) checking for a returned value of -1 from the call to fork()
and calling
if( pid < 0 )
{
perror( "fork failed");
exit( 1 );`
}
answered Nov 10 at 0:38
user3629249
10.8k1914
10.8k1914
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53233502%2fproblem-to-understand-how-it-works-theses-processes%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
4
When you
fork()
, the original process and the child both continue from the point of the fork, initially with the same state (subject to some exceptions that don't matter here). In particular, in your case both continue to execute the loop, independently. If that doesn't answer the question then I'm afraid you're going to need to explain your more clearly what it is that confuses you.– John Bollinger
Nov 9 at 21:47
You might find it helpful to draw out a process tree, or to write out a table of events for each of the processes.
– John Bollinger
Nov 9 at 21:48
1
it cannot explain the value of the variable "i" in the root process
. You actually only printi
of the root process one time at the end. In the loop you only print whenfork()
returns0
and that only applies for child processes.– Osiris
Nov 9 at 21:52
OT: When the parameters to
main()
argc
andargv
are not going to be used, to avoid the compiler outputting warning messages about the unused variables, use the signature:int main( void )
– user3629249
Nov 10 at 0:23
regarding:
if(pid == 0) { total = total + i; printf("n"); printf("Child process %dn", getpid()); printf("Parent process %dn", getppid()); printf("i = %dn", i); }
at the end of the child process, MUST have anexit( 0 );
statement, otherwise the execution of the child will continue around the loop– user3629249
Nov 10 at 0:26