Vous êtes sur la page 1sur 9

KSU/CCIS CSC227

Tutorial # 7 – Process Synchronization & Disk Scheduling


SUMMER - 2010

Question # 1:

Define the meaning of a race condition? Answer the question first and use an execution sequence to
illustrate your answer.
Answer Q 1:
A race condition is a situation in which more than one processes or threads access a shared resource
concurrently, and the result depends on the order of execution.

The following is a simple counter updating example. The value of count may be 9, 10 or 11,
depending on the order of execution of the machine instructions of count++ and count--.
int count = 10;
Thread_1(...) Thread_2(...)
{ {
// do something // do something
count++; count--;
} }

The following execution sequence shows a race condition. Two threads run concurrently (condition1).
Both threads access the shared variable count at the same time (condition 2). Finally, the computation
result depends on the order of execution of the SAVE instructions (condition 3). The execution
sequence below shows the result being 9; however, switching the two SAVE instructions yields 11.
Since all conditions are met, we have a race condition.

Thread_1 Thread_2 Comment


do something do something count = 10
LOAD count Thread_1 executes count++
ADD #1
LOAD count Thread_2 executes count--
SUB #1
SAVE count count is 11 in memory
SAVE count Now, count 9 in memory

Stating that “count++ followed by count--” or “count-- followed by count++” would produce
different results and hence a race condition is incorrect, because the threads do not access the
shared variable count at the same time (i.e., condition 2).

Question # 2:

What is the critical section? A good solution to the critical section problem must satisfy three
conditions, what are three conditions? Explain the meaning of each condition? Does starvation violate
the progress condition?
Answer Q 2:

Critical Section: Each process has a segment of code, when one process is executing in its critical
section, no other process is to be allowed to execute in its critical section.

• Mutual Exclusion --- if Pi executing in one of its critical sections, no Pj, j ≠ i , is executing in
its critical sections.
• Progress --- a process operating outside of its critical section cannot prevent other processes
from entering theirs; processes attempting to enter their critical sections simultaneously must
decide which process enters eventually.
• Bounded Waiting --- a process attempting to enter its critical region will be able to do so
eventually.

The progress condition only guarantees the decision of selecting one process to enter a critical section
will not be postponed indefinitely. It does not mean a waiting process will enter its critical section
eventually. Therefore, starvation is not a violation of the progress condition. Instead, starvation
violates the bounded waiting condition.

Question # 3:

Suppose N processes with IDs 0, 1, 2, … , N - 1 need to share some critical region of code, which only
one at a time is allowed to execute, and where they are required to repeatedly cycle through this
section in the natural order (0, 1, 2, … , N - 1). If turn is a shared integer variable initialized to zero,
and current pid is the ID of the currently executing process, and each executes the following (pseudo)
code, could a race condition occur? Why or why not?

while( TRUE )
{
while( turn != current pid ) /* wait */ ;
critical section( );
turn = (turn + 1) % N;
noncritical section ( );
}

Answer Q 3:

No race condition here. Process i must change the value of turn from i to i + 1 (modulo n)
before Process i + 1 can escape its busy waiting loop to enter the critical region. It will be the only
process in this critical region.
Question # 4:
Consider two processes Pi and Pj run concurrently. Also consider the following structures of each
process. Is it meeting all the three conditions of critical section problem or not? Explain?

flag[i] = TRUE; flag[j] = TRUE;


while (flag[j]); while (flag[i]);
CSi; CSj;
flag[i] = FALSE; flag[j] = FALSE;

Structure of Pi Structure of Pj

Answer Q 4:

• Guarantees mutual exclusion


• Violates progress --- both processes could set flag and then deadlock on the while.
• Bounded waiting violated

Question # 5:
Change the structure of process Pi in the previous question to meet all conditions? Use turn shared
variable that indicates which process has priority in entering its critical section

Answer Q 5:

flag[i] = TRUE; flag[j] = TRUE;


turn = j; turn = i;
while (flag[j] && turn == j); while (flag[i] && turn == i);
CSi; CSj;
flag[i] = FALSE; flag[j] = FALSE;

Structure of Pi Structure of Pj
Question # 6:

According to the above disk structure, define the Seek Time, Rotational Latency, Access Time and
Service Time?

Answer Q 6:

Seek time- the time it takes to move the disk arm to required cylinder.
Rotational Latency- the time it takes for the disk to rotate so that the required sector is above the disk
head.
Access Time – the time it takes to get in position to read or write. (Seek Time + Rotational Latency)
Service Time – the time to get the data (Access Time + Transfer Time)

Question # 7:

Why we need disk Scheduling? Provide six schedules?

Answer Q 7:

Disk scheduling is used to deciding which particular request for data from the hard disk should be
serviced first. In other words, when several processes are trying to pull data from the hard disk at once,
which one gets the data first?
• When a request is initiated by a process, if the disk is availabe, the request can be serviced
immediately.
• If the disk is busy, a request queue can form.
• Upon completion of a disk request, the operating system can choose which pending request to
service next.
Disk Schedules:
1. FCFS
2. SSTF (Shortest Seek Time First)
3. SCAN (to end and then return serving OR to beginning and then return to end serving)
4. C-SCAN (to end and then return to beginning without serving OR to beginning and then return
to end without serving)
5. LOOK (to last request and then return serving OR to first request and then return to end
serving)
6. C-LOOK (to last request and then return to beginning without serving OR to first request and
then to end without serving)

Question # 8:
Suppose that a disk drive has 5000 cylinders, numbered 0 to 4999. The drive is currently serving a
request at cylinder 143, and the previous request was at cylinder 125. The queue of pending requests,
in FIFO order, is
86, 1470, 913, 1774, 948, 1509, 1022, 1750, 130
Starting from the current head position, what is the total distance (in cylinders) that the disk arm
moves to satisfy all a pending requests, for each of the following disk scheduling algorithms?
a. FCFS
b. SSTF
c. SCAN
d. LOOK

Answer Q 8:
a. FCFC schedule is
143  86  1470  913  1774  948  1509  1022  1750  130
The total seek distance is (57 + 1384 + 557 + 861 + 826 + 561 + 487 + 728 + 1620) = 7081

b. SSTF schedule is
143  130  86  913  948  1022  1470  1509  1750  1774
The total seek distance is (13 + 44 + 827 + 35 + 74 + 448 + 39 + 241 + 24) = 1745

c. SCAN schedule
143  913  948  1022  1470  1509  1750  1774  4999  130  86
The total seek distance is 9769

d. LOOK schedule
143  913  948  1022  1470  1509  1750  1774  130  86
The total seek distance is 3319
Question # 9:

Given a disk unit with 200 tracks, numbered from 0 to 199.


At time 0 there is a queue of read requests, the head is positioned over track 89 and “moving towards”
the lower tracks. The disk queue of pending requests is (given in order of arrival)
2, 156, 78, 192, 19, 127, 90, 100
Two further requests on track numbers 140 and 60 arrive when servicing the request for track number
127.
Starting from the current head position of 89 for SSTF and SCAN disk scheduling algorithms:
(i) Draw a diagram showing the disk head movements.
(ii) Total distance (in tracks) that the disk head moves to satisfy all the 10 requests.
(iii) The average number of tracks.

Answer Q 9:

SSTF

0 2 19 60 78 89 90 100 127 140 156 192 199


×

SCAN

0 2 19 60 78 89 90 100 127 140 156 192 199


×

Total number of tracks Average


SSTF 337 337/10 = 33.7
SCAN 427 427/10 = 42.7
Question # 10:

Given a disk with 200 tracks, numbered from 0 to 199. It takes 1 ms for the disk Read/Write head to
travel from the current track to the next one, and that it is originally positioned at track 89 and
“moving towards” the lower tracks.
These requests arrive at the following times as shown in the table below:

Track Arrival time


Request (ms)
2 0
156 0
78 0
192 0
19 30
127 30
90 30
100 150
140 150
60 200

a) Draw diagrams showing the order in which the above 10 requests are processed, under each of
these disk scheduling algorithms?
(i) SCAN
(ii) LOOK

b) Compute the Total Service Time of all these requests for each disk scheduling algorithms;
ignore the transfer time and latency time?

Answer Q 10:

SCAN

0 2 19 60 78 89 90 100 127 140 156 192 199


×
LOOK

0 2 19 60 78 89 90 100 127 140 156 192 199


×

Total number of tracks AST(ms)


SCAN 427 427 * 1 = 427/10
LOOK 409 409 * 1 = 409/10

Question # 11:
a) Assume a moving arm disk with 200 cylinders numbered from 0 to 199. Show on a diagram
how the following requests {10, 130, 100, 40, 10, 180, 10, 130} are processed according to
FCFS policy. Assume that initially the disk head is at cylinder 120.
b) What is the total distance (in cylinders) that the disk arm moves to satisfy all pending requests?
c) Assume that the seek time between two consecutive cylinders is 0.1 ms and the transfer time
for each request cylinder is 1 ms and the latency time is negligible, what is the Average
Service Time when serving the above requests using FCFS policy?

Answer Q 11:

a) FCFS

0 10 40 100 120 130 180 200


×

b)
Total number of cylinders
FCFS 810

c) Service Time = number of cylinders * seek time + number of request * transfer time
Service Time = 810 * 0.1 + 8 * 1 = 81 ms
AST = 81 / 8 = 10.125 ms
Question # 12:
Consider a moving arm disk with 200 tracks, numbered from 0 to 199. The seek time is 0.1 ms and the
transfer time is null. Assume that the Read/Write head, which was on track 40, is currently on track 60.
Both tracks have been already served. Given the actual first list {120, 100, 20, 160, 180} of waiting
requests. A second list {140, 10, 60, 40, 120} arrives when request on track 160 of the first list is
served. Using the LOOK disk scheduling algorithm, compute the Total Service Time when serving
these 2 lists.

Answer Q 12:

LOOK (move towards the highest track (to end side)) default
×
0 10 20 40 60 100 120 140 160 180 199

Total number of tracks AST(ms)


LOOK 120 + 170 = 290 290 * 0.1 = 29 ms

LOOK (move towards the lower track (to beginning side))


×
0 10 20 40 60 100 120 140 160 180 199

Total number of tracks AST(ms)


LOOK 40+160+170 = 370 370 * 0.1 = 37 ms