SOLUTIONS
SE/CS 3SH3 & CS 3MH3: Operating System Concepts
Term II (Winter 2009
)
Test 2 March 17, 2009 Time: 50 Minutes 5 Questions: 100 Marks Instructor: Dr. Kamran Sartipi
Name: Student ID:
WRITE CLEARLY, YOU MAY LOOSE MARKS FOR UNREADABLE ANSWERS
Question 1 (File System) [25 marks]
We want to create a file with full pathname “/sw/user/3SH3/test2” in the Unix system. Answer the following questions:
a) For this operation, how many files (or directories) must be read into the memory? Mention the file names.
4 files (or directories) must be read into the memory:
‘/’ (root), ‘sw’, ‘user’, ‘3SH3’ directories and then file ‘test2’ will be added to the ‘3SH3’ directory.
b) In general, what are the contents of one directory entry?
The contents of directories depend on the implementation of the file system but in general the list of the “pathname component” (i.e., file/dir names in the directory) and their associated “i-node numbers” (e.g., “sw, 5” in the “root” directory) are contained in one directory entry.
c) What are the contents of an entry in the System‐wide‐open‐file table (I‐node table), and an entry in the per‐process‐open‐file table (fd table)?
Contents of an entry in the system-wide-open-file table: • Copy of the FCB of each open file.
• Number of processes that have opened the file.
Contents of an entry in the per-process-open-file table (fd table):
• Pointer to the appropriate entry in the system-wide-open-file table. • Pointer to the current location in the file.
• Access mode with which the file is opened.
d) Briefly explain index‐allocation mechanism used in Unix OS to allocate disk‐ blocks to a file.
Using the “hard‐disk blocks” shown below and an “I‐node table” and a “fd‐ table” allocate all the black‐colored disk‐blocks (with an arbitrary order) to the file “test2” such that the operation
create (“/sw/user/3SH3/test2”) returns file‐descriptor 3 (i.e., fd = 3).
Indexed allocation is a type of allocation that brings all the pointers to the blocks of a file into one location: i.e., “index block”. Each file has its own index block, which is an array of disk-block addresses. The ith
entry in the index block points to ith
block of the file [1].
Hard‐disk
Question 2 (CPU Scheduling) [15 marks]
Consider the following set of processes, with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 6
P2 1
P3 3
P4 2
P5 5
b) What is the turnaround time of each process for each of the scheduling algorithms in part “a”?
Turnaround time is the interval from the time of submission of a process to the time of completion [1].
P1 P2 P3 P4 P5
SJF 17 1 6 3 11
RR 16 3 12 7 17
a) c) What is the waiting time of each process for each of the scheduling algorithms?
Waiting time is the sum of the periods a process spends waiting in the ready queue [1].
P1 P2 P3 P4 P5
SJF 11 0 3 1 6
RR 10 2 9 5 12
d) Which of the algorithms results in the minimal average waiting time. Show your calculations.
Average Waiting Time for SJF = (11+0+3+1+6)/5 = 21/5 = 4.2 msec Average Waiting Time for RR = (10+2+9+5+12)/5 = 38/5 = 7.6 msec
As it was expected, average waiting time for SJF is minimal since SJF is provably optimal, in that it gives the minimum average waiting time for a given set of processes [1].
P2 P4 P3 P5 P1
0 1 3 6 1
1 1
7 Shortest Job First (SJF)
0
P1 P2 P3 P4 P5 P1 P3 P5 P1 P5
2 3 5 7 9 1
1 1
2 1
4 1
6 1
Question 3 (Semaphore) [10 marks]
a) Define two semaphore operations “wait(S)” and “signal(S)” using a pseudocode we had in the class slides.
wait(S): signal(S):
S.value--;
if(S.value < 0){
add this process to S.L; block();
}
S.value++;
if(S.value <= 0){
remove a process from S.L; wakeup(P);
}
b) What would be the value of semaphore “S” when it is used for:
- “process synchronization”:
- “mutual exclusion”:
- “resource allocation for a number of resources”:
“Process Synchronization” S.value = 0; “Mutual Exclusion” S.value = 1;
“Resource Allocation for a Number of Resources” S.value = ‘number of resources’
Question 4 (Process Synchronization) [25 marks]
Below the structure of the Readers & Writers problem is shown using the semaphores
mutex & wrt and an integer variable readCount.
READER WRITER
1 wait(mutex); wait(wrt)
2 readCount++; ……
3 if (readCount == 1) writing section 4 wait(wrt); ……
5 signal(mutex); signal(wrt)
6 …
13 signal(mutex):
a) In the above algorithm what are the initial values of mutex, wrt, and readCount?
mutex = 1, wrt = 1, readCount = 0
b) What class of real-world problems does the readers & writers solution refer to? Give an example of a real-world problem we discussed in class.
An example can be online ticket reservation. All users that are checking schedules for tickets can be considered as readers who can do their job simultaneously. Users that are reserving (or buying) the ticket are writers since their activity changes the content of database. During the time the content of database is being updated, no one can read from database.
c) Suppose the reader and writer processes arrive at different times and want to enter to their “reading or writing sections” as follows:
• Readers R1 to R4 arrive at times 1, 2, 3, and 4 sec (seconds) and each reader stays in its reading section for 2 sec.
• Writers arrive at times W1: 2 sec, W2: 4 sec, W3: 5 sec, each stay in its writing section for 2 sec.
• Finally reader R5 arrives at time 8 sec and wants to stay in its reading section for 4 sec.
Draw two Gantt charts for readers and writers and show the arrival and departure times of readers and writer processes.
Arrival
Time Sec:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Readers:
d) Define the term “starvation”. Using this algorithm, can the readers suffer from “starvation”? Can the writers suffer from starvation? You may use the above example to justify your answers.
Starvation is a situation in which processes wait indefinitely within the semaphore or in general in waiting state [1].
Readers don’t suffer from starvation. As it can be seen from above example, readers can read simultaneously. Therefore the only way they may suffer from starvation is by having lots of writers. When a reader is blocked at wait(wrt), it will be added to the end of wrt queue. If new writers arrive after the reader, they would be added to the queue after reader. When all the processes that are ahead of the reader have done their jobs, the reader will start its read operation and new writers will not block reader from doing its operation.
Writers may suffer from starvation.When the first reader starts its operation it changes wrt semaphore to 0 and it is signaled only when the last reader has finished its operation. Therefore if a writer arrives at the middle of reading operation and then a sequence of readers comes, the writer will suffer from starvation.
R1
1 2 3 4 5 6 7 8 9 1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7
R2 R3
R4
R5
W1
W2 W3 Arrival
Time:
Readers:
Question 5 (Deadlock Avoidance) [25 marks]
Consider the following snapshot of a system:
Allocation Max Available
ABCD ABCD ABCD
P0 0012 0012 1520
P1 1000 1750
P2 1354 2356
P3 0632 0652
P4 0014 0656
Answer the following questions using the banker’s algorithm: a. What is the content of the matrix Need?
b. Is the system in a safe state? Show the steps.
c. If a request from process P1 arrives for (0, 4, 2, 0), can the request be granted immediately? Show the steps for your answer.
See the solution for question 10 in assignment 3.
Reference:
[1] Silberschatz, Galvin, and Gagne, “Operating Systems Concepts with Java, 8th Edition”, John Wiley & Sons Inc., 2009.