Operating Systems

Laboratory 5

Learning goals: in this laboratory activity you will learn how to use waitpid and exec system
calls. You will understand how to wait for a specific child and how to excute command in your
own programs.

Exercise 1
Using system calls fork and waitpid write a program that receive a number n from the command
line and do the following:
1. creates n processes
2. the first child created sleep n second, the second child sleep n-1 seconds and so on.....(the
last child sleep 1 second)
3. the parent wait for “all” the child(it waits sequentially starting from the first created to the
last – you have to use waitpid system call)

-----------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------

Exercise 2

Modify the exercise 1 using wait instead waitpid . How many times the parent has to call “wait” ?
Does this program behaves as the previous one? Check it out!
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

Exercise 3

Run, in background mode, the programs you wrote in the exercises 1 and 2 and using ps command
try to figure out how many zombie process you have.