0% found this document useful (0 votes)
123 views3 pages

Wait and Waitpid

The document describes an experiment to block a parent process from terminating before its child process using wait() and waitpid() system calls. It explains that waitpid() allows a parent to wait for a specific child process by PID, whereas wait() waits for any child. The algorithm has the parent fork a child, use waitpid() in a loop to check the child's status, and print messages until the child exits. This ensures the parent doesn't terminate before its child completes.

Uploaded by

pranay ambare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views3 pages

Wait and Waitpid

The document describes an experiment to block a parent process from terminating before its child process using wait() and waitpid() system calls. It explains that waitpid() allows a parent to wait for a specific child process by PID, whereas wait() waits for any child. The algorithm has the parent fork a child, use waitpid() in a loop to check the child's status, and print messages until the child exits. This ensures the parent doesn't terminate before its child completes.

Uploaded by

pranay ambare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO.

AIM: Write a C program to block a parent process before termination of child process using
wait and waitpid.

THEORY:

wait() and waitpid():

 pid_t data type represents process IDs. Get the process ID of a process by calling getpid.
 The wait() system call suspends execution of the calling process until one of its children
terminates.
 The syntax of waitpid: pid_t waitpid (child_pid, &status, options);
 The waitpid() system call suspends execution of the calling process until a child specified
by pid argument has changed state. By default, waitpid() waits only for terminated
children, but this behavior is modifiable via the options argument.
 The interpretation of the pid argument for waitpid depends on its value:

 pid == −1 - Waits for any child process. In this respect, waitpid is equivalent


to wait.
 pid > 0 - Waits for the child whose process ID equals pid.
 pid == 0 - Waits for any child whose process group ID equals that of the calling
process.
 pid < −1 - Waits for any child whose process group ID equals the absolute value
of pid.

 Options Parameter
 WNOHANG: Return immediately if no child has exited.
 WIFEXITED: Returns true if the child terminated normally by returning
from main().
 WEXITSTATUS: Returns the exit status of the child. It employs only if
WIFEXITED returned true.
 WIFSIGNALED: Returns true if the child process was terminated by a
signal.

 The waitpid function provides three features that aren’t provided by the wait function:

1. The waitpid function lets us wait for one particular process, whereas


the wait function returns the status of any terminated child (popen function)
2. The waitpid function provides a nonblocking version of wait. There are times
when we want to fetch a child’s status, but we don’t want to block.
3. The waitpid function provides support for job control with
the WUNTRACED and WCONTINUED options.
ALGORITHM:
1. Declare pid of type pid_t
2. Declare t of type time_t
3. if pid < 0 then PRINT “Fork() error”
4. Else if pid = = 0 then Sleep a process for a while and exit.
5. Else If pid = = -1 then PRINT “Wait() Error”
6. Else
if pid = = 0 then get a time in seconds process, PRINT “Child Still running” and sleeps
the process.
7. Else if WIFEXITED(status) = true then returns the exit status of the child
8. Else returns child did not exit.

CONCLUSION: The waitpid wait for one particular process, whereas the wait function returns
the status of any terminated child process. Hence blocked a parent process before terminating the
chid process using wait() and waitpid() system call.

PROGRAM:

#define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
main() {
pid_t pid;
time_t t;
int status;

if ((pid = fork()) < 0)


perror("fork() error");
else if (pid == 0) {
sleep(5);
exit(1);
}
else do {
if ((pid = waitpid(pid, &status, WNOHANG)) == -1)
perror("wait() error");
else if (pid == 0) {
time(&t);
printf("child is still running at %s", ctime(&t));
sleep(1);
}
else {
if (WIFEXITED(status))
printf("child exited with status of %d\n", WEXITSTATUS(status));
else puts("child did not exit successfully");
}
} while (pid == 0);
}

OUTPUT:-

Child is still running at Thu Apr 12  07:15:06  2018


Child is still running at Thu Apr 12  07:15:07 2018
Child is still running at Thu Apr 12  07:15:08 2018
Child is still running at Thu Apr 12  07:15:09 2018
Child is still running at Thu Apr 12  07:15:10 2018
Child exited with status of 1

You might also like