By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,179 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,179 IT Pros & Developers. It's quick & easy.

About sleep() in child process

P: n/a
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.

May 24 '06 #1
Share this Question
Share on Google+
7 Replies


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

I suggest you post this to comp.unix.programmer, as the code is UNIX
specific, thus off topic here.

--
Ian Collins.
May 24 '06 #2

P: n/a

newgoat wrote:

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.
I think this is happening because the parent process is getting the
time slice first than the child process. If you put a sleep(3).
statement as soon as you enter the parent process block, you will get
the correct output. else{
int j; sleep(3);
printf("\nParent: PID is %d\n", getpid());
for(j = 0; j < 10; j++){
printf("p ");
}
printf("\n");
}


bash-2.03# ./sleep
Child: PID is 581686
c c c c c c c c c c

Parent: PID is 958654
p p p p p p p p p p

HTH,
Anunay

May 24 '06 #3

P: n/a

newgoat wrote:
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 parent process is executing first not beacuse of your sleep but
beacuse of the scheduler.Due to which the loop in the parent is
executed first and then the loop in the child is executed along with
the sleep, which is not making any difference to the output.
Try to put some sleep in the parent and then check the output......You
will get the exepcted resutts.

Cheers,
Sandeepksinha
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.


May 24 '06 #4

P: n/a
newgoat wrote:
sleep() process unistd.h sys/types.h sys/wait.h pid_t fork() _exit(1) getpid() sleep(2) _exit(0)


Everything that I've quoted above,
is off topic for this newsgroup.

Read the document at this URL:
http://www.ungerhu.com/jxh/clc.welcome.txt
and try to select the appropriate newsgroup.

--
pete
May 24 '06 #5

P: n/a
newgoat wrote:

I just wrote a short program to see process switch when sleep() is
invoked within a process. The code is as follows: [...] 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?


Although you would have to go someplace where fork(), child processes,
and so on are on-topic, in order to get a detailed explanation, there
is one part of your code which an on-topic answer can help explain.
It's called "buffered I/O", and stdout on your system is probably
buffered.

And, even taking this into account, you can't guarantee anything about
the order of the parent-vs-child output without doing a lot more work.
Again, something like comp.unix.programmer is probably the place to
ask. (Though check their FAQ first to make sure.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

May 24 '06 #6

P: n/a
"newgoat" <ne*****@gmail.com> wrote:

# #include<stdio.h>

# f = fork();

Left to their own devices fork() and stdio stomp on each other's
feet. You have to take responsibility for synchronizing them by
hand, or use write() instead of printf(), or get nondeterministic
results. Any further analysis is futile.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I hope it feels so good to be right. There's nothing more
exhilarating pointing out the shortcomings of others, is there?
May 24 '06 #7

P: n/a
sandy wrote:
newgoat wrote:
I just wrote a short program to see process switch when sleep()
is invoked within a process. The code is as follows:
.... snip ...
Try to put some sleep in the parent and then check the output.
.....You will get the exepcted resutts.


Please do not reply to off-topic postings with other than advice as
to what newsgroups may be applicable. The reason is there is
nobody monitoring this newsgroup who has the ability to correct any
possible errors (at least in theory), and that we do not want to
clutter the group with such OT material.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 24 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.