140 likes | 408 Views
Operating Systems TSL & Priority Inversion. Hardware Solutions. No Interrupt Checking No Context Switching. Disabling/Enabling Interrupts. Check for Interrupt: Process Interrupt. Decode Instruction. Fetch Next Instruction. Execute Instruction. Start. Halt. This solution is dangerous
E N D
Hardware Solutions No Interrupt Checking No Context Switching • Disabling/Enabling Interrupts Check for Interrupt: Process Interrupt Decode Instruction Fetch Next Instruction Execute Instruction Start Halt • This solution is dangerous • It will not work if we have multiple CPUs • Since, the Disable Interrupt instruction will execute on only one CPU • Rest of the CPUs, will continue checking the interrupts
Software Lock FLAG = TRUE FLAG = FALSE FLAG = TRUE FLAG = FALSE FLAG = FALSE The solution will work, if Testing and Setting the Flag are atomic Process 1 1.while (FLAG == FALSE); 2.FLAG = FALSE; Process 2 1.while (FLAG == FALSE); 1.while (FLAG == FALSE); 2. 2.FLAG = FALSE; 3.critical_section(); 3.critical_section(); 4.FLAG = TRUE; 5.noncritical_section(); Timeout No two processes may be simultaneously inside their critical sections
Hardware Lock • Some instruction sets assist us in implementing mutual exclusion on multiple processors • Test and Set Lock (TSL) • TSL Rx, LOCK_VARIABLE • Reads the contents of the memory variable LOCK_VARIABLE • Stores it in the Rx register • Stores back the value 1 at the address of LOCK_VARIABLE. Guaranteed to be Atomic
TSL is “Atomic” • Atomic => Uninterruptible • No other process can access the memory until TSL is complete • The CPU executing the TSL instruction locks the memory bus • Thus, prohibits others CPUs from accessing the memory
Mutual Exclusion Using TSL 1.TSL Rx, LOCK; //copy LOCK to register //and LOCK = 1 2.cmp Rx,0 //Check if Old value of //LOCK was 0. (Rx == 0) 3.jne Line1 //Jump if not equal to //Line 1 4.Critical_Section(); 5.mov LOCK,0 //Lock == 0;
Busy Waiting and Priority Inversion • Peterson’s Solution and TSL both solve the mutual exclusion problem • However, both of these solutions sit in a tight loop waiting for a condition to be met (busy waiting). • Wasteful of CPU resources
Busy Waiting and Priority Inversion • Suppose we have two processes • One of high priority H • One of low priority L • The scheduling algorithm runs Hwhenever it is in ready state • Suppose • L is in its critical section • H becomes ready • L will be placed in a ready state • H will be in the run state
Busy Waiting and Priority Inversion • But, the H will not be able to run • Even L cannot run again to release H • This is sometimes known as the • Priority inversion problem.
Sleep and Wakeup • The solution is that if a process Pi cannot enter the critical section • Pi should not waste CPU’s time by executing the Idle loop. • Pi’s status should be changed from Run to Block • Pi should be removed from the ready list • Entered in a block list • Since, Pi is not in the ready list, no context switch will bring Pi in the run state • Thus, Pi will not waste CPU’s cycle.
Sleep and Wakeup • Later on, when Pi can enter the critical section, Pi’s status should be changed from block to ready • It should be entered in the ready list. Dispatch Ready Running No CPU time wastage Can Enter the Critical Section Cannot Enter the Critical Section Blocked
Sleep and Wakeup: Implementation • Sleep() and Wakeup() can be two system calls. • When a process Pi determines that it can not enter the critical section it calls sleep() • Pi Entered in the block list • When the other process finishes with the critical section, it wakesup Pi, Wakeup(Pi) • Pi Entered in the ready list • Wakeup takes a single input parameter, which is the ID of the process to be unblocked
Summary of the Synchronization Solutions discussed so far • 1. Hardware solution • Disable Interrupts on Uniprocessor System • Limitation: Dangerous in user space + No Time slicing • TSL (or something like TSL) on Multiprocessor system • Limitation: Busy Waiting + Priority Inversion • 2. Software solution • Peterson’s solution for two processes • Limitation: Busy Waiting + Priority Inversion • Bakery algorithm for multiple processes • Limitation: Busy Waiting + Priority Inversion