COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Similar documents
Threads Synchronization

Java s Implementation of Concurrency, and how to use it in our applications.

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

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Faculty of Computers & Information Computer Science Department

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

CMSC 132: Object-Oriented Programming II

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

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

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

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

Threads Chapter 5 1 Chapter 5

Java Threads. COMP 585 Noteset #2 1

What is a thread anyway?

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

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

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

Synchronization in Concurrent Programming. Amit Gupta

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

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

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

CS 159: Parallel Processing

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

CS420: Operating Systems

Threads and Parallelism in Java

G52CON: Concepts of Concurrency

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

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

Parallel Programming Languages COMP360

COMP346 Winter Tutorial 4 Synchronization Semaphores

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

Introduction to Java Threads

Lecture #7: Implementing Mutual Exclusion

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

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

Last Class: Synchronization

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Multiple Inheritance. Computer object can be viewed as

Threads. studykorner.org

Lecture 8: September 30

Programming in Parallel COMP755

User Space Multithreading. Computer Science, University of Warwick

Multithreaded Programming

Java Monitor Objects: Synchronization (Part 1)

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Outline of lecture. i219 Software Design Methodology 10. Multithreaded programming. Kazuhiro Ogata (JAIST)

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

Synchronization for Concurrent Tasks

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

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

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

CS370 Operating Systems

Introduction to Locks. Intrinsic Locks

Synchronization I. Jo, Heeseung

Concurrent Programming using Threads

Synchronization in Java

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Chapter 3 Process Description and Control

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Where are we in the course?

Module 6: Process Synchronization

CS370 Operating Systems

Concurrency, Thread. Dongkun Shin, SKKU

COMP SCI 3SH3: Operating System Concepts (Term 2 Winter 2006) Test 2 February 27, 2006; Time: 50 Minutes ;. Questions Instructor: Dr.

Concurrent Processes Rab Nawaz Jadoon

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

Only one thread can own a specific monitor

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

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Java Threads. Introduction to Java Threads

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Concurrency: a crash course

Overview. Thread Packages. Threads The Thread Model (1) The Thread Model (2) The Thread Model (3) Thread Usage (1)

High Performance Computing Course Notes Shared Memory Parallel Programming

CS370 Operating Systems Midterm Review

Programming Language Concepts: Lecture 11

CS 351 Design of Large Programs Threads and Concurrency

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

Part IV Other Systems: I Java Threads

Definition Multithreading Models Threading Issues Pthreads (Unix)

Synchronization

Concurrent & Distributed Systems Supervision Exercises

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

Programmazione di sistemi multicore

Chapter 6 Process Synchronization

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

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

Transcription:

COMP 346 WINTER 2018 1 Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

REVIEW - MULTITHREADING MODELS 2 Some operating system provide a combined user level thread and Kernel level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multithreading models are three types Many to many relationship. Many to one relationship. One to one relationship. 2

MANY TO MANY MODEL 3 The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads. The following diagram shows the many-to-many threading model where 6 user level threads are multiplexing with 6 kernel level threads. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency and when a thread performs a blocking system call, the kernel can schedule another thread for execution. 3

MANY TO MANY MODEL 4 4

MANY TO ONE MODEL 5 Many-to-one model maps many user level threads to one Kernel-level thread. Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors. If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes. 5

MANY TO ONE MODEL 6 6

ONE TO ONE MODEL 7 There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency than the many-toone model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors. Disadvantage of this model is that creating user thread requires the corresponding Kernel thread. windows NT and windows 2000 use one to one relationship model. 7

ONE TO ONE MODEL 8 8

REVIEW - CONCEPTS 9 There are three very important concepts when doing concurrent programming : Atomicity : An operation is said atomic when it cannot be interrupted. There is almost no atomic operations in Java, the only we've is the assignation a = 5, but a = b++ is not atomic. In some cases, you'll have to make atomic some actions with synchronization. Visibility : This occurs when a thread must watch the actions of the other threads, for example the termination of the thread. This also implies some kind of synchronization. Order of execution : When you have normal program, all you lines of code run in the same order every time you launch the application. This is not the case when you make concurrent programming. You first instruction can followed by an instruction of the thread B or by the first instruction. And that can change every time you launch the application. The order of execution is not guaranteed! 9

SHARED DATA MANIPULATION A shared data manipulation shows how things are really interleaved and shows their potential problems. Lets create a shared data array and allow our threads to manipulate it. 10

SHARED DATA EXAMPLE DESCRIPTION First lets create a static integer array, called a. This single array will be shared among the two threads in our program. Each thread will deposit its id on a slot on the array. We will have our array size set to 2*NI, and let each thread deposit its id on a slot NI times. Ideally, each thread should deposit on an empty slot. 11

SHARED DATA EXAMPLE IMPLEMENTATION To implement this, we will also maintain a shared index. Initially the shared index will be set to 0, indicating that slot 0 is empty. As a thread deposits its id on a slot, it will increment the index 12

THREAD CLASS public class SharedArrayGame extends Thread{ private static final int NI = 100000; private static int a[] = new int[2*ni]; private static int index = 0; private int tid; public SharedArrayGame(int tid){ this.tid = tid; } public void run() { for (int i=0;i < NI; i++) { a[index] = tid; index = index + 1; } } 13

MAIN METHOD public static void main(string[] args){ System.out.println("SharedArrayGame starts"); SharedArrayGame t1 = new SharedArrayGame(1); SharedArrayGame t2 = new SharedArrayGame(2); t1.start(); t2.start(); try{ t1.join(); t2.join(); } catch(interruptedexception e){} System.out.println("SharedArrayGame done"); for(int i=0;i<2*ni;i++){ if(a[i]==1) System.out.println("000"); else if (a[i]==2) System.out.println("- - - - - "); else System.out.println("EEEEEEEE"); } } 14

SHARED DATA MANIPULATION If you execute the program, you should see mix of 000 and ------ being printed, which is expected. But you would also see some EEEEEEEE being printed, specially at the end. We might not have expected this, but this is all logical. What happened is a memory inconsistency. Lets go over a scenario. 15

SHARED DATA MANIPULATION We have two threads and both are running the same code. The problem is they are reading and updating a shared variable. Because we have no protection, reading and updating a shared variable will lead to problem. Consider the code of run: for (int i= 0; i < NI; i++) { a[index] = tid; index = index+1; } 16

SHARED DATA PROBLEM [1] Lets say, thread corresponding the object t1 just finished executing the instruction a[index]=tid; where index is, say 15. But before it can update the value of index, to make it 16, a context switch occurred and thread corresponding the object t2 gets to execute. Because t1 could not update the index, t2 will use the same slot as t1, namely slot 15. As a result, deposit of t1 will be lost. Now, thread t2 will probably execute for a while, say it deposits its tid 10 times. 17

SHARED DATA PROBLEM [2] Value of index becomes 25. Which means, next slot to be filled is 25. A context switch occurs again and t1 gets to execute. But t1 did not finish updating index, last time. So as it comes back to the instruction index=index +1, index becomes 26. Slot corresponding to index 25 never gets filled! and thus we have a printout of EEEEEEEE for that slot. 18

SHARED DATA MANIPULATION Its good that multiple threads gets a share of the processing unit but we may risk correctness of the computation when sharing is involved. The above problem only occurs because threads are being kicked out of the running state to ready state time to time to give other threads a chance to run. We will see how we can bring order to the house. 19

SYNCHRONIZING WITH JAVA If the body of the for loop in the run() method, were executed uninterruptedly, then we would not get any inconsistency with our output. One easy way to do this is with synchronized statement. Any object in java has an implicit intrinsic lock. When a synchronized statement is executed based on an object, its intrinsic lock is acquired by the calling thread. Another thread cannot execute a synchronized statement based on the same object as long as its intrinsic lock is owned by some other thread. 20

JAVA SYNCRONIZATION for (int i= 0; i < NI; i++) { synchronized(o) { } } a[index] = tid; index = index+1; where o is a shared object between thread t1 and t2. We can declare it at the top of our file as: private static Object o = new Object(); 21

SYNCHRONIZED METHOD VS SYNCHRONIZED BLOCK Java programming language provides two basic synchronization idioms: synchronized methods synchronized statements or Block The synchronized keyword helps to build an atomic action, this action cannot stop in the middle. 22

SYNCHRONIZED COUNTER EXAMPLE class Synchronized Counter { private int c = 0; } public synchronized void increment() { c++; } public synchronized void decrement() { c- - ; } public synchronized int value() { return c; } 23

SYNCHRONIZED STATEMENTS Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock: public void addname(string name) { synchronized(this) { lastname = name; namecount++; } namelist.add(name); } 24

DIFFERENCE BETWEEN SYNCHRONIZED BLOCK AND METHOD IN JAVA Two ways to use synchronized keyword in Java and implement mutual exclusion on critical section of code Java provides inbuilt synchronized keyword to achieve synchronization in Java Main difference between synchronized method and synchronized block is selection of lock on which critical section is locked. Though both block and method can be used to provide highest degree of synchronization in Java, use of synchronized block over method is considered as better Java coding practices. 25

DIFFERENCE BETWEEN SYNCHRONIZED BLOCK AND METHOD IN JAVA 1.One significant difference between synchronized method and block is that, Synchronized block generally reduce scope of lock. As scope of lock is inversely proportional to performance, its always better to lock only critical section of code. This improves performance drastically because locking is only required one or two times. 2. Synchronized block provide granular control over lock, as you can use arbitrary any lock to provide mutual exclusion to critical section code. On the other hand synchronized method always lock either on current object represented by this keyword or class level lock, if its static synchronized method. 3. In case of synchronized method, lock is acquired by thread when it enter method and released when it leaves method, either normally or by throwing Exception. On the other hand in case of synchronized block, thread acquires lock when they enter synchronized block and release when they leave synchronized block. 26

WHAT IS THE PROBLEM? 27 27

OUTPUT 28 28

QUESTION 1 OS COMPONENTS Give the name of each layer of the following schema. Provide a short description for each layers. 29

SOLUTION 1 Source: Course slides Lesson 1 30

SOLUTION 1 Hardware Layer: The actual electronic hardware of the computer, including and not restricted to: buses, registers, clock, CPU s Operating System Layer: Manages and controls resources, as well as program execution. Serves as an interface between the user programs and the hardware. System Software Layer: Software that is not part of the operating system, but which is needed for the proper functioning of other software. For example, a graphic API (DirectX) needed to run a specific game on your computer. Application software Layer: Combines the end user programs and applications. Examples: Microsoft Word, Mozilla Firefox, Temple Run 31

QUESTION 2 OS COMPONENTS What is the difference between user mode and system mode? What if the user software need to change mode? Why do we need them? 32

SOLUTION 2 Source: Silberschatz et al. 9 th edition 33

QUESTION 3 OS COMPONENTS What are the resources in this schema? Can an application work with the processor directly? What is the programmer s interface to the resources? What would drive the resources? 34 Source: Course slides Lesson 1

SOLUTION 3 Resources: Processor(s), main memory, peripherals o An application can NOT work directly with the processor or any resource o All applications use the operating system as an interface to the resources o The drivers directly control the resources 35

QUESTION 4 CONTEXT SWITCH Define each step of a context switch Specify the overheads that occur during a context switch and that are not considered in the context switch time. 36

SOLUTION 4 Step 1: Interruption (through a trap) Step 2: OS scheduling is woke-up Step 3: Scheduler store registry (in main memory) associated to the stopped process Step 4: Scheduler look at the ready-queue for the next process to run Step 5: Scheduler restore registry of the next process to run Step 6: Scheduler set the IR registry (next instruction to execute) so the next process will restart exactly where he left Step 7: Scheduler interrupt himself 37

SOLUTION 4 The time that the dispatcher executes to handle the context switch The context switch time between the dispatcher and the other two processes 38

REFERENCES [1] http://download.oracle.com/docs/cd/e17409_01/javase/tutorial/essential/concurrency/syncmeth.html [2] Questions are prepared by:alimohammad Firooz,François Gingras, Tamara Finide Dittimi, 39