Operating Systems Semaphores Dr M Tanvir Afzal Handling

  • Slides: 8
Download presentation
Operating Systems Semaphores Dr. M. Tanvir Afzal

Operating Systems Semaphores Dr. M. Tanvir Afzal

Handling Competition in Processes Scenario ü Process A wants to use printer. ü OS

Handling Competition in Processes Scenario ü Process A wants to use printer. ü OS must decide if the printer is available, then it must be given otherwise not? 2

Handling Competition in Processes How to track Printer Availability ü Using a single bit

Handling Competition in Processes How to track Printer Availability ü Using a single bit in memory, having states set (1) and clear (0) ü Flag clear (0) means printer is available ü Flag set (1) means printer is not available. 3

Handling Competition in Processes Issue in flag-implementation ü Set and clear is not executed

Handling Competition in Processes Issue in flag-implementation ü Set and clear is not executed in single instruction. ü Consider printer is available, ‘Process A’ reads this and before it could set the bit to 1, someone other reads the available zero bit. 4

Handling Competition in Processes Solution #1: disabling interrupts ü Disable interrupts before starting to

Handling Competition in Processes Solution #1: disabling interrupts ü Disable interrupts before starting to execute the retrieve memory flag and enabling after it has been set as 1. 5

Handling Competition in Processes Solution #2: executing in one step ü Test-and-set instruction. ü

Handling Competition in Processes Solution #2: executing in one step ü Test-and-set instruction. ü CPU will complete this instruction and then will take next interrupt. ü A properly implemented flag is called Semaphore. 6

Handling Competition in Processes Critical Region ü Train track example ü Such a sequence

Handling Competition in Processes Critical Region ü Train track example ü Such a sequence of instruction is called Critical Region. ü The requirement that at one time one process can execute critical region is called mutual exclusion. 7

Summary Semaphores ü Scenario ü How to track resource availability ü Issues ü Solutions

Summary Semaphores ü Scenario ü How to track resource availability ü Issues ü Solutions ü Critical region ü Mutual exclusion 8