Source: Operating System Concepts by Silberschatz, Galvin and Gagne.

Slides:



Advertisements
Similar presentations
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Advertisements

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.
5.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Multithreaded Programming. 4.2 Multithreaded Programming n Overview n Multithreading Models n Thread Libraries n Threading Issues n Operating.
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.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Thread Libraries  Pthreads  Windows.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
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  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.
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.
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  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
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.
Chapter 4: Threads.
Chapter 4:Threads Book: Operating System Principles , 9th Edition , Abraham Silberschatz, Peter Baer Galvin, Greg Gagne.
Presentation transcript:

Source: Operating System Concepts by Silberschatz, Galvin and Gagne. Threads Source: Operating System Concepts by Silberschatz, Galvin and Gagne. CSC 4103: Operating System

Overview Process can have a single thread of control or activity multiple threads of control or activity. A thread is a flow of control within a process a basic unit of CPU utilization – also called a LWP thread ID, program counter, register set and stack all threads share the same address space of their process. Multithreaded computer systems are common. e.g., desktop PCs Web browser can have two threads, one for display and the other for data retrieving. Pthreads Win32 Threads Java Threads CSC 4103: Operating System

Single and Multithreaded Processes Threads belonging to a given process share with each other code section, data section and other resources, e.g., open files. CSC 4103: Operating System

Benefits Increased responsiveness to user: Resource Sharing Economy A program continues running with other threads even if part of it is blocked or performing a lengthy operation in one thread. Resource Sharing Threads share memory and resources of their process. Economy Less time consuming to create and manage threads than processes as threads share resources, e.g., thread creating is 30 times faster than process creating in Solaris. Utilization of Multiprocessor Architectures Increases concurrency because each thread can run in parallel on a different processor. CSC 4103: Operating System

Thread Types User Threads Threads are implemented at the user level by a thread library Library provides support for thread creation, scheduling and management. User threads are fast to create and manage. Kernel Threads Supported and managed directly by the OS. Thread creation, scheduling and management take place in kernel space. Slower to create and manage. Relationship between user threads and kernel threads. CSC 4103: Operating System

Multithreading Models Three common ways of establishing a relationship between user-level threads and kernel-level threads Many-to-One One-to-One Many-to-Many CSC 4103: Operating System

Many-to-One Many user-level threads mapped to single kernel thread. Easier thread management. Blocking-problem. No concurrency. Examples: Green threads for Solaris CSC 4103: Operating System

One-to-One Each user-level thread maps to a kernel thread. Overhead of creating kernel threads, one for each user thread. No blocking problem Provides concurrency. Examples: Linux, family of Windows CSC 4103: Operating System

Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. Allows the OS to create a sufficient number of kernel threads. Users can create as many as user threads as necessary. No blocking and concurrency problems. Two-level model. CSC 4103: Operating System

Threading Issues – fork and exec Change in semantics of fork() and exec() system calls. Two versions of fork system call: One duplicates only the thread that invokes the call. Another duplicates all the threads, i.e., duplicates an entire process. exec system call: Program specified in the parameters to exec will replace the entire process – including all threads. If exec is called immediately after forking, duplicating all threads is not required. CSC 4103: Operating System

Cancellation Task of terminating a thread before it has completed. Canceling one thread in a multithreaded searching through a database. Stopping a web page from loading. Asynchronous cancellation One thread immediately terminates the target thread (one to be canceled). Deferred cancellation The target thread can periodically check if it should terminate, allowing a normal exit. CSC 4103: Operating System

Signal Handling Signal to notify a process that a particular event has occurred. Default or user defined signal handler. Synchronous signal is related to the operation performed by a running process. Illegal memory access or division by zero. Asynchronous signal is caused by an event external to a running process. Terminating a process (<control><C>) or a timer expires. Options for delivering signals in a multithreaded process: Signal to the thread to which the signal applies. Signal to all threads. Signal to certain threads. Signal to a specific thread. CSC 4103: Operating System

Thread Pools Create a number of threads at process startup and place them into a pool where they sit and wait for work. e.g. for multithreading a web server. A thread from the pool is activated on the request, and it returns to the pool on completion. Benefits of thread pool: Faster service Limitation on the number of threads, according to the need. Thread-pool-architecture allows dynamic adjustment of pool size. CSC 4103: Operating System

Specific Data Certain data required by a thread. These data are not shared with the other threads. Example of thread-specific data: In a transaction processing system, different transaction services will be provided by different threads. CSC 4103: Operating System

Scheduler Activations Communication between the user-thread library and the kernel threads. An intermediate data structure known as LWP. User thread runs on a virtual processor (LWP) Corresponding kernel thread runs on a physical processor Each application gets a set of virtual processors from OS Application schedules threads on these processors Kernel informs an application about certain events issuing upcalls which are handled by thread library. CSC 4103: Operating System

Pthreads The POSIX standard defining API for thread creation and synchronization. A set of C language programming types and procedure calls. Implemented with pthread.h header/include file and a thread library. Common in UNIX operating systems. CSC 4103: Operating System

Multithreading with Pthread API Two threads: initial thread in main function and a new thread performing summation in runner function # include <pthread.h> void *runner(void *param); main (int argc, char *argv[1]){ pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&tid, &attr, runner, argv[1]); pthread_join(tid, Null); } CSC 4103: Operating System

Multithreading with Pthread API (Contd.) The new thread begins control in the runner function to perform summation of a non-negative integer. 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); CSC 4103: Operating System

Java Threads Summation thrd = new Summation(); thrd.start(); Create a new thread that is derived from the Thread class: Summation thrd = new Summation(); Override the run method of the Thread class by calling start method thrd.start(); Two threads: one starts execution of main method. second begins execution in its run method. CSC 4103: Operating System

Win32 Threads: Windows XP Implementation Window XP implements the Win32 API and one-to-one and many-to-many mappings. Data structures of a thread: ETHREAD (executive thread block) Pointer to the belonging process Pointer to the corresponding KTHREAD Address to the routine in which the thread starts control. KTHREAD (kernel thread block) Scheduling and synchronization information Kernel stack Pointer to TEB. TEB (thread environment block) User-space data structure for user-level thread User stack Array of thread-specific data (called thread-local storage). CSC 4103: Operating System

Summary A thread is a flow of control within the process. A process can have several different flows of control or activity within the same address space. Multithreading benefits - increased responsiveness, resource sharing, economy and concurrency. User level threads are visible to programmer and are unknown to kernel – a thread library manages them. Kernel level threads are supported by OS. Three different models: many-to-one, one-to-one, and many-to-many. Multithreading is challenging: many thread-specific issues. Thread libraries: Pthreads, Win 32 threads and Java threads. CSC 4103: Operating System