473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

About sleep() in child process

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
7 9967
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

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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent...
8
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the...
1
by: binaryboy | last post by:
In msdn library, there is an example about "Creating a Child Process with Redirected Input and Output". now in my case, the child process whill use "printf" to write to its stdout which is...
3
by: David Hirschfield | last post by:
An xmlrpc client/server app I'm writing used to be super-simple, but now threading has gotten into the mix. On the server side, threads are used to process requests from a queue as they come in....
3
by: michdoh | last post by:
Hi All I'm looking for some help on creating a basic multi threaded application. i'n new to threads in this environment so for test purposes I've produced a very basic program. (which doesn't...
21
by: JackCoke | last post by:
Hello. I am new at this so forgive me if there is a simple answer, although it is one I could not find. My VB 2003 program can scan a folder, determine the size of the file it finds, wait 30...
22
by: Jason Zheng | last post by:
This may be a silly question but is possible for os.wait() to lose track of child processes? I'm running Python 2.4.4 on Linux kernel 2.6.20 (i686), gcc4.1.1, and glibc-2.5. Here's what happened...
2
by: Karthik Gurusamy | last post by:
Hi, Wondering if there is a way to measure a child process's cpu usage (sys and user) when the child is still running. I see os.times() working fine in my system (Linux 2.6.9-42.7.ELsmp), but it...
2
by: virupax | last post by:
Which is the best one to use sleep or delay ( for a delay in the process ) .As i understand use of sleep, allows the kernel to schedule the process. And if a sleep of microseconds is used, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.