Wait System Call
Wait System Call
1. If there are at least one child processes running when the call to wait() is
made, the caller will be blocked until one of its child processes exits. At that
moment, the caller resumes its execution.
2. If there is no child process running when the call to wait() is made, then this
wait() has no effect at all. That is, it is as if no wait() is there.
Consider the following program. Click here to download a copy of this file fork-03.c.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
void main(void)
{
pid_t pid1, pid2, pid;
int status;
int i;
char buf[BUF_SIZE];
pid = getpid();
sprintf(buf, "%s%s child process starts (pid = %d)\n",
space, number, pid);
write(1, buf, strlen(buf));
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "%s%s child's output, value = %d\n", space, number, i);
write(1, buf, strlen(buf));
}
sprintf(buf, "%s%s child (pid = %d) is about to exit\n",
space, number, pid);
write(1, buf, strlen(buf));
exit(0);
}
This program shows some typical process programming techniques. The main
program creates two child processes to execute the same printing loop and display a
message before exit. For the parent process (i.e., the main program), after creating
two child processes, it enters the wait state by executing the system call wait().
Once a child exits, the parent starts execution and the ID of the terminated child
process is returned in pid so that it can be printed. There are two child processes
and thus two wait()s, one for each child process. In this example, we do not use the
returned information in variable status.
However, the parent does not have to wait immediately after creating all child
processes. It may do some other tasks. The following is an example. Click here for
this file fork-04.c.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
void main(void)
{
pid_t pid1, pid2, pid;
int status;
int i;
char buf[BUF_SIZE];
ParentProcess();
void ParentProcess(void)
{
int a, b, c, d;
int abcd, a4b4c4d4;
int count = 0;
char buf[BUF_SIZE];
pid = getpid();
sprintf(buf, "%s%s child process starts (pid = %d)\n",
space, number, pid);
write(1, buf, strlen(buf));
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "%s%s child's output, value = %d\n",
space, number, i);
write(1, buf, strlen(buf));
}
sprintf(buf, "%s%s child (pid = %d) is about to exit\n",
space, number, pid);
write(1, buf, strlen(buf));
exit(0);
}
The main program creates two child processes. Both processes call function
ChildProcess(). The main program, the parent process, calls function
ParentProcess(). This function computes all Armstrong numbers in the range of 0
and 9999. An Armstrong number in the range of 0 and 9999 is an integer whose
value is equal to the sum of its digits raised to the fourth power. After this, the
parent enters the wait state, waiting for the completion of its child processes. Note
that since we have two processes running concurrently, we have no way to predict
which one will terminate first and hence waiting for a specific child process is a risky
move. This is why we don't have a "specific" wait in all of the previous programs.
If the returned pid is unimportant, we can treat function wait() as a procedure. The
following code is a simple modification to the last few statements (in the main
function) of the previous example.
sprintf(buf, "*** Parent enters waiting status .....\n");
write(1, buf, strlen(buf));
wait(&status);
sprintf(buf, "*** Parent detects a child process was done ***\n");
write(1, buf, strlen(buf));
wait(&status);
printf("*** Parent detects another child process was done ***\n");
printf("*** Parent exits ***\n");