CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.

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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
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.
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.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
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 ©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.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
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.
Chapter 4: Threads.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: 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:

CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011

2Operating Systems What is a thread? A thread is a basic unit of CPU utilization contains a thread ID, a program counter, a register set, and a stack shares with other threads belonging to the same process  code section  data section  other operating-system resources, such as open files

3Operating Systems Single and Multithreaded Processes

4Operating Systems Motivation Threads run within application Multiple tasks with the application can be implemented by separate threads Update display Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light-weight Increase efficiency of C-S applications Kernels are generally multithreaded

5Operating Systems Benefits Responsiveness A program continues running even if part of it is blocked or is performing a lengthy operation Resource Sharing Threads share the memory and the resources of the process to which they belong IPC techniques are not needed Economy Creating a thread is much faster than creating a process Scalability Multithreading on a multi-CPU machine increases concurrency.

6Operating Systems Parallel Execution on a Multi-core System multi-core

7Operating Systems Drawbacks Make the programming more complicated Make the debugging harder Possible error when threads concurrently access the shared resources Poorly divided jobs can cause even worse system performance......

8Operating Systems Process 1. independent 2. carries considerably more state information 3. has separate address space 4. interact only through IPC 5. context switching is relatively slow Thread 1. exists as subsets of a process 2. shares process state as well as memory and other resources 3. shares process’s address space 4. more ways to communicate 5. context switching in the same process is typically faster Process vs. Thread

9Operating Systems Supports for Threads Kernel Threads Supported by the operating system kernel Examples  Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X User Threads Thread management done by user-level threads library Three primary thread libraries:  POSIX Pthreads  Win32 threads  Java threads

10Operating Systems Thread Model User thread Lightweight process Kernel thread

11Operating Systems Multithreading Models Four common relationships between user threads and kernel threads Many-to-One One-to-One Many-to-Many Two-Level Model

12Operating Systems Many-to-One Model Many user-level threads are mapped to a single kernel thread Strength Multiple threads are hided by user-level thread library Weaknesses The entire process will block if a thread makes a blocking system call Multiple threads are unable to run in parallel on multiprocessors Examples: Solaris Green Threads GNU Portable Threads

13Operating Systems One-to-One Each user-level thread is mapped to a kernel thread Strength More concurrency Weakness Creating a user thread requires creating the corresponding kernel thread, which incurs overhead Examples Windows NT/XP/2000 Linux Solaris 9 and later

14Operating Systems Many-to-Many Model Allows many user level threads to be mapped to many kernel threads The operating system to create a sufficient number of kernel threads Examples Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

15Operating Systems Two-Level Model Similar to Many-to-Many, except that it allows a user thread to be bound to a kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

16Operating Systems Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing User-level threads library  All code and data structures for the library exist in user space  Invoking a function in the library results in a local function call in user space Kernel-level threads library supported by the OS  Code and data structures for the library exist in kernel space  Invoking a function in the library results in a system call to the kernel Three primary thread libraries: POSIX Pthreads, Win32 threads, Java threads

17Operating Systems Pthreads Is provided either in user-level or kernel-level A POSIX standard (IEEE c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)

18Operating Systems Example Using Pthreads #include int sum; /* this data is shared by the thread(s) */ /* The thread will begin control in this function */ void *runner(void *param); { int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; pthread_exit ( 0) ; }

19Operating Systems Example Using Pthreads (Cont.) int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ /* get the default attributes */ pthread_att_init (&attr); /* create the thread */ pthread_create(&tid, &attr, runner, argv[l]) ; /* wait for the thread to exit */ pthread_join(tid, NULL) ; printf (" sum = %d\n", sum) ; }

20Operating Systems Threading Issues Semantics of fork() and exec() system calls Does fork() duplicate only the calling thread or all threads? exec() will replace the entire process with the program specified in the parameter Thread cancellation of target thread 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.

21Operating Systems Threading Issues (Cont.) Signal handling Signals are used in UNIX systems to notify a process that a particular event has occurred. Synchronous and asynchronous A signal handler is used to process signals  Signal is generated by particular event  Signal is delivered to a process  Signal is handled Delivery 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

22Operating Systems Threading Issues (Cont.) 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 Create Facility needed for data private to thread 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

23Operating Systems Operating System Examples Linux Thread Windows XP Threads

24Operating Systems Linux Threads fork() and clone() system calls n clone() takes options to determine sharing on process create n struct task_struct points to process data structures (shared or unique)

25Operating Systems 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)

26Operating Systems Windows XP Threads Data Structures

27Operating Systems Pop-quiz if (pid == 0) { pthread_attLinit (&attr) ; pthread_create(&tid, &attr, runner, NULL); pthread_join(tid,NULL) ; printf(“Child: value = %d", value); } else if (pid > 0) { wait (NULL) ; printf(“Parent: value = %d", value); } int value = 0; void *runner(void *param) { value = 5; pthread_exit ( 0) ; } int main(int argc, char *argv[]) { int pid; pthread_t tid; pthread_attr_t attr; pid = fork(); What are the outputs from the above program?

28Operating Systems Homework Reading: Chapter 4