Threads CSCI 444/544 Operating Systems Fall 2008.

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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
Chapter 4: Multithreaded Programming
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
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.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
3.5 Interprocess Communication
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.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
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 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
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. 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.
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.
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 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Threads G.Anuradha (Reference : William Stallings)
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.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
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.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Operating System Concepts
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  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.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
OPERATING SYSTEM CONCEPT AND PRACTISE
CS 6560: Operating Systems Design
Threads and Cooperation
Chapter 4: Threads.
Presentation transcript:

Threads CSCI 444/544 Operating Systems Fall 2008

Agenda Thread concept Thread vs process Thread implementation - user-level - kernel-level - hybrid Inter-process (inter-thread) communication

What is Thread A thread is a fundamental unit of CPU utilization –a thread ID –a program counter –a register set –a stack It shares with other threads belonging to the same process –code section and data section –other OS resources, such as open files

Single and Multi-threaded Processes

Process vs. Thread (1) Process: unit of allocation - defines the address space and consumed OS resources Thread: unit of execution - defines a sequential execution stream within a process Each process has one or more threads Each thread belongs to one process

Process vs Thread (2) A process is just a container in which threads execute Sharing data between the threads of a process is cheap: in the same address space –one thread may corrupt another thread (no memory protection) –one process cannot corrupt another process Creating threads is cheap too

Why Threads Help Multi-threading supports concurrency/parallelism - concurrent events (e.g., Web servers) - parallel program in multi-processor architecture But why not multiple processes? - memory sharing - efficient synchronization between threads - less context switch overhead

Common Multithread Models Manager/worker –Single manager handles input and assigns work to the worker threads Producer/consumer –Multiple producer threads create data (or work) that is handled by one of the multiple consumer threads Pipeline –Task is divided into series of sub-tasks, each of which is handled in series by a different thread

Thread Control Block Each thread is described by a thread control block (TCB) A TCB usually includes –Thread ID –Space for saving registers –Information about its stack –A pointer so it can be chained into a linked list

OS Support for Threads There are three ways for thread support –User-level threads Thread data structure is in user-mode memory Scheduling/switching done at user mode –Kernel-level threads Thread data structure is in kernel memory Scheduling/switching done by the OS kernel –Hybrid of user-level and kernel-level threads M user threads run on N kernel threads (M>N>1)

User-level Threads Many-to-one thread mapping Implemented as user-level runtime library (thread library) - thread creation, termination, scheduling and switching at user-level Kernel sees one execution context per process and is unaware of thread activity

Many-to-One Examples: Solaris Green Threads and GUN Portable Threads

Pros vs Cons of User-level Threads Advantages –Performance: low-cost thread operation since no system calls –Flexibility: scheduling policy can be application- specific –Portability: does not require OS support, easy to port Disadvantages –Cannot leverage multiprocessors Kernel assigns one process to only one processor –Entire process blocks if one thread blocks

Kernel-level Threads One-to-one mapping: - map each thread to its own execution context in the kernel OS manages threads and sees multiple execution contexts per process Each kernel thread scheduled independently Thread operations performed by OS –Thread operations are all system calls –Must maintain kernel state for each thread

Pros vs Cons of Kernel-level Thread Advantages - each kernel-level thread can in parallel in a multiprocessor - when one thread blocks, other threads from process can be scheduled Disadvantages - Higher overhead for thread operations - OS must scale well with increasing number of threads

Hybrid Threads Hybrid of kernel and user-level threads: m-to-n thread mapping (m>n) –Application creates m threads –OS provides pool of n kernel threads –A few user-level threads mapped to each kernel-level thread Both kernel and user-level thread library involved in thread management

Many-to-Many Mapping Examples: Solaris prior to version 9

A Variation (Two-level Model)

Thread/Process Operation Latencies OperationUser-level Thread (  s) Kernel Threads (  s) Processe s (  s) Null fork ,300 Signal- wait ,840 VAX uniprocessor running UNIX-like OS, GHz Pentium 4 uniprocessor running Linux, OperationKernel Threads (  s) Processes (  s) Null fork45108

Interprocess Communication (IPC) To cooperate usefully, processes (threads) must communicate with each other How do processes and threads communicate? –Shared memory –Message passing –Signals

IPC: Shared Memory Process –Each process has private address space –Explicitly set up shared memory segment within each address space Threads –Always share address space Advantages –Fast and easy to share data Disadvantages –Must synchronize data access; error prone

IPC: Message Passing (1) Message passing most commonly used between processes –Explicitly pass data btwn sender (src) + receiver (destination) –Example: Unix pipes Advantages: –Makes sharing explicit –Improves modularity (narrow interface) –Does not require trust between sender and receiver Disadvantages: –Performance overhead to copy messages

IPC: Message Passing (2) Issues: –How to name source and destination? One process, set of processes, or mailbox (port) –Does sending process wait (I.e., block) for receiver? Blocking: Slows down sender Non-blocking: Requires buffering between sender and receiver

IPC: Signal Signal –Like software interrupt that notifies a process of an event By one process to another process By the kernel to a process What happens when a signal is received? –Catch: Specify signal handler to be called –Ignore: Rely on OS default action Example: Abort, memory dump, suspend or resume process –Mask: Block signal so it is not delivered May be temporary (while handling signal of same type) Disadvantage –Does not specify any data to be exchanged –Complex semantics with threads

Thread Libraries Pthreads –A POSIX standard API for thread creation and synchronization –May be provided as either user- or kernel-level library Win32 Threads –Kernel-level library available on Windows Java threads –Managed by the JVM

Thread interface This is taken from the POSIX pthreads API: –t = pthread_create(attributes, start_procedure) creates a new thread of control new thread begins executing at start_procedure –pthread_cond_wait(condition_variable) the calling thread blocks, sometimes called thread_block() –pthread_signal(condition_variable) starts the thread waiting on the condition variable –pthread_exit() terminates the calling thread –pthread_wait(t) waits for the named thread to terminate

Thread Switch (1) Everyone to cooperate –A thread willingly gives up the CPU by calling yield() –yield() calls into the scheduler, which context switches to another ready thread –what happens if a thread never calls yield() ?

Thread Switch (2) Use preemption –scheduler requests that a timer interrupt be delivered by the OS periodically usually delivered as a signal –at each timer interrupt, scheduler gains control and context switches as appropriate

Thread Context Switch –save context of currently running thread push machine state onto thread stack –restore context of the next thread pop machine state from next thread’s stack –return as the new thread execution resumes at PC of next thread