SWEN-220 Mathematical Models of Software. Process Synchronization Critical Section & Semaphores

Similar documents
2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

G52CON: Concepts of Concurrency

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

UNIX Input/Output Buffering

SPIN part 2. Verification with LTL. Jaime Ramos. Departamento de Matemática, Técnico, ULisboa

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

Tool demonstration: Spin

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

Concurrency: a crash course

CSCI 447 Operating Systems Filip Jagodzinski

The University of Texas at Arlington

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

IV. Process Synchronisation

SWEN-220 Mathematical Models of Software. Concurrency in SPIN Interleaving

Models of concurrency & synchronization algorithms

System Software Assignment 8 Deadlocks

Synchronization for Concurrent Tasks

Chapter 5 Asynchronous Concurrent Execution

SPIN, PETERSON AND BAKERY LOCKS

Concept of a process

Week 3. Locks & Semaphores

Process Synchronization Mechanisms

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Synchronization: Semaphores

Synchronization Mechanisms

CSE Traditional Operating Systems deal with typical system software designed to be:

Concurrent Processes Rab Nawaz Jadoon

The University of Texas at Arlington

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

Lecture 3 SPIN and Promela

Process Management And Synchronization

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Software Engineering using Formal Methods

CMSC421: Principles of Operating Systems

Distributed Systems Programming F29NM1 2. Promela I. Andrew Ireland. School of Mathematical and Computer Sciences Heriot-Watt University Edinburgh

Formal Specification and Verification

Programming Languages

Formal Verification by Model Checking

CoSc 450: Programming Paradigms. The Critical Section Problem

Software Engineering using Formal Methods

Silberschatz and Galvin Chapter 6

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

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Computer Lab 1: Model Checking and Logic Synthesis using Spin (lab)

Introduction to Operating Systems

Process Synchronization

IT 540 Operating Systems ECE519 Advanced Operating Systems

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution).

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Copyright 2008 CS655 System Modeling and Analysis. Korea Advanced Institute of Science and Technology

Topic 4: Synchronization with Semaphores

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

What is SPIN(Simple Promela Interpreter) Elements of Promela. Material About SPIN. Basic Variables and Types. Typical Structure of Promela Model

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Lecture 8: September 30

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

CSE 153 Design of Operating Systems

CS420: Operating Systems. Process Synchronization

Example Threads. compile: gcc mythread.cc -o mythread -lpthread What is the output of this program? #include <pthread.h> #include <stdio.

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Programming in Parallel COMP755

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Semaphores. Mutual Exclusion. Inference Rules. Fairness: V may release a waiting process. Weakly fair, Strongly fair, or FIFO

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Formal Specification and Verification

Concurrent Programming TDA381/DIT390

CS 153 Design of Operating Systems Winter 2016

What is SPIN(Simple Promela Interpreter) Material About SPIN. Elements of Promela. Basic Variables and Types. Typical Structure of Promela Model

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CS477 Formal Software Development Methods / 39

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

Introduction to OS Synchronization MOS 2.3

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

Reminder from last time

1 Process Coordination

Threading and Synchronization. Fahd Albinali

Patrick Trentin Formal Methods Lab Class, Feb 26, 2016

Locks. Dongkun Shin, SKKU

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization

CS3502 OPERATING SYSTEMS

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

The Spin Model Checker : Part I/II

CONCURRENCY:MUTUAL EXCLUSION AND SYNCHRONIZATION

Dealing with Issues for Interprocess Communication

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization!

Last Class: Synchronization

Last Class: Deadlocks. Today

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Transcription:

SWEN-220 Mathematical Models of Software Process Synchronization Critical Section & Semaphores 1

Topics The critical section Synchronization using busy-wait Semaphores 2

The Critical Section Processes can have a critical and non-critical section Non-critical section works with local resources Critical section works with global resources In general, processes may halt in their non-critical section, but not in their critical section 4

The Critical Section Problem Need to ensure Safety & Liveness Mutual exclusion At most one process is executing its critical section at any time No Deadlock It is impossible to reach a state in which some processes are trying to enter their critical sections, but no process is successful Fairness If any process is trying to execute its critical section, then eventually that process is successful 5

The Critical Section Example bool wantp = false, wantq=false; active proctype P(){ :: printf("noncritical sec P\n"); wantp = true; printf("critical section P\n"); wantp = false } active proctype Q(){ :: printf("noncritical sec Q\n"); wantq = true; printf("critical section Q\n"); wantq = false } 6

Solving the Critical Section Problem One of the main issues is that wantp and wantq is never read by the other process What can we? One way to try and solve the problem is have process P read wantq and process Q read wantp 7

Synchronization using Busy-wait Make the process wait until the other process is finished executing its critical section :: printf("noncritical sec P\n"); ::!wantq -> break; :: else -> skip; wantp = true; printf("critical section P\n"); wantp = false 8

Problem with using Busy-wait The loop performs no useful computation. It is repeatedly evaluating the expression until the other guard becomes true This wastes cycles and is unacceptable, except for multiprocessors, which have a large number of cycles 9

Alternative to using Busy-wait, Blocking Most computer systems are based on blocking Synchronization by blocking is used in operating systems that implement multitasking. Synchronization is achieved by having a process execute an operation that causes it to block, thus enabling another process to run 10

From Busy-wait to Blocking :: printf("noncritical sec P\n"); ::!wantq -> break; :: else -> skip; wantp = true; printf("critical section P\n"); wantp = false :: printf("noncritical sec P\n"); ::!wantq -> break; wantp = true; printf("critical section P\n"); wantp = false But we can simply this even further! 11

Executability of Statements in Promela An expression statement is executable in Promela, if and only if it evaluates to true :: printf("noncritical sec P\n"); ::!wantq -> break; wantp = true; printf("critical section P\n"); wantp = false :: printf("noncritical sec P\n");!wantQ; wantp = true; printf("critical section P\n"); wantp = false Assignment statements and printf statements are always executable 12

Synchronization using Blocking A process is blocked until the other process is finished executing its critical section :: printf("noncritical sec P\n"); atomic{!wantq; wantp = true; } printf("critical section P\n"); wantp = false 13

Issue with using Blocking Blocking in these previous examples requires obtaining a lock (boolean flag) before entering a critical section of the process. The lock is often referred to as a mutual exclusion lock, or mutex. Example: Think of a public restroom that requires obtaining a key. What happens if we want to synchronize more than two processes, or allow more than one to be in the critical section? Example: Think of multiple vehicles crossing a bridge that has a limit of n vehicles being on the bridge at the same time. 14

Semaphore - Definition 1. A semaphore can be initialized to any integer value, but after that the only operations you are allowed to perform are increment (increase by one) and decrement (decrease by one). 2. You cannot read the current value of the semaphore. 3. When a process decrements - wn() or wait(), the semaphore, if the result is negative, the process blocks itself and cannot continue until another process increments the semaphore. 4. When a process increments the semaphore up() or signal(), if there are other processes waiting, one of the waiting processes gets unblocked. 15

Semaphore - Usage In SPIN, a semaphore is a variable of type byte, that has the two atomic operations decrement, wn, or wait(sem): operation is executable when sem>0; executing the operation decrements sem increment, up, or signal(sem): operation is always executable; executing the operation increments the value of sem Historical note: Semaphores were proposed by Edgar Dijkstra in the 1960 s, being Dutch he used: V() Verhogen to increment for up() or signal() P() Proberen to test for wn() or wait() 16

Semaphore Example :: printf("noncritical sec in P = %d\n", _pid); atomic{ sem > 0; sem--; } printf("critical section in P = %d\n", _pid); sem++; 17

Semaphore functions #define semaphore byte semaphore sem = 1; // Pseu-type semaphore // Declare the semaphore (sem), initalize to max // processes sharing protected resource /* Up and wn functions on semaphores. * wn blocks if the semaphore is 0 on entry. */ inline unlock(s) { s++ } inline lock(s) { atomic{ s > 0 ; s-- } } :: printf("noncritical sec in P = %d\n", _pid); // _pid is process identifier (0-n) lock(sem); printf("critical section in P = %d\n", _pid); unlock(sem); 18

Signaling Semaphores Semaphores can also be used as a signaling" mechanism between processes. Java threads have this behavior via wait() & notify(). A process will reach a point in its execution where it needs to block and await a signal from another process to proceed. The waiting process will perform a wn(sem) and then proceed when a notifying process performs an up(sem). Note that signaling semaphores are initialized to zero. 19

Signaling Semaphore Example The same semaphore functions used for mutex semaphores are also used for signaling semaphores with the exception being that signalling semaphores are initialized to zero. semp = 0; semq = 0; proctype P(){ // some stuff, then block waiting for a signal from Q() wait(semp); // Note sem starts at zero, so this blocks P() // continue here when signaled by Q() notify(semq); // Unblock Q() } proctype Q(){ // some stuff, then signal P() to proceed noify(semp); // This unblocks P() // continue here after signaling P() wait(semq) ; // Block here until notified by P() } 20