DOC PREVIEW
UMass Amherst ECE 397A - Scheduling

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 counteris 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 countmay 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■nprocesses 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 existon 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, P0and P1■General structure of process Pi(other process Pj)do{entry sectioncritical sectionexit sectionreminder section} while (1);■Processes may share some common variables to synchronize their actions.8Algorithm 1■Shared variables: ✦int turn;initially turn = 0✦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 2 cannot enter until Process 1 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■Combined shared variables of algorithms 1 and 2.■Process Pido{flag [i]:= true;turn = j;while (flag [j] and turn = j) ;critical 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 processesdeveloped by Lamport 197410Bakery Algorithm ■Notation <≡lexicographical order (ticket #, process id #)✦(a,b) < c,d) if a< cor if a= cand b < d✦max (a0,…, an-1) is a number, k, such that k≥aifor ia number in 0, …, n– 1■Shared databoolean choosing[n];int number[n];Data structures are initialized to falseand 0respectivelyBakery Algorithm do { choosing[i] = true; // signal choosing a numbernumber[i] = max(number[0], number[1], …, number [n – 1])+1;choosing[i] = false; // done with choosing a numberfor (j = 0; j < n; j++) {while (choosing[j])


View Full Document

UMass Amherst ECE 397A - Scheduling

Download Scheduling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scheduling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scheduling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?