You are on page 1of 3

CS2257 Operating System Lab

Exp# 1b wait system call

Aim
To block a parent process until child completes using wait system call.

Algorithm

1. Create a child process using fork system call.


2. If return value is -1 then
a. Print "Process creation unsuccessfull"
b. Terminate using exit system call.
3. If return value is > 0 then
a. Suspend parent process until child completes using wait system call
b. Print "Parent starts"
c. Print even numbers from 0–10
d. Print "Parent ends"
4. If return value is 0 then
a. Print "Child starts"
b. Print odd numbers from 0–10
c. Print "Child ends"
5. Stop

Result
Thus using wait system call zombie child processes were avoided.

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Program

/* Wait for child termination - wait.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

main()
{
int i, status;
pid_t pid;

pid = fork();
if (pid < 0)
{
printf("\nProcess creation failure\n");
exit(-1);
}
else if(pid > 0)
{
wait(NULL);
printf ("\nParent starts\nEven Nos: ");
for (i=2;i<=10;i+=2)
printf ("%3d",i);
printf ("\nParent ends\n");
}
else if (pid == 0)
{
printf ("Child starts\nOdd Nos: ");
for (i=1;i<10;i+=2)
printf ("%3d",i);
printf ("\nChild ends\n");
}
}

http://cseannauniv.blogspot.com Vijai Anand


CS2257 Operating System Lab

Output

$ gcc wait.c

$ ./a.out
Child starts
Odd Nos: 1 3 5 7 9
Child ends

Parent starts
Even Nos: 2 4 6 8 10
Parent ends

http://cseannauniv.blogspot.com Vijai Anand

You might also like