Chapter 6 Process Synchronization

Similar documents
Lesson 6: Process Synchronization

Chapter 6: Process Synchronization

Synchronization Principles

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Process Synchronization

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Module 6: Process Synchronization

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization

CS420: Operating Systems. Process Synchronization

CS370 Operating Systems

Chapter 7: Process Synchronization!

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

CS370 Operating Systems

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization

Chapter 7: Process Synchronization. Background

CSE 4/521 Introduction to Operating Systems

Real-Time Operating Systems M. 5. Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Process Synchronization

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 6 Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CS370 Operating Systems

Chapter 6: Process Synchronization

CS370 Operating Systems

CSE Opera,ng System Principles

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Chapter 6: Synchronization

Process Synchronization

Chapter 6: Process Synchronization

Process Co-ordination OPERATING SYSTEMS

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Introduction to Operating Systems

Synchronization Spinlocks - Semaphores

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Chapter 7 Process Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Lecture 3: Synchronization & Deadlocks

Process Synchronization(2)

IV. Process Synchronisation

Concurrency: Mutual Exclusion and

Process Synchronization

Process Synchronization

Process Synchronization

Process Synchronization

Interprocess Communication By: Kaushik Vaghani

Process Synchronization(2)

Lecture 5: Inter-process Communication and Synchronization

High-level Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

UNIT II PROCESS MANAGEMENT 9

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

UNIT-II PROCESS SYNCHRONIZATION

PESIT Bangalore South Campus

Chapter 6 Synchronization

Process/Thread Synchronization

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG

Comp 310 Computer Systems and Organization

Process Synchronization(2)

Process/Thread Synchronization

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

1. Motivation (Race Condition)

Process Coordination

Dealing with Issues for Interprocess Communication

Dept. of CSE, York Univ. 1

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Lecture 6. Process Synchronization

Processes-Process Concept:

CS3733: Operating Systems

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Prof. Hui Jiang Dept of Computer Science and Engineering York University

Process Synchronization

Process Management And Synchronization

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Module 6: Process Synchronization

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Synchronization I. Jo, Heeseung

Module 5: Process Synchronization. Reading: Chapter 6

Transcription:

Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages concurrent access to shared data may result in data inconsistency Race Condition Multiple processes access and manipulate a variable counter concurrently Outcome depends upon the particular order in which the access takes place Process Synchronization & Coordination Critical Section Implementation Entry Section Critical Section Exit Section Remainder Section while(true); Operating System Critical Sections (Race Conditions) Updating kernel data structures Open Files List Memory Allocation Structures Active Process Lists Inactive Process Lists Interrupt Handling Preemptive Kernels allows a process to be preempted while running in kernel mode may be subjected to race conditions difficult to design & implement in SMP highly desired in Real-Time Systems Non-preemptive Kernels es not allow a process to be preempted while running in kernel mode process will run until it o exits kernel mode o blocks o voluntarily yields control of the CPU not subject to race conditions

Critical Section Requirements Mutual Exclusion o only one process may be in its critical section Progress o no process is in its critical section o one or more processes wants to enter its critical section o only those processes not in their remainder sections can decide which process can enter its critical section o the decision cannot be postponed indefinitely Bounded Waiting o there is a bound on the number of times other processes may enter their critical sections after a process has made a request to enter its critical section the decision es not wait for all processes to complete executing their remainder section code Peterson s Solution == software solution fails because of pipelines & parallel instruction streams Synchronization Hardware Atomic Hardware Instructions 1. TestAndSet(&lock); Hardware Definition boolean TestAndSet( boolean *target ); boolean rv = *target; *target = TRUE; return rv; Software Implementation lock = false; while(testandset(&lock)); fails the bounded waiting requirement rv FALSE P 0 while(testandset(&lock)); while(true); while(true); Effect: executes TestAndSet(&lock); P0 gains access to its critical section; lock = TRUE; P1 executes TestAndSet(&lock); P1 hangs in the while(testandset(&lock)) loop, i.e., spin cycle since (lock == TRUE) until P0 exits its critical section lock FALSE TRUE P 1 while(testandset(&lock)); while(true);

2. Swap(&lock, &key) Hardware Definition void Swap(boolean *a, boolean *b) boolean temp = *a; *a = *b; *b = temp; fails the bounded waiting requirement Software Implementation while(key == TRUE) Swap(&lock, &key); while(true); P 1 boolean key; while(key == TRUE) Swap(&lock, &key); while(true); P 0 boolean key; while(key == TRUE) Swap(&lock, &key); while(true); P 0 key TRUE FALSE P 1 key FALSE lock FALSE TRUE P 0 executes P 0 executes while(key == TRUE) and then P 0 executes Swap(&lock, &key); P 0 executes in its critical section P 1 executes P 1 executes while(key == TRUE) and spinlocks because its key is FALSE P 0 executes P 1 executes Swap(&lock, &key);

3. TestAndSet(&lock) Software Implementation which satisfies Mutual Exclusion, Progress, & Bounded Waiting P i for, i.e., there are n+1 processes waiting[i] = FALSE; lock = true key = TestAndSet(&lock); key == true && lock == true P i boolean key = FALSE; waiting[i] = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; j = (i+1)%n; while((j!= i) &&!waiting[j]) j = (j+1)%n; if(j == i) else waiting[j] = FALSE; while(true); lock FALSE TRUE waiting 0 1 2 FALSE FALSE FALSE TRUE FALSE lock = false key = TestAndSet(&lock); key == false && lock == true TRUE TRUE P 0 boolean key = FALSE; waiting[0] = TRUE; while(waiting[0] && key) key = TestAndSet(&lock); waiting[0] = FALSE; j = 1; while((j!= i) &&!waiting[j]) j = (j+1)%2; if(j == 0) else waiting[j] = FALSE; while(true); P 1 boolean key = FALSE; waiting[1] = TRUE; while(waiting[1] && key) key = TestAndSet(&lock); waiting[1] = FALSE; j = 2; while((j!= 1) &&!waiting[j]) j = (j+1)%2; if(j == 1) else waiting[j] = FALSE; while(true); P i boolean key = FALSE; waiting[i] = TRUE; while(waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; j = 0; while((j!= i) &&!waiting[j]) j = (j+1)%2; if(j == 2) else waiting[j] = FALSE; while(true); P 0 starts waiting[0] = TRUE, P 0 key = TRUE P 0 enters the while loop and executes TestAndSet(&lock) lock = TRUE && key = FALSE && waiting[0] = FALSE P 0 drops thru the loop and enters its critical section P 1 starts waiting[1] = TRUE, P 1 key = TRUE P 1 drops thru while loop and executes TestAndSet(&lock) lock = TRUE && key = TRUE P 1 hangs in the while loop P i enters Critical Section only if waiting[i] == false; or key == false;

Semaphores API construct implementation via synchronization hardware S: semaphore integer variable directly initialized, i.e., assignment statement accessed only via atomic operations o wait( ) proberen (P) to test o signal( ) verhofen (V) to increment Definition -- wait( ) wait(s) while (S <= 0 ); S- -; atomic operations Definition -- signal(s) signal(s) S++; Counting Semaphore unrestricted main Binary Semaphore main restricted to [0, 1] Mutex Locks Binary Semaphores provide Mutual Exclusion critical section multiple processes P i : processes 0 < i < n+1 mutex mx = n; gain access to a resource, process executes wait(mx); releasing resource, process executes signal(mx); mx == 0 no resources available control access to a given resource consisting of n instances Synchronization Process P 1 with statement S1 Process P 2 with statement S2 synchronize statements: S1 must execute before S2 mutex synch = 0; S1; signal(synch); wait(synch); S2; Busy Waiting CPU cycles that could have been spend ing productive work are wasted processing waits Spinlock multiprocessor systems no context switch One thread can spin on one processor while another thread performs a critical section on another processor

Blocking process finds mutex mx <= 0 process executes block( ) operation, i.e., o block( ) places process on queue associated with the mutex mx o process state is changed to the waiting state scheduler selects another process to execute executing process executes signal( ) operation process at head of wait queue is restarted with a wakeup( ) operation reawakened process is placed in the ready queue Blocking Semaphore typedef struct int value; struct process *list; semaphore; block( ) & wakeup( ) are basic system calls, they must also be atomic, i.e., must not be interruptible Deadlocks semaphore S; wait(semaphore *S) S value- -; if ( S value < 0 ) // add the calling process to S list; block( ); signal(semaphore *S) S value++; if(s value <= 0) // remove a process, P, from S list; wakeup(p); The proper use of semaphores is under the control of each individual programmer one error and the system fails single processor: disable interrupts multiprocessor: spinlock linked list of process control blocks Starvation linked list is a LIFO queue, i.e., stack P 0 P 1 wait(s); wait(q); wait(q); wait(s); signal(s); signal(q); signal(q); signal(s); resource acquisition and release

Priority Inversion higher priority process blocked by lower priority process modifying kernel data Priority Inheritance Protocol processes accessing resources which are controlled by a higher priority, e.g., kernel resources, inherit the higher priority until they have finished Monitor ADT (class-object) construct that encapsulates the proper sequences of wait( ) and signal( ) to ensure correct usage of semaphores monitor may contain multiple (user) procedures monitor ensures that only one procedure will be active at any one time condition variables condition x; condition operations wait( ) & signal( ) o process P 0 invokes x.wait( ); P 0 suspended until another process P 1 issues x.signal( ) x.signal( ) resumes exactly one suspended process if there are no suspended processes, x.signal( ) has no effect signal( ) used in semaphores always has an effect process P 1 issues x.signal( ) P 0 resumes o P 1 waits until P 0 leaves the monitor o P 1 waits for another condition o P 0 waits until P 1 leaves the monitor o P 0 waits for another condition signal & wait signal & continue

Dining-Philosophers Monitor monitor dp enum THINKING, HUNGRY, EATING state[5]; possible states condition self[5]; provides a wait condition void pickup(int i) state[ i ] = HUNGRY; test( i ); if (state[ i ]!= EATING) self[ i ].wait( ); philosopher can only pickup the chopsticks if both of them are available; else changes condition to waiting void putwn(int i) state[ i ] = THINKING; test((i + 4) % 5); test((i + 1) % 5); when philosopher is finished eating, both chopsticks must be released; then a check is made as to philosophers on both the left and right sides of the relinquisher who may have requested access to the chopsticks void test(int i) if ((state[(i + 4)%5]!= EATING) && (state[ i ] == HUNGRY) && ((state[(i + 1)%5]!= EATING) state[ i ] = EATING; self[ i ].signal( ); Determines if the philosophers on either side of the requester are using the chopsticks; if not, the test returns the state of EATING to the requester, in effect, it allocates the chopsticks to the requester initialization_code( ) for (int i = 0; i < 5; i++)state[ i ] = THINKING; Usage dp.pickup( i ); // Philosopher i eats until full dp.putwn( i ); Implementing a Monitor using Semaphores see Silberschatz pages 250-252

Java Monitors java.util.concurrency synchronized methods lock acquisition lock s entry set set of threads waiting for the lock to become available wait( ) & notify( ) i.e., signal( ) semaphores, condition variables, mutex locks public class SimpleClass public synchronized void safemethod( ) SimpleClass sc = new SimpleClass( ); In order to execute sc.safemethod( ) the thread on which object sc is running must own the lock; lock is available calling thread becomes the owner of the lock if the lock is owned by another thread, the sc.safemethod( ) thread blocks and is put on the entry set for the lock thread exits the method lock is made available & a thread from the entry set is selected as the new owner of the lock

Solaris semaphores (long code segments) condition variables (long code segments) adaptive mutex (short code segments) o standard semaphore spinlock o data locked o lock held by thread running on another CPU, spin until released o thread holding lock is not running, blocks until awakened by lock release reader-writer locks (long code segments) turnstile o list of threads waiting to acquire adaptive mutex reader-writer lock o is a queue structure holding threads blocked on a lock turnstile acquisition o each synchronized object with a thread blocked on a lock requires its own turnstile o each kernel thread has a turnstile o o o the kernel turnstile on which the first thread blocks becomes the turnstile for the synchronized object subsequent threads blocking on this same lock will be added to the same kernel turnstile thread releases the lock releasing object is ASSIGNED A NEW THREAD from a free turnstile list maintained by the kernel kernel selects a new owner of the lock from the thread s turnstile priority-inheritance protocol lower-priority thread currently holds a lock higher-priority thread is currently blocked on the same lock lower-priority thread acquires the priority of the higher-priority thread until it releases the lock

Winws XP multithreaded kernel supports Real-Time processes multiple processors Kernel Threads UNIPROCESSOR SYSTEMS kernel thread accesses a global resource disables all interrupt handlers with access to that resource MULTIPROCESSOR SYSTEMS kernel thread naccesses a global resource uses spinlocks to protect access to that resource ensures that threads holding spinlocks are not preempted User Threads Dispatcher Objects Signaled State o object is available; o thread will not block when acquiring the object Nonsignaled State o object is not available; o thread will block when attempting to acquire the object synchronizes threads by using o mutexes o semaphores o events o timers Events notify waiting threads that a desired condition has occurred Times notify thread(s) that a specified time interval has expired Actions thread blocks on a nonsignaled dispatcher object o thread state changes to waiting o thread is placed on a wait queue for that object dispatcher object changes state to signaled o if there are threads in the object s wait queue, the kernel moves thread(s) to the ready state; if the queue is for an Event Object kernel moves all threads waiting for that event a Mutex kernel moves one thread to the ready state thread attempts to acquire a mutex dispatcher object in a nonsignaled state; thread is suspended and placed in a wait queue for the mutex object; another thread releases the mutex lock; thread at the front of the mutex queue has its state changed to ready and it acquires the mutex lock

Linux (starting with Version 2.6) pre-emptive kernel Kernel Threads Spinlocks Semaphores Reader-Writer Spinlocks Reader-Writer Semaphores SYMMETRIC MULTIPROCESSOR SYSTEMS acquiring/releasing short duration spinlocks UNIPROCESSOR SYSTEMS enabling/disabling kernel preemption preempt_disable( ) preempt_enable( ) kernel-mode task is holding a lock kernel is not preemptible task thread-info structure preempt_count number of locks held by the task lock acquired preempt_count ++; lock released preempt_count - -; preempt_count > 0 not safe to preempt kernel no outstanding call to preempt && preempt_count == 0 safe to preempt Pthreads API Mutex Locks o pthread_mutex_t first_mutex; o pthread_mutex_t second_mutex; o pthread_mutex_init (&first_mutex, NULL); o pthread_mutex_init (&second_mutex, NULL); o pthread_mutex_acquire( ); o pthread_mutex_lock( ); o pthread_mutex_unlock( ); Condition Variables Read-Write Locks Spinlocks not all versions may be portable POSIX SEM Semaphores