1Last lectures: scheduling Preemptive and non-preemptive Non-preemptive works because of typical IO/CPU burst sizes Constraints in scheduling are reflecting application domain, e.g., Throughput Average waiting time Predictability, worst-case execution time Load-sharing in multiprocessor systems Based on what we learned you should be able to design a different scheduling algorithm that fits your constraints, application domain.Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris 2 & Windows 20002Background Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Example: producer-consumer type of problems, bounded-buffer scheme.Bounded-Buffer Shared data#define BUFFER_SIZE 10typedef struct {. . .} item;item buffer[BUFFER_SIZE];int in = 0;int out = 0;int counter = 0;3Bounded-Buffer Producer process item nextProduced;while (1) {while (counter == BUFFER_SIZE); /* do nothing */buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;counter++;}Bounded-Buffer Consumer process item nextConsumed;while (1) {while (counter == 0); /* do nothing */nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;counter--;}4Bounded Buffer Note that variable “counter” is accessed from both consumer and producer The statementscounter++;counter--;must therefore be performed atomically. Atomic operation means an operation that completes in its entirety without interruption.Bounded Buffer The statement “count++” may be implemented in machine language as:register1 = counter [lw $1, (counter)] // MIPS like ISAregister1 = register1 + 1 [addi, $1,$1,1]counter = register1 [sw $1, (counter)] The statement “count—” may be implemented as:register2 = counterregister2 = register2 – 1counter = register25Bounded Buffer If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. There is no guarantee that the two C statements are executed atomically Interleaving depends upon how the producer and consumer processes are scheduled. Not something you control…Bounded Buffer Assume counter is initially 5. One interleaving of statements is:producer: register1 = counter (register1 = 5)producer: register1 = register1 + 1 (register1 = 6)consumer: register2 = counter (register2 = 5)consumer: register2 = register2 – 1 (register2 = 4)producer: counter = register1 (counter = 6)consumer: counter = register2 (counter = 4) The value of count may be either 4 or 6, where the correct result should be 5.6Race Condition Race condition: The situation where several processes access – and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized.The Critical-Section Problem The problem called critical-section n processes all competing to use some shared data Each process has a code segment, called critical section, in which the shared data is accessed. Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.7Solution to Critical-Section Problem1. Mutual Exclusion. If process Piis executing in its critical section, then no other processes can be executing in their critical sections.2. Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.3. Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the nprocesses.Initial Attempts to Solve Problem Let us assume first only 2 processes General structure of process Pi(other process Pj)do {entry sectioncritical sectionexit sectionremainder section} while (1); Processes may share some common variables to synchronize their actions.8Algorithm 1 Shared variables: int turn;initially turn = i // can be set to any process turn = i Pican enter its critical section Process Pido {while (turn != i) ;critical sectionturn = j;reminder section} while (1); Satisfies mutual exclusion, but not progress Process j cannot enter until Process i entersAlgorithm 2 Shared variables boolean flag[2]; //retain state about each thread/procs…initially flag [0] = flag [1] = false. flag [i] = true Piready to enter its critical section Process Pido {flag[i] := true;while (flag[j]) ;critical sectionflag [i] = false;remainder section} while (1); Satisfies mutual exclusion, but not progress requirement. Both flag[i] could be set concurrently, both will loop forever9Algorithm 3 Combine shared variables of algorithms 1 and 2. turn is a shared variable, note access to flag[i or j] is not overlapping Process Pido {flag [i]:= true;turn = j; // if turn is written at same time only one lastswhile (flag [j] and turn == j) ; // if flag[j] is true you enter based on turncritical sectionflag [i] = false;remainder section} while (1); Meets all three requirements; solves the critical-section problem for two processes. Let us analyze thisBakery Algorithm Before entering its critical section, process receives a number (ticket). Holder of the smallest number enters the critical section. If processes Piand Pjreceive the same number, if i < j, then Piis served first; else Pjis served first. The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...Critical section algorithm for n processes developed by Lamport 197410Bakery Algorithm Notation <≡ lexicographical order (ticket #, process id #) (a,b) < (c,d) if a < c or if a = c and b < d max (a0,…, an-1) is a number, k, such that k ≥ aifor i a number in 0, …, n – 1 Shared databoolean choosing[n];int
View Full Document