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.

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.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Multithreaded Programming
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.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
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.
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.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
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.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
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.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
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.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
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 ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

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 within a program It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals A traditional (or heavyweight) process has a single thread of control If a process has multiple threads of control, it can perform more than one task at a time. Threads are a way for a program to split itself into two or more simultaneously running tasks. That is the real excitement surrounding threads

Single and Multithreaded Processes

Thread Examples A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background

Thread Example: Multithreaded Server Architecture

Single-Threaded Example Imagine the following C program main() { ComputePI( “ pi.txt ” ); PrintClassList( “ clist.text ” ); } What is the behavior here?

Use of Threads Version of program with Threads main() { CreateThread(ComputePI( “ pi.txt ” )); CreateThread(PrintClassList( “ clist.text ” )); } What does “ CreateThread ” do? – Start independent thread running given procedure What is the behavior here? – This should behave as if there are two separate CPUs CPU1CPU2CPU1CPU2 Time CPU1CPU2

Memory Footprint of Two-Thread Example If we stopped this program and examined it with a debugger, we would see – Two sets of CPU registers – Two sets of Stacks Code Global Data Heap Stack 1 Stack 2 Address Space

Per Thread State Each Thread has a Thread Control Block (TCB) – Execution State: CPU registers, program counter, pointer to stack – Scheduling info: State (more later), priority, CPU time – Accounting Info – Various Pointers (for implementing scheduling queues) – Pointer to enclosing process (PCB) – Etc

Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures

Multithreading Models Support for threads may be provided either at the user level, for user threads (supported above the kernel and managed without kernel support), or by the kernel, for kernel threads (supported and managed directly by the OS) Three common ways of establishing relationship between user and kernel threads – Many-to-One – One-to-One – Many-to-Many

User Threads Thread management done by user-level threads library Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads

Kernel Threads Supported by the Kernel Examples –Windows XP/2000 –Solaris –Linux –Tru64 UNIX –Mac OS X

Many-to-One (User-Level Threads) Many user-level threads mapped to single kernel thread

Many-to-One (User-Level Threads) Basically, the kernel is not aware of the existence of threads. Thread switching does not require kernel mode privileges and scheduling is application specific. Thread management is done by the thread library in user space, so it is efficient Just as a uniprocessor provides the illusion of parallelism by multiplexing multiple processes on a single CPU, user-level threads packages provide the illusion of parallelism by multiplexing multiple user threads on a single kernel thread

Many-to-One (User-Level Threads) Since there is only one kernel thread, if a user thread executes a blocking system call, the entire process blocks, since no other user thread can execute until the kernel thread (which is blocked in the system call) becomes available Multithreaded programs will run no faster on multiprocessors than they run on uniprocessors. The single kernel thread acts as a bottleneck, preventing optimal use of the multiprocessor

Many-to-One (User-Level Threads) Advantages – Thread switching does not involve kernel  no mode switching – Scheduling can be application specific  choose best algorithm – ULTs can run on any OS  only needs a thread library Disadvantages – Most system calls are blocking and the kernel blocks processes  all threads within the process will be blocked – Kernel can only assign processes to processors  threads within same process cannot run simultaneously on processors

One-to-One (Kernel-Level Threads) Each user-level thread maps to kernel thread

One-to-One (Kernel-Level Threads) Because each kernel thread is actually a different kernel-schedulable entity, multiple threads can run concurrently on different processors – Can achieve significant speedups when migrated from uniprocessors to multiprocessors Unlike the many-to-one model, threads blocking in the kernel do not impede process progress under the one-to-one model. When one user thread and its kernel thread block, the other user threads can continue to execute since their kernel threads are unaffected The only drawback is that creating a user thread requires creating the corresponding kernel thread – Overhead of creating kernel threads can burden the performance of the application

Many-to-Many Model Allows many user-level threads to be mapped to many kernel threads Idea is to combine the best of both approaches

Solaris combines both ULT and KLT

Thread Libraries A thread library provides the programmer with an API for creating and managing threads Two primary ways of implementing a thread library – Provide the library entirely in user space with no kernel support. All code and data structures for the library exist in user space. Invoking a function in the library results in a local function in user space and not a system call – Implement kernel-level library supported directly by the OS. In this case, code and data structures for the library exist in kernel space. Invoking a function results in a system call to the kernel – Three main thread libraries in use today: (1) POSIX Pthreads, (2) Win32, and (3) Java

Pthreads Example: Design a multithreaded program that performs the following summation in a separate thread N sum =  i i = 0

Pthreads #include int sum; //this data is shared by the thread(s) void *runner(void *param); // the thread main(int argc, char* argv[]) { pthread_t tid; // the thread identifier pthread_create(&tid,NULL,runner,argv[1]); // create thread pthread_join(tid,NULL); // now wait for the thread to exit printf("sum = %d\n",sum); }

Pthreads void *runner(void *param) { int upper = atoi(param); int i; sum = 0; if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }

Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread –Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

Thread Cancellation Terminating a thread before it has finished Two general approaches: –Asynchronous cancellation terminates the target thread immediately –Deferred cancellation allows the target thread to periodically check if it should be cancelled

Semantics of fork() and exec() If one thread in a program calls fork(), does the new process duplicate only the calling thread or all threads? – Some UNIX systems provide two versions of fork() – One duplicates all threads – The other duplicates only the thread that invoked fork() The exec() system call typically works in the same way as described before. That is, if a thread invokes exec(), the program specified in the parameter to exec() will replace the entire process – including all threads

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals  Signal is generated by particular event  Signal is delivered to a process  Signal is handled Options: –Deliver the signal to the thread to which the signal applies –Deliver the signal to every thread in the process –Deliver the signal to certain threads in the process –Assign a specific thread to receive all signals for the process

Thread Pools Create a number of threads in a pool where they await work Advantages: –Usually slightly faster to service a request with an existing thread than create a new thread –Allows the number of threads in the application(s) to be bound to the size of the pool

Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool)

Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library This communication allows an application to maintain the correct number kernel threads

Operating System Examples Windows XP Threads Linux Thread

Windows XP Threads

Linux Threads

Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains –A thread id –Register set –Separate user and kernel stacks –Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: –ETHREAD (executive thread block) –KTHREAD (kernel thread block) –TEB (thread environment block)

Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)