About sleep() in a child process
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 > About sleep() in a child process




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

    About sleep() in a child process  
newgoat@gmail.com


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


 
05-24-06 06:16 PM

I just wrote a short program to see process switch when sleep() is
invoked within a process. The code is as follows:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main(void){
pid_t f;

f = fork();

if(f < 0){
printf("Failed to fork\n");
_exit(1);
}

else if(f == 0){
int i;

printf("\nChild: PID is %d\n", getpid());
for(i = 0; i < 10; i++){
printf("c ");
if(i == 5)
sleep(2);
}
printf("\n");

_exit(0);
}

else{
int j;

printf("\nParent: PID is %d\n", getpid());
for(j = 0; j < 10; j++){
printf("p ");
}
printf("\n");
}

return 0;

}

The output is : (Case 1)

Child: PID is 11059

Parent: PID is 11058
p p p p p p p p p p
user@localhost:~$ c c c c c c c c c c

If I commented the if...sleep(2) lines above (two lines),
the output would be: (Case 2)

Child: PID is 11068
c c c c c c c c c c

Parent: PID is 11067
p p p p p p p p p p

In Case 1, I expected the child process to print five c's (because
the child process does not start sleeping until i = 5) then sleep for 2
seconds,
during which time the parent process executes its own block of code.
But the real output suggests otherwise. Why is that?

Thanks in advance.






[ Post a follow-up to this message ]



    Re: About sleep() in a child process  
Jim Cochrane


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


 
05-25-06 12:16 AM

On 2006-05-24, newgoat@gmail.com <newgoat@gmail.com> wrote:
> I just wrote a short program to see process switch when sleep() is
> invoked within a process. The code is as follows:

You forgot to flush the stdout buffer.

>
> #include<stdio.h>
> #include<unistd.h>
> #include<sys/types.h>
> #include<sys/wait.h>
>
> int main(void){
>    pid_t f;
>
>    f = fork();
>
>    if(f < 0){
>       printf("Failed to fork\n");
>       _exit(1);
>    }
>
>    else if(f == 0){
>       int i;
>
>       printf("\nChild: PID is %d\n", getpid());
>       for(i = 0; i < 10; i++){
>          printf("c ");
>          if(i == 5)
>             sleep(2);
>       }
>       printf("\n");
>
>       _exit(0);
>    }
>
>    else{
>       int j;
>
>       printf("\nParent: PID is %d\n", getpid());
>       for(j = 0; j < 10; j++){
>          printf("p ");
>       }
>       printf("\n");
>    }
>
>    return 0;
>
> }
>
> The output is : (Case 1)
>
> Child: PID is 11059
>
> Parent: PID is 11058
> p p p p p p p p p p
> user@localhost:~$ c c c c c c c c c c
>
> If I commented the if...sleep(2) lines above (two lines),
> the output would be: (Case 2)
>
> Child: PID is 11068
> c c c c c c c c c c
>
> Parent: PID is 11067
> p p p p p p p p p p
>
> In Case 1, I expected the child process to print five c's (because
> the child process does not start sleeping until i = 5) then sleep for 2
> seconds,
> during which time the parent process executes its own block of code.
> But the real output suggests otherwise. Why is that?
>
> Thanks in advance.
>

fflush will solve your problem - e.g., run 'patch' on your C file with this
diff -u output:


--- old.c        2006-05-24 12:34:53.000000000 -0600
+++ new.c    2006-05-24 12:40:22.000000000 -0600
@@ -19,6 +19,7 @@
printf("\nChild: PID is %d\n", getpid());
for(i = 0; i < 10; i++){
printf("c ");
+                fflush(stdout);
if(i == 5)
sleep(2);
}
@@ -33,6 +34,7 @@
printf("\nParent: PID is %d\n", getpid());
for(j = 0; j < 10; j++){
printf("p ");
+                fflush(stdout);
}
printf("\n");
}


--


*** Posted via a free Usenet account from http://www.teranews.com ***





[ Post a follow-up to this message ]



    Re: About sleep() in a child process  
newgoat


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


 
05-25-06 12:16 AM

Hi Jim:

Thanks for writing. I modified the code as you did. The output
became

Child
parent.

While the printf() seems not to work at all. I wonder what has
happened?

I am quite new to system programming. Thanks for your reply!


Jim Cochrane wrote:
> On 2006-05-24, newgoat@gmail.com <newgoat@gmail.com> wrote: 
>
> You forgot to flush the stdout buffer.
> 
>
> fflush will solve your problem - e.g., run 'patch' on your C file with thi
s
> diff -u output:
>
>
> --- old.c        2006-05-24 12:34:53.000000000 -0600
> +++ new.c    2006-05-24 12:40:22.000000000 -0600
> @@ -19,6 +19,7 @@
>        printf("\nChild: PID is %d\n", getpid());
>        for(i = 0; i < 10; i++){
>           printf("c ");
> +                fflush(stdout);
>           if(i == 5)
>              sleep(2);
>        }
> @@ -33,6 +34,7 @@
>        printf("\nParent: PID is %d\n", getpid());
>        for(j = 0; j < 10; j++){
>           printf("p ");
> +                fflush(stdout);
>        }
>        printf("\n");
>     }
>
>
> --
>
>
> *** Posted via a free Usenet account from http://www.teranews.com ***






[ Post a follow-up to this message ]



    Re: About sleep() in a child process  
Jim Cochrane


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


 
05-25-06 12:16 AM

On 2006-05-24, newgoat <newgoat@gmail.com> wrote:
> Hi Jim:
>
> Thanks for writing. I modified the code as you did. The output
> became
>
> Child
> parent.
>
> While the printf() seems not to work at all. I wonder what has
> happened?
>
> I am quite new to system programming. Thanks for your reply!

Hmmm - that's rather odd.  I did a quick test before posting and on my
system got what was expected - e.g., running it again:

$ ./a.out

Child: PID is 14182
c c c c c c
Parent: PID is 14181
p p p p p p p p p p
$ c c c c


where the last line appears a couple seconds later.  You might want to try
adding some debugging printfs to find out more about what's going on and
then post back here with as much info. as possible if you don't find a
solution.  I'm not an expert at this stuff, but it's likely someone here
will be able to help if you give enough info.  Here's the code I used so
that you can make sure your new version matches:


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main(void){
pid_t f;

f = fork();

if(f < 0){
printf("Failed to fork\n");
_exit(1);
}

else if(f == 0){
int i;

printf("\nChild: PID is %d\n", getpid());
for(i = 0; i < 10; i++){
printf("c ");
fflush(stdout);
if(i == 5)
sleep(2);
}
printf("\n");

_exit(0);
}

else{
int j;

printf("\nParent: PID is %d\n", getpid());
for(j = 0; j < 10; j++){
printf("p ");
fflush(stdout);
}
printf("\n");
}

return 0;

}

--


*** Posted via a free Usenet account from http://www.teranews.com ***





[ Post a follow-up to this message ]



    Re: About sleep() in a child process  
davids@webmaster.com


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


 
05-25-06 06:23 AM

>In Case 1, I expected the child process to print five c's (because
>the child process does not start sleeping until i = 5) then sleep for 2
>seconds,
>during which time the parent process executes its own block of code.
>But the real output suggests otherwise. Why is that?

There are a lot of issues with your program. The two most severe are:

1) The parent could finish writing all of its output before the child
even gets to execute at all.

2) The child is using buffered I/O. Calling 'sleep' does not flush the
buffer.

DS






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:38 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