You are on page 1of 1


Lab Assignment

1. Write a collection of programs p1, p2, p3 such that they execute sequentially with the same
process-id, and each program should also print its PID (Process id). The user should be able to
invoke any combination of these programs, to achieve the required functionality. For example
consider three programs twice, half, square which accept only one integer as argument and Does
some specific operation.
$twice 10 prints 20 and its process-id as output
$square 10 prints 100 and its process-id as output
$half 10 prints 5 and its process-id as output
Now the user should be able to combine these programs in any combination to achieve the
required result.
For example: $twice square half twice half 10
should calculate half(twice(half(square(twice(10))))) and print 200 as result. It should also print
the process ids of each program as it executes. Note that the process-id printed by each of these
programs should be the same, in this case.
$square twice 2 should calculate twice(square(2)) and print 8 as result, and the process id of
square and twice, which should be the same. The evaluation order is from left to right
Note that the last argument is the integer, and the remaining arguments are the programs to be
invoked. This should be generally applicable to any n number of processes, all of which are
written by you. minimum of three should be written pl p2 p3 ... pn arg_value
Remember that any number of programs should executed within one pid. The last process to
execute may print the final result. Use execvp family of system calls.
2. Consider a keyboard-reader program, which simply exits after any alphanumeric character is
pressed on the keyboard. Create two processes (or threads); one of them will read characters from
the keyboard, and the other will continuously check for alphanumeric characters. Write down the
keyboard-reader program-using pipe.
3. Write a solution to the benchmark synchronization problem of producers and consumers as
described below.
Shared memory: 1 bounded buffer data structure implemented as shared memory
Startup process: creates bounded buffer, creates needed semaphores and exits
Producer process: do 5 times: produce, sleep for 1 second enddo
Consumer process: do 5 times: consume, sleep for 1 second enddo
Any number of producer and consumer processes can be started after the startup process.
Cleanup process: deletes semaphores, deletes shared memory.
4. Once class is over, professors like to sleep — except when students bother them to answer
questions. You are to write procedures to synchronize threads representing one professor and an
arbitrary number of students. A professor with nothing to do calls IdleProf(), which checks to see
if a student is waiting outside the office to ask a question. IdleProf sleeps if there are no students
waiting, otherwise it signals one student to enter the office, and returns. A student with a question
to ask calls ArrivingStudent(), which joins the queue of students waiting outside the office for a
signal from the professor; if no students are waiting, then the student wakes up the sleeping
professor. The idea is that the professor and exactly one student will return from their respective
functions “at the same time”: after returning they discuss a topic of mutual interest, then the
student goes back to studying and the professor calls IdleProf again. Implement IdleProf and
ArrivingStudent using semaphores. You may assume that semaphores are fair (e.g., FIFO).