?
Solved

wait and waitpid

Posted on 2004-08-10
4
Medium Priority
?
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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