Synchronization COMPSCI 386

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

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

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

Synchronization I. Jo, Heeseung

Chapter 2 Processes and Threads

Lesson 6: Process Synchronization

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

Chapter 6: Process Synchronization

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

Dealing with Issues for Interprocess Communication

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

Synchronization for Concurrent Tasks

Synchronization Spinlocks - Semaphores

Interprocess Communication By: Kaushik Vaghani

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

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

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

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Module 6: Process Synchronization

Process Synchronization

Problem Set 2. CS347: Operating Systems

Chapter 6: Process Synchronization

IV. Process Synchronisation

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

Process Synchronization

CS370 Operating Systems

Synchronization Principles

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 6 Process Synchronization

Lecture 5: Synchronization w/locks

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Concurrency. Chapter 5

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Chapter 6: Process Synchronization

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Dept. of CSE, York Univ. 1

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Subject: Operating System (BTCOC403) Class: S.Y.B.Tech. (Computer Engineering)

Process/Thread Synchronization

CS370 Operating Systems

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

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

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

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Midterm Exam. October 20th, Thursday NSC

Process Synchronization (Part I)

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

CS 153 Design of Operating Systems Winter 2016

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization

PROCESS SYNCHRONIZATION

Process/Thread Synchronization

Chapter 7: Process Synchronization!

Operating Systems. Synchronization

Chapter 6 Synchronization

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

OS Process Synchronization!

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Chapter 5: Process Synchronization

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

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

Process Synchronization

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Process Co-ordination OPERATING SYSTEMS

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Models of concurrency & synchronization algorithms

Process Synchronization

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Prof. Dr. Hasan Hüseyin

PROCESSES & THREADS. Charles Abzug, Ph.D. Department of Computer Science James Madison University Harrisonburg, VA Charles Abzug

C09: Process Synchronization

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

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

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

CSE 4/521 Introduction to Operating Systems

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Concept of a process

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Lecture 8: September 30

Chapter 6: Process Synchronization

Interprocess Communication and Synchronization

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

Process Synchronization(2)

Synchronization 1. Synchronization

Multitasking / Multithreading system Supports multiple tasks

Transcription:

Synchronization COMPSCI 386

Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose top is 5. What will its value be after one push and one pop operation?

Race Conditions register = top register = register + 1 top = register top++ register = top register = register 1 top = register top-- Machine instructions may be interleaved in such a way that top is 4, 5, or 6 after one push and one pop.

Race Conditions A race condition arises when multiple threads access the same data and the result depends on the order in which the threads are scheduled. The kernel manages various data structures like the file desriptor table and the ready queue, so it must be carefully designed to avoid race conditions. Easy solution: non-preemptive kernel. But most kernels are preemptive (e.g., Linux 2.6+) for responsiveness (and for real-time systems).

Critical Section Problem A critical section is a segment of code in which a process may be accessing or modifying resources shared with other processes. The critical section problem is to design a protocol for access to critial section code in order to prevent race conditions. Required properties of a solution: mutual exclusion progress bounded waiting

Peterson s Solution (1981) Software solution that does not really work because of the way modern systems perform basic machine language instructions like load and store (caching). But it provides a good conceptual starting point for the study of the critical section problem. Works for two processes sharing a single resource. Can be generalized, but becomes more complicated.

Peterson s Solution (1981) do { interested[i] = true; turn = j; while (interested[j] && turn == j) ; entry section for P i CRITICAL SECTION interested[i] = false; exit section for P i } while (true);

Classic Synchronization Problems Illustrate many common concurrency problems. Used for testing new synchronization schemes. Bounded Buffer Readers-Writers Dining Philosphers

Synchronization Objects A mutex lock guarantees mutual exclusion. Spin locks are suitable when resource will be held for short period. A semaphore is a counter that can be atomically incremented and decremented across a prescribed range of values. Initialized to number of instances of a sharable resource. A mutex is simply a binary semaphore.

Synchronization Objects A monitor is a software abstraction that hides some of the notoriously tricky details involved in the use of mutex locks and semaphores. Like a locked room for execution of critical section code. Consider a Java a class with methods for entering/exiting the critical section. Hardware support for synchonization atomic test-and-set instruction atomic compare-and-swap instructions

Synchronization in Linux Using machine instructions for atomic arithmetic operations, provides an opaque type atomic_t. atomic_t counter; atomic_set(&counter, 5); atomic_inc(&counter); atomic_add(10, &counter); atomic_sub(4, &counter); int x = atomic_read(&counter); Also provides mutex locks, spinlocks, semaphores.

Java Prior to 5.0 Every object has a built-in lock that can be acquired by calling a synchronized method. Every class has a lock that can be aquired by calling a static synchronized method. Blocks can be synchronized. If one thread owns a lock and another tries to obtain it, the latter sleeps; when owner releases the lock, the JVM awakens one of the sleeping threads.

Java Prior to 5.0 Every object has a wait set of threads. When wait is called the lock is released, the thread blocks, and is placed in the wait set for the object. When notify is called, the JVM selects a thread from the wait set, moves it to the entry set, and sets its state to runnable.

Java 5.0 and Beyond The concurrency API includes: Lock interface. Semaphore class. Condition class. Much more. Packages: java.util.concurrent.atomic java.util.concurrent.locks