Docsity

Assignment 3 for Operating Systems - Fall 2006 | CSCI 3753, Assignments of Operating Systems

Toggle sidebar
bg1
CSCI 3753
Spring 2006
Homework 3
Due date: 03/14/06
Grade: 7% of your final grade is allocated for this homework.
Submission deadline: This homework is due in class on March 14, 2006.
What to submit: Submit a hardcopy of your answers.
1. [25 Points] Give all possible final values of variables xand yin the following program
that consists of three parallel processes P1, P2 and P3. The three processes share
integer variables xand y, and semaphore variables s1 and s2. Explain how you got
your answer.
int x = 10; y = 15;
semaphore s1 = 1, s2 = 1;
P1:
{P(s1); x = x+1; P(s2); x = x * y; y = y-5; V(s2); V(s1);}
P2:
{P(s2); y = 10; V(s2); P(s1) x = 0; V(s1);}
P3:
{P(s1); P(s2); x = y*10; V(s2); V(s1);}
2. [20 Points] The Dining Savages Problem. A tribe of savages eats communal dinners
from a large pot that can hold M servings of stewed missionary. When a savage wants
to eat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage
wakes up the cook and then waits until the cook has refilled the pot. Implement the
actions of the savages and cook using monitors. Your solution should avoid deadlock
and awaken the cook only when the pot is empty.
pf2

Partial preview of the text

Download Assignment 3 for Operating Systems - Fall 2006 | CSCI 3753 and more Assignments Operating Systems in PDF only on Docsity!

CSCI 3753

Spring 2006 Homework 3

Due date: 03/14/

Grade: 7% of your final grade is allocated for this homework.

Submission deadline: This homework is due in class on March 14, 2006.

What to submit: Submit a hardcopy of your answers.

  1. [25 Points] Give all possible final values of variables x and y in the following program that consists of three parallel processes P 1, P 2 and P 3. The three processes share integer variables x and y, and semaphore variables s1 and s2. Explain how you got your answer.

int x = 10; y = 15; semaphore s1 = 1, s2 = 1;

P1: { P(s1); x = x+1; P(s2); x = x * y; y = y-5; V(s2); V(s1);}

P2: { P(s2); y = 10; V(s2); P(s1) x = 0; V(s1);}

P3: { P(s1); P(s2); x = y*10; V(s2); V(s1);}

  1. [20 Points] The Dining Savages Problem. A tribe of savages eats communal dinners from a large pot that can hold M servings of stewed missionary. When a savage wants to eat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage wakes up the cook and then waits until the cook has refilled the pot. Implement the actions of the savages and cook using monitors. Your solution should avoid deadlock and awaken the cook only when the pot is empty.
  1. Single Bathroom Problem. There is only one bathroom in your department and you are given the task of designing an algorithm for the usage of this bathroom under the following constraints: - The bathroom can be used by men, women, kids, and a janitor. - At any time, there can be at most MAX individuals in the bathroom. - The janitor must have exclusive access to the bathroom. - Both men and women cannot be in the bathroom at the same time. - Kids can share the bathroom with men or women. - There must be at least one man or woman present in the bathroom for a kid to enter the bathroom.

(a) [20 Points] Use semaphores to design your algorithm. Do not worry about starvation in your design. (b) [15 Points] Modify your solution in (a) to avoid starvation. Because cleanliness is important, design your algorithm in such a way that it gives preference to the janitor and treats all other individuals equally.

  1. [20 Points] Sleeping professor problem. You have been hired by the Computer Sci- ence department to help synchronize a professor and his/her students during office hours. The professor, of course, wants to take a nap if no students are around to ask questions. If there are students who want to ask questions, they must synchronize with each other and with the professor so that (i) only one person is speaking at any one time, (ii) each student question is answered by the professor, and (iii) no stu- dent asks another question before the professor is done answering the previous one. You are to write four procedures: AnswerStart(), AnswerDone(), QuestionStart(), and QuestionDone(). The professor loops running the code: AnswerStart(); give an- swer; AnswerDone(). AnswerStart() doesn’t return until a question has been asked. Each student loops running the code: QuestionStart(); ask question; QuestionDone(). QuestionStart() doesn’t return until it is the student’s turn to ask a question. Pro- fessors consider it rude if a student does not wait until his/her question has been completely answered. So, QuestionDone() should not return until the professor has finished answering the question. Use CCRs for synchronization.