Reintroduction to Concurrency

Size: px
Start display at page:

Download "Reintroduction to Concurrency"

Transcription

1 Reintroduction to Concurrency The execution of a concurrent program consists of multiple processes active at the same time. 9/25/14 7

2 Dining philosophers problem Each philosopher spends some time thinking and some time eating In order to eat, he needs two forks When he finishes thinking he picks up the forks on either side of him, eats for a while, then replaces the forks and starts thinking again How do we ensure that no philosopher starves? What are the properties of the system? 8

3 Exercise Solve the Dining Philosophers problem! 8 to a group 10 minutes max Devise a strategy that each philosopher can follow blindly (i.e. an algorithm) No philosopher should starve Test your strategy with your group No Solution? What are the problems? A Solution? Show us. 9

4 Philosophical Possibilities 1. Each philosopher simultaneously picks up the fork on his left, then goes for the one on his right, which will be held by another philosopher deadlock 2. A philosopher can simultaneously pick up the two forks when they become available atomic actions 3. An important philosopher can take a fork away from a less important one pre-emptive acquisition 10

5 Why do things concurrently? Performance execute as fast as possible Throughput get as much done as possible Efficiency minimise the time spent unproductively Programmability some problems are naturally concurrent what problems? 11

6 Logical concurrency Several happen together in the designer s mind Accept new orders Dispatch good from the factory ACME ACME ACME 12

7 Multitasking Multitasking is where a single processor executes more than a single activity Shares its times between the activities Periodically the processor changes from one task to another Tasks run together, rather than one after the other Executes a context switch to change its state The order and frequency of context switches are controlled by a scheduling algorithm 13

8 True concurrency True concurrency sometimes referred to as parallelism happens when a system has more than one processor working on a job Multiprocessor workstation Dedicated parallel computer Co-operating systems on a network 14

9 Processes Most operating systems have the notion of a process An executing program Typically, processes are somewhat isolated from each other each owns An address space A set of resources such as open files, sockets etc. By default, processes don t directly interact No interference No co-operation 15

10 Concurrency in processes A system will usually have several processes running The processes co-operate to provide a system, rather than in providing a single task Each task is provided by a single simple program of the normal kind a sequence of instructions 16

11 Threads Threads (sometimes called lightweight processes) Lots of executing sequences within a single process Share memory, files, windows, Can interact Can interfere Threads are provided by some operating systems as standard (Win32, Solaris), and by others as run-time libraries (some Unix variants) 17

12 Thread scheduling How do we order the execution of threads? Are all threads equally important? If yes give each the same time on the processor If no give some more time, or run them preferentially Can the programmer force a thread to keep running? If yes rely on the programmer to let other threads have a chance If no forcibly evict a thread and run another 18

13 Co-routines Co-routines are a co-operative approach to multitasking A thread starts running, and keeps running until it voluntarily gives up the processor First thread yields the processor to second thread Second thread yields, first thread resumes If a thread doesn t yield, it keeps the processor Locks out other threads Lets the programmer keep running something important More control, more work 19

14 Pre-emptive scheduling Simplicity doesn t offer enough compensation for the disadvantages of co-operative scheduling All too easy for a thread not to yield Can lead to some very convoluted programming structures Almost all modern systems use pre-emptive scheduling, where context switches occur based on the rules of the scheduling algorithm No lock-outs, fairer, more potential problems You still encounter co-routines in some embedded real-time systems where fine control is too important to lose 20

15 Interleaving If the processor s being shared, there s the possibility that actions from one task will be mixed-in with actions from another This is known as interleaving 21

16 Interleaving Interleaving also occurs in truly concurrent systems Any concurrent computation will behave, in the final analysis, like some interleaved sequence of its basic indivisible actions Interleaving of actions is the key concept in concurrent programming 22

17 Interleaving Any kind of interleaving has an important side effect Suppose the two tasks share some structure or resource. Interleaving their actions can cause some rather bizarre effects Open the file Read its contents Modify and re-write Close the file Open the file Read its contents Open the file Delete some records Modify and re-write Close the file Close the file Open the file Delete some records Close the file 23

18 Interference This phenomenon is referred to as interference the actions of two tasks, correct in themselves, conflict when combined concurrently Interference is the basic correctness problem in concurrent programming The essence of the problem Tasks use several actions to manipulate the shared structure and these actions are mixed up together and the effects of actions in one task conflicts with the behaviour of actions in the other 24

19 Interference inconsistency Some actions are naturally tied together people: totalmarks: newmark() add 1 to people add total mark re-compute average average() get number of people get total marks divide one by the other Interleaving made the structure inconsistent for a time in which period a calculation took place on bogus data

20 Interference race conditions The is an example of a race condition Different results depending on exactly which action runs first newmark() average() 60 newmark() average() 61 newm average() ark() 57 26

21 Atomicity Each routine is composed of several actions, and those actions must occur indivisibly atomically for the result to be correct This idea of atomicity is the key to avoiding undesirable artefacts from concurrency Identify the critical sections of code that must execute without any extraneous actions in between them Make these sections atomic 27

22 Locking and signalling To implement shared data structures without interference, we need to do one of two things Signal when a thread is using the structure, so other threads can check whether it s safe to modify it Lock the structure so that, if one thread is accessing it, no-one else can Signalling protocols are still quite co-operative and flexible, as a thread gets to check and choose whether to change a shared structure Locking is safer and more structured, but sometimes a little restrictive 28

23 Synchronisation Some concurrently executing tasks have a natural ordering with respect to each other Tasks need to be able to synchronise with each other to enforce such orderings 29

24 Semaphores A semaphore is an abstract data type with two operations, wait() and signal() wait() waits for the semaphore to be dropped, then raises it and continues in one atomic action signal() drops the semaphore Associate a semaphore with every shared structure and bracket all accesses with wait() signal() E.W. Dijkstra, Co-operating sequential processes, in Programming languages, ed. F. Genuys, Prentice-Hall (1968) 30

25 Monitors Monitors enforce mutual exclusivity on mutually interacting critical sections of code The actions in the monitor are the only ones able to manipulate the data structure This call will block until the first has completed Internally, of course, the monitor is just a safe wrapper round a semaphore C.A.R. Hoare, Monitors: an operating system structuring concept, 9/25/14 Communications 2004 Trinity College of the Dublin ACM 17(10) (October 1974) 31

26 Monitors Monitors can provide language-level concurrency control The language construct can be checked and analysed Programmers can t avoid the concurrency control, accidentally or maliciously Less error prone when compared to sockets Monitors are the basis of concurrency control in lots of modern programming languages 32

27 Monitors and objects In object-oriented languages monitors are typically associated with objects Object = data + operations so if we wrap a monitor around the operations, we protect the data from corruption This notion of a protected value is very useful Typically speak of locking a variable while it s being accessed, and blocking on a variable waiting for it to become free 33

28 Problems, problems computationone() lock a lock b add 10 to a + b store the result in b a: 22 b: 79 Blocks waiting for b Blocks waiting for a lock a lock b lock b lock a computationtwo() lock b lock a subtract 10 from b * a store the result in a 34

29 Deadlock This situation everyone waiting for everyone else, with no-one making progress is called deadlock Well studied problem in computer science Several solutions, but no really general ones circular waiting: each task is waiting for (one or more of) the others There are some very convoluted cases not always just two tasks It appears in applications with depressing regularity, and we ll look at it more later 35

30 Deadlock Deadlock may occur when code has the following properties: Piecemeal acquisition of resources Locks one at a time Resources acquired in any order Circular waiting Ability to hold lock indefinitely 36

31 Concept summary Activity Processes, threads Actions, interleaving Critical sections, atomicity Synchronisation Signalling and locking Constructs Semaphores Blocking Monitors, objects Deadlock 37

32 Java Threads In Java a process is represented by a thread. To make a thread run you call its start() method. This registers the thread with the thread scheduler. start() does not cause the thread to run immediately it only makes it eligible to run. The thread must contend with other threads for the CPU. When a thread gets to execute, it executes a run() method: 38

33 threads in Java A Thread class manages a single sequential thread of control. Threads may be created and deleted dynamically. Thread run() MyThread run() The Thread class executes instructions from its method run(). The actual code executed depends on the implementation provided for run() in a derived class. class MyThread extends Thread { public void run() { //... Thread x = new MyThread(); 39

34 threads in Java Since Java does not permit multiple inheritance, we often implement the run() method in a class not derived from Thread but from the interface Runnable. Runnable target Thread run() MyRun run() public interface Runnable { public abstract void run(); class MyRun implements Runnable{ public void run() { //... Thread x = new Thread(new MyRun()); 40

35 threads in Java Write a Java program that creates three Java threads (by extending Thread). The threads should sleep for a specified amount of time and then print their name and the length of time they slept for to the console. The three threads should sleep for 500ms, 250ms, 1000ms respectively. Repeat the exercise implementing Runnable 41

36 Extend Thread Example class ExtendThread extends Thread{ private int sleeptime; public ExtendThread(String name, int time){ super(name); sleeptime = time; public void run(){ while(true){ try{ sleep(sleeptime); catch (InterruptedException e){ System.out.println(getName() + " " + sleeptime); 42

37 Extend Thread Example class ExtendMain{ public static void main(string args[]){ ExtendThread t1 = new ExtendThread("Thread 1", 500); ExtendThread t2 = new ExtendThread("Thread 2", 250); ExtendThread t3 = new ExtendThread("Thread 3", 1000); t1.start(); t2.start(); t3.start(); 43

38 Implement Runnable Example class ImplementRunnable implements Runnable{ private Thread t; private int sleeptime; public ImplementRunnable(String name, int time){ sleeptime = time; t = new Thread(this, name); public void start(){ t.start(); public void run(){ while(true){ try{ Thread.sleep(sleepTime); catch (InterruptedException e){ System.out.println(t.getName() + " " + sleeptime); 44

39 Implement Runnable Example class ImplementMain{ public static void main(string args[]){ ImplementRunnable t1 = new ImplementRunnable("Thread 1", 500); ImplementRunnable t2 = new ImplementRunnable("Thread 2", 250); ImplementRunnable t3 = new ImplementRunnable("Thread 3", 1000); t1.start(); t2.start(); t3.start(); 45

40 Executing a Thread class Interleave { public static int c1 = 2; public static int c2 = 3; public static void main (String[] args) { Thread p1 = new P1 (); Thread p2 = new P2 (); p1.start (); p2.start (); Extend the Thread class and override its run() method. class P1 extends Thread { public void run () { Interleave.c1 = Interleave.c1 *Interleave.c2; class P2 extends Thread { public void run () { Interleave.c1 = Interleave.c1 +Interleave.c2; 46

41 Executing a Thread class Interleave { public static int c1 = 2; public static int c2 = 3; public static void main (String[] args) { Thread p1 = new Thread(new P1()); Thread p2 = new Thread(new P2()); p1.start (); p2.start (); Sometimes it is desirable to implement the run() method in a class not derived from Thread but from the interface Runnable. class P1 implements Runnable { public void run () { Interleave.c1 = Interleave.c1 *Interleave.c2; class P2 implements Runnable { public void run () { Interleave.c1 = Interleave.c1 +Interleave.c2; 47

42 Executing a Thread When the run() method ends the thread is considered dead. A dead thread cannot be started again, but it still exists and, like any other object, its methods and data can still be accessed. 48

43 Thread States When a thread s start() method is called, the thread goes into a ready-to-run state and stays there until the scheduler moves it to the running state. In the course of execution the thread may temporarily give up the CPU and enter some other state. Running Monitor states Suspended Asleep Blocked Ready 49

44 Thread states - Yielding If there are any other threads in the Ready state, the thread that just yielded may have to wait before it gets to execute again. If there are no waiting threads in the Ready state the thread that just yielded will get to continue executing immediately. Running Thread.yield() scheduled Ready 50

45 Thread states - Sleeping A call to the static sleep() method requests the currently executing thread to cease executing for an approximately specified period in milliseconds. Running Ready Asleep Note: when the thread finishes sleeping it does not continue execution directly. 51

46 Thread states - Blocking If a method needs to wait for an indeterminable amount of time until some I/O occurrence takes place it should step out of the Running state. This is know as blocking. All Java I/O methods behave this way. Running Ready Blocked A thread can also become blocked if it fails to acquire the lock for a monitor or if it issues a wait() call. This will be explained later. 52

47 Thread Priorities and Scheduling Every thread has a priority (from 1..10). All newly created threads have their priority set to that of the creating thread. Higher priority threads get preference over lower priority threads. The scheduler generally chooses the the highest-priority waiting thread. 53

48 Thread Priorities and Scheduling If there is more than one waiting thread the scheduler chooses one of them. There is no guarantee that the one chosen is the one that has been waiting longest. int oldpriority = athread.getpriority(); int newpriority = Math.min(oldPriority+1, Thread.MAX_PRIORITY); athread.setpriority(newpriority); Note: The way thread priorities affect scheduling is platform dependent. 54

Performance Throughput Utilization of system resources

Performance Throughput Utilization of system resources Concurrency 1. Why concurrent programming?... 2 2. Evolution... 2 3. Definitions... 3 4. Concurrent languages... 5 5. Problems with concurrency... 6 6. Process Interactions... 7 7. Low-level Concurrency

More information

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

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science Multithreaded Programming Part II CSE 219 Stony Brook University, Thread Scheduling In a Java application, main is a thread on its own Once multiple threads are made Runnable the thread scheduler of the

More information

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17 Concurrency & Parallelism So far, our programs have been sequential: they do one thing after another, one thing at a. Let s start writing programs that do more than one thing at at a. Threads, Concurrency,

More information

Threads, Concurrency, and Parallelism

Threads, Concurrency, and Parallelism Threads, Concurrency, and Parallelism Lecture 24 CS2110 Spring 2017 Concurrency & Parallelism So far, our programs have been sequential: they do one thing after another, one thing at a time. Let s start

More information

Multiple Inheritance. Computer object can be viewed as

Multiple Inheritance. Computer object can be viewed as Multiple Inheritance We have seen that a class may be derived from a given parent class. It is sometimes useful to allow a class to be derived from more than one parent, inheriting members of all parents.

More information

Animation Part 2: MoveableShape interface & Multithreading

Animation Part 2: MoveableShape interface & Multithreading Animation Part 2: MoveableShape interface & Multithreading MoveableShape Interface In the previous example, an image was drawn, then redrawn in another location Since the actions described above can apply

More information

THREADS & CONCURRENCY

THREADS & CONCURRENCY 27/04/2018 Sorry for the delay in getting slides for today 2 Another reason for the delay: Yesterday: 63 posts on the course Piazza yesterday. A7: If you received 100 for correctness (perhaps minus a late

More information

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Threads and Synchronization May 8, 2007 Computation Abstractions t1 t1 t4 t2 t1 t2 t5 t3 p1 p2 p3 p4 CPU 1 CPU 2 A computer Processes

More information

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

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

Concurrent Programming

Concurrent Programming Concurrency Concurrent Programming A sequential program has a single thread of control. Its execution is called a process. A concurrent program has multiple threads of control. They may be executed as

More information

Threads and Parallelism in Java

Threads and Parallelism in Java Threads and Parallelism in Java Java is one of the few main stream programming languages to explicitly provide for user-programmed parallelism in the form of threads. A Java programmer may organize a program

More information

THREADS & CONCURRENCY

THREADS & CONCURRENCY 4/26/16 Announcements BRING YOUR CORNELL ID TO THE PRELIM. 2 You need it to get in THREADS & CONCURRENCY Prelim 2 is next Tonight BRING YOUR CORNELL ID! A7 is due Thursday. Our Heap.java: on Piazza (A7

More information

7. MULTITHREDED PROGRAMMING

7. MULTITHREDED PROGRAMMING 7. MULTITHREDED PROGRAMMING What is thread? A thread is a single sequential flow of control within a program. Thread is a path of the execution in a program. Muti-Threading: Executing more than one thread

More information

THREADS AND CONCURRENCY

THREADS AND CONCURRENCY THREADS AND CONCURRENCY Lecture 22 CS2110 Spring 2013 Graphs summary 2 Dijkstra: given a vertex v, finds shortest path from v to x for each vertex x in the graph Key idea: maintain a 5-part invariant on

More information

Concurrent Programming. Concurrent Programming. Today. Comp 104: Operating Systems Concepts 05/01/2017. Concurrent Programming & Threads

Concurrent Programming. Concurrent Programming. Today. Comp 104: Operating Systems Concepts 05/01/2017. Concurrent Programming & Threads Comp 104: Operating Systems Concepts Concurrent Programming & Threads Today Introduction to Concurrent Programming Threads Java Threads and Realisation 1 2 Concurrent Programming Consider a program that

More information

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team http://101companies.org/wiki/ Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team Non-101samples available here: https://github.com/101companies/101repo/tree/master/technologies/java_platform/samples/javathreadssamples

More information

Process Management And Synchronization

Process Management And Synchronization Process Management And Synchronization In a single processor multiprogramming system the processor switches between the various jobs until to finish the execution of all jobs. These jobs will share the

More information

The Deadlock Lecture

The Deadlock Lecture Concurrent systems Lecture 4: Deadlock, Livelock, and Priority Inversion DrRobert N. M. Watson The Deadlock Lecture 1 Reminder from last time Multi-Reader Single-Writer (MRSW) locks Alternatives to semaphores/locks:

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

Explain briefly how starvation may occur in process scheduling. (2 marks)

Explain briefly how starvation may occur in process scheduling. (2 marks) OMP25111 Lecture 8 1/40 From last time Explain briefly how starvation may occur in process scheduling. (2 marks) In round-robin scheduling, new processes are typically placed at the end of the ready-state

More information

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

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to

More information

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3 Operating Systems Antonio Vivace - 2016 revision 4 Licensed under GPLv3 Process Synchronization Background A cooperating process can share directly a logical address space (code, data) or share data through

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

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

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II Definitions A glossary Threads From Volume II Copyright 1998-2002 Delroy A. Brinkerhoff. All Rights Reserved. Threads Slide 1 of 30 PMultitasking: (concurrent ramming, multiramming) the illusion of running

More information

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process. Deadlock A set of processes is deadlocked if each process in the set is waiting for an event that can be caused by another process in the set. The events that we are mainly concerned with are resource

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers 1 Critical sections and atomicity We have been seeing that sharing mutable objects between different threads is tricky We need some

More information

Threads & Concurrency

Threads & Concurrency 2 Due date of A7 Due d About A5-A6 We have changed the due date of A7 Friday, 28 April. Threads & Concurrency But the last date to submit A7 remains the same: 29 April. We make the last date be 29 April

More information

Threads & Concurrency

Threads & Concurrency Threads & Concurrency Lecture 24 CS2110 Spring 2017 Due date of A7 Due d About A5-A6 2 We have changed the due date of A7 Friday, 28 April. But the last date to submit A7 remains the same: 29 April. We

More information

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Note: Each loop has 5 iterations in the ThreeLoopTest program. Lecture 23 Multithreading Introduction Multithreading is the ability to do multiple things at once with in the same application. It provides finer granularity of concurrency. A thread sometimes called

More information

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads? Objectives MultiThreading What are Threads? Interrupting threads Thread properties By Võ Văn Hải Faculty of Information Technologies Summer 2012 Threads priorities Synchronization Callables and Futures

More information

Threads Chate Patanothai

Threads Chate Patanothai Threads Chate Patanothai Objectives Knowing thread: 3W1H Create separate threads Control the execution of a thread Communicate between threads Protect shared data C. Patanothai Threads 2 What are threads?

More information

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

CMSC 132: Object-Oriented Programming II. Threads in Java CMSC 132: Object-Oriented Programming II Threads in Java 1 Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read & write files

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 4: Atomic Actions Natasha Alechina School of Computer Science nza@cs.nott.ac.uk Outline of the lecture process execution fine-grained atomic actions using fine-grained

More information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 6: PROCESS SYNCHRONIZATION CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background

More information

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE

MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE MS Windows Concurrency Mechanisms Prepared By SUFIAN MUSSQAA AL-MAJMAIE 163103058 April 2017 Basic of Concurrency In multiple processor system, it is possible not only to interleave processes/threads but

More information

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course Advanced Java Programming Course MultiThreading By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Session objectives Introduction Creating thread Thread class

More information

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Advanced Java Programming Course MultiThreading By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City Session objectives Introduction Creating thread Thread class

More information

CSCD 330 Network Programming

CSCD 330 Network Programming CSCD 330 Network Programming Lecture 12 More Client-Server Programming Winter 2019 Reading: References at end of Lecture 1 Introduction So far, Looked at client-server programs with Java Sockets TCP and

More information

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Need for synchronization: If threads comprise parts of our software systems, then they must communicate. Thread communication and synchronization There are two main aspects to Outline for Lecture 19 multithreaded programming in Java: I. Thread synchronization. thread lifecycle, and thread synchronization.

More information

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading Process Characteristics Threads Chapter 4 Reading: 4.1,4.4, 4.5 Unit of resource ownership - process is allocated: a virtual address space to hold the process image control of some resources (files, I/O

More information

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Threads Chapter 4. Reading: 4.1,4.4, 4.5 Threads Chapter 4 Reading: 4.1,4.4, 4.5 1 Process Characteristics Unit of resource ownership - process is allocated: a virtual address space to hold the process image control of some resources (files,

More information

Concurrency. Glossary

Concurrency. Glossary Glossary atomic Executing as a single unit or block of computation. An atomic section of code is said to have transactional semantics. No intermediate state for the code unit is visible outside of the

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

Synchronized Methods of Old Versions of Java

Synchronized Methods of Old Versions of Java Administrivia Assignment #4 is out Due Thursday April 8, 10:00pm no late assignments will be accepted Sign up in labs next week for a demo time In case you hadn t noticed Classes end Thursday April 15

More information

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright MultiThreading Object Orientated Programming in Java Benjamin Kenwright Outline Review Essential Java Multithreading Examples Today s Practical Review/Discussion Question Does the following code compile?

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

Liveness properties. Deadlock

Liveness properties. Deadlock Liveness properties From a theoretical viewpoint we must ensure that we eventually make progress i.e. we want to avoid : blocked threads/processes waiting for each other Livelock: processes/threads execute

More information

Multi-threading in Java. Jeff HUANG

Multi-threading in Java. Jeff HUANG Multi-threading in Java Jeff HUANG Software Engineering Group @HKUST Do you use them? 2 Do u know their internals? 3 Let s see File DB How can they service so many clients simultaneously? l 4 Multi-threading

More information

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 Threads and Locks CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015 1 Goals Cover the material presented in Chapter 2, Day 1 of our concurrency textbook Creating threads Locks Memory

More information

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

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

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads Concurrency - Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads 1 Introduction Concurrency can occur at four levels: Machine instruction

More information

COMP346 Winter Tutorial 4 Synchronization Semaphores

COMP346 Winter Tutorial 4 Synchronization Semaphores COMP346 Winter 2015 Tutorial 4 Synchronization Semaphores 1 Topics Synchronization in Details Semaphores Introducing Semaphore.java 2 Synchronization What is it? An act of communication between unrelated

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 11 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel Feedback Queue: Q0, Q1,

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling Multithreaded Programming Topics Multi Threaded Programming What are threads? How to make the classes threadable; Extending threads;

More information

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Week 7 Concurrent Programming: Thread Synchronization CS 180 Sunil Prabhakar Department of Computer Science Purdue University Announcements Exam 1 tonight 6:30 pm - 7:30 pm MTHW 210 2 Outcomes Understand

More information

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

More information

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

Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5 Concurrency, Mutual Exclusion and Synchronization C H A P T E R 5 Multiple Processes OS design is concerned with the management of processes and threads: Multiprogramming Multiprocessing Distributed processing

More information

CS3502 OPERATING SYSTEMS

CS3502 OPERATING SYSTEMS CS3502 OPERATING SYSTEMS Spring 2018 Synchronization Chapter 6 Synchronization The coordination of the activities of the processes Processes interfere with each other Processes compete for resources Processes

More information

Introduction to Java Threads

Introduction to Java Threads Object-Oriented Programming Introduction to Java Threads RIT CS 1 "Concurrent" Execution Here s what could happen when you run this Java program and launch 3 instances on a single CPU architecture. The

More information

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit Threads Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multitasking Thread-based multitasking Multitasking

More information

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency, Thread. Dongkun Shin, SKKU Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Synchronization in Java Department of Computer Science University of Maryland, College Park Multithreading Overview Motivation & background Threads Creating Java

More information

CS 556 Distributed Systems

CS 556 Distributed Systems CS 556 Distributed Systems Tutorial on 4 Oct 2002 Threads A thread is a lightweight process a single sequential flow of execution within a program Threads make possible the implementation of programs that

More information

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems : Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks on either

More information

Chapter 6 Process Synchronization

Chapter 6 Process Synchronization Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages

More information

Parallel Programming Languages COMP360

Parallel Programming Languages COMP360 Parallel Programming Languages COMP360 The way the processor industry is going, is to add more and more cores, but nobody knows how to program those things. I mean, two, yeah; four, not really; eight,

More information

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on

More information

Concurrent & Distributed Systems Supervision Exercises

Concurrent & Distributed Systems Supervision Exercises Concurrent & Distributed Systems Supervision Exercises Stephen Kell Stephen.Kell@cl.cam.ac.uk November 9, 2009 These exercises are intended to cover all the main points of understanding in the lecture

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Review for Midterm. Starring Ari and Tyler

Review for Midterm. Starring Ari and Tyler Review for Midterm Starring Ari and Tyler Basic OS structure OS has two chief goals: arbitrating access to resources, and exposing functionality. Often go together: we arbitrate hardware by wrapping in

More information

Programming in Parallel COMP755

Programming in Parallel COMP755 Programming in Parallel COMP755 All games have morals; and the game of Snakes and Ladders captures, as no other activity can hope to do, the eternal truth that for every ladder you hope to climb, a snake

More information

Threading the Code. Self-Review Questions. Self-review 11.1 What is a thread and what is a process? What is the difference between the two?

Threading the Code. Self-Review Questions. Self-review 11.1 What is a thread and what is a process? What is the difference between the two? Threading the Code 11 Self-Review Questions Self-review 11.1 What is a thread and what is a process? What is the difference between the two? Self-review 11.2 What does the scheduler in an operating system

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst Clicker Question #1 public static void main(string[] args) { (new Thread(new t1())).start(); (new Thread(new t2())).start();}

More information

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter Concurrency in Object Oriented Programs 1 Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter Outline Concurrency: the Future of Computing Java Concurrency Thread Safety

More information

CSCD 330 Network Programming

CSCD 330 Network Programming CSCD 330 Network Programming Lecture 12 More Client-Server Programming Winter 2016 Reading: References at end of Lecture 1 Introduction So far, Looked at client-server programs with Java Sockets TCP and

More information

Midterm Exam Amy Murphy 19 March 2003

Midterm Exam Amy Murphy 19 March 2003 University of Rochester Midterm Exam Amy Murphy 19 March 2003 Computer Systems (CSC2/456) Read before beginning: Please write clearly. Illegible answers cannot be graded. Be sure to identify all of your

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) Past & Present Have looked at two constraints: Mutual exclusion constraint between two events is a requirement that

More information

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks

More information

Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science

Concurrency COMS W4115. Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science Concurrency COMS W4115 Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science Concurrency Multiple, simultaneous execution contexts. Want to walk and chew gum at the same

More information

Advanced Concepts of Programming

Advanced Concepts of Programming Berne University of Applied Sciences E. Benoist / E. Dubuis January 2005 1 Multithreading in Java Java provides the programmer with built-in threading capabilities The programmer can create and manipulate

More information

Implementing Coroutines. Faking Coroutines in Java

Implementing Coroutines. Faking Coroutines in Java Concurrency Coroutines Concurrency COMS W4115 Prof. Stephen A. Edwards Spring 2002 Columbia University Department of Computer Science Multiple, simultaneous execution contexts. Want to walk and chew gum

More information

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems More Synchronization; Concurrency in Java CS 475, Spring 2018 Concurrent & Distributed Systems Review: Semaphores Synchronization tool that provides more sophisticated ways (than Mutex locks) for process

More information

Multithreading in Java Part 2 Thread - States JAVA9S.com

Multithreading in Java Part 2 Thread - States JAVA9S.com Multithreading in Java Part 2 Thread - States By, Srinivas Reddy.S When start() method is invoked on thread It is said to be in Runnable state. But it is not actually executing the run method. It is ready

More information

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 6 Concurrency: Deadlock and Starvation Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Deadlock

More information

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall

COMP 150-CCP Concurrent Programming. Lecture 12: Deadlock. Dr. Richard S. Hall COMP 150-CCP Concurrent Programming Lecture 12: Deadlock Dr. Richard S. Hall rickhall@cs.tufts.edu Concurrent programming February 28, 2008 Scenario Process 1 gets the lock for object A and wants to lock

More information

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009 CS211: Programming and Operating Systems Lecture 17: Threads and Scheduling Thursday, 05 Nov 2009 CS211 Lecture 17: Threads and Scheduling 1/22 Today 1 Introduction to threads Advantages of threads 2 User

More information

Chapter 32 Multithreading and Parallel Programming

Chapter 32 Multithreading and Parallel Programming Chapter 32 Multithreading and Parallel Programming 1 Objectives To get an overview of multithreading ( 32.2). To develop task classes by implementing the Runnable interface ( 32.3). To create threads to

More information

CS-537: Midterm Exam (Spring 2001)

CS-537: Midterm Exam (Spring 2001) CS-537: Midterm Exam (Spring 2001) Please Read All Questions Carefully! There are seven (7) total numbered pages Name: 1 Grading Page Points Total Possible Part I: Short Answers (12 5) 60 Part II: Long

More information

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008. CSC 4103 - Operating Systems Spring 2008 Lecture - XII Midterm Review Tevfik Ko!ar Louisiana State University March 4 th, 2008 1 I/O Structure After I/O starts, control returns to user program only upon

More information

Concurrency in Java Prof. Stephen A. Edwards

Concurrency in Java Prof. Stephen A. Edwards Concurrency in Java Prof. Stephen A. Edwards The Java Language Developed by James Gosling et al. at Sun Microsystems in the early 1990s Originally called Oak, first intended application was as an OS for

More information

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 13 Robert Grimm, New York University 1 Review Last week Exceptions 2 Outline Concurrency Discussion of Final Sources for today s lecture: PLP, 12

More information