CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrent Programming Abstraction & Java Threads
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Interprocess Communication
Multithreading Horstmann ch.9. Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library.
Chapter 9 (Horstmann’s Book) Multithreading Hwajung Lee.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Big Picture Lab 4 Operating Systems Csaba Andras Moritz.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ThreadThread Thread Basics Thread Synchronization Animations.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms May 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CS 152: Programming Language Paradigms April 30 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Distributed and Parallel Processing George Wells.
Interprocess Communication Race Conditions
Multithreading / Concurrency
CS703 – Advanced Operating Systems
COT 4600 Operating Systems Fall 2009
Background on the need for Synchronization
Applied Operating System Concepts -
Multithreaded Programming in Java
Multithreading Chapter 9.
ITEC324 Principle of CS III
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Multithreading Chapter 23.
Lecture 9: Process Synchronization
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Concurrency: Mutual Exclusion and Process Synchronization
Computer Science & Engineering Electrical Engineering
CSCI1600: Embedded and Real Time Software
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Process Synchronization
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
CSCI1600: Embedded and Real Time Software
CS 144 Advanced C++ Programming May 7 Class Meeting
Process Synchronization
Chapter 9 Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 2 CS Department Colloquium  Dr. Richard Stallman President, Free Software Foundation Developer of the GNU operating system MacArthur Foundation fellowship  Talk: “For a Free Digital Society” About the many threats to freedom in the digital society.  Thursday, November 21, 4:30 PM  Washington Square Hall Room 109

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 3 Thread States  Each thread has a state and a priority.  A state can be blocked if it is: sleeping waiting for I/O waiting to acquire a lock waiting for a condition No state for a thread that’s running.

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 4 Activating Threads  The thread scheduler activates a thread whenever: a running thread has completed its time slice a running thread blocks itself a thread with a higher priority becomes available  The thread scheduler determines which thread to run: It selects the highest priority thread that is currently runnable. The Java standard does not specify which thread among the eligible ones should be scheduled.  random selection?  round robin?

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 5 Terminating Threads  A thread terminates “naturally” when its run() method completes.  You can also interrupt a running thread from another thread: The thread can test whether the interrupt flag has been set: Or it can catch the InterruptedException thrown by the Thread.sleep() method when the thread is interrupted.  Terminate the run() method after an interruption. t1.interrupt(); if (Thread.currentThread().isInterrupted()) {... }

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 6 Thread Synchronization Example  Recall the bounded queue object (circular buffer).  Suppose it is shared among several threads.  Two producer threads add messages. One adds “A Message” The other adds “B Message” Each prints the messages as it adds them.  One consumer thread removes messages. Prints the messages as it removes them. _

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 7 Thread Synchronization, cont’d  The run() method of the Producer class: int i = 1; while (i <= count) { if (!queue.isFull()) { Message msg = new Message(index, i, text); queue.add(msg);... i++; } Thread.sleep((int) (DELAY*Math.random())); }

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 8 Thread Synchronization, cont’d  The run() method of the Consumer class: int i = 1; while (i <= count) { if (!queue.isEmpty()) { Message msg = queue.remove();... i++; } Thread.sleep((int) (DELAY*Math.random())); }

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 9 Thread Synchronization, cont’d  Create the queue, and create and start the threads: BoundedQueue queue = new BoundedQueue(10); final int PRODUCER_C0UNT = 2; final int MESSAGE_COUNT = 100; Runnable run1 = new Producer("A Message", queue, 1, MESSAGE_COUNT); Runnable run2 = new Producer("B Message", queue, 2, MESSAGE_COUNT); Runnable run3 = new Consumer(queue, PRODUCER_C0UNT*MESSAGE_COUNT); Thread thread1 = new Thread(run1); Thread thread2 = new Thread(run2); Thread thread3 = new Thread(run3); thread1.start(); thread2.start(); thread3.start(); Demo: queue1

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 10 A Race Condition  What can go wrong with the program? The consumer can’t retrieve all the messages that the producers inserted. The consumer retrieves the same message multiple times. _

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 11 Quiz 2013Nov21

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 12 A Race Condition, cont’d  How can such problems occur? 1. The first producer thread calls add() and executes elements[tail] = newValue; 2. The first producer thread reaches the end of its time slice. 3. The second producer thread calls add() and executes elements[tail] = newValue; tail++; 4. The second producer thread reaches the end of its time slice. 5. The first producer thread executes tail++;  Bad consequences! Step 1 starts to add a message to the queue. Step 3 overwrites the message added in step 1. Step 5 increments the tail index without filling the location, leaving behind garbage.

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 13 "Message B" "Message A"

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 14 A Race Condition, cont’d  A race condition occurs when multiple threads access and modify some shared data, and the final result depends on the order that the threads modified the data. An extremely bad situation. Results are often unpredictable and unrepeatable. Extremely hard to debug. Must avoid! _

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 15 Another Race Condition Example  Suppose variable count is shared.  count++ can be implemented as: register1 = count register1 = register1 + 1 count = register1  count-- can be implemented as: register2 = count register2 = register2 – 1 count = register2  Suppose the value of count is 5 and that thread T1 executes count++ at the same time that thread T2 executes count--.

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 16 Another Race Condition Example ThreadOperationResult T1register1 = countregister1 = 5 T1register1 = register1 + 1register1 = 6 T2register2 = countregister2 = 5 T2register2 = register2 - 1register2 = 4 T1count = register1count = 6 T2count = register2count = 4  Whether count ends up equal to 6 or 4 depends on the order of the last two instructions. _

SJSU Dept. of Computer Science Fall 2013: November 21 CS 151: Object-Oriented Design © R. Mak 17 Critical Region  One way to avoid race conditions is to prevent more than one thread from reading and writing the shared data at the same time. This is called mutual exclusion. Thread schedulers provide different primitives to achieve mutual exclusion.  A critical region (AKA critical section) is the part of a program’s code that operates on some shared data. We need to prevent two threads from accessing that shared data at the same time. Therefore, we need to prevent two threads from being in their critical regions at the same time.