Solved

wait and waitpid

Posted on 2004-08-10
4
523 Views
Last Modified: 2013-12-26
Could any1 plz explain the differences between wait and waitpid ? Im a bit confused cuz i got this problem:

1. create a child process using fork();
2. child process does st.
3. partent process does st, then wait for child process to finish-> print out exit code of child process.

my program looks like:

pid=fork();
if(pid==0)
{  ... // child process does st
}
else
{... //parent process does st
int exit_code
 wait(&exit_code);
 printf("%d",exit_code);
}

is that correct ? n could i use waitpid() func in this problem ?

Thanks for ur time.

0
Comment
Question by:lebuihung
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11764377
The manpage pretty much tells the difference:

NAME
       wait, waitpid - wait for process termination

SYNOPSIS
       #include <sys/types.h>
       #include <sys/wait.h>

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

DESCRIPTION
       The  wait  function suspends execution of the current pro­
       cess until a child has exited, or until a signal is deliv­
       ered  whose  action is to terminate the current process or
       to call a  signal  handling  function.   If  a  child  has
       already  exited by the time of the call (a so-called "zom­
       bie" process), the function returns immediately.  Any sys­
       tem resources used by the child are freed.

       The  waitpid  function  suspends  execution of the current
       process until a child as specified by the pid argument has
       exited,  or until a signal is delivered whose action is to
       terminate the current process or to call a signal handling
       function.   If  a  child  as  requested by pid has already
       exited by the time of the call (a so-called "zombie"  pro­
       cess),  the  function  returns  immediately.   Any  system
       resources used by the child are freed.


In short - 'wait()' waits for unspecified child processes, 'waitpid()' waits for a process specified by it's PID.

>> could i use waitpid() func in this problem ?

Sure, e.g.

pid=fork();
if(pid==0)
{  ... // child process does st
}
else
{... //parent process does st
int exit_code;
waitpid(pid,&exit_code,0);
printf("%d",exit_code);
}
0
 

Author Comment

by:lebuihung
ID: 11764494
Thansk for ur quick reply. How about the case i put some constants as WNOHANG... into waitpid()

i.e: waitpid(pid,&exit_code,WNOHANG) ?

could u plz explain the diff between those constants ?

Cheers.
0
 

Author Comment

by:lebuihung
ID: 11764509
and by the way, after calling fork(), control will be returned to Parent process first or Child one first ? or it gonna depend on the OS at the moment ? Regards.
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 11765317
>>How about the case i put some constants as WNOHANG... into waitpid()

In this case, 'waitpid()' will return immediately, and you can check the status of the child using

WIFEXITED(exit_status)

>> and by the way, after calling fork(), control will be returned to Parent process first or Child one first ?

That's unpredictable and may differ from call to call. Theoretically, to both at the same time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Powershell based monitoring system 2 137
Whole sheet autoscrub still needed 19 48
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
lucky13 challenge 11 120
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now