The Zen of Threads. 3 Types of Execution Single, lone process doing all the work No problems with multiple-access Can be inconvenient/inefficient -- blocking,

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Chap 2 System Structures.
Operating-System Structures
Computer Systems/Operating Systems - Class 8
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Parallel Programming and Synchronization P&H Chapter 2.11.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
Ceng Operating Systems Chapter 2.5 : Threads Process concept  Process scheduling  Interprocess communication  Deadlocks  Threads.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
The Joy of Synchronization Lecture 19, Nov 5. Administrivia Reminder: Schedule P3M1 with us Only 3 groups scheduled so far… Time slots running out...
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...
The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread... Lecture 18, Nov 03.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Processes & Threads Emery Berger and Mark Corner University.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
1 Confidential Enterprise Solutions Group Process and Threads.
Threads G.Anuradha (Reference : William Stallings)
Threads Rethreaded. Execution of Synchronized Sections Thread A enters synch section 1. A checks for lock; points lock to self 2. Enters code segment.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Java Thread and Memory Model
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 Chapter 2.5 : Threads Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication  Interprocess communication.
Threads. Readings r Silberschatz et al : Chapter 4.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Tutorial 2: Homework 1 and Project 1
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Introduction to threads
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Processes and Threads Processes and their scheduling
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Processes in Unix, Linux, and Windows
ICS 143 Principles of Operating Systems
More examples How many processes does this piece of code create?
Processes in Unix, Linux, and Windows
System Structure and Process Model
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Processes and Threads.
Thread Implementation Issues
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads and Concurrency
Threads Chapter 4.
Jonathan Walpole Computer Science Portland State University
Threads and Concurrency
Lecture 6: Multiprogramming and Context Switching
Processes in Unix, Linux, and Windows
CS703 – Advanced Operating Systems
CS Introduction to Operating Systems
Presentation transcript:

The Zen of Threads

3 Types of Execution Single, lone process doing all the work No problems with multiple-access Can be inconvenient/inefficient -- blocking, single-user, etc. Multiple processes, possibly communicating via sockets/files/FIFOs (traditional UNIX model) More efficient; no blocking, >1 user, etc. Hard to share data (all in terms of messages) No synchronization problems Threads Efficient; no blocking, >1 user, etc. Synchronization & data sharing tricky

The Weakness of One Process public class mySwingApp { public static void main(String[] args) { mySwingApp app=new mySwingApp(args); app._init(); } private void _init() { JFrame jf=_buildGui(); jf.pack(); jf.setVisible(true); } // more GUI initialization private class _buttonHandler { public void actionPerformed(ActionEvent e) { if (e.getCommand().equals(“Connect to Web Site”)) { // look up web site; read data from it; store // data in memory }

The Weakness of One Process Problem 1: If any subroutine takes a long time, everything else (including UI) will have to wait for it (blocking) Typical blocking operations I/O Network communication expensive computation (PuzzleMuncher) Problem 2: no support for multiuser app/OS Pure batch mode Can’t have >1 person talk to DB/web page/etc. at a time

The Multi-Process Model Process (in UNIX sense) is a separate program, running in its own memory space, independent from every other program/process Process has its own copy of code, its own PC, own stack & heap, etc. Separation enforced by kernel & hardware All resource sharing passes through kernel Sockets, files, pipes, FIFOs, etc. New proc created (in UNIX) w/ fork()

The Multi-Process Model memory proc 0 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... PC text stack obj0 obj1 objN heap proc 2 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... PC text stack obj1 objN heap proc 1 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... PC text stack obj0 obj1 objN heap objN-1

Inter-Process Communication memory proc 0 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... PC text stack obj0 obj1 objN heap proc 1 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... PC text stack obj0 obj1 objN heap objN-1 pipe.write() pipe.read() pipe.write()

The Multi-Thread Model Threads run within a single process Each thread has its own Program Counter Registers Stack (subroutine local vars) All threads share Memory (all objects in common) Program code (same methods/subroutines) No kernel/hardware separation (!) -- all separation done by programmer (you!)

The Multi-Thread Model memory proc 0 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... text obj0 obj1 objN heap stack PC registers thread 0 stack PC registers thread 1 stack PC registers thread 2 obj3 obj2 objN-1 objN-3 objN-2

Inter-Thread Communication memory proc 0 addi $0, 3 bne $3, $4, 23 nop mult $7, $19 mflo $3 sw $3, $11... text obj0 obj1 objN heap stack PC registers thread 0 stack PC registers thread 1 stack PC registers thread 2 obj3 obj2 objN-1 objN-3 objN-2

The Joy & Danger of Threads All threads share same data Communication is trivial All threads share same data Easy to stomp on another thread’s data! Two threads can write to same location at same time, or one can write & one can read Can violate pre/post conditions of methods => Inconsistent data state Have to synchronize accesses to ensure only one thread is meddling w/ important data at a time

Example Thread Data Corruption private int[] _multArray[] = new int[128]; private constructor() { for (int i=0;i<_multArray.length;++i) { _multArray[i]=i; } private void _seedArray(int s) { for (int i=0;i<_multArray.length;++i) { _multArray[i]=i*s; } private void _writeToFile() { _myFile.print(“_multArray=[ “); for (int i=0;i<_multArray.length;++i) { _myFile.print(_multArray[i] + “ “); } _myFile.println(“]”); }

Example Thread Data Corruption _multArray After constructor() After _seedArray(2) After _seedArray(3) _multArray

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 1 runs and gets to here...

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 0 runs and gets to here...

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 2 runs and gets to here... _myFile: “_multArray=[ “

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 0 runs and gets to here... _myFile: “_multArray=[ “

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 2 runs and gets to here... _myFile: “_multArray=[ “

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 1 runs and gets to here... _myFile: “_multArray=[ “

Example Thread Data Corruption _multArray After constructor() Thread 0 calls _seedArray(2); Thread 1 calls _seedArray(3); Thread 2 calls _writeToFile(); _multArray Thread 2 completes... _myFile: “_multArray=[ ]“

Synchronization Problem is that you can’t control when/how long each thread runs What you can do is ensure that the threads don’t work with the same data at the same time Defn: Critical section -- one or more pieces of code that should not be invoked by >1 thread simultaneously Defn: Mutual exclusion -- only one thread can be executing this block at a time Defn: Lock -- data struct used to ensure mutual exclusion w/in critical sections

Synchronization in Java Java provides synchronized keyword synchronized method: only one thread can execute in this method at a time synchronized block: only one thread in this block at a time Synchronization uses (invisible) lock assoc. w/ object Normal method -- the invoking object Static method -- the global “class” obj Block -- the argument obj to that block