Solved

wait and waitpid

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 90
Doc'in system (example?) BA 7 110
either24  challenge 19 97
"Black Box" Testing of Control System Software 2 51
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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