HARDWARE APPROACHES TO MUTUAL EXCLUSION 1

Similar documents
Monitors, Java, Threads and Processes

Outline of this lecture G52CON: Concepts of Concurrency

Semaphores and Monitors: High-level Synchronization Constructs

Last Class: Semaphores

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization

Mutual Exclusion using Monitors

Lecture 6: Semaphores and Monitors

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Concurrent Programming

Synchronization. Todd C. Mowry CS 740 November 24, Topics. Locks Barriers

Chapter 6 Concurrent Programming

Thread Synchronization and the Java Monitor

Monitors and Semaphores

Priority Based Implementation in Pintos

Lecture 6: Introduction to Monitors and Semaphores

Shared Address Space Computing: Programming

A Survey of Parallel Processing in Linux

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

CS Fall 2008 Homework 2 Solution Due September 23, 11:59PM

Hoare-Style Monitors for Java

COS 318: Operating Systems. Semaphores, Monitors and Condition Variables

6 Synchronization with Semaphores

Distributed Systems [Fall 2012]

Parallel Programming

CS11 Java. Fall Lecture 7

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Monitors & Condition Synchronization

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Priority Inversion Problem and Deadlock Situations

Real Time Programming: Concepts

CS414 SP 2007 Assignment 1

Chapter 1 FUNDAMENTALS OF OPERATING SYSTEM

Linux Process Scheduling Policy

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

Java Virtual Machine Locks

Java Concurrency Framework. Sidartha Gracias

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

First-class User Level Threads

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Replication on Virtual Machines

Tasks Schedule Analysis in RTAI/Linux-GPL

Chapter 6, The Operating System Machine Level

Why Threads Are A Bad Idea (for most purposes)

3C03 Concurrency: Condition Synchronisation

Linux Driver Devices. Why, When, Which, How?

Design Patterns in C++

SYSTEM ecos Embedded Configurable Operating System

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

An Implementation Of Multiprocessor Linux

Concurrent programming in Java

W4118 Operating Systems. Instructor: Junfeng Yang

Facing the Challenges for Real-Time Software Development on Multi-Cores

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

Multi-Threading Performance on Commodity Multi-Core Processors

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Platform-wide Deadlock Immunity for Mobile Phones

Programming with Data Structures

CS0206 OPERATING SYSTEMS Prerequisite CS0201, CS0203

Chapter 11 I/O Management and Disk Scheduling

Control 2004, University of Bath, UK, September 2004

Centralized Systems. A Centralized Computer System. Chapter 18: Database System Architectures

Performance Evaluation and Optimization of A Custom Native Linux Threads Library

Java Memory Model: Content

Concurrency and Parallelism

CS 33. Multithreaded Programming V. CS33 Intro to Computer Systems XXXVII 1 Copyright 2015 Thomas W. Doeppner. All rights reserved.

Shared Memory Segments and POSIX Semaphores 1

Operating System: Scheduling

Chapter 1 Computer System Overview

EECE 276 Embedded Systems

Jorix kernel: real-time scheduling

Stacks. Linear data structures

Chapter 2: OS Overview

Comp 204: Computer Systems and Their Implementation. Lecture 12: Scheduling Algorithms cont d

1.4 SOFTWARE CONCEPTS

Introduction to Promela and SPIN. LACL, Université Paris 12

The Little Book of Semaphores

What is an RTOS? Introduction to Real-Time Operating Systems. So what is an RTOS?(contd)

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Effective Computing with SMP Linux

Chapter 2: Processes, Threads, and Agents

Introduction to SPIN. Acknowledgments. Parts of the slides are based on an earlier lecture by Radu Iosif, Verimag. Ralf Huuck. Features PROMELA/SPIN

W4118 Operating Systems. Instructor: Junfeng Yang

Kernel Synchronization and Interrupt Handling

Concepts of Concurrent Computation

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

COS 318: Operating Systems. Virtual Machine Monitors

Embedded Systems. 6. Real-Time Operating Systems

Have both hardware and software. Want to hide the details from the programmer (user).

Road Map. Scheduling. Types of Scheduling. Scheduling. CPU Scheduling. Job Scheduling. Dickinson College Computer Science 354 Spring 2010.

Transcription:

HARDWARE APPROACHES TO MUTUAL EXCLUSION Software solution: Peterson s algorithm How can the hardware help us to implement mutual exclusion? Interrupt disabling: Slide 1 P 0: flag[0] = true; turn = 1; while (flag[1] && turn == 1) flag[0] = false; P 1: flag[1] = true; turn = 0; while (flag[0] && turn == 0) flag[1] = false; Slide 3 Useful on uniprocessor systems only Prevents preemption <disable interrupts/signals> <enable interrupts/signals> Example: OS/161 spl = splhigh(); splx(spl); useful within OS, not appropriate for user processes Peterson s algorithm: SPECIAL MACHINE INSTRUCTIONS Slide 2 implements mutual exclusion not widely used: burns CPU cycles can be extended to work for n processes, but overhead increases Slide 4 Software approaches exploit a property guaranteed by the hardware: each memory access is atomic Problems occured as we sometimes would like a number of memory accesses to be atomic cannot be extended to work for an unknown number of processes Could the hardware provide complex atomic operations that help us? HARDWARE APPROACHES TO MUTUAL EXCLUSION 1 SPECIAL MACHINE INSTRUCTIONS 2

Advantages of special machine instructions: Test and set: Exchange: Applicable to any number of processes on either a single processor or multiple processors sharing main memory Slide 5 atomic bool testset (int i) { if (0 == i) { i = 1; return true; else return false; atomic void exchange (int register, int memory) { int tmp; tmp = memory; memory = register; register = tmp; Slide 7 Simple and therefore easy to verify Disadvantages of special machine instructions: 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 requires access, the higher priority process will obtain the processor to wait for the critical region Mutual exclusion with test-and-set: SEMAPHORES Slide 6 int bolt = 0; void proc (int i) { while (!testset (bolt)) bolt = 0; <remainder> void main () { bolt = 0; parbegin (proc (1), proc (2),, proc (N)); Slide 8 Dijkstra (1965) introduced the concept of a semaphore in his study of cooperating sequential processes Semaphores are variables that are used to signal the status of shared resources to processes How does that work? If a resource is not available, the corresponding semaphore blocks any process waiting for the resource Blocked processes are put into a process queue maintained by the semaphore (avoids busy waiting!) When a process releases a resource, it signals this by means of the semaphore Signalling resumes a blocked process if there is any Wait and signal operations cannot be interrupted Complex coordination can be specified by multiple semaphores SPECIAL MACHINE INSTRUCTIONS 3 SEMAPHORES 4

Slide 9 How are semaphores implemented? A semaphore is a variable s consisting of an integer value count and a process queue queue Initially, count is set to a nonnegative value and queue is empty There are two operations that a process current can apply: Slide 11 There are various flavours of semaphores: Counting semaphores versus binary semaphores: In a counting semaphore, count can take arbitrary integer values In a binary semaphore, count can only be 0 or 1 Counting semaphores can be implemented in terms of binary semaphores (how?) wait(s): Decrement count; if count becomes negative, put current into queue signal(s): Increment count; if count is not positive, unblock a process from queue Strong semaphores versus weak semaphores: In a strong semaphore, queue adheres to the FIFO policy In a weak semaphore, any process may be taken from queue Strong semaphores can be implemented in terms of weak semaphores (how?) Slide 10 typedef struct { int count; queue_t queue; semaphore; void wait (semaphore s) { s.count--; if (s.count < 0) { <place current in s.queue> <block current> void signal (semaphore s) { s.count++; if (s.count <= 0) <remove a process P from s.queue> <place P on ready list> Slide 12 MUTUAL EXCLUSION Implementation of mutual exclusion with semaphores: semaphore s; s.count = 1; s.queue = empty_queue (); void proc (int i) { wait (s); signal (s); <remainder> void main () { parbegin (proc (1), proc (2),, proc (n)); SEMAPHORES 5 MUTUAL EXCLUSION 6

void P(struct semaphore *sem) { int spl; assert(sem!= NULL); Slide 13 Mutex: A semaphore that allows only one process in a critical section is often called a mutex There exist various flavours, such as, read-write mutexes and read-write-update mutexes Given exchange or test-and-set are available, easy to implement in user-level: ➀ test-and-set lock ➁ if succesful, return ➂ if not, yield current thread, repeat Slide 15 /* May not block in an interrupt handler. * For robustness, always check, even if we can actually * complete the P without blocking. */ assert(in_interrupt==0); spl = splhigh(); while (sem->count==0) { thread_sleep(sem); assert(sem->count>0); sem->count--; splx(spl); SEMAPHORES IN OS/161 defined in src/kern/thread/synch.c and src/kern/include/synch.h Slide 14 operations are called: - P (proberen: try), instead of wait - V (verhogen: increase), instead of signal definition of data type semaphore struct semaphore { char * name; volatile int count; ; struct semaphore* sem_create (const char *name, int initial_count); void P (struct semaphore *); void V (struct semaphore *); void sem_destroy(struct semaphore *); Slide 16 void V(struct semaphore *sem) { int spl; assert(sem!= NULL); spl = splhigh(); sem->count++; assert(sem->count>0); thread_wakeup(sem); splx(spl); where is the queue?? SEMAPHORES IN OS/161 7 MUTEXES IN OS/161 8

int in, out; elem_t b[]; Slide 17 struct lock { char * name; struct thread ; MUTEXES IN OS/161 *volatile holder; Slide 19 producer: <produce item v> b[in] = v; in++; struct lock *lock_create (const char *name); void lock_acquire (struct lock *); void lock_release (struct lock *); int lock_do_i_hold (struct lock *); void lock_destroy (struct lock *); consumer: while (in <= out) w = b[out]; out++; <consume item w> PRODUCER/CONSUMER PROBLEM semaphore n = init_sem (0); /* number of items in buffer */ semaphore s = init_sem (1); /* access to critical section */ Slide 18 One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time Slide 20 void producer () { v = produce (); wait (s); append (v); signal (s); signal (n); b[1] b[2] out b[3] b[4] b[5] in void consumer () { wait (n); wait (s); w = take (); signal (s); consume (w); PRODUCER/CONSUMER PROBLEM 9 PRODUCER WITH CIRCULAR BUFFER 10

PRODUCER WITH CIRCULAR BUFFER MONITORS A monitor is a software module implementing mutual exclusion b[1] b[2] b[3] b[4] b[5] b[n] Monitors are easier to program than semaphores Natively supported by a number of programming languages: Concurrent Pascal, Modula-[23] & Java Slide 21 out in Slide 23 Chief characteristics: Local data variables are accessible only by the monitor (not externally) b[1] b[2] b[3] b[4] b[5] b[n] Process enters monitor by invoking one of its procedures Only one process may be executing in the monitor at a time Main problem: provides less control; coarse grain in out int in, out; elem_t b[]; Slide 22 Producer: <produce item v> while ((in + 1) % n == out) b[in] = v; in = (in + 1) % n; Consumer: while (in == out) w = b[out]; out = (out + 1) % n; <consume item w> Slide 24 Synchronisation in a monitor: cwait (c): Suspend current on condition c (opens monitor to other processes) csignal (c): Resume execution of a processes suspended on condition c (ignored if no such process) MONITORS 11 MONITORS 12

Structure of a monitor: Slide 25 monitor waiting area condition c1 cwait(c1) queue of entering processes Entrance MONITOR local data condition variables Procedure 1 Slide 27 MONITORS IN JAVA Resources or critical sections can be protected using the synchronized keyword: synchronized (<expression>) { <statements> condition cn cwait(cn) urgent queue csignal Procedure k initialization code <expression> must evaluate to an object or array thread only proceeds after obtaining the lock of the object synchronized can be applied to a method: entire method is a critical section Exit Producer/consumer using a monitor: char buffer[n]; int nextin = 0, nextout = 0, count = 0; condition_t not_full, not_empty; Slide 26 void append (char c) { if (N == count) cwait (not_full); buffer[nextin] = c; nextin = (nextin + 1) % N; count++; csignal (not_empty); void take (char c) { if (0 == count) cwait (not_empty); x = buffer[nextout]; nextout = (nextout + 1) % N; count--; csignal (not_full); MONITORS IN JAVA 13 MONITORS IN JAVA 14