Threads & multithreading

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.
Threads.
Chapter 4: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
A. Frank - P. Weisberg Operating Systems Threads Implementation.
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.
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.
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.
Operating Systems Yasir Kiani. 22-Sep Agenda for Today Review of previous lecture Process management commands: bg, fg, ^Z, jobs, ^C, kill Thread.
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.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
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.
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.
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.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Multithreaded Programming
Chapter 4: Threads 羅習五.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Operating Systems Lecture 12.
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 Book: Operating System Principles , 9th Edition , Abraham Silberschatz, Peter Baer Galvin, Greg Gagne.
Presentation transcript:

Threads & multithreading Lecture 08

Thread Concept A thread is a “lightweight” process which executes within the address space of a process. A thread can be scheduled to run on a CPU as an independent unit and terminate. Multiple threads can run simultaneously. 20 September 2018

Thread Concept Threads have their own Thread ID CPU context (PC, SP, register set, etc.) Stack Priority errno 20 September 2018

Thread Concept Threads share Code and data Open files (through the PPFDT) Current working directory User and group IDs Signal setups and handlers PCB 20 September 2018

Single and Multithreaded Processes   20 September 2018

Threads are Similar to Processes A thread can be in states similar to a process (new, ready, running, blocked, terminated) A thread can create another thread 20 September 2018

Threads are Different from Processes Multiple threads can operate within the same address space No “automatic” protection mechanism is in place for threads—they are meant to help each other 20 September 2018

Advantages of Threads Responsiveness Multi-threaded servers (e.g., browsers) can allow interaction with user while a thread is formulating response to a previous user query (e.g., rendering a web page) 20 September 2018

Advantages of Threads Resource sharing Process resources (code, data, etc.) OS resources (PCB, PPFDT, etc.) 20 September 2018

Advantages of Threads Economy Take less time to create, schedule, and terminate Solaris 2: thread creation is 30 times faster than process creation and thread switching is five times faster than process switching 20 September 2018

Advantages of Threads Performance in multi-processor and multi-threaded architectures (e.g., Intel’s P4 HT) Multiple threads can run simultaneously 20 September 2018

Disadvantages of Threads Resource sharing— synchronization needed between threads Difficult to write and debug multi-threaded programs 20 September 2018

Single-Threaded Process main() { … f1(…); f2(…); } f1(…) { … } f2(…) Thread f1 f2 Process Terminated 20 September 2018

Multi-Threaded Process main() { … thread(t1,f1); thread(t2,f2); } f1(…) { … } f2(…) Process Address Space main t1 t2 PC PC PC 20 September 2018

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

User Threads Thread management done by user-level threads libraries Kernel not aware of threads CPU not interrupted during thread switching A system call by a thread blocks the whole process Fair scheduling: P1 has one thread and P2 has 100 threads 20 September 2018

User Threads Examples POSIX Pthreads Mach C-threads Solaris 2 threads 20 September 2018

Kernel Threads Thread management done by kernel Kernel aware of threads CPU switched during context switching A system call does not block the whole process Fair scheduling: P1 has one thread and P2 has 100 20 September 2018

Kernel Threads Examples Windows NT/2000 Solaris 2 Linux 20 September 2018

Multithreading Models Support for both user and kernel threads Many-to-One: Many user threads per kernel thread; process blocks when a thread makes a system call Solaris Green threads Pthreads 20 September 2018

Many-to-One Model User–level Threads Kernel–level Thread 20 September 2018

Multithreading Models One-to-One: One user thread per kernel thread; process does not block when a thread makes a system call Overhead for creating a kernel thread per user thread True concurrency achieved Windows NT/2000, OS/2 20 September 2018

One-to-One Model User–level Threads Kernel–level Threads P1 P2 20 September 2018

Multithreading Models Many-to-Many: Multiple user threads multiplexed over a smaller or equal number of kernel threads True concurrency not achieved because kernel can only schedule one thread at a time Kernel can schedule another thread when a user thread makes a blocking system call Solaris 2, HP-UX 20 September 2018

Many-to-Many Model User–level Threads Kernel–level Threads P1 P2 P3 20 September 2018

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)

Windows XP Threads

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)

Linux Threads

Thread Control Block (TCB) TCB contains info on a single thread - Just processor state and pointer to corresponding PCB PCB contains information on the containing process - Address space and OS resources ... but NO processor state! TCB's are smaller and cheaper than processes - Linux TCB (thread_struct) has 24 fields - Linux PCB (task_struct) has 106 fields

Thread Control Block (TCB)

Context switch TCB is now the unit of a context switch - Ready queue, wait queues, etc. now contain pointers to TCB's - Context switch causes CPU state to be copied to/from the TCB

Context switch Context switch between two threads in the same process: - No need to change address space Context switch between two threads in different processes: - Must change address space, sometimes invalidating cache - This will become relevant when we talk about virtual memory

Context switch If multiple threads, running thread's state on CPU; need to store state of non running threads somewhere. Need to keep this per thread state somewhere: TCB

Context switch Thread control block -one per thread -execution state: registers, program counter, pointer to stack scheduling information etc So: for each process, kernel has list of TCBs one per thread. Kernel can switch running thread by (1) taking state of running thread and moving to TCB and then (2) taking state from TCB and putting it on processor.

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

Assignment 01 Dekker’s Algorithms