About sleep() in child process

N

newgoat

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.
 
I

Ian Collins

newgoat said:
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.
 
A

Anunay

newgoat said:
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
 
S

sandy

newgoat said:
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
 
K

Kenneth Brody

newgoat said:
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:[email protected]>
 
C

CBFalconer

sandy said:
.... 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/>
 
S

SM Ryan

# #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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,871
Messages
2,569,919
Members
46,171
Latest member
A.N.Omalum

Latest Threads

Top