




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Solutions to homework problems related to cpu scheduling algorithms, including round robin, priority scheduling, fcfs, shortest job first (sjf), and multilevel feedback queues. It includes calculations for turnaround times and mean turnaround times for various scheduling algorithms and process orders.
Typology: Assignments
1 / 8
This page cannot be seen from the preview
Don't miss anything!





September 27, 2005
(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
(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,
(c) S < Q < T We can expect d TQ e times of context switch for each process. Therefore,
T + d TQ e · S
Approximation can be made when T ¿ Q, as follows.
(d) Q = S The same argument applies to this case. Replacing Q with S gives
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.
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:
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:
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
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
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.
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.
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.