|
Home > Archive > Unix Programming > June 2007 > diff outputs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| #include <stdio.h>
main()
{
int pid = fork();
if(pid==0) {
printf("1:the child about to become a zombie.pid = %d",getpid());
} else {
printf("\n0.0:parent here. pid = %d",getpid());
getchar(); // inclusion of this line changes the output. why?
pid=fork();
if (pid==0) {
printf("\n2:the child about to become a zombie. pid =
%d",getpid());
} else {
printf("\n0.1:parent here. pid = %d",getpid());
printf("\n0.2:now look into process table");
getchar();
}
}
}
the output with getchar:
1:the child about to become a zombie.pid = 5153
0.0:parent here. pid = 5152
2:the child about to become a zombie. pid = 5154
0.1:parent here. pid = 5152
0.2:now look into process table
the output without getchar:
1:the child about to become a zombie.pid = 5167
0.0:parent here. pid = 5166
0.1:parent here. pid = 5166
0.2:now look into process table0.0:parent here. pid = 5166
2:the child about to become a zombie. pid = 5168
how come these two are different
| |
| Jens Thoms Toerring 2007-06-30, 7:21 pm |
| Ravi <ra.ravi.rav@gmail.com> wrote:
> #include <stdio.h>
> main()
You better make
int main( void )
> {
> int pid = fork();
Please note that the return value of fork() not necessarily is a
simple int, that's why it's declared to return value of type
pid_t (which can be an int, but also e,g. a long int).
> if(pid==0) {
> printf("1:the child about to become a zombie.pid = %d",getpid());
> } else {
> printf("\n0.0:parent here. pid = %d",getpid());
> getchar(); // inclusion of this line changes the output. why?
> pid=fork();
> if (pid==0) {
> printf("\n2:the child about to become a zombie. pid =
> %d",getpid());
> } else {
> printf("\n0.1:parent here. pid = %d",getpid());
> printf("\n0.2:now look into process table");
> getchar();
> }
> }
> }
> the output with getchar:
> 1:the child about to become a zombie.pid = 5153
> 0.0:parent here. pid = 5152
> 2:the child about to become a zombie. pid = 5154
> 0.1:parent here. pid = 5152
> 0.2:now look into process table
> the output without getchar:
> 1:the child about to become a zombie.pid = 5167
> 0.0:parent here. pid = 5166
> 0.1:parent here. pid = 5166
> 0.2:now look into process table0.0:parent here. pid = 5166
> 2:the child about to become a zombie. pid = 5168
> how come these two are different
Pure chance. You can't make any assumption about the sequence
in which parent and child processes get invoked, treat this as
random. Actually, if you have two or more process never make
any assumptions about which process is going to run at which
time unless you have some interprocess synchronization put in
plase. Without that you could as well have output like this
(with or without the getchar() call):
0.0:parent here. pid = 5166
1:the child about to become a zombie.pid = 5167
0.1:parent here. pid = 5166
2:the child about to become a zombie. pid = 5168
0.2:now look into process table0.0:parent here. pid = 5166
or some other combinations. It all depends on how the system
schedules (and interrupts) the processes, and you can't forsee
that (and even if you should find some sort of rule it rather
likely will be different on another system).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
|
|
|