LINUX TIPS AND TRICKS --- September 21, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

Waiting for a Child
By Danny Kalev

Collecting the exit status of a child is called "waiting on the 
process". Linux defines four functions that return the exits 
status. The first, wait4(), takes four arguments, hence its 
name. wait4() has the following prototype:

    pid_t wait4(pid_t pid, int *status, int options, struct
    rusage *usage);

The first argument is the pid of the process whose status is 
returned. It may contain one of the following ranges of values: 

    < -1 -- Wait for any child whose pgid is the same as the
            absolute value of pid.
      -1 -- Waits for any child to terminate.
       0 -- Waits for any child in the same process group as
            the current process.
     > 0 -- Waits for a process whose pid was passed as an
            argument.

The second argument is a pointer to int. wait4() assigns the 
exit status of the examined process to it. The third argument 
controls the behavior of wait4(). The WNOHANG flag causes 
wait4() to return immediately. If no processes are available, 
then wait4() returns 0 rather than a valid pid. The WUNTRACED 
flag causes wait4() to return if a child has been stopped. You 
can combine these two flags with the bitwise or operator. The 
final argument is a pointer to a rusage struct that wait4() 
fills with resource usage information of the examined process 
and its children. If you pass a NULL value, then no resource 
usage information is returned. 

Each of the three additional functions offers a subset of 
wait4()'s functionality:

    pid_t wait(int* status);

The call always blocks until a child has returned.

    pid_t waitpid(pid_t pid, int *status, int options);

Identical to wait4() except that it doesn't retrieve resource 
usage information.

    pid_t wait3(int *status,  int options, struct rusage *usage);

Identical to wait4() except that the user cannot specify which 
child should be checked.

    --Analyzing the Exit Status

As the exit status format is somewhat convoluted, a set of 
macros enables you to extract information about the causes of 
the examined process's exit:

    WIFEXITED(status) 

Returns true if the process exited normally (i.e., if it exited 
as a result of calling exit() from the main() function or if 
its main() executed a return statement). If WIFEXITED(status) 
is true, WEXITSTATUS (status) returns the process's exit code.

    WIFSIGNALED(status)

Returns true if the process exited due to a signal. If 
WIFSIGNALED (status) is true, WTERMSIG(status) returns the 
signal number that terminated the process.

    WIFSTOPPED(status)

Returns true if the process has been stopped due to a signal 
(read more on Linux processes' modes here: 
http://www.itworld.com/nl/lnx_tip/03022001/). If 
WIFSTOPPED(status) is true, then WSTOPSIG(status) returns the 
signal that stopped the process. Note: wait4() returns 
information on stopped processes only if you specify the 
WUNTRACED flag as on option.
________________________________________________________________

SPONSORED LINK

ITworld.com's RFP Exchange is the place for your outsourcing 
needs!

Post an RFP for FREE and receive proposals from qualified IT 
providers.
Go to the RFP Exchange and get your projects started today!
http://itw.itworld.com/GoNow/a14724a42318a76537031a0
_________________________________________________________________

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and distributed 
computing. He is also a member of the ANSI C++ standardization 
committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Danny can be reached at 
linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

wait()
http://www.mkssoftware.com/docs/man3/wait.3.asp

waitpid()
http://www.mkssoftware.com/docs/man3/waitpid.3.asp

wait4
http://www.linuxbase.org/spec/gLSB/gLSB/baselib-wait4-2.html

wait3, wait4
http://www.linux.com/develop/man/2/wait4/

Process Information
http://sunland.gsfc.nasa.gov/info/elisp/Process_Information.html
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your
  account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter 
- When finished, click submit

Questions? Please e-mail customer service at: 
mailto:support@itworld.com
________________________________________________________________

CONTACTS

* Editorial: Andrew Santosusso, Associate Editor, Newsletters, 
  andrew_santosusso@itworld.com

* Advertising: Clare O'Brien, Vice President of Sales, 
  clare_obrien@itworld.com

* Recruitment advertising: Jamie Swartz, Eastern, Regional Sales 
  Manager, jamie_swartz@itworld.com or Paul Duthie, Western
  Regional Sales Manager, paul_duthie@itcareers.net

* Other inquiries: Jodie Naze, Senior Product Marketing Manager, 
  jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
