brettmjohnson has hit the nail on the head ...
waitpid = wait3(&status, WNOHANG, &ru_start); should actually have been
waitpid = wait3(&status, 0, &ru_start);
WNOHANG is causing you the woes
Main Topics
Browse All TopicsHi.....I was wondering if anyone can help me with the following problem that I am having. I just wrote a problem that forks a new child process and tracks the user and system time statistics for it. I used the getrusage() function to track the statistics of the child process. However, I used the wait() function to have the parent wait for the child to terminate. I would like to rewrite my code to use the wait3() function instead of using the wait() function and print out the same results. I have looked at the Unix man pages and some online resources but I can't seem to get it work correctly. Below is a copy of my source code. You will also find a commented out line that I tried to implement wait3() with but it did not seem to work. Any help will be greatly appreciated. Thanks :-)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
struct rusage ru_start;
double sys;
double user;
pid_t pid;
pid_t waitpid;
time_t ltime;
int status;
time(<ime);
printf("Start Time: %s\n", ctime(<ime));
if ((pid = fork()) < 0)
{
perror("fork() failed");
exit(1);
}
if (pid == 0)
{
printf("Child pid = %d, ppid = %d\n", getpid(), getppid());
execl("child_program","chi
perror("execl");
exit(1);
}
/* waitpid = wait3(&status, WNOHANG, &ru_start); */
waitpid = wait(NULL);
getrusage(RUSAGE_CHILDREN,
printf("user time is %ld.%06ld\n", ru_start.ru_utime.tv_sec, ru_start.ru_utime.tv_usec)
printf("system time is %ld.%06ld\n", ru_start.ru_stime.tv_sec, ru_start.ru_stime.tv_usec)
printf("got to the end\n");
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: brettmjohnsonPosted on 2004-05-28 at 20:15:54ID: 11185457
wait3() with WNOHANG does not actually wait. If a child process has exited (or some other
signal is pending), wait3(..., WNOHANG, ...) returns with the status, otherwise it returns
immediately with a status of 0. At the time wait3() is called in the parent program, the child
probably hasn't even started executing yet. Since there is no status, wait3(WNOHANG)
returns immediately, and rusage structure is either empty or only contains information about
the parent.