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.

Slides:



Advertisements
Similar presentations
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Advertisements

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
Chapter 5 Threads os5.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
Chapter 4: Multithreaded Programming
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
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.
Threads CSCI 444/544 Operating Systems Fall 2008.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
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.
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 &
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
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.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Chapter 4 MultiThreaded Programming. 2015/9/18os Outline Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows XP/2000.
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.
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.
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.
Threads, Thread management & Resource Management.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
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 – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
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 ©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.
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 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 ©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.
Threads Overview Benefits, User and Kernel Threads.
CS 6560: Operating Systems Design
Day 12 Threads.
Chapter 4 Threads.
Threads & multithreading
Threads Chapter 4.
Presentation transcript:

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 two types of threads: user-level threads kernel-level threads multiple thread models thread pools thread APIs

Two Aspects of a Process process can be viewed two ways: unit of resource ownership a process has an address space, containing program code and data a process may have open files, may be using an I/O device, etc. unit of scheduling the CPU scheduler dispatches one process at a time onto the CPU associated with a process are values in the PC, SP, and other registers insight (~1988) — these two are usually linked, but they don’t have to be; many recent operating systems attempt to separate these two aspects (modern Unices, Windows XP): process = unit of resource ownership thread = unit of scheduling

Process vs. Threads data program code process has – address space, program code, global variables, heap, stack, OS resources (files, I/O devices, etc.) thread – is a single sequential execution stream within a process has its own (other threads can access but shouldn’t) registers, program counter (PC) stack, stack pointer (SP) with other threads of the process shares process resources executes concurrently can block, fork, terminate, synchronize thread A process' address space thread B stack PC program code data access to file access to printer

What to Program with Threads good programs to multithread: programs with multiple independent tasks: most programs with GUIs – need to provide quick user-response while carrying out main task server which needs to process multiple independent requests simultaneously OS kernel repetitive numerical tasks — break large problem, such as weather prediction/matrix multiplication, down into small pieces and assign each piece to a separate thread programs difficult to multithread: programs that don’t have multiple concurrent tasks (99% of all programs) multi-task programs that require protection between tasks (maybe one needs to run as root)

Why Use Threads alternative – implement ad hoc switching and scheduling between tasks within process threads are more intuitive to program, easy to switch between task contexts and coordinate between alternative – create multiple processes threads are cheaper to create — only need a stack and storage for registers use very little resources — don’t need new address space, global data, program code, or OS resources context switches are fast — only have to save / restore PC, SP, and registers communication between threads is easy – they share address space from “Unix Internals” by Uresh Vahalia

User-Level Threads provide a library of functions to allow user processes to manage (create, delete, schedule) their own threads OS is not aware of threads advantages: doesn’t require modification to the OS simple representation — each thread is represented simply by a PC, registers, stack, and a small control block, all stored in the user process’ address space which segment? simple management — creating a new thread, switching between threads, and synchronization between threads can all be done without intervention of the kernel fast — thread switching is not much more expensive than a procedure call flexible — CPU scheduling (among those threads) can be customized to suit the needs of the algorithm

User-Level Threads (Cont.) disadvantages: lack of coordination between threads and OS kernel process as a whole gets one time slice same time slice, whether process has 1 thread or 1000 threads also — up to each thread to relinquish control to other threads in that process requires non-blocking system calls otherwise, entire process will blocked in the kernel, even if there are runnable threads left in the process

Kernel-Level Threads kernel provides system calls to create and manage threads advantages kernel has full knowledge of all threads - scheduler may choose to give a process with 10 threads more time than process with only 1 thread good for applications that frequently block (e.g., server processes with frequent interprocess communication) disadvantages: slower — thread operations are significantly slower than for user-level threads significant overhead and increased kernel complexity — kernel must manage and schedule threads as well as processes - requires a full thread (task) control block (TCB) for each thread

Multithreading Models Many-to-One (Solaris’ green threads) – all user threads are mapped to one kernel thread: same problems as with user threads One-to-One (Windows XP, OS/2) – one user thread to one kernel thread programmer has better control of concurrency does not block the process on one thread blocking may potentially waste resources Many-to-Many – multiplexes many user-level threads to a smaller or equal number of kernel-level threads allocation is specific to a particular machine (more k. threads may be allocated on a multiprocessor)

Two-Level Thread Model (Digital UNIX, Solaris, IRIX, HP-UX) user-level lightweight task 1 task 2 task 3 thread process kernel CPU user-level threads for user processes “lightweight process” (LWP) serves as a “virtual CPU” where user threads can run kernel-level threads for use by kernel one for each LWP others perform tasks not related to LWPs OS supports multiprocessor systems ULTs can be multiplexed on lighweight processes ULTs can be “pinned “ to a processor kernel thread may not have a corresponding LWP

Thread Pools threads may not be created infinitely – overwhelm computer resources consider web-server on-demand thread creation may not be fast enough thread pool create a number of threads in a pool where they remain blocked until activated activate when request arrives if more requests than threads – extra requests have to wait performance controllable process size fast response until pool exhausted

Thread APIs POSIX threads (pthreads) A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization Common in UNIX operating systems (Solaris, Linux, MacOS X) implemented on top of “native” OS threads or as a user-level package Win32 threads one-to-one mapping implemented on Windows OSes Java threads specified as part of Java maintained by JVM implemented on native threads or as a user-level package

Lecture Review thread is a stream of execution process may have several threads threads share address space of the process and own register and stack threads are good for programming tasks that require several relatively independent tasks threads are more efficient than processes and provide more convenient programming primitives than ad hoc context switching threads can be implemented in kernel-level or user-level there are several models of thread implementation: many-to-one, one-to-one, many-to-many thread pool is a technique of thread programming used to optimize memory usage and response time