Solved

wait and waitpid

Posted on 2004-08-10
4
569 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
viewing source code from eclipse 13 110
has77  challenge 9 90
Detect file exist or not 3 179
Excel file not created as expected 7 79
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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