CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 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.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
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.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
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.
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.
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. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
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.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
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 Outline n Overview n Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
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.
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
Nadeem MajeedChoudhary.
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:

CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Contents Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Lightweight Process and Heavyweight Process Lightweight Process (LWP) or thread  Basic unit of CPU control  Typically has private –Id, PC, register set, stacks, local storage  Shares OS resources with containing process –Address space (Code section, data section), open files, etc Heavyweight Process (HWP)  Single thread

Silberschatz, Galvin and Gagne  Operating System Concepts Single and Multithreaded Processes

Silberschatz, Galvin and Gagne  Operating System Concepts Benefits Responsiveness  Interactive program responds to user even when some threads are blocked doing other activities Resource Sharing  Shared address space, etc Economy  Lower overhead in creating and context switching threads than processes  context switch is 5 times faster  Thread creation is 30 times faster Utilization of multi-processor architectures  Multiple threads can run on multiple processors

Silberschatz, Galvin and Gagne  Operating System Concepts User Threads Thread management done by a user-level threads library  Kernel is unaware of user-level threads  User-level threads are faster to create and manage  However, if a thread is blocked on a system call, the process is blocked too, and none of its other threads continues to run Examples - POSIX Pthreads - Mach C-threads - Solaris 2 threads

Silberschatz, Galvin and Gagne  Operating System Concepts Kernel Threads Thread management is supported by the Kernel  Slower than user threads  But kernel can schedule another thread when one thread performs a blocking system call Examples - Windows 95/98/NT/ Solaris - Tru64 UNIX - BeOS - Linux

Silberschatz, Galvin and Gagne  Operating System Concepts Multithreading Models Three models for implementing threads  Many-to-One  One-to-One  Many-to-Many

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-One Model

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-One Many user-level threads are mapped to a single kernel thread. Multiple threads CANNOT run in parallel in a multiprocessor system A blocked thread blocks its process Used on systems that do not support kernel threads. Example  Solaris 2 Green Threads Library

Silberschatz, Galvin and Gagne  Operating System Concepts One-to-one Model

Silberschatz, Galvin and Gagne  Operating System Concepts One-to-One Each user-level thread maps to kernel thread. Can burden OS and slowdown application when many threads are created (due to kernel overhead) Examples - Windows 95/98/NT/ OS/2

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-Many Model

Silberschatz, Galvin and Gagne  Operating System Concepts Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads, and map user threads to them Addresses the shortcomings of the many-to-one and one- to-one models Examples  Solaris 2  Windows NT/2000 with the ThreadFiber package

Silberschatz, Galvin and Gagne  Operating System Concepts Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data

Silberschatz, Galvin and Gagne  Operating System Concepts fork() and exec() semantics fork()  Does it duplicate ALL threads of the forking process?  Two flavors: one that duplicates and one that does not Exec()  Replaces the whole process  Including all threads (LWPs)

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Cancellation Canceling a target thread  Asynchronous cancellation (immediate termination)  Deferred cancellation  Target thread periodically checks if it should terminate Issues:  reclaiming resources of cancelled target thread  Shared resources with other threads Cancellation points

Silberschatz, Galvin and Gagne  Operating System Concepts Signal Handling Signal => Notify the process of the occurrence of a certain event Types of signals  Synchronous  Delivered to the same process that generated the signal  Illegal memory access, division by zero, overflow  Asynchronous  Generally, delivered to a different process than the one generating the signal , timer expiry Signals handled using  Default signal handler (run by the kernel)  User-defined signal handler

Silberschatz, Galvin and Gagne  Operating System Concepts Signal Handling (Cont.) Options for delivering signals (depending on signal)  Only to the thread that generated the signal  To all threads of a process  To all threads not blocking the signal  To a specific/dedicated thread Threads many choose to block certain signals

Silberschatz, Galvin and Gagne  Operating System Concepts Thread Pools and Thread-specific Data Thread pools  Creating a large number of threads in a system can exhaust system resources  Allocate a pool of thread’s  Allocate available threads from the thread pool to a new “thread”  Reduces thread creation time when a request arrives Thread-specific data  Need for supporting private storage for threads that need to manage their own private data

Silberschatz, Galvin and Gagne  Operating System Concepts Pthreads a POSIX standard (IEEE c) API for thread creation, synchronization, and management API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems

Silberschatz, Galvin and Gagne  Operating System Concepts Solaris 2 Threads Implements the Pthread API + support for user and kernel threads Uses LWP to multiplex user threads  Implements many-to-many model LWP reside in kernel space Allocates a kernel thread to each LWP User threads can be bound to a LWP or can be unbound Each user thread contains  Thread ID, register set (PC and stack pointer), stack,, and priority Each LWP contains  Register set for running user thread, stack, memory, and accounting info

Silberschatz, Galvin and Gagne  Operating System Concepts Solaris 2 Threads

Silberschatz, Galvin and Gagne  Operating System Concepts Solaris Process

Silberschatz, Galvin and Gagne  Operating System Concepts Pthreads Example #include int sum = 0; /* shared data of the threads */ void *runner(void *p); int main(int argc, char *argv[]) { pthread_attr_t attr; pthread_t tid; pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); /* create a thread and exec runner*/ pthread_join(tid, NULL); /* wait for thread to finish exec */ printf(“%d\n”, sum); exit(0); } void *runner(void *param) { int n = 0, i; n = atoi(param); sum = 0; for(i=0; i;<n; i++) sum += i; pthread_exit(0); }

Silberschatz, Galvin and Gagne  Operating System Concepts Windows 2000 Threads Implements the one-to-one mapping. Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area

Silberschatz, Galvin and Gagne  Operating System Concepts Linux Threads Thread creation is done through clone() system call Linux’s trick  Store process information in separate structures and use pointers to point to them instead of storing it directly in the data structure for the process Clone() allows a child task to share the address space of the parent task (process) Linux refers to them as tasks rather than threads.

Silberschatz, Galvin and Gagne  Operating System Concepts Java Threads Java threads may be created by:  Extending Thread class  Implementing the Runnable interface Java threads are managed by the JVM. Java thread implementation depends on how the JVM is implemented on the host OS  Can be one-to-one for JVMs on Windows 2000 etc systems  Can be many-tone on Solaris 2 green thread JVM systems

Silberschatz, Galvin and Gagne  Operating System Concepts Java Thread Example class Summation extends Thread { private int bound = 0; public Summation(int n) { bound = n; } public void run() { int sum = 0; for(int I=0; I<bound; I++) sum += I; System.out.println(“Sum = “ + sum); } public class Test { public static void main(String[] args) { Summation thr = new Summation(Integer.parseInt(args[0]); thr.start(); }

Silberschatz, Galvin and Gagne  Operating System Concepts Java Thread States