Docsity

CPU Scheduling: Round Robin, Priority, FCFS, SJF, and Multilevel Feedback Queues, Assignments of Operating Systems

Toggle sidebar
bg1
CS423UG Homework 2 Solution
September 27, 2005
1. Let Ebe the CPU efficiency.
(a) Q→ ∞
In this case, round robin behaves exactly same as FCFS, i.e., once process possesses CPU, it
finishes its job. Therefore, in average, CPU spends Tseconds for serving one process, and S
seconds for context switch, which gives us
E=T
T+S
(b) Q>T
Same as above. Since quantum is greater than the average process runtime, a process can finish
its job within the quantum. Therefore,
E=T
T+S
(c) S<Q<T
We can expect dT
Qetimes of context switch for each process. Therefore,
E=T
T+dT
Qe · S
Approximation can be made when TÀQ, as follows.
E≈1
1 + S
Q
=Q
Q+S
(d) Q=S
The same argument applies to this case. Replacing Qwith Sgives
E=T
T+dT
Se · S
If TÀQ, we get E=1
2= 0.5.
(e) Q→0
In this case, most of the CPU time is used only for the context switch. Therefore E→0.
2. (a) Round Robin
Processes hold CPU in the following order, each for the quantum 0.1 sec:
ABCDE · · · ABC DE : 1,200times
ABDE · · · AB DE : 1,200times
ABE · · · ABE : 1,200times
AE ···AE : 1,200times
A· · · A: 1,200times
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CPU Scheduling: Round Robin, Priority, FCFS, SJF, and Multilevel Feedback Queues and more Assignments Operating Systems in PDF only on Docsity!

CS423UG Homework 2 Solution

September 27, 2005

  1. Let E be the CPU efficiency.

(a) Q → ∞ In this case, round robin behaves exactly same as FCFS, i.e., once process possesses CPU, it finishes its job. Therefore, in average, CPU spends T seconds for serving one process, and S seconds for context switch, which gives us

E =

T

T + S

(b) Q > T Same as above. Since quantum is greater than the average process runtime, a process can finish its job within the quantum. Therefore,

E =

T

T + S

(c) S < Q < T We can expect d TQ e times of context switch for each process. Therefore,

E =

T

T + d TQ e · S

Approximation can be made when T ¿ Q, as follows.

E ≈

1 + QS

Q

Q + S

(d) Q = S The same argument applies to this case. Replacing Q with S gives

E =

T

T + d TS e · S

If T ¿ Q, we get E = 12 = 0.5. (e) Q → 0 In this case, most of the CPU time is used only for the context switch. Therefore E → 0.

  1. (a) Round Robin

Processes hold CPU in the following order, each for the quantum 0.1 sec:

ABCDE · · · ABCDE : 1 , 200 times ABDE · · · ABDE : 1 , 200 times ABE · · · ABE : 1 , 200 times AE · · · AE : 1 , 200 times A · · · A : 1 , 200 times

Turnaround time for each process can be calculated as follows:

C : 1199 · 0 .5 + 0. 3 = 599. 8 D : 1200 · 0 .5 + 1199 · 0 .4 + 0. 3 = 1079. 9 B : 1200 · 0 .5 + 1200 · 0 .4 + 1199 · 0 .3 + 0. 2 = 1439. 9 E : 1200 · 0 .5 + 1200 · 0 .4 + 1200 · 0 .3 + 1200 · 0. 2 = 1680 A : 1200 · (0.5 + 0.4 + 0.3 + 0.2 + 0.1) = 1800

Therefore, the mean turnaround time is: 599 .8 + 1079.9 + 1439.9 + 1680 + 1800 5

= 1319. 92 sec

(b) Priority Scheduling

Processes hold CPU in the following order, each for its length:

BEACD

Turnaround time for each process can be calculated as follows:

B : 6 = 6 E : 6 + 8 = 14 A : 6 + 8 + 10 = 24 C : 6 + 8 + 10 + 2 = 26 D : 6 + 8 + 10 + 2 + 4 = 30

Therefore, the mean turnaround time is: 6 + 14 + 24 + 26 + 30 5

= 20min = 1200sec

(c) FCFS Processes hold CPU in the following order, each for its length:

ABCDE

Turnaround time for each process can be calculated as follows:

A : 10 = 10 B : 10 + 6 = 16 C : 10 + 6 + 2 = 18 D : 10 + 6 + 2 + 4 = 22 E : 10 + 6 + 2 + 4 + 8 = 30

Therefore, the mean turnaround time is: 10 + 16 + 18 + 22 + 30 5

= 19. 2 min = 1152sec

  1. The following program is a possible solution.

typedef int semaphore; semaphore mutex = 1; semaphore tobacco = 0; semaphore paper = 0; semaphore matches = 0;

void agent(void) { while (TRUE) { down(&mutex}; Select random semaphore from tobacco, paper, or matches: call this ; Put missing ingredients on table; up(&); } }

void smoker(has tobacco) { while (TRUE) { down(&tobacco); Get ingredients from table, make cigarette smoke; up(&mutex); } }

Smoker who has paper or matches can be defined similarly. This solution avoids starvation.

  1. The following is one possible solution.

semaphore mutex = 1; // accessing the sliding marker semaphore menaccess = 1, womenaccess = 1; // wait for men/women semaphore menwait = 0, womenwait = 0; // queue to enter the bathroom int menwaiting = 0, womenwaiting = 0; // Boolean: are there men/women waiting to enter? int mencount = 0, womencount = 0; // number of men/women inside bathroom string sliding_marker = "empty"; // defined in problem

void Woman(void) { down(womenaccess); down(mutex) if (sliding_marker == "men present") up(mutex); womenwaiting = 1; down(womenwait); // first waiting woman blocks - rest block on womenaccess above down(mutex); else if (sliding_marker == "empty") sliding_marker = "women present"; womencount++; up(mutex);

up(womenaccess);

Use bathroom;

down(womenaccess); down(mutex); womencount--; if (womencount == 0) // sliding_marker=="women present" if (menwaiting == 1) sliding_marker = "men present"; menwaiting = 0; up(menwait); // wake up first waiting man else sliding_marker = "empty" up(mutex) up(womenaccess); }

void Man(void) { down(menaccess); down(mutex); if sliding_marker == "women present" up(mutex); menwaiting = 1; down(menwait); // first waiting man blocks - rest block on menaccess above down(mutex); else if (sliding_marker == "empty") sliding_marker = "men present"; mencount++; up(mutex); up(menaccess);

Use bathroom;

down(menaccess); down(mutex); mencount--; if (mencount == 0) // sliding_marker=="men present" if (womenwaiting == 1) sliding_marker = "women present"; womenwaiting = 0; up(womenwait); // wake up first waiting woman else sliding_marker = "empty"; up(mutex); up(menaccess); }

The above solution(s) may cause starvation and/or deadlocks.

  1. #define free 0 #define busy 1 semaphore mutex = 1; semaphore calvin_cust = 0, terri_cust = 0, eddie_cust = 0; semaphore calvin_barb = 0, terri_barb = 0, eddie_barb = 0; semaphore chairs = 0; int waiting = 0;

void customers(void) { down(mutex); if (waiting>=N){ up(mutex); /* exit the shop / exit; } else if (waiting>0){ waiting++; / wait in the chairs / up(mutex); down(chairs); checkbarberswithpriority(); } / no one is waiting - some barber may be free / else checkbarberswithpriority(); else{ / all barbers busy - take chair / waiting++; / wait in the chairs */ up(mutex); down(chairs); checkbarberswithpriority(); } }

void checkbarberswithpriority(){ down(mutex); if (calvin==free){ /* check barbers in order of priority / up(calvin_cust); / wake up barber / calvin=busy; up(mutex); down(calvin_barb); / the customer waits for the barber */ get_haircut(); } else if (terri==free){ up(terri_cust); terri=busy; up(mutex); down(terri_barb); get_haircut(); } else if (eddie==free){ up(eddie_cust); eddie=busy; up(mutex); down(eddie_barb);

get_haircut(); } }

Code for Calvin is as follows.

void calvin(void) { while (TRUE) { down(mutex); if(waiting>0) up(chairs); /* wake up some customer / waiting--; calvin=free; up(mutex); down(calvin_cust); / sleep. zzz. / down(mutex); / barber woken up / up(calvin_barb); / the barber gets the customer */ up(mutex); cut_hair(); } }

Code for terri() and eddie() are similar. Caveat: the above solution might cause starvation - under some circumstances, a customer may leave the shop without being serviced.