Critical sections, locking, monitors, etc.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Lecture 5 Concurrency and Process/Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Threads in C# Threads in C#.
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Concurrency: Deadlock and Starvation Chapter 6.
Fundamentals of Python: From First Programs Through Data Structures
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Task Parallel Library (TPL)
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
C# I 1 CSC 298 Threads. C# I 2 Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The execution.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Synchronizing threads, thread pools, etc.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Lecture 3 Concurrency and Thread Synchronization     Mutual Exclusion         Dekker's Algorithm         Lamport's Bakery Algorithm.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Synchronization (Threads Accessing Shared Data). Contents I.The Bank Transfer Problem II.Doing a Simulation on the Bank Transfer Problem III.New Requirement:
Tutorial 2: Homework 1 and Project 1
Multithreading / Concurrency
Multithreading.
Background on the need for Synchronization
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Threads, Concurrency, and Parallelism
143a discussion session week 3
Threading And Parallel Programming Constructs
Multithreading.
Threading using C# and .Net
Lecture 2 Part 2 Process Synchronization
Critical section problem
Parallelism and Concurrency
Dr. Mustafa Cem Kasapbaşı
Another Means Of Thread Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Threads and Multithreading
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Software Engineering and Architecture
CSE 542: Operating Systems
CSE 542: Operating Systems
Chapter 3: Process Management
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Critical sections, locking, monitors, etc. Threads Critical sections, locking, monitors, etc. Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Some lines in the program needs sequential executions To avoid incorrect results The designer / programmer must find the critical sections And that can be hard! The critical section must be protected against concurrency. More critical sections, means less concurrency Example: ThreadBank, without lock Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Thread safe objects An object is thread safe if it is ready to be used with more than one thread. How to obtain thread safety Immutable objects (like String) All critical sections are protected, so that at most one thread can work inside the critical section (on this object) Other methods Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Locking, the keyword lock Syntax lock(someObject) { …. Statements = Critical section … } Semantics Obtains the mutual exclusion lock for someObject, executes the critical section, and releases the lock. No other thread can work in any critical section, locked by someObject Example: ThreadBank Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Deadlock Deadlock is a two (or more) threads are waiting on each other. Example: Bank, transfer money from account A to account B. If two customers do transfer at the same time deadlock might occur One customer has a lock on account A and wants a lock on account B Another customer has a lock on Account B and wants a lock on Account B Example: ThreadBank Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Deadlock handling Running a C# program there is no automatic deadlock detection or handling. The program just hangs … The programmer must be careful when acquiring locks. Deadlock prevention Try to use lock free algorithms Number the resources and only acquire resources with a higher number Etc. Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Monitor class Monitor is a class with some static methods Do not make objects from the Monitor class Monitor vs. lock(…) lock(someObject) { …. Statements = Critical section … } Is equivalent to Bool lockTaken = false; Try { Monitor.Enter(someObject, ref lockTaken); … critical section … } Finally { if (lockTaken) Monitor.Exit(someObject); } Semantics Monitor.Enter obtains the lock on someObject. This prevents other threads from entering the block. Monitor.Exit releases the lock. Monitor.Exit should always be in a finally block to make sure the lock is released even if there was an exception in the block lockTaken is necessary in case there was an exception at Monitor.Enter(…) Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Monitor, Wait and Pulse Problem Entering a synchronized method a thread might realize that it is not in a state to fulfill its task – and it cannot simply return from the method because the return value is not “ready”. Solution Use a monitor and ask it to wait for another thread to do something. Monitor class, with static methods Wait Monitor.Wait(someObject); Monitor.Wait releases the lock and suspends the thread from running. No busy waiting! Pulse Monitor.PulseAll(someObject) Notifies all threads waiting on someObject. Monitor.Pulse(someObject) Notifies on thread waiting on someObject Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Threads: Critical sections, locking, monitors, etc. /Anders Børjesson Wait “code pattern” While (conditionDoesNotHold) { Monitor.Wait(someObject); } // critical section Monitor.Wait(…) should always be called in a loop, since Monitor.PulseAll(…) wakes up all thread waiting for the object. Only the first (quickest) should execute the critical section. Other thread go back to wait (because of the while loop) Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

Task parallelism vs. Data parallelism Lots of data, same operation applied to all data. Example: Encrypting 100 passwords Task parallelism Many different operations can run currently. Example: Similar to what an operating system does all the time Pipelining Mix of task and data parallelism. Requires coordination between multiple concurrent tasks Tasks with queues to hold partial results Example: Password cracking Read encrypted passwords -> make variations -> encrypt -> compare to password file General example: Producer-Consumer Threads: Critical sections, locking, monitors, etc. /Anders Børjesson

References and further readings Joe Albahari: Threading in C#, part 1 Getting Started http://www.albahari.com/threading/ John Sharp: Microsoft Visual C# 2012, Microsoft Press, 2012 Chapter 23 Improving Throughput by Using Tasks, page 541-584 Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 29 Threading and Synchronization, page 1443-1511 Mark Michaelis: Essential C# 5.0, Addison Wesley 2013 Chapter 18 Multithreading, page 727-808 Gaston Hillar: Professional Parallel Programming with C#, Wrox 2011 547 pages Threads: Critical sections, locking, monitors, etc. /Anders Børjesson