Docsity

Operating System Lab Assignment: Forking Processes and Synchronization, Exercises of Operating Systems

Toggle sidebar
bg1
Operating System
Lab Assignment
Issue date: 5th Oct. 14
Deadline: 12th Oct. 14 by 11:59:59 p.m.
Assignments to be submitted at: [email protected]
Note: Assignment shall be submitted as a word document with the code and outputs for the
corresponding questions. The document shall contain your Roll No and Name on the top.
1. Are all processes created using fork(). Yes/No, Explain with the help of a C program.
2. Write a program using fork and exec system calls to print all the file details of a file
which is read by the child process in its exec system call. Also print pid’s of both parent
and child processes.
3. A group of very hungry Linux users accidentally entered Microsoft’s headquarters in
Redmond. They are so hungry, however, that they all have to eat at the headquarters.
There is only one restaurant and it can serve to at most C Linux users at the same time.
Since the Linux users are outnumbered by the Microsoft employees, they like to ensure
their safety by entering the restaurant in groups of C people. Once all Linux users in the
group have finished eating, they leave the restaurant as a group again. Of course, the
Linux users want to leave the area as quickly as possible and therefore should the
restaurant be available, a group of Linux users should enter it as fast as they can.
Now write a C program, using POSIX threads and POSIX mutexes and condition
variables, that creates N threads, each of which is supposed to model a Linux user in the
above scenario. Your program should accept the two input parameters N and C. N is the
number of Linux users and C is described above. You can assume that N is divisible by
C. The Linux users are numbered with 1 up to N. Each thread representing a Linux user
should call the following functions, which you should also implement, in the specified
order:
static void enter_restaurant(...)
{
// print on stdout that Linux user i is entering the restaurant
}
static void eat(...)
{
// sleep(n); where n is a pseudo-random number between 1 and 3
// print on stdout that Linux user i is starting to eat
// sleep(n); where n is a pseudo-random number between 2 and 6
// print on stdout that Linux user i finished eating
}
static void exit_restaurant(...)
{
// print on stdout that Linux user i is leaving the restaurant
pf2

Partial preview of the text

Download Operating System Lab Assignment: Forking Processes and Synchronization and more Exercises Operating Systems in PDF only on Docsity!

Operating System

Lab Assignment

Issue date: 5 th^ Oct. 14

Deadline: 12 th^ Oct. 14 by 11:59:59 p.m.

Assignments to be submitted at: [email protected]

Note: Assignment shall be submitted as a word document with the code and outputs for the corresponding questions. The document shall contain your Roll No and Name on the top.

  1. Are all processes created using fork(). Yes/No, Explain with the help of a C program.
  2. Write a program using fork and exec system calls to print all the file details of a file which is read by the child process in its exec system call. Also print pid’s of both parent and child processes.
  3. A group of very hungry Linux users accidentally entered Microsoft’s headquarters in Redmond. They are so hungry, however, that they all have to eat at the headquarters. There is only one restaurant and it can serve to at most C Linux users at the same time. Since the Linux users are outnumbered by the Microsoft employees, they like to ensure their safety by entering the restaurant in groups of C people. Once all Linux users in the group have finished eating, they leave the restaurant as a group again. Of course, the Linux users want to leave the area as quickly as possible and therefore should the restaurant be available, a group of Linux users should enter it as fast as they can.

Now write a C program, using POSIX threads and POSIX mutexes and condition variables, that creates N threads, each of which is supposed to model a Linux user in the above scenario. Your program should accept the two input parameters N and C. N is the number of Linux users and C is described above. You can assume that N is divisible by C. The Linux users are numbered with 1 up to N. Each thread representing a Linux user should call the following functions, which you should also implement, in the specified order:

static void enter_restaurant(...) { // print on stdout that Linux user i is entering the restaurant }

static void eat(...) { // sleep(n); where n is a pseudo-random number between 1 and 3 // print on stdout that Linux user i is starting to eat // sleep(n); where n is a pseudo-random number between 2 and 6 // print on stdout that Linux user i finished eating }

static void exit_restaurant(...) { // print on stdout that Linux user i is leaving the restaurant

Note: Make sure all error situations are handled appropriately. Hint: When testing your program, you can use additional sleep () calls before/after printing on stdout.

  1. A single-lane bridge connects the two Vermont villages of North Tunbridge and South

Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.) Using semaphores, design an algorithm that prevents deadlock. Initially, do not be concerned about starvation (the situation in which northbound farmers prevent southbound farmers from using the bridge, or vice versa).

  1. Consider the deadlock situation that could occur in the dining-philosophers problem when the philosophers obtain the chopsticks one at a time. Discuss how the four necessary conditions for deadlock indeed hold in this setting. Discuss how deadlocks could be avoided by eliminating any one of the four conditions.