20
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software Engineering University of Wisconsin-Platteville

Department of Computer Science and Software Engineering University of Wisconsin-Platteville

Embed Size (px)

DESCRIPTION

Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization. Department of Computer Science and Software Engineering University of Wisconsin-Platteville. Outlines. Critical Section Problem Mutual Exclusion Mutual Exclusion Algorithms - PowerPoint PPT Presentation

Citation preview

Page 1: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Computer Architecture and Operating Systems

CS 3230: Operating System Section

Lecture OS-5Process Synchronization

Department of Computer Science and Software Engineering University of Wisconsin-Platteville

Page 2: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Outlines Critical Section Problem Mutual Exclusion Mutual Exclusion Algorithms Synchronization Hardware Semaphores

Page 3: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Critical Section Problem 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.

Mutual Exclusion (MX) – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section MX – basic synchronization problem

Page 4: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Hardware Support 1 A process runs until it invokes an operating-

system service or until it is interrupted Interrupt Problem: MX violation

Hardware Solution: Single Processor:

• Interrupt Disabling– Processor is limited in its ability to interleave programs

• Disabling interrupts guarantees mutual exclusion

Multiprocessor:• disabling interrupts will not guarantee mutual exclusion

Page 5: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: General Solution There is a set of threads/processes that switch

between executing the critical section (CS) of code and non-critical section

Before entering the CS, a thread/process requests it

MX Solution requirement: safety – at most one thread at a time execute the CS liveness – a thread requesting the CS is given a

chance to enter it• Liveness violation: starvation – a requesting thread is

not allowed to enter the CS– deadlock – all threads are blocked and cannot

proceed – livelock – threads continue to operate but none

could enter the CS

Page 6: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Process Structure General structure of process Pi

Page 7: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Algorithm 1 P1 ( ) {

while (true) { while (turn != 1)

; /* do nothing */

CS turn = 2; non-CS

}}

P2 ( ) {while (true) { while (turn != 2)

; /* do nothing */

CS turn = 1; non-CS}

}

Pre condition: only two processes/threads in the system

Shared variables: int turn; OS sets turn to an

initial value (1 or 2)

Advantages: Safety

Problems: Violates liveness

Page 8: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Algorithm 2a Pre condition: only

two processes/threads in the system

Shared variables: Boolean variables

• P1_in_CS• P2_in_CS• Boolean variables

initially false Advantages:

liveness Problems:

Violates Safety

P1 ( ) {while (true) { while (P2_in_CS ==

true); /* do nothing

*/ P1_in_CS = true; CS P1_in_CS = false; non-CS}

}P2 ( ) {

while (true) { while (P1_in_CS ==

true); /* do nothing

*/ P2_in_CS = true; CS P2_in_CS = false; non-CS}

}

Page 9: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Algorithm 2b Pre condition: only

two processes/threads in the system

Shared variables: Boolean variables

• P1_in_CS• P2_in_CS• Boolean variables

initially false Advantages:

Safety Problems:

Violates liveness

P1 ( ) {while (true) {

P1_in_CS = true; while (P2_in_CS ==

true); /* do nothing

*/ CS P1_in_CS = false; non-CS}

}P2 ( ) {

while (true) { P2_in_CS = true;

while (P1_in_CS == true)

; /* do nothing */

CS P2_in_CS = false; non-CS}

}

Page 10: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Algorithm 3 (Peterson’s) Pre condition: only two

processes/threads in the system

Shared variables: int turn;

initially turn = 0 Boolean variables

• P1_in_CS• P2_in_CS• Boolean variables

initially false

Advantages: Safety liveness

P1 ( ) {while (true) {

P1_in_CS = true; turn=2; while (P2_in_CS == true

&& turn==2); /* do nothing */

CS P1_in_CS = false; non-CS}

}P2 ( ) {

while (true) { P2_in_CS = true; turn=1;

while (P1_in_CS == true && turn==1)

; /* do nothing */ CS P2_in_CS = false; non-CS}}

Page 11: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Multiple Processes Bakery Algorithm

MX solution for n processes

Algorithm: Before entering its critical section, process receives a

number. Holder of the smallest number enters the critical section.

If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is served first.

The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...

Page 12: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Multiple Processes Shared data

boolean choosing[n] int number[n] Data structures are

initialized to false and 0 respectively

int i; /* unique process id */ while (true) { choosing[i]=true; number[i]=max(number[0],…, number[n-1])+1; choosing[i]=false; for (j=0; j<n; j++) { while (choosing[j])

; /* do nothing */ while (number[j]>0 &&

(number[j]<number[i] || number[j]==number[i]

&& j<i)) ; /do nothing */

} CS number[i] = 0; non-CS}

Page 13: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Hardware Support 2 Special Machine Instructions

Test and Set Instruction Swap (Exchange) Instruction

Page 14: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Test and Set Instruction

boolean TestAndSet (boolean &target) {

boolean rv = target;

tqrget = true;return rv;

}

Shared data: boolean lock = false;

Process Pi

while (true) { while (TestAndSet(lock))

/* do nothing */ CS lock = false; non-CS}

Page 15: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Swap Instruction

void Swap (boolean &a, boolean &b) {

boolean temp = a;

a = b;b = temp;

}

Shared data: boolean lock=false;

Process Pi

while (true) { boolean key=true; while (key == true)

Swap(lock,key); CS lock = false;

non-CS}

Page 16: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

MX: Special Machine Instructions Advantages

Applicable to any number of processes on either a single processor or multiple processors sharing main memory

It is simple and therefore easy to verify It can be used to support multiple critical sections

Disadvantages Busy-waiting consumes processor time Starvation is possible when a process leaves a critical

section and more than one process is waiting. Deadlock

• If a low priority process has the critical region and a higher priority process needs, the higher priority process will obtain the processor to wait for the critical region

Page 17: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Semaphores Semaphore is a special integer variable that is

used for synchronization Semaphore supports two atomic operations :

wait and signal The atomicity means that the two operations on the

same semaphore can not overlap

How semaphore works ? The semaphore initialized to 1 or a positive value Before entering the critical section, a process/thread

calls wait (semaphore) After leaving the critical section, a process /thread

calls signal (semaphore)

Page 18: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Semaphores Assume our semaphore is called s

wait (s): signal (s):s = s – 1 s = s + 1if (s < 0) if (s <= 0)

block the thread wake up & run one of

that called wait(s) the waiting threadsotherwise

continue into CS

Note : Negative semaphore = number of blocked threads,

where only one right now in the critical section

Page 19: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Using Semaphores for MXP1 () {

while (true) {wait(s);/* CS */

signal(s);/* non-

CS */}

}

P2 () {while (true) {

wait(s);/* CS */

signal(s);/* non-

CS */}

}

Page 20: Department of Computer Science and Software Engineering  University of Wisconsin-Platteville

Readers /Writers Problemint readcount=0;semaphore wrt(1),mx(1);writer() {

wait(wrt);/* perform write */signal(wrt);

}reader() {

wait(mx);readcount++;if(readcount==1) wait(wrt);signal(mx);/* perform read */wait(mx);readconut--;if(readcount==0) signal(wrt);signal(mx);

}

Readers and writers can access concurrently same item

writers cannot concurrently writing same item, readers can

If a writer is writing the item, no reader may read it

Any writer must wait for all readers First reader races with writers Last reader signals writers Readers can starve writers

readcount - number of readers wishing to read the item

mx - protects manipulation with readcount

wrt- writer can get to item if open