<Exercise 2> Due date: April21, 11:59 P.M. 1. What is the meaning of the term busy waiting? What other kinds of waiting are there in an operating system? Can busy waiting be avoided altogether? Explain your answer. Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop without relinquish the processor. Alternatively, a process could wait by relinquishing the processor, and block on a condition and wait to be awakened at some appropriate time in the future. Busy waiting can be avoided but incurs the overhead associated with putting a process to sleep and having to wake it up when the appropriate program state is reached. 2. Solve the dining philosopher’s problem using monitors instead of semaphores. CONCEPT: A number of philosophers sits forever around a circular table. In the center of the table is a bowl of rice. Between each two philosophers is a shared chopstick. Each philosopher spends his/her life alternating between thinking and eating. When a philosopher gets hungry, he/she must gain exclusive access to both chopsticks before eating. We seek a mechanism that provides the necessary synchronization and meets the usual requirements of progress (efficiency), freedom from deadlock, and bounded waiting (fairness). A monitor is used to control access to state variables and condition variables. The chopsticks themselves are not part of the monitor (nor is the rice). If they were, then the entire act of eating would have to be a monitor procedure and only one philosopher could eat at a time. Monitor procedures are defined for the actions of obtaining the chopsticks and putting them down. These are used as entry and exit protocols for program segments which actually use the chopsticks; this use takes place outside the monitor. The number of philosophers is given by NUM_PHILS. Each philosopher can be in one of the three states THINKING, HUNGRY, or EATING. For each philosopher there is a condition variable on which that philosopher waits when he/she is hungry but one or both chopsticks are unavailable. A philosopher who wants to eat checks the state of both neighbors and proceeds if neither is eating. Otherwise the philosopher waits. A philosopher who has finished eating gives each neighbor a chance to eat, if they are hungry and if their other chopstick is free. MONITOR: DATA: condition can_eat[NUM_PHILS]; enum states {THINKING, HUNGRY, EATING} state[NUM_PHILS-1]; int index; INITIALIZATION: for (index=0; index<NUM_PHILS; index++) { flags[index] = THINKING; } MONITOR PROCEDURES: /* request the right to pickup chopsticks and eat */ entry void pickup(int mynum) { /* announce that we're hungry */ state[mynum] = HUNGRY; /* if neighbor's aren't eating, proceed */ if ((state[mynum-1 mod NUM_PHILS] != EATING) && (state [mynum+1 mod NUM_PHILS] != EATING)) { state[mynum] = EATING; } /* otherwise wait for them */ else can_eat[mynum].wait; /* ready to eat now */ state[mynum] = EATING; } /* announce that we're finished, give others a chance */ entry void putdown(int mynum) { /* announce that we're done */ state[mynum] = THINKING; /* give left (lower) neighbor a chance to eat */ if ((state [mynum-1 mod NUM_PHILS] == HUNGRY) && (state [mynum-2 mod NUM_PHILS] != EATING)) { can_eat[mynum-1 mod NUM_PHILS].signal; } /* give right (higher) neighbor a chance to eat */ if ((state [mynum+1 mod NUM_PHILS] == HUNGRY) && (state [mynum+2 mod NUM_PHILS] != EATING)) { can_eat[mynum+1 mod NUM_PHILS].signal; } } PHILOSOPHER: /* find out our id, then repeat forever */ me = get_my_id(); while (TRUE) { /* think, wait, eat, do it all again ... */ think(); pickup(me); eat(); putdown(me); } 3. The banker's algorithm is being run in a system with m resource classes and n processes. In the limit of large m and n, the number of operations that must be performed to check a state for safety is proportional to manb. What are the values of a and b? Comparing a row in the matrix to the vector of available resources takes m operations. This step must be repeated on the order of n times to find a process that can finish and be marked as done. Thus, marking a process takes on the order of mn steps. Repeating the algorithm for all n processes means that the number of steps is then mn2. 4. Compare the circular-wait scheme with the deadlock-avoidance schemes (like the banker’s algorithm) with respect to the following issues: a. Runtime overheads b. System throughput A deadlock-avoidance scheme tends to increase the runtime overheads due to the cost of keep track of the current resource allocation. However, a deadlock-avoidance scheme allows for more concurrent use of resources than schemes that statically prevent the formation of deadlock. In that sense, a deadlock-avoidance scheme could increase system throughput. 5. Consider the following snapshot of a system: Allocation Max Available ABCD ABCD P0 0 0 1 2 0012 1520 P1 1 0 0 0 1750 P2 1 3 5 4 2356 ABCD P3 0 6 3 2 0652 P4 0 0 1 4 0656 Answer the following questions using the banker’s algorithm: a. What is the content of the matrix Need? The values of Need for processes P0 through P4 respectively are (0, 0, 0, 0), (0, 7, 5, 0), (1, 0, 0, 2), (0, 0, 2, 0), and (0, 6, 4, 2) b. Is the system in a safe state? Yes. With Available being equal to (1, 5, 2, 0), either process P0 or P3 could run. Once process P3 runs, it releases its resources which allow all other existing processes to run. c. If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately? Yes it can. This results in the value of Available being (1, 1, 0, 0).One ordering of processes that can finish is P0, P2, P3, P1, and P4. 6. What advantage is there in having different time-quantum sizes on different levels of a multilevel queuing system? Processes that need more frequent servicing, for instance, interactive processes such as editors, can be in a queue with a small time quantum. Processes with no need for frequent servicing can be in a queue with a larger quantum, requiring fewer context switches to complete the processing, making more efficient use of the computer. 7. Three processes are running on the following figure time line. 0 P0 10 CPU (10) P1 P2 CPU(4) 20 I/O (11) CPU(9) I/O(16) I/O(4) CPU (4) I/O(12) 30 CPU(4) 40 CPU(8) I/O (8) CPU(7) CPU(6) I/O(4) CPU (6) For each of the following scheduling algorithms, draw four Gantt charts and determine the average waiting time. 50 a. SJF (preemptive) b. Priority (Non-preemptive) (Priority - P0: 1, P1: 2, P2: 3) c. RR (Quantum = 5) d. Multi-level queue (Priority - P0: 1, P1 & P2: 0), SJF (preemptive) with priority 0, FCFS with priority 1, Fixed Priority Queue & Preemptive