Solved

wait and waitpid

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video discusses moving either the default database or any database to a new volume.

757 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

16 Experts available now in Live!

Get 1:1 Help Now