The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

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.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Processes CSCI 444/544 Operating Systems Fall 2008.
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,
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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...
Threads CSCI 444/544 Operating Systems Fall 2008.
Oh what a tangled web we weave when first to thread we do conceive.
Inner Classes & the True Nature of Static. Administrivia Today: R2 Next Wed (Mar 30): Quiz 3 Multithreading and synchronization JDK java.lang.Thread,
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Oh what a tangled web we weave when first to thread we do conceive.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
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.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
QNX – A real-time operating system PRANSHU GUPTA CS550.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Threads G.Anuradha (Reference : William Stallings)
Processes Introduction to Operating Systems: Module 3.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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.
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.
CSE 466 – Fall Introduction - 1 User / Kernel Space Physical Memory mem mapped I/O kernel code user pages user code GPLR virtual kernel C
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Threads. Readings r Silberschatz et al : Chapter 4.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Processes and threads.
Thread Programming.
Process Management Presented By Aditya Gupta Assistant Professor
Chapter 4 Threads.
Nachos Threads and Concurrency
Operating Systems Processes and Threads.
ICS 143 Principles of Operating Systems
Threads and Data Sharing
More examples How many processes does this piece of code create?
Operating Systems Threads ENCE 360.
Threads Chapter 4.
Threads and Concurrency
Threads Chapter 4.
Prof. Leonardo Mostarda University of Camerino
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
Chapter 4 Threads, SMP, and Microkernels
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Chapter 3: Process Concept
Operating Systems Concepts
Threads CSE 2431: Introduction to Operating Systems
CS Introduction to Operating Systems
Presentation transcript:

The Zen of Threads When you can snatch the lock from the object, grasshopper, then you are ready to spawn the thread...

Time keeps on rollin’... Last time: Q3 P3 handed out This time: P3 grading Fail fast principle Threads vs. procs Synchronization Design exercise (?)

Design principle o’ the day Want a bug to show up as soon as possible As close to the creation of the bug as you can Bug should produce (highly) visible evidence Helps debugging -- bug isn’t hidden by many layers of propagation Fail fast principle: design so that bugs cause failures immediately -- as close to source as possible

Fail fast example Use illegal default values when you don’t know the correct final value for a thing private int _arrMax=0; vs. private int _arrMax=-1; Also shows up in java.util.*.iterator() Most built-in JDK iterators are fail fast -- die if you change the collection out from under them

Fail fast iterator public class MyCollection implements Iterable { private Object _data[]; private int _modCount=0; public void add(Object o) { ++_modCount;... } private class _myIter implements Iterator { private int _iterModCount=_modCount; public Object next() { if (_iterModCount!=_modCount) { throw new ConcurrentModificationException(); }... }

Three models of execution Single process, running alone Multiple access? No worries Can be inconvenient/inefficient -- single user, blocking, etc. Multiple procs, comm via sockets/pipes/etc. More efficient: no blocking, >1 user, etc. Data sharing: hard -- all via messages Little synchronization problem

Three modes of execution Threads Called “light weight processes” Efficient: >1 user, no blocking, efficient use of resources Data sharing: shared memory Multiple threads read/write same memory space Synchronization: very tricky

Multi-process model Process (UNIX sense): Separate program, running in own memory space, independent of all other programs/processes on computer Each process has own copy of: Program instructions/code (“text” segment) Program counter (PC) Registers Method local variables (stack) Object local variables (heap)

Multi-process model

Multi-process commun. Process separation enforced by kernel & hardware Communication via kernel mechanisms File I/O Sockets Pipes FIFOs etc. New process created via: fork() (UNIX system call) Process.start() or Runtime.exec() (Java)

Inter-process comm. (IPC)

Multi-thread model Threads run within a single process Each thread has its own Program counter (PC) Registers Method-local variables (stack) All threads in a single process share: Program instructions/code (text) Object local variables/“main memory” (heap) No kernel/hardware separation! All separation is done by you!

Multi-thread model

Inter-thread communication

The joy/hazards of threads All threads share same memory Communication is trivial All threads share same memory Incredibly easy to stomp on another thread’s data! Two threads can write to same data at same time, or can read/write same data at same time Inconsistent data state Violates pre/post conditions & expectations There be monsters...

Synchronization Can’t control when/how long each thread runs Can ensure that threads don’t work on same data at same time Definition: critical section Segment of code that should not be invoked by >1 thread at a time Definition: Mutual exclusion Only 1 thread can be executing block at a time Definition: Lock Data struct used to ensure mutual exclusion w/in critical section

The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done

The truth of synchronized public class Panopticon { private double[] _data; public synchronized void set(int idx, double v) { _data[idx]=v; } public synchronized double get(int idx) { return _data[idx]; }

The truth of synchronized public class Panopticon { private double[] _data; hidden Object _lockID_=null; public void set(int idx, double v) { while (!_testAndSet_(_lockID_==null || _lockID_==this,this)); _data[idx]=v; _atomicSet_(_lockID,null); } public double get(int idx) { while (!_testAndSet_(_lockID_==null || _lockID_==this,this)); int result=_data[idx]; _atomicSet_(_lockID_,null); return result; }

The truth of synchronized Java synchronized keyword just means: get lock before proceeding; release lock when done synchronized method: only one thread can execute this method at a time (*) synchronized block: only one thread can execute this block at a time (*)