UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-12-16 03:36 PM

sangfroid
Member
Registered: 2006-09-18
Posts: 29

Re: wait and waitpid

what is the difference between workings of wait and waitpid? One kills all child and the other simple one of them...but how do they differ in their workings ?

Offline

#2 2006-12-16 06:20 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: wait and waitpid

Offline

#3 2006-12-16 10:18 PM

sangfroid
Member
Registered: 2006-09-18
Posts: 29

Re: wait and waitpid

well, you trap the signal sent by kernel by calling Signal(SIGCHLD,function)

and in function you have

function(int signo)
{
pid_t pid;
int stat;
pid=wait(&stat)
return;
}

i have few questions here.

1. what will be the values in stat in general after wait returns ?
2. we are not passing any parameter ie, values of signo ...and i also dont see this variable being used inside the function..from where is this value assigned ( is it by kernel ) and where it is used ??

3. I found that the size of int and pid_t is the same ( at least in my system)...if the size be the same, then why we are having different variable type called "pid_t" ?

your help will be highly appreciated..

Offline

#4 2006-12-16 11:39 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: wait and waitpid

i3839 already told you that neither do kill children.

These functions are used to GET the exit status of dead children, not to kill them.

Read the man pages.

Offline

#5 2006-12-17 12:28 AM

sangfroid
Member
Registered: 2006-09-18
Posts: 29

Re: wait and waitpid

Offline

#6 2006-12-17 02:42 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: wait and waitpid

Our points still stands, the info you're seeking is in the waitpid manpage (except for question 3). We can quote parts of it to answer your questions, or you could start reading the thing yourself, and learn also other things you didn't ask yet about.

In general it's better to search the answer to your question first, before asking other people to help you. If you can't bother to go looking at the most obvious place (the manpage of the function), then why should other people bother trying to help you at all?

Offline

#7 2006-12-17 05:09 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: wait and waitpid

That's probably not the way you want to write a SIGCHLD handler, either...  Since
there may be multiple dead children to reap for only a single signal, or perhaps no
children to reap at all (because a prior run of the handler already reaped them
first)...  So, you really want to do something like this:

void reaper (int sig)
{
    while (waitpid (-1, NULL, WNOHANG) > 0)
        ;
}

And, just because I'm apparently in a nicer mood than Loco and i3839, I'll take a
crack at answering your questions, too... ;-)

1. If wait*() returns successfully, then "stat" will contain the encoded exit status of
the child...  Do as they say: read the man page for wait()/waitpid()...  It explains
this status better than I have the energy to do here, and how to use the W*() macros
to extract various info from this status...

2. Yeah, the kernel is going to call your signal handler when you get hit with the
signal, and yes it always passes the signal as the first arg to the handler...  (In
some situations, it may pass more args...  See the man page for sigaction(), and
the SA_SIGINFO flag, and the "sa_sigaction" function...)

3. Because POSIX (and other similar standards groups) just love to give things new
names, even when they really don't need them... ;-)  In some cases, it almost makes
sense (things like size_t, which will vary depending on arch), but in other cases it's
just utterly stupid (things like socklen_t which MUST always remain exactly "int", or
else it'll break everything)...  I think pid_t tends to be one of the latter cases... ;-)  I
suppose a 64-bit system MIGHT have 64-bit PIDs, but honestly I'm not sure how
bloody useful that'd be...  Maybe sometime in the distant future, we'll have machines
that can run more than 2G processes simultaneously, but I wouldn't worry about it
anytime soon... ;-)  But, anyway, that's the reason: pedantry...

Offline

#8 2006-12-18 05:44 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: wait and waitpid

I'm sorry.

I saw only the same question i3839 saw... looks like a bug in my brain software.

And yes: I was in a bad mood.

If you already read the man pages and Rob's reply, you'll know that you can get your children's exit status using waitpid and wait.

An example may help you understand:

--- in your child process ---
   if (something_s_wrong) {
      exit(100);
   }
   else {
      exit(0);
   }

--- In the parent's SIGCHLD handler ---
stat = 0;
do {
   chldpid = waitpid(-1, &stat, WNOHANG);
   if (chldpid > 0) {
      if (WIFEXITED(stat)) {
         printf("Children PID=%d, had a %s death\n", chldpid,
                 (WEXITSTATUS(stat)==0)?"nice normal":"awful");
      }
   }      
} while (chldpid > 0);

Offline

#9 2006-12-18 06:11 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: wait and waitpid

I saw his other questions, but I know the answer to those is in the manpage too, to which I just pointed. IMHO asking another question while the answer is in the manpage you supposedly just read and expect us to not get grumpy is a bit optimistic.

I'm not here to spoonfeed basic manpage info to people in my free time, sorry. Pointing out where such info can be found, sure, but I expect people to spend some effort finding the info for themselves, especially if they get such strong hints like "look, manpage!"

What you should learn is how to find information, not a handful of specific info pried from kind people in a good mood. It's just not efficient, nor wise.

You should try to only ask questions which you can't find the answer yourself, for things that either are hard to search for, or for which you don't know where to start looking. Or a specific problem with your code. That sort of things.

Abusing the forum as a sort of lookup thing isn't nice at all. Not toward the moderators, nor toward the people who spend time and effort in writing helpful documentation which is then blatantly ignored.

Offline

#10 2007-04-25 06:33 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: wait and waitpid

--- in your child process ---
   if (something_s_wrong) {
      exit(100);
   }
   else {
      exit(0);
   }

--- In the parent's SIGCHLD handler ---
stat = 0;
do {
   chldpid = waitpid(-1, &stat, WNOHANG);
   if (chldpid > 0) {
      if (WIFEXITED(stat)) {
         printf("Children PID=%d, had a %s death\n", chldpid,
                 (WEXITSTATUS(stat)==0)?"nice normal":"awful");
      }
   }     
} while (chldpid > 0);

in above programming,what is the meanning of WNOHANG in the waitpid(),
why to use exit(100),once terminal receive the 100,how it process it?
thanks !Best wishes!

Offline

#11 2007-04-25 08:21 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: wait and waitpid

Offline

#12 2007-04-25 12:39 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: wait and waitpid

Offline

Board footer

Powered by FluxBB