Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, 2005 5.1 Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.

Slides:



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

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
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.
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 Objectives Thread definitions and relationship to process Multithreading.
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
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.
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.
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.
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.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 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.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
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  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.
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.
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.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
1 OS Review Processes and Threads Chi Zhang
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
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.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Threads & multithreading
Operating System Concepts
Chapter 4: Threads.
Presentation transcript:

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Threads A thread (a lightweight process) is a basic unit of CPU utilization. A thread has a single sequential flow of control. A thread is comprised of: A thread ID, a program counter, a register set and a stack. A process is the execution environment in which threads run.  (Recall previous definition of process: program in execution). The process has the code section, data section, OS resources (e.g. open files and signals). Traditional processes have a single thread of control Multi-threaded processes have multiple threads of control  The threads share the address space and resources of the process that owns them.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Single and Multithreaded Processes

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Processes vs. Threads Which of the following belong to the process and which to the thread? Program code: local or temporary data: global data: allocated resources: execution stack: memory management info: Program counter: Parent identification: Thread state: Registers: Process Thread Process Thread Process Thread Process Thread

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Control Blocks The thread control block (TCB) contains: Thread state, Program Counter, Registers PCB' = everything else (e.g. process id, open files, etc.) The process control block (PCB) = PCB' U TCP

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Why use threads? Because threads have minimal internal state, it takes less time to create a thread than a process (10x speedup in UNIX). It takes less time to terminate a thread. It takes less time to switch to a different thread. A multi-threaded process is much cheaper than multiple (redundant) processes.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Examples of Using Threads Threads are useful for any application with multiple tasks that can be run with separate threads of control. A Word processor may have separate threads for:  User input  Spell check  grammar check  displaying graphics  document layout A web server may spawn a thread for each client  Can serve clients concurrently with multiple threads.  It takes less overhead to use multiple threads than to use multiple processes.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Benefits Responsiveness:  Threads allow a program to continue running even if part is blocked.  For example, a web browser can allow user input while loading an image. Resource Sharing:  Threads share memory and resources of the process to which they belong. Economy:  Allocating memory and resources to a process is costly.  Threads are faster to create and faster to switch between. Utilization of Multiprocessor Architectures:  Threads can run in parallel on different processors.  A single threaded process can run only on one processor no matter how many are available.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts User Threads Thread management may be done at either the user level or the kernel (OS) level. User Threads: Thread management done by user-level threads library The kernel is unaware that the process is multithreaded. Thread creation and scheduling is done in user space without kernel intervention.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Many-to-One Model

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts States of User Threads and Processes What happens when a process or thread is blocked? Suppose process B has two threads, of which thread 2 is running. The following conditions could occur: Thread 2 makes an I/O or other system call:  The kernel puts process B into a blocked state.  The thread data structure will still show thread 2 in the running state.  Thread 2 is not actually running. (The entire process is blocked). Process B exhausts its time slice:  The kernel puts process B into the ready state.  Thread 2 still in running state (but not actually running!) Thread 2 needs action performed by Thread 1:  Thread 2 goes into blocked state.  Thread 1 starts running.  Process B remains in running state.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Pros and Cons of User Level Threads: Pros:  Fast switch between threads (kernel is not needed).  Can have multi-threaded process on a system that does not understand threads.  Cheap to create and destroy threads.  User has complete control over threads (e.g. can assign priorities, etc.) Cons:  System call from one thread blocks all threads in the same process.  Process cannot use multiple processors.  Extra work for user.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts User Level Thread Libraries User Level Thread Libraries include: POSIX Pthreads Mach C-Threads Solaris-2 UI threads.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Kernel Level Threads Kernel Level Threads: All thread management is done by the Operating System. Each user-level thread maps to kernel thread. Kernel does creation, scheduling and management of threads. Examples - Windows 95/98/NT/ OS/2 - Solaris-2 - Some flavors of UNIX

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts One-to-one Model

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Pros and Cons of Kernel Level Threads Pros:  System call from a thread does not block other threads in the same process.  One process can use multiple processors.  Create/destroy/switch of threads is less expensive than for processes. Cons:  Create/destroy/switch of threads is more expensive than for user level threads.  CPU scheduling algorithms are unfair: Each thread is given the same time slice. Tasks with more threads are given more CPU time than those with fewer threads.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Combining User and Kernel Level Threads User and Kernel level threads execute simultaneously. Allows many user level threads to be mapped to many kernel threads. If one user level thread is blocked, another kernel thread may be chosen to execute. Assigning a User Level thread to a Kernel Level thread is implicit and hidden from the programmer. Examples of systems with combination of User and Kernel Level threads:  Solaris 2  Windows NT/2000 with the ThreadFiber package  Some other UNIX flavors.

Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Many-to-Many Model