Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 3.Threads.

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.
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.
國立台灣大學 資訊工程學系 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: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP 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.
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.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
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. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
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.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
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 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
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.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 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.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Nadeem MajeedChoudhary.
Chapter 4: Threads.
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 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/ Threads

2 Disclaimer This slides and notes are heavily based on the companion material of [Silberschatz05].The original material can be found at: In some cases, material from [Stallings04] may also be used. The original material can be found at: The respective copyrights belong to their owners.

3 What’s a Process? MS Excel (1 process) MS Word (1 process) Process = The living “Image” of a program running

4 What’s a Thread? Process Thread 1 Thread 2Thread 3 Thread = A flow of execution inside a process

5 Motivation Process switching is an extremely heavy operation It’s necessary to remap address spaces It’s necessary to establish new security contexts It’s necessary to manage all information associated to processes Thread It’s a flow of execution inside a program The address space is the same (…changing a global variable in a thread affects all others) No context switching between processes Commutation among threads is very fast Communication among threads is easy to do and fast

6 Threads – Example in C

7 And the result is…

8 Threads – The same example in Java

9 And the result is the same…

10 Important! Threads share the same address space and resources! Changing one thing in one thread affects all others! It is the responsibility of the programmer to assure the correctness in the concurrent access to data and resources (more on this later…)

11 Why use threads? They are very light weight compared to processes Light context switches Fast to create and terminate Fast to synchronize Fast to communicate among threads Very useful in multiprocessor/multi-core architectures Economy or resources Much easier to program than shared memory! Everything is already shared Be careful to synchronize accesses!

12 What it looks like

13 PCB vs. Thread Control Blocks

14 Multithreading Models User threads (N-to-1 model) Threads are implemented in a library at user space. The kernel is completely unaware of the existence of threads User Mode Kernel Mode Thread Library

15 Multithreading Models Kernel threads (1-to-1 model) Threads are implemented exclusively in kernel space The kernel does all the scheduling of threads User Mode Kernel Mode

16 Advantages and Disadvantages User Level Threads Commutation among threads is fast since it doesn’t imply a mode switch Thread creation and termination is fast Doesn’t require any special support by the kernel, thus being available in any OS  If one thread blocks all threads block (e.g. on I/O)  Only one system call can happen at a time  Scheduling is not fair  One thread terminates all threads terminate Kernel Threads  Commutation among threads is slower since it implies going to the kernel  Thread creation and termination is slower  Requires that the kernel supports user visible threads If one thread blocks the others can continue Simultaneous system calls Fair scheduling Somewhat independent thread termination

17 Multithreading Models (3) The Many-to-Many Model A number of kernel threads can map to a different number of user threads User Mode Kernel Mode

18 Current Operating Systems User-level Threads E.g. Solaris before version 9 (Green Threads) E.g. Windows NT/2K with Fibers package Kernel-level Threads Virtually all modern operating systems: LINUX, Solaris, Windows XP, Mac OS X, Tru64 Unix Thread Libraries  Threads must be implemented by an API (library).  Libraries can be user-level or kernel-level  Using a library doesn’t mean that it’s user-level threading  Main libraries in current use: Java, PThreads, Win32

19 Why do all modern operating systems use Kernel Threads? Why do all modern operating systems use Kernel Threads? Quick Quiz

20 Some technical hurdles… What happens if a thread calls fork(), exec() or exit()? How to perform thread cancellation (a.k.a. kill-a-thread)? What happens if a signal is sent to a process? How can a thread have its own private data?

21 Thread Pools Since most operating systems use an 1-to-1 model, it’s important not to waste much time creating and destroying threads E.g. if you have a web server concurrently accepting requests from clients, it makes no sense to: 1) create a thread to serve a client; 2) the thread actually serves the client; 3) the thread dies; 4) start all over again… Thread 4 Thread 3 Thread 2 Thread 1 Thread 0 Thread Pool Some threads are previously created and BLOCKED waiting for work Whenever work appears, a managing thread wakes one thread in the pool and assigns it the work to be done After the thread completes the work it returns to the pool waiting for more work

22 PThreads vs Windows Threads PThreads 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) Windows Threads Implements the one-to-one mapping 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

23 Linux Threads Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call. Creates a new process with a shared address space Note that in a “normal” fork() you have copy-on-write! In Linux 2.4… (with LinuxThreads) Threads were really processes with shared address spaces… Signal handling is incorrect. An extra management thread is created by the pthreads library ps shows all threads in a process (PID per thread) Core dumps don’t contain the stack and machine register information for all threads. getpid() returns a different result for each thread A thread cannot wait for a thread created by another thread Threads have parent-child, not peer, relationships

24 Linux 2.6: NGPT and NPTL IBM and Intel released the first version of the New Generation Posix Thread (NGPT) library. Aimed to solve the problems with LinuxThreads Abandoned in mid-2003 In September 2002, RedHat released the Native POSIX Thread Library (NPTL) NPTL is based on the 1:1 thread model It still uses clone() but now the kernel has the necessary support for e.g. fast synchronization It’s the current standard Nowadays: Typically glibc uses NPTL by default In some cases, you may still decide to revert to LinuxThreads

25 Thread Performance Comparison

26 Reference Linux threading models compared: LinuxThreads and NPTL, by Vikram Shukla, IBM developerWorks threading.html?ca=dgr-lnxw07LinuxThreadsAndNPTL Chapter 4: Threads All chapter 4! 4.1, 4.2, 4.3, 4.4, 4.5, 4.6