Systems Programming & Scripting

Similar documents
Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

C# Threading. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Lecture 8: Threads. Lisa (Ling) Liu

Contents. 6-1 Copyright (c) N. Afshartous

Concurrency: a crash course

DAD Lab. 2 Additional C# Topics

Lecture 21: Concurrency in Other Environments (Part 2)

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Advanced Programming C# Lecture 13. dr inż. Małgorzata Janik

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

The University of Texas at Arlington

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Last Class: Deadlocks. Today

The University of Texas at Arlington

Synchronization Spinlocks - Semaphores

Performance Throughput Utilization of system resources

Advanced Programming C# Lecture 12. dr inż. Małgorzata Janik

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Threads and Java Memory Model

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5

Ch 9: Control flow. Sequencers. Jumps. Jumps

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Concurrency, Thread. Dongkun Shin, SKKU

COMP346 Winter Tutorial 4 Synchronization Semaphores

Synchronising Threads

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CMSC 330: Organization of Programming Languages

CMSC 132: Object-Oriented Programming II. Threads in Java

CS 351 Design of Large Programs Threads and Concurrency

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

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

IV. Process Synchronisation

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

Concurrency. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Lecture 8: September 30

Concurrency in Java Prof. Stephen A. Edwards

What's wrong with Semaphores?

Today: Synchronization. Recap: Synchronization

Synchronization for Concurrent Tasks

High Performance Computing Course Notes Shared Memory Parallel Programming

CMSC 132: Object-Oriented Programming II

Concurrency: Mutual Exclusion and Synchronization. Concurrency

Advanced Concepts of Programming

G52CON: Concepts of Concurrency

Concurrency. Fundamentals of Computer Science

Synchronization COMPSCI 386

Java Threads. Written by John Bell for CS 342, Spring 2018

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

Synchronization I. Jo, Heeseung

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

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

Introduction to Java Threads

Programming Languages

IT 540 Operating Systems ECE519 Advanced Operating Systems

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

CS533 Concepts of Operating Systems. Jonathan Walpole

Programming in Parallel COMP755

Process Synchronization

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Threads & Networking

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Concurrency and Synchronisation

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

Multi-core Architecture and Programming

CS 3305 Intro to Threads. Lecture 6

Synchronization. Coherency protocols guarantee that a reading processor (thread) sees the most current update to shared data.

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

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

Lecture 9: Introduction to Monitors

Concurrency: Deadlock and Starvation

Chapter 5 Asynchronous Concurrent Execution

Multi-threaded Programming in C++

Lecture #7: Implementing Mutual Exclusion

Process Synchronization

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

PROCESS SYNCHRONIZATION

Concurrency. Chapter 5

Exercise Session Week 8

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

7. MULTITHREDED PROGRAMMING

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

HY 345 Operating Systems

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

Resource management. Real-Time Systems. Resource management. Resource management

Concurrent Programming

Lecture 5 Threads and Pthreads II

CS11 Java. Fall Lecture 7

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

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

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

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

Process Synchronization

Transcription:

Systems Programming & Scripting Lecture 10: C# Threading Introduction, Accessing Shared Resources Based on: An Introduction to programming with C# Threads By Andrew Birrell, Microsoft, 2005 Examples from Programming C# 3.0, Jesse Liberty, O'Reilly. Chapter 20. Systems Prog. & Script. - Heriot W 1

Processes and Threads Traditionally, a process in an operating system consists of an execution environment and a single thread of execution (single activity). However, concurrency can be required in many programs (e.g in GUIs) for various reasons. The solution was to improve the notion of a process to contain an execution environment and one or more threads of execution. Systems Prog. & Script. - Heriot W 2

Processes and Threads (cont'd) An execution environment is a collection of kernel resources locally managed, which threads have access to. It consists of: An address space. Threads synchronization and communication resources Higher-level resources such as file access. Systems Prog. & Script. - Heriot W 3

Processes and Threads (cont'd) Threads represent activities which can be created and destroyed dynamically as required and several of them can be running on a single execution environment. The aim of using multiple threads in a single environment is: To maximise the concurrency of execution between operations, enabling the overlap of computation with input and output. E.g. one thread can execute a client request while another thread serving another request (optimising server performance). Systems Prog. & Script. - Heriot W 4

Concurrency and Parallelism In some applications concurrency is a natural way of structuring your program: In GUIs separate threads handle separate events Concurrency is also useful operating slow devices including e.g. disks and printers. IO operations are implemented as a separate thread while the program is progressing through other threads. Concurrency is required to exploit multi-processor machines. Allowing processes to use the available processors rather than one. Systems Prog. & Script. - Heriot W 5

Sources of Concurrency (cont'd) The support of user interaction profits from concurrency. Program could be processing a user request in the background and at the same time responding to user interactions by updating GUI. Concurrency is also used when building servers. A web server is multi-threaded to be able to handle multiple user requests concurrently. Systems Prog. & Script. - Heriot W 6

Threads Primitives Thread Creation. Mutual Exclusion. Event waiting. Waking up a thread. The above primitives are supported by C# s System.Threading namespace and C# lock statement. Systems Prog. & Script. - Heriot W 7

Thread Creation A thread is constructed in C# by: Creating a Thread object. Passing to it a ThreadStart delegate. Calling the start method of the created thread. Creating and starting a thread is called forking. Systems Prog. & Script. - Heriot W 8

Thread Creation Example Thread t = new Thread(new ThreadStart(func.A)); t.start(); func.b(); t.join(); The code above executes functions func.a() and func.b() concurrently. Thread t is created and started. Execution completes when both method calls have completed. Systems Prog. & Script. - Heriot W 9

Mutual Exclusion Threads interact through access to shared memory. In order to avoid errors, mutual exclusion techniques are required to control how threads access a shared resource. Systems Prog. & Script. - Heriot W 10

Mutual Exclusion Mutual exclusion is required to control threads access to a shared resource. We need to be able to specify a region of code that only one thread can execute at any time. Sometimes called critical section. Systems Prog. & Script. - Heriot W 11

C# Mutual Exclusion Support Mutual exclusion is supported in C# by class Monitor and the lock statement. lock(expression) statement The lock argument can be any C# object. By default, C# objects are unlocked. The lock statement locks the object passed as its argument, executes the statements, then unlocks the object. If another thread attempts to access the locked object, the second thread is blocked until the lock releases the object. Systems Prog. & Script. - Heriot W 12

Example: Swap public void Swap() { lock (this) { Console.WriteLine("Swap enter: x={0}, y={1}", this.x, this.y); int z = this.x; this.x = this.y; this.y = z; Console.WriteLine("Swap leave: x={0}, y={1}", this.x, this.y); } } Examples from Programming C# 3.0, Jesse Liberty, O'Reilly. Chapter 20. Systems Prog. & Script. - Heriot W 13

Example: Swap public void DoTest() { Thread t1 = new Thread( new ThreadStart(Swap)); Thread t2 = new Thread( new ThreadStart(Swap)); } t1.start(); t2.start(); t1.join(); t2.join(); Systems Prog. & Script. - Heriot W 14

Waiting for a Condition Locking an object is a simple scheduling policy. The shared memory accessed inside the lock statement is the scheduled resource. More complicated scheduling is sometimes required. Blocking a thread until a condition is true. Supported in C# using the Wait, Pulse and PulseAll functions of class Monitor. Systems Prog. & Script. - Heriot W 15

Waiting for a Condition (cont'd) A thread must hold the lock to be able to call the Wait function. The Wait call unlocks the object and blocks the thread. The Pulse function awakens at least one thread blocked on the locked object. The PulseAll awakens all threads currently waiting on the locked object. When a thread is awoken after calling Wait and blocking, it re-locks the object and return. Systems Prog. & Script. - Heriot W 16

Example: Increment/Decrement public void Decrementer() { try { // synchronise this area Monitor.Enter(this); if (counter < 1) { Console.WriteLine("[{0}] In Decrementer. Counter: {1}. Waiting...", Thread.CurrentThread.Name, counter); Monitor.Wait(this); } while (counter > 0) { long temp = counter; temp--; Thread.Sleep(1); counter = temp; Console.WriteLine("[{0}] In Decrementer. Counter:{1}.", Thread.CurrentThread.Name, counter); } } finally { Monitor.Exit(this); } } Systems Prog. & Script. - Heriot W 17

Example: Increment/Decrement public void Incrementer() { try { // synchronise this area Monitor.Enter(this); while (counter < 10) { long temp = counter; temp++; Thread.Sleep(1); counter = temp; Console.WriteLine("[{0}] In Incrementer.{1}.", Thread.CurrentThread.Name, counter); } Monitor.Pulse(this); } finally { Console.WriteLine("[{0}] Exiting...", Thread.CurrentThread.Name); Monitor.Exit(this); } } Systems Prog. & Script. - Heriot W 18

Example: Increment/Decrement public void DoTest() { Thread[] mythreads = { new Thread( new ThreadStart(Decrementer)), new Thread( new ThreadStart(Incrementer)) }; int n = 1; foreach (Thread mythread in mythreads) { mythread.isbackground = true; mythread.name = "Thread"+n.ToString(); Console.WriteLine("Starting thread {0}", mythread.name); mythread.start(); n++; Thread.Sleep(500); } foreach (Thread mythread in mythreads) { mythread.join(); } Console.WriteLine("All my threads are done"); } private long counter = 0; Systems Prog. & Script. - Heriot W 19

Example: Increment/Decrement public void DoTest() { Thread[] mythreads = { new Thread( new ThreadStart(Decrementer)), new Thread( new ThreadStart(Incrementer)) }; int n = 1; foreach (Thread mythread in mythreads) { mythread.isbackground = true; mythread.start(); mythread.name = "Thread"+n.ToString(); n++; Console.WriteLine("Started thread {0}", mythread.name); Thread.Sleep(50); } foreach (Thread mythread in mythreads) { mythread.join(); } Console.WriteLine("All my threads are done"); } private long counter = 0; Systems Prog. & Script. - Heriot W 20

Example Explained 2 threads are created: one for incrementing another for decrementing a global counter A monitor is used to ensure that reading and writing of the counter is done atomically Monitor.Enter/Exit are used for entering/leaving an atomic block (critical section). The decrementer first checks whether the value can be decremented. Monitor.Pulse is used to inform the waiting thread of a status change. Systems Prog. & Script. - Heriot W 21

Thread Interruption Interrupting a thread is sometimes required to get the thread out from a wait. This can be achieved in C# by using the interrupt function of the Thread class. A thread t in a wait state can be interrupted by another thread by calling t.interrupt(). t will then resume execution by relocking the object (maybe after waiting for the lock to become unlocked). Interrupts complicates programs and should be avoided if possible. Systems Prog. & Script. - Heriot W 22

Race Conditions Example: Thread A opens a file Thread B writes to the file The program is successful, if A is fast enough to open the file, before B starts writing. Systems Prog. & Script. - Heriot W 23

Deadlocks Example: Thread A locks object M1 Thread B locks object M2 Thread A blocks trying to lock M2 Thread B blocks trying to lock M1 None of the 2 threads can make progress Systems Prog. & Script. - Heriot W 24

Avoiding Deadlocks Involving Locks Maintain a partial order for acquiring locks in the program. For any pair of objects {M1, M2}, each thread that needs to have both objects locked simultaneously should lock the objects in the same order. E.g. M1 is always locked before M2. This avoids deadlocks caused by locks. Systems Prog. & Script. - Heriot W 25

Deadlock Caused By Wait Example: Thread A acquires resource 1 Thread B acquires resource 2 Thread A wants 2, so it calls Wait to wait for 2 Thread B wants 1, so it calls Wait to wait for 1 Again, partial order can be used to avoid the deadlock. Systems Prog. & Script. - Heriot W 26

Other Potential Problems Starvation: When locking objects or using Monitor.Wait() on an object, there is a risk that the object will never make progress. Program complexity. Systems Prog. & Script. - Heriot W 27