Chapter 5: Threads Overview. Multithreading Models. Threading Issues. Thread Pools. Java ThreadLocal. Pthreads. Solaris 2 Threads. Java Threads.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
1 OS Review Processes and Threads Chi Zhang
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models(多线程模型)
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Chapter 5: Threads Overview. Multithreading Models. Threading Issues. Thread Pools. Java ThreadLocal. Pthreads. Solaris 2 Threads. Java Threads.

Single and Multithreaded Processes

Benefits Responsiveness: can still type in Word (main thread) while Word formats document for printing in other thread. Resource Sharing: all threads share same process memory. Economy: context-switching less expensive (e.g. page table registers need not be saved). Utilization of Multi-Processor Architectures: different threads, different CPUs.

User vs. Kernel Threads Pure user threads:  Thread management done by user-level library, i.e. kernel has no awareness application is multi-threaded.  No pre-emption: one thread has to willingly relinquish CPU and let other go ahead. To kernel, switch looks like process just did a jump.  If one thread blocks, kernel suspends process, so all other threads can’t move forward either. Pure kernel threads:  Managed by kernel.  Thread can be pre-empted if overusing CPU.  If thread blocks; other threads can proceed. Pure user threads are cheap: no context-switching, no kernel data.

Multithreading Models OS offers both user and kernel threads. Apps always work with user-level thread API which may relate to kernel threads in one of three ways:  Many-to-One: no kernel threads, only pure user threads; see previous slide for discussion.  One-to-One: each user thread is a shallow shell around a single kernel thread; see previous slide for discussion.  Many-to-Many: use as many kernel threads as needed, e.g. create one if a user thread blocks. Most efficient.

Threading Issues Semantics of fork() and exec() system calls:  Does fork() duplicate all threads in new process?  Does exec() destroy all threads and overwrite complete process? Thread cancellation:  Asynchronous: Java stop().  Synchronous: thread checks “should I quit?” at cancellation points. Preferred because thread can release resources. Signal handling: which thread gets signal?  Signal is to user processes as are interrupts for kernels: asynchronous notification.  Some go to all threads (e.g. Ctrl+C to terminate).  UNIX: first thread that is not blocking (not masking).  Solaris 2: special thread to handle signals.

Threading Pools If database server starts one thread for each transaction, then on busy day, disk thrashes: disk heads spend most time moving between cylinders, not serving data. Solution: create pool with 10 threads, assign to transactions. If more transactions than threads, queue them up or refuse service. Thread reuse also reduces overhead: no thread creation/deletion (very expensive for kernel threads).

Java ThreadLocal Local data to each thread (besides stack variables): class TransactionRunner { ThreadLocal mPriority=new ThreadLocal(); void setPriority(int p) {mPriority.set(new Integer(p)); } void exec(Transaction t) { int p=((Integer)(mPriority.get())).intValue(); /* Transaction t is executed based on thread priority p. */ } } class ClientSaleProcessor extends Thread { static Object sLock=new Object(); static int sNext=0; static TransactionRunner sRunner=new TransactionRunner(); public void run() { int p; synchronized (sLock) { p=sNext++; } sRunner.setPriority(p); /* All calls to sRunner.exec() by ANY method EVER called by this thread will receive priority p. */ } } No need to pass p around to every call during execution of this thread. ThreadLocal is like Hashtable with implicit key:  set(x) is like put(Thread.currentThread(),x);  get() is like get(Thread.currentThread());  Unlike Hashtable, entry automatically removed when thread ends.

Pthreads A POSIX standard (IEEE c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to development of the library. Often user-level. Common in UNIX operating systems.

Solaris 2 Threads

Solaris Process

Java Threads Java thread creation:  Extend Thread class: class MyThread extends Thread { public void run() { /* computation */ } } // Creation. (new MyThread()).start(); // NOT.run()  Implementing the Runnable interface class MyRunnable implements Runnable { public void run() { /* computation */ } } // Creation. (new Thread(new MyRunnable())).start();  Necessary because Java disallows multiple parents. Java threads are managed by the JVM: can be one-to- one or any other model. Key element of assignment 1.

Java Thread States Deprecated:  suspend().  resume().  stop(). Why?  suspend(), stop() prevent thread from releasing its resources.  resume() deprecated because it is inverse of suspend().  See Thread class javadoc.