diff outputs
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > diff outputs




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    diff outputs  
Ravi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-30-07 06:20 PM

#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






[ Post a follow-up to this message ]



    Re: diff outputs  
Jens Thoms Toerring


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-01-07 12:21 AM

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",get
pid());
>         } else {
>                 printf("\n0.0:parent here. pid = %d",getpid());
>                 getchar(); // inclusion of this line changes the output. w
hy?
>                 pid=fork();
>                 if (pid==0) {
>                         printf("\n2:the child about to become a zombie. pi
d =
> %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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:37 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register