1
play

1 User Threads Benefits Responsiveness Thread management done by - PDF document

Contents CMSC 421 Spring 2004 Section 0202 Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Part II: Process Management Linux Threads Chapter 5 Java Threads Threads


  1. Contents CMSC 421 Spring 2004 Section 0202 � Overview � Multithreading Models � Threading Issues � Pthreads � Solaris 2 Threads � Windows 2000 Threads Part II: Process Management � Linux Threads Chapter 5 � Java Threads Threads Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.2 Lightweight Process and Heavyweight Single and Multithreaded Processes 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.3 Operating System Concepts 5.4 1

  2. User Threads Benefits � Responsiveness � Thread management done by a user-level threads library � Interactive program responds to user even when some � Kernel is unaware of user-level threads threads are blocked doing other activities � User-level threads are faster to create and manage � Resource Sharing � However, if a thread is blocked on a system call, the process is blocked too, and none of its other threads � Shared address space, etc continues to run � Economy � Examples � Lower overhead in creating and context switching threads - POSIX Pthreads than processes � context switch is 5 times faster - Mach C-threads � Thread creation is 30 times faster - Solaris 2 threads � Utilization of multi-processor architectures � Multiple threads can run on multiple processors Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.5 Operating System Concepts 5.6 Kernel Threads Multithreading Models � Thread management is supported by the Kernel � Three models for implementing threads � Slower than user threads � Many-to-One � But kernel can schedule another thread when one thread performs a blocking system call � One-to-One � Examples � Many-to-Many - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.7 Operating System Concepts 5.8 2

  3. Many-to-One Model 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.9 Operating System Concepts 5.10 One-to-one Model 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/2000 - OS/2 Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.11 Operating System Concepts 5.12 3

  4. Many-to-Many Model 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.13 Operating System Concepts 5.14 Threading Issues fork() and exec() semantics � Semantics of fork() and exec() system calls � fork() � Thread cancellation � Does it duplicate ALL threads of the forking process? � Two flavors: one that duplicates and one that does not � Signal handling � Exec() � Thread pools � Replaces the whole process � Thread specific data � Including all threads (LWPs) Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.15 Operating System Concepts 5.16 4

  5. Thread Cancellation Signal Handling � Signal => Notify the process of the occurrence of a � Canceling a target thread certain event � Asynchronous cancellation (immediate termination) � Types of signals � Deferred cancellation � Synchronous � Target thread periodically checks if it should terminate � Delivered to the same process that generated the signal � Issues: � Illegal memory access, division by zero, overflow � reclaiming resources of cancelled target thread � Asynchronous � Shared resources with other threads � Generally, delivered to a different process than the one � Cancellation points generating the signal � <control><C>, timer expiry � Signals handled using � Default signal handler (run by the kernel) � User-defined signal handler Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.17 Operating System Concepts 5.18 Signal Handling (Cont.) Thread Pools and Thread-specific Data � Options for delivering signals (depending on signal) � Thread pools � Only to the thread that generated the signal � Creating a large number of threads in a system can exhaust system resources � To all threads of a process � Allocate a pool of thread’s � To all threads not blocking the signal � Allocate available threads from the thread pool to a new � To a specific/dedicated thread “thread” � Threads many choose to block certain signals � 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.19 Operating System Concepts 5.20 5

  6. Pthreads Solaris 2 Threads � Implements the Pthread API + support for user and kernel � a POSIX standard (IEEE 1003.1c) API for thread threads creation, synchronization, and management � Uses LWP to multiplex user threads � API specifies behavior of the thread library, � Implements many-to-many model implementation is up to development of the library. � LWP reside in kernel space � Common in UNIX operating systems � 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.21 Operating System Concepts 5.22 Solaris 2 Threads Solaris Process Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.23 Operating System Concepts 5.24 6

  7. Pthreads Example Windows 2000 Threads #include <pthread.h> #include <stdio.h> int sum = 0; /* shared data of the threads */ � Implements the one-to-one mapping. void *runner(void *p); � Each thread contains int main(int argc, char *argv[]) { pthread_attr_t attr; - a thread id pthread_t tid; pthread_attr_init(&attr); - register set pthread_create(&tid, &attr, runner, argv[1]); /* create a thread and exec runner */ pthread_join(tid, NULL); /* wait for thread to finish exec */ - separate user and kernel stacks printf(“%d\n”, sum); exit(0); } - private data storage area 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.25 Operating System Concepts 5.26 Linux Threads Java Threads � Thread creation is done through clone() system call � Java threads may be created by: � Linux’s trick � Extending Thread class � Implementing the Runnable interface � Store process information in separate structures and use pointers to point to them instead of storing it directly in the � Java threads are managed by the JVM. data structure for the process � Java thread implementation depends on how the JVM is � Clone() allows a child task to share the address space of implemented on the host OS the parent task (process) � Can be one-to-one for JVMs on Windows 2000 etc systems � Linux refers to them as tasks rather than threads . � Can be many-tone on Solaris 2 green thread JVM systems Silberschatz, Galvin and Gagne  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.27 Operating System Concepts 5.28 7

  8. Java Thread Example Java Thread States 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  2002 Silberschatz, Galvin and Gagne  2002 Operating System Concepts 5.29 Operating System Concepts 5.30 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend