wait4


 
Thread Tools Search this Thread
Top Forums Programming wait4
# 1  
Old 02-27-2009
wait4

hello again!
i have the following code,
Code:
int main()
{
 struct rusage rusage;
for(int i=0;i<100;i++)
{
 pid=fork();
  if(pid==0)execl("","",NULL);
 else if(pid>0) {  
     wait4(pid,&status,0,&rusage);
     printf("%lu.%06lu\n",rusage.ru_utime.tv_sec,rusage.ru_utime.tv_usec);
 }
 else perror();

The result of printf command is alwayw 0.000000(zero). why this happens?can anybody give a explanation!

thanx

Warning: The preceding code is dangerous. Append a "return 0;" statement after the printf to run this on your system.

Last edited by otheus; 02-27-2009 at 08:39 AM.. Reason: added [code] tags, warning
# 2  
Old 02-27-2009
I get the same most of the time, but if you look carefully, you will occasionally get a process that actually used user time. Grep the output like this: "prog | grep -v 00000$".

Also, note that by not exiting or returning after the printf, you create a "fork bomb". Your process will run endlessly, forking processes on end.

Why the exec takes so little user-time? Because you're making exactly one library function call as a user, which calls the system exec() call, which from at that point belongs to the system time.
# 3  
Old 02-27-2009
First of all, i would like to thanx you for the help.
is there an explanation why wait4 and getrusage(RUSAGE_CHILDREN,..) give me different values!!
for example, wait4=most values are in the range of (0.00000, 0.01000)
and in the other hand, getrusage values varies from 4.17 to 4.20
why this happens?

thanx
# 4  
Old 02-27-2009
I already gave you the answer: getrusage(RUSAGE_CHILDREN...) is cumulative.
Cumulative means: it adds all children together. It reports all values (added together) for all of the children waited for.

wait4 waits for ONE child, it does not add resources used for all children together.

wait4 == one child
getrusage(RUSAGE_CHILDREN...) == all children.

Your example gets almost no printable results but zero because your child process does almost nothing: get created and then call exec. For this one operation on a fast machine, it is possible to be a very small amount of time, smaller than the granularity of the timeval struct.

If you do hundreds of these eventually they add up to a value that displays non-zero.

To see results:

Try having execl() call some tiny C program that does something both pointless and hopefully harmless: opens a process-specific filename, writes to the file, closes the file, then removes the file.

see man tmpnam to be able to create a safe filename. Be sure to remove the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

what is diff b/w wait3() and wait4() system call

helo, i read man pages but still confuse about wait3() and wait4() stystem call what is exact diff b/w wait3() and wait4() system call and what is diff b/w waitpid() and wait3() and wait4() Regards, Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies
Login or Register to Ask a Question