|
Home > Archive > Unix Programming > May 2006 > About sleep() in a child process
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]
| Author |
About sleep() in a child process
|
|
| newgoat@gmail.com 2006-05-24, 1: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.
| |
| Jim Cochrane 2006-05-24, 7:16 pm |
| 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 ***
| |
| newgoat 2006-05-24, 7:16 pm |
| 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 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 ***
| |
| Jim Cochrane 2006-05-24, 7:16 pm |
| 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 ***
| |
| davids@webmaster.com 2006-05-25, 1: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
|
|
|
|
|