Example of waitpid() in use?
Asked Answered
M

3

37

I know that waitpid() is used to wait for a process to finish, but how would one use it exactly?

Here what I want to do is, create two children and wait for the first child to finish, then kill the second child before exiting.

//Create two children
pid_t child1;
pid_t child2;
child1 = fork();

//wait for child1 to finish, then kill child2
waitpid() ... child1 {
kill(child2) }
Matthieu answered 21/1, 2014 at 3:53 Comment(2)
I note that the wait(2) man page on my Linux system includes an actual example of how to use the waitpid() system call.Leash
I think you could find the answer rather easily by searching on SO for [c] waitpid; rather less easily by searching for [c] [waitpid]. I've answered 48 questions with the first search and 5 with the second, and I'm by no means the only person who has answered such questions.Sarto
C
48

Syntax of waitpid():

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

The value of pid can be:

  • < -1: Wait for any child process whose process group ID is equal to the absolute value of pid.
  • -1: Wait for any child process.
  • 0: Wait for any child process whose process group ID is equal to that of the calling process.
  • > 0: Wait for the child whose process ID is equal to the value of pid.

The value of options is an OR of zero or more of the following constants:

  • WNOHANG: Return immediately if no child has exited.
  • WUNTRACED: Also return if a child has stopped. Status for traced children which have stopped is provided even if this option is not specified.
  • WCONTINUED: Also return if a stopped child has been resumed by delivery of SIGCONT.

For more help, use man waitpid.

Chape answered 21/1, 2014 at 4:17 Comment(1)
support.sas.com/documentation/onlinedoc/sasc/doc/lr2/… provides much more detailed information with an example. I personally still could not understand how to use waitpid effectively from the 2 answers to this question here.Diplomacy
F
21

The syntax is

pid_t waitpid(pid_t pid, int *statusPtr, int options);

1.where pid is the process of the child it should wait.

2.statusPtr is a pointer to the location where status information for the terminating process is to be stored.

3.specifies optional actions for the waitpid function. Either of the following option flags may be specified, or they can be combined with a bitwise inclusive OR operator:

WNOHANG WUNTRACED WCONTINUED

If successful, waitpid returns the process ID of the terminated process whose status was reported. If unsuccessful, a -1 is returned.

benifits over wait

1.Waitpid can used when you have more than one child for the process and you want to wait for particular child to get its execution done before parent resumes

2.waitpid supports job control

3.it supports non blocking of the parent process

Favata answered 21/1, 2014 at 5:18 Comment(0)
A
5
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main (){
    int pid;
    int status;

    printf("Parent: %d\n", getpid());

    pid = fork();
    if (pid == 0){
        printf("Child %d\n", getpid());
        sleep(2);
        exit(EXIT_SUCCESS);
    }

//Comment from here to...
    //Parent waits process pid (child)
    waitpid(pid, &status, 0);
    //Option is 0 since I check it later

    if (WIFSIGNALED(status)){
        printf("Error\n");
    }
    else if (WEXITSTATUS(status)){
        printf("Exited Normally\n");
    }
//To Here and see the difference
    printf("Parent: %d\n", getpid());

    return 0;
}
Alienation answered 21/5, 2019 at 11:56 Comment(3)
Have you read the documentation about 1) return value of waitpid, 2) when it is appropriate to use WEXITSTATE, 3) what would the most normal of normal exits return for WEXITSTATUS...Marsupium
You might explain your answer a bit?Renegado
very very bad example, error handling, and waking up on EINTR (happens a lot) etc. not handled.Broca

© 2022 - 2024 — McMap. All rights reserved.