Operating System Concepts

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.
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.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Silberschatz, Galvin and Gagne ©2009 Operating 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.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
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: 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.
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.
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.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
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: Threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
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.
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
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
Operating System Concepts
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:

Operating System Concepts

Lecture Contents Motivation for threads Thread Concept Similarity and Difference between Process and Thread Advantages of threads Implementation of Multithreading Multithreading Models Thread Library Concept Multithreading Issues Case Study *Please follow Silberchatz, Galvin and Gagne (Chapter 4), 7th edition

Issues with Processes So far we have discussed Process, Creation of new process concept and its execution; However there are some issues with processes. Processes are EXPENSIVE ! (E.g. resources that are to be allocated with every process) Hardware resources (memory), kernel resources (PCB) Link establishment for IPC (unrelated processes/related processes) Concurrency Motivation ? .. A mechanism that provides an alternative of a process (so that a new process is not needed)

THREAD Thread- A piece of code in a process which runs concurrently with execution of other piece of codes within the same process (same address space) No need to create a new process Resources that thread need to update/allocate privately: Thread ID CPU Context (Program Counter (PC), register set) Stack Priority Kernel maintained external variable errno, (in order to tell the programmer that an error has occurred)

Threads within a same process share… Multiple and concurrent threads within a process share: Code Data Open File descriptor table PCB (Process Control Block) For a thread creation, only need to allocate CPU context and Stack !! Threads are in the same process address space

Single and Multithreaded Processes

How processes are similar to threads or different ? A thread can be in a state, similar to process (new, ready, running, waiting, terminate) Threads can also create other threads. Difference Processes execute only within their own address space, whereas threads of same process execute within address space of one major process Automatic protection mechanism for processes but what about threads… ? Synchronization issues ?

Advantages of thread Responsiveness - allow a program to continue running even if part of it is blocked (e.g. In a browser, multiple threads are working simultaneously to increase responsiveness) Resource sharing - threads share the memory and resources of the process (allows an application to have several threads of activity within the same address space) Economy - it is more economical to create and context-switch threads (because they share resources of the process to which they belong) In Solaris a process is thirty times slower to create and five times slower to context-switch Scheduling a thread is easy (just save the CPU context) Scalability (utilizing multiprocessor architectures)

Some other things.. The main reason for having threads is that in many applications, multiple activities are going on at once. Some of these may block from time to time. By decomposing such an application into multiple sequential threads that run in parallel, the programming model becomes simpler.

How multithreading is implemented ? There are two techniques for implementing multithreading User-Level threads Thread creation and management is done by the application using a thread library Any application can be programmed to be multithreaded by using a thread library which is a package of application level functions. The kernel is not aware of existence of threads Kernel Level threads Thread management and implementation is done by operating system Kernel provides a way for user processes to create multiple threads

Difference between the two techniques… Advantages of User level threads User-level threads can be created in a system that doesn’t provide multithreading facility Management and scheduling of thread is done by threading library in user space, so user level threads don’t invoke kernel for doing these tasks User level threads can run on any OS, no changes are required to do on underlying kernel (Portability) Disadvantages Using User Level threading strategy, a multithreaded application cannot take advantage of multiprocessor system. (different threads of a same process cannot run on different CPU’s because OS considers a process a single threaded program) If a thread is blocked, all the threads within a process gets blocked.

Advantages of Kernel level threads Since kernel threads use the kernel scheduler, different kernel threads can run on different CPUs (taking advantage of multiprocessor system) If a thread within a process gets blocked (e.g. waiting for an I/O), the scheduler can schedule another thread Disadvantages Frequent invoking of kernel for management of threads More expensive to create than user threads

Examples User Threads Kernel Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux POSIX: an acronym for Portable Operating System Interface, is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems. This standard defines a standard operating system interface and environment, including a command interpreter (or "shell"), and common utility programs to support applications portability at the source code level.  Pthreads: Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread creation and synchronization. This is a specification for thread behavior, not an implementation. Threads have been standardized by the IEEE Technical Committee on Operating Systems, POSIX (Portable Operating System Interface). This standard allows users to write portable multi-thread programs.

Multithreading Models Based on the number of kernel threads allocated correspondingly to the user level threads, we have three multithreading models Many-to-one Model One-to-one Model Many-to-Many Model

Many-to-One Model Many user-level threads mapped to single kernel thread Thread management is done by the thread library in user space, so it is efficient. Drawback - the entire process will block if a thread makes a blocking system call. Examples: Solaris Green Threads GNU Portable Threads Kernel is scheduling a single process

One-to-One Model Each user-level thread maps to kernel thread True concurrency - compared to many-to-one model since another thread will be able to run when a thread makes a blocking system call. Allows multiple threads to run in parallel on multiprocessors. Drawback: Creating a user thread requires creating a corresponding kernel thread. (Overhead) Examples Windows NT/XP/2000 Linux Solaris 9 and later

Many-to-Many Model (Hybrid Model) Multiple user level threads multiplexed over a smaller or equal number of kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris 2 Windows NT/2000 with the ThreadFiber package Which of three models solaris model is built?

Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS -> invoking a function in the library results with a system call

Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library (i.e. the OS designers) This standard has been defined to make the writing of PORTABLE threaded program possible This standard defines over 60 function calls May be provided either as user-level or kernel-level. Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Threading Issues Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

Thread Cancellation Terminating a thread before it has finished Example: multiple threads concurrently searching through a database and one thread returns the result Stopping a webpage from loading any further Two general approaches: Asynchronous cancellation terminates the target thread immediately – troublesome if it’s in middle of updating some data Deferred cancellation allows the target thread to periodically check if it should be cancelled – checking a flag. Can only be cancelled at defined points called cancellation points

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred Example: illegal memory access, division by zero A signal handler is used to process signals Signal is generated by particular event Signal is delivered to a process Signal is handled In a multithreaded process, where should a signal be delivered? 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 Reason for the need of thread pool: Time consumed to create a new thread and destroy the previous ones – leads to slow performance Unlimited threads could exhaust system resources, such as CPU time or memory Solution: Thread Pools Create a number of threads at process startup and place them in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Also allows an application to specify the number of threads it requires

Thread Specific Data Scheduler activation Allows each thread to have its own copy of specific data Example, in a transaction-processing system, service each transaction in a separate thread. Furthermore, each transaction might be assigned a unique identifier. To associate each thread with its unique identifier, use thread-specific data. Scheduler activation User-Level Threads: If a single user-level thread blocks, the operating system blocks the entire multithreaded process. Another limitation to a many-to-one thread mapping is that a process's threads cannot simultaneously execute on multiple processors. Scheduler activations attempt to address these limitations to user-level threads. A scheduler activation is a kernel thread that can notify a user-level threading library of events (e.g., a thread has blocked or a processor is available). This type of kernel thread is called a "scheduler activation,“ because the user-level threading library can perform thread-scheduling operations when "activated" by an event notification, sometimes called an upcall.

Case Study Windows XP Linux

Windows XP

Windows XP Threads

Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() accepts arguments that specify which resources to share with the child process