Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.

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.
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.
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 – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
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. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
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.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Silberschatz, Galvin and Gagne ©2009Operating 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 Lecture 4: Threads Advanced Operating System Fall 2010.
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.
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 (7 th Edition)
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.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Nadeem MajeedChoudhary.
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:

Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems

Threads Overview Multithreading Models Threading Issues

Overview A thread is a basic unit of CPU utilization A traditional (or heavyweight) process has a single thread of control Single-threaded applications − Simple implementations e.g. assignments Multi-threaded applications − Web browser − Web server

Single and Multithreaded Processes ThreadID Program counter Registers Stack

Relationship between threads and processes The operating system creates a process for the purpose of running a program. − Every process has at least one thread. On some operating systems, a process can have more than one thread. − Some programs like word processors are designed to have only one instance of themselves running at the same time. Sometimes, such programs just open up more windows to accommodate multiple simultaneous use. After all, you can go back and forth between five documents, but you can only edit one of them at a given instance. − Command line interpreters and multiple users ~ processes Access rights Protection of other users from failures − Graphical User Interface ~ threads Multiple aspects at one instance, user input and painting

Processes and Threads The concept of a process and thread are interrelated by a sense of ownership and of containment Process − A process is the "heaviest" unit of kernel scheduling − Processes own resources allocated by the operating system Resources include memory, file handles, sockets, device handles, and user interfaces. − Processes do not share address spaces or file resources except through explicit methods − Processes are typically pre-emptively multitasked. However, Windows 3.1 and older versions of Mac OS used co- operative or non-preemptive multitasking.

Processes and Threads Thread − A thread is the "lightest" unit of kernel scheduling. − At least one thread exists within each process. − If multiple threads can exist within a process, then they share the same memory and file resources. − Threads are pre-emptively multitasked if the operating system's process scheduler is pre-emptive. − Threads do not own resources except for a stack and a copy of the registers including the program counter.

Benefits Responsiveness − blocking and non-blocking requests Resource Sharing − same address space Economy − Allocating resources to processes as compared to sharing resources via threads Utilization of MP Architectures − Multi-threaded applications can utilize multiprocessor platforms by enhancing concurrency

Multi-threading and reality In an ideal world, five processors would do five times the work of one processor But we live in a world of contention for shared resources, of disk and memory bottlenecks, single- threaded applications, multithreaded applications that require synchronous processing, and poorly coordinated processors

Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Types of threads Kernel threads User level threads

Kernel threads A kernel thread is a kernel entity, like processes and interrupt handlers It is the entity handled by the system scheduler. Kernel threads consist of a set of registers, a stack, and a few corresponding kernel data structures. − The user structure contains process-related information − The uthread structure contains thread-related information. Unlike processes, all threads within a process share the same address space. Similar to processes, when a kernel thread makes a blocking call, only that thread blocks. All modern machines support kernel threads, most often via the POSIX threads interface ``pthreads''. − Some dedicated parallel machines support kernel threads poorly or not at all. For example, the Blue Gene/L microkernel does not support pthreads.

Kernel threads The advantage of kernel threads over processes is faster creation and context switching compared with processes. Parallel programming Kernel threads cannot be accessed from the user mode environment, except through the threads library.

Kernel Threads Supported by the Kernel Examples − Windows XP/2000 − Solaris − Linux − Tru64 UNIX − Mac OS X

User level threads Like a kernel thread, a user-level thread includes a set of registers and a stack, and shares the entire address space with the other threads in the enclosing process Unlike a kernel thread, however, a user-level thread is handled entirely in user code OS is unaware of a user-level thread's existence The primary advantages of user-level threads are efficiency and flexibility − Because the operating system is not involved, user-level threads can be made to use very little memory − User-level threads are also more flexible because the thread scheduler is in user code, for example, the application's priority structure can be directly used by the thread scheduler

User level threads The primary disadvantage of user-level threads compared to kernel threads is the lack of operating system support. − For example, when a user-level thread makes a blocking call, the kernel does not start running another user-level thread. Instead, the kernel suspends the entire calling kernel thread or process, even though another user-level thread might be ready to run.

User Threads User threads are mapped to kernel threads by the threads library, in an implementation dependent manner. − The threads library uses a proprietary interface to handle kernel threads. Three primary thread libraries: − POSIX Pthreads − Win32 threads − Java threads

Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Questions?

Multithreading Models Many-to-One One-to-One Many-to-Many

Many-to-One Many user-level threads mapped to single kernel thread Entire process will block if a thread makes a blocking call Examples: − Solaris Green Threads − GNU Portable Threads

One-to-One Each user-level thread maps to kernel thread Examples − Windows NT/XP/2000 − Linux − Solaris 9 and later

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

Many-to-Many Model

Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples − IRIX − HP-UX − Tru64 UNIX − Solaris 8 and earlier

Two-level Model

Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations

Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads?

Thread Cancellation Terminating a thread before it has finished − E.g. Multiple threads searching in a DB, if one thread finds the result, cancel the others Two general approaches: − Asynchronous cancellation terminates the target thread immediately Cancellation during data update ~ consistency issues − Deferred cancellation allows the target thread to periodically check if it should be cancelled Cancellation points

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred − Synchronous ~ same process, divide by zero − Asynchronous ~ external event, terminate process A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Handlers − Default − User defined 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

Thread Pools Create a number of threads in a pool where they await work − Web server 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

Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Questions? Recommended Reading: − Book ~ − OSRC − Reading