Chapter 4 Threads “Most modern applications are multithreaded”

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.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
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.
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.
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 &
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.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
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.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
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.
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.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
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.
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.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Operating System Concepts 7th Edition Abraham SilBerschatz Peter Baer Galvin Greg Gagne Prerequisite: CSE212.
Chapter 3 Threads and Multithreading
Threads CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
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 Bashair Al-harthi OPERATING SYSTEM
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4:Threads Book: Operating System Principles , 9th Edition , Abraham Silberschatz, Peter Baer Galvin, Greg Gagne.
Presentation transcript:

Chapter 4 Threads “Most modern applications are multithreaded” Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues Operating-System Examples “Most modern applications are multithreaded”

4.1 Overview A web browser might have one thread display images or texts, another retrieves data from the network A server creates one thread for each request

Thread = light-weight process (LWP) 4.1 Overview Thread = light-weight process (LWP) Basic unit of CPU utilization Consists of a thread ID, a program counter, a register set, and a stack space Shares with peer threads its code section, data section, and OS resources (open files, signals), collectively known as a task Threads reside in the same address space and have access to the same data (one alters data; others see) One thread opens a file for reading; others can also read from that file Traditional or heavy-weight process is equal to a task with one single thread

Threads play a vital role in remote procedure call 4.1 Overview Threads play a vital role in remote procedure call RPC servers are multithreaded When a server receives a message, it services the message using a separate thread

Utilization of multiprocessor architecture 4.1 Overview: Benefits Responsiveness A program continues running even if part of it is blocking or is performing a lengthy operation Resource sharing Code sharing Economy More economical to create and context switch between threads (compared with processes) Utilization of multiprocessor architecture Each thread may run on different processors concurrently

4.2 Multicore Programming Multithreaded programming provides a mechanism for more efficient use of multiple cores and improves concurrency Consider an application with four threads Challenges? Read pages 165—166 of the textbook.

4.2 Multicore Programming:二個名詞 Parallelism vs. concurrency 平行(parallelism):表示系統可以同時執行一項以上的工作 並行(concurrency):支援一個以上的工作/任務保持進展 單處理器/核心,排班器(scheduler)提供並行 Possible to have concurrency without parallelism

4.2 Multicore Programming:關於平行 資料平行(data parallelism) 分配同一份資料的子集合到多個運算核心,並在每一個核心執行相同的操作 Distributes subsets of the same data across multiple cores and perform the same operation on each core 任務平行(task parallelism) 分配執行緒到多個運算核心,每一個執行緒執行一個獨一無二的操作 硬體架構對於執行緒的支援:CPU有更多的核心和硬體執行緒的支援 例:Oracle SPARC T4有8核,每個核心有8個硬體執行緒 Oracle SPARC T4 有 8個核心,每個核心有8個硬體執行緒

4.3 Overview: User Threads User-level threads are implemented in user-level libraries, rather than via system calls The library supports thread creation, scheduling, and management without kernel intervention Thread switching does not need to involve the OS Switching between user-level threads can be done independently of the OS and, hence, very fast But, if the kernel is single-threaded A system call can cause the entire process to wait until the system call returns A blocked process cannot get CPU time Scheduling can be unfair

4.1 Overview: Kernel Threads Kernel threads are supported directly by OS Thread creation, scheduling, and management are done by the kernel in kernel space Code and data structures for the thread library exist in kernel space Invoking a function in the API for the library results in a system call to the kernel While one thread is blocked, other threads of the same task can still get CPU time Scheduling is fair Disadvantage Transfer of control from one thread to another within the same process requires mode switch

User Threads vs. Kernel Threads

4.3 Multithreading Models: Many-to-One Model Many user-level threads mapped to one kernel thread Thread management is done in user space The entire process will block if a thread makes a blocking system call Only one thread can access the kernel at a time; multiple threads cannot run in parallel on multiprocessors Employed when user-level thread libraries are implemented on an OS that does not support kernel threads

4.3 Multithreading Models: One-to-One Model Each user-level thread mapped to a kernel thread Allow another thread to run when a thread makes a blocking system call Drawback Creating a user thread requires creating the corresponding kernel thread  overhead 例: Windows NT/XP/2000, Linux

4.3 Multithreading Models: Many-to-Many Model Many user level threads mapped to many kernel threads Allows the OS to create a sufficient number of kernel threads (can run in parallel on a multiprocessor) Free from shortcomings of N-to-1 and 1-to-1 models When a thread performs a blocking system call, the kernel can schedule another thread for execution

Many-to-Many Model: Two-level Model Similar to M:M, but allows a user thread to be bound to kernel thread

Two primary ways of implementing 4.4 Thread Libraries Thread library provides programmers with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS

4.4 Thread Libraries: Pthread POSIX (Portable Operating System Interface) standard (IEEE 1003.1c) API (Application Program Ingterface) for thread creation and synchronization POSIX = set of standard UNIX-based interfaces API specifies behavior of the thread library; implementation is up to the library development Common in UNIX operating systems

Pthread Example #include <pthread.h> #include <stdio.h> int sum; /* shared by the threads */ void *runner(void *param) main(int argc, char *argv[]) { pthread_t tid; /* thread id */ pthread_attr attr; /* set of thread attributes as stack size, scheduling */ …. pthread_attr_init(&attr); /* get the default attributes */ pthread_create(&tid,&attr, runner, argv[1]); /* now wait for the thread to exit */ pthread_join(tid,NULL); printf("sum= %d\n",sum); } void *runner(void *param) { … sum = …. pthread_exit(0); } 應用:多執行緒程式,計算1+2+…+100總和,Thread1計算1+2+…+20, Thread2計算21+22+…+40,…,Thread5計算81+82+…+100

書Fig. 4.9範例程式

於樹莓派平台編譯Fig. 4.9範例程式

擴充Fig. 4.9範例程式

使用Windows thread library創建多執行緒的範例

使用Java API創建多執行緒的範例

Java threads may be created by Java supports (at the language level) for creation and management of threads i.e., managed by JVM, not by user-level library or kernel Java threads may be created by Extending Thread class Implementing the runnable interface Java threads are managed by the JVM

4.4 Java Threads: 使用 Extending Thread Class 之範例* class Summation extends Thread { public Summation(int n) { upper = n; } public void run() { int sum = 0; …. public class ThreadTester { public static void main(…) { … Summation thrd = new Summation(…); thrd.start(); } 2nd thread Creates the thread Allocate memory and initialize a new thread in JVM Call run() method

4.4 Java Threads: The Runnable Interface* public interface Runnable { public abstract void run(); } Implement the Runnable interface class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); }

4.4 Java Threads: Creating the Thread* public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println(“I am the main thread”); }

4.4 Java Threads: Java Thread Management* suspend() – suspends execution of the currently running thread sleep() – puts the currently running thread to sleep for a specified amount of time resume() – resumes execution of a suspended thread stop() – stops execution of a thread

4.4 Java Threads: JVM and the Host OS* JVM specification does not indicate how Java threads are mapped to the underlying OS Leaving decision to a particular implementation Winows NT/2000: one-to-one model One java thread  one kernel thread Solaris 2 Initially, many-to-one model Solaris 2.6: many-to-many model

Semantics of fork() and exec() system calls Thread cancellation 4.6 Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Associate each thread with its unique identifier

4.6 Threading Issues: fork() and exec() Semantics of fork() and exec() system calls Thread fork() Duplicate the process (i.e. all threads) Or the new process is single-threaded UNIX systems have two different forks Thread exec() The program replace the entire process – all threads and LWPs fork()  exec() : duplicating all threads is unnecessary fork()  No exec(): duplicate all threads (i.e. the process)

4.6 Threading Issues: Thread Cancellation Thread cancellation (cancelling the target thread) 例: Multiple threads search a database and one returns the result, the remaining might be cancelled Asynchronous cancellation One thread immediately terminates the target thread Deferred cancellation The target thread can periodically (at a cancellation point) check if it should terminate safely

4.6 Threading Issues: Thread Cancellation Thread cancellation (cancelling the target thread) Difficulties Resources have been allocated to a cancelled thread Thread cancelled in the middle of updating shared data (especially troublesome with asynchronous cancellation) OS reclaims the system resources from a cancelled thread (but OS will not reclaim ALL RESOURCES) Most OS’ provide asynchronous cancellation Pthread provides deferred cancellation

4.6 Threading Issues: Signal Handling Used in UNIX to notify a process that a particular event has occurred Receive types Synchronous (internal to the process) 例: division by zero, illegal memory access Asynchronous (external to the process) 例: <Ctrl>+<C>, timer expire Signal patterns Occurrence of a particular event triggers a signal Generated signal is delivered to the process Process must handle the signal

4.6 Threading Issues: Signal Handling Every signal may be handled by A default signal handler Every signal has a default signal handler that is run by the kernel A user-defined signal handler The default signal handler may be overridden by a user-defined signal handler

4.6 Threading Issues: Signal Handling in Multithreaded Programs Options Deliver the signal to the thread to which the signal applies Synchronous signals to the thread that generated the signal Deliver the signal every thread in the process 例: <Ctrl>+<C> asynchronous signal Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process (eg. Solaris)

4.5 Implicit Threading: Thread Pools Thread creation per service request Overhead of creation Unlimiting the number of threads  exhausting the system resources Solution: thread pools Create a number of threads at process startup, and place them into a pool They wait for work No free thread in the pool, the service has to wait

Linux refers to them as tasks rather than threads 4.7 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call fork() duplicates a process clone() allows a child task to share the address space of the parent task (process) Parameters to specify the degree of sharing clone() without flag set = fork()

4.7 Linux Threads: clone() vs. fork() Memory Fork() Clone() P1 P1 P1, P2 Copy task_struct fields P2

4.7 Linux Threads: Linux Process Table Task table P1 Task_struct P2 P3 Task_struct P2 is a clone of P1 P3 is fork of P1

4.7 Linux Threads: Fields of task_struct State: executing, ready, suspended, stopped, zombie Scheduling information: normal or real-time, a priority Identifiers: unique process ID, user and group IDs Interprocess communication: Links: link to parent; links to siblings; links to its children Times and timers: process creation time, the amount of processor time consumed File system: pointers to opened files (by this process) Virtual memory: defines the virtual memory assigned to this process Processor-specific context registers and stack