Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Similar documents
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4: Threads. Chapter 4: Threads

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CS307: Operating Systems

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Chapter 4: Multi-Threaded Programming

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 4: Multithreaded

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Multithreaded Programming

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Chapter 5: Threads. Single and Multithreaded Processes

Operating Systems Prof. Ashok K Agrawala

Chapter 4 Multithreaded Programming

Chapter 5: Threads. Outline

CSE 4/521 Introduction to Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Chapter 4: Multithreaded Programming

CSCE 313: Intro to Computer Systems

Chapter 4: Multithreaded Programming

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Processes and Threads

Processes and Threads

Chapter 3: Processes. Operating System Concepts 8th Edition

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

CS420: Operating Systems

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Chapter 4: Threads. Operating System Concepts 8 th Edition,

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

Multithreaded Programming

Lecture 4 Threads. (chapter 4)

CISC2200 Threads Spring 2015

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Semantics of fork() and exec()

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

CS420: Operating Systems

Process Description and Control

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

Lecture 2 Process Management

CS370 Operating Systems

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Threads. CS3026 Operating Systems Lecture 06

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

ICS Principles of Operating Systems

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

THREADS. Jo, Heeseung

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Definition Multithreading Models Threading Issues Pthreads (Unix)

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Yi Shi Fall 2017 Xi an Jiaotong University

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

CSE Opera,ng System Principles

Computer Systems Laboratory Sungkyunkwan University

CHAPTER 2: PROCESS MANAGEMENT

4.8 Summary. Practice Exercises

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Chapter 3 Process Description and Control

Chapter 4: Threads. Operating System Concepts 9 th Edition

Concurrency, Thread. Dongkun Shin, SKKU

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

COP 4610: Introduction to Operating Systems (Fall 2016) Chapter 4: Threads. Zhi Wang Florida State University

CS370 Operating Systems

Distributed Systems Operation System Support

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Lecture 9. Remote Procedure Calls Introduction to Multithreaded Programming

Questions from last time

CS420: Operating Systems. OS Services & System Calls

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Thread. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Chapter 3: Processes. Operating System Concepts 9 th Edition

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

CS370 Operating Systems

CS370 Operating Systems

Transcription:

Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads 5.1 Silberschatz, Galvin and Gagne 2003

More About Processes A process encapsulates a running program, providing an execution state along with certain resources, including file handles and registers, along with: a program counter (Instruction Pointer) a process id, a process group id, etc. a process stack one or more data segments a heap for dynamic memory allocation a process state (running, ready, waiting, etc.) Informally, a process is an executing program 5.2 Silberschatz, Galvin and Gagne 2003

Multiprocessing A multiprocessing or multitasking operating system (like Unix, as opposed to DOS) can have more than one process executing at any given time This simultaneous execution may either be concurrent, meaning that multiple processes in a run state can be swapped in and out by the OS parallel, meaning that multiple processes are actually running at the same time on multiple processors 5.3 Silberschatz, Galvin and Gagne 2003

What is a Thread? A thread is an encapsulation of some flow of control in a program, that can be independently scheduled Each process is given a single thread by default A thread is sometimes called a lightweight process, because it is similar to a process in that it has its own thread id, stack, stack pointer, a signal mask, program counter, registers, etc. All threads within a given process share resource handles, memory segments (heap and data segments), and code. 5.4 Silberschatz, Galvin and Gagne 2003

Single and Multithreaded Processes 5.5 Silberschatz, Galvin and Gagne 2003

Process/Thread A P R O C E S S A T H R E A D P r o c e s s I D T h r e a d I D P r o g r a m C o u n t e r P r o g r a m C o u n t e r S i g n a l D i s p a t c h T a b l e S i g n a l D i s p a t c h T a b l e R e g i s t e r s R e g i s t e r s P r o c e s s P r i o r i t y T h r e a d P r i o r i t y S t a c k P o i n t e r & S t a c k S t a c k P o i n t e r & S t a c k H e a p M e m o r y M a p F i l e D e s c r i p t o r T a b l e A l l t h r e a d s s h a r e t h e s a m e m e m o r y, h e a p, a n d f i l e h a n d l e s ( a n d o f f s e t s ) 5.6 Silberschatz, Galvin and Gagne 2003

Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures 5.7 Silberschatz, Galvin and Gagne 2003

Processes and Threads: Creation Times Because threads are by definition lightweight, they can be created more quickly that heavy processes: Sun Ultra5, 320 Meg Ram, 1 CPU 94 forks()/second 1,737 threads/second (18x faster) Sun Sparc Ultra 1, 256 Meg Ram, 1 CPU 67 forks()/second 1,359 threads/second (20x faster) Sun Enterprise 420R, 5 Gig Ram, 4 CPUs 146 forks()/second 35,640 threads/second (244x faster) Linux 2.4 Kernel,.5 Gig Ram, 2 CPUs 1,811 forks()/second 227,611 threads/second (125x faster) 5.8 Silberschatz, Galvin and Gagne 2003

Benefits of Multithreading Performance gains Amdahl s Law: speedup = 1 / ((1 p) + (p/n)) the speedup generated from parallelizing code is the time executing the parallelizable work (p) divided by the number of processors (n) plus 1 minus the parallelizable work (1-p) The more code that can run in parallel, the faster the overall program will run If you can apply multiple processors for 75% of your program s execution time, and you re running on a dual processor box: 1 / ((1 -.75) + (.75 / 2)) = 60% improvement Why is it not strictly linear? How do you calculate p? 5.9 Silberschatz, Galvin and Gagne 2003

User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX (IEEE Portable Operating System Interface) Pthreads Java threads Win32 threads 5.10 Silberschatz, Galvin and Gagne 2003

Kernel Threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X 5.11 Silberschatz, Galvin and Gagne 2003

Many-to-One One-to-One Many-to-Many Multithreading Models 5.12 Silberschatz, Galvin and Gagne 2003

Many-to-One Many user-level threads mapped to single kernel thread Examples Solaris Green Threads used by early JVMs GNU Portable Threads 5.13 Silberschatz, Galvin and Gagne 2003

Many-to-One Model 5.14 Silberschatz, Galvin and Gagne 2003

One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later 5.15 Silberschatz, Galvin and Gagne 2003

One-to-one Model 5.16 Silberschatz, Galvin and Gagne 2003

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 5.17 Silberschatz, Galvin and Gagne 2003

Many-to-Many Model 5.18 Silberschatz, Galvin and Gagne 2003

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 5.19 Silberschatz, Galvin and Gagne 2003

Two-level Model 5.20 Silberschatz, Galvin and Gagne 2003

Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations Does fork() duplicate only the calling thread or all threads? 5.21 Silberschatz, Galvin and Gagne 2003

Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled 5.22 Silberschatz, Galvin and Gagne 2003

Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals Signal is generated by particular event CPU interrupt, I/O completion, mouse click,... Signal is delivered to a process Signal is handled 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 threa to receive all signals for the process 5.23 Silberschatz, Galvin and Gagne 2003

Thread Pools Create a number of threads in a pool where they await work 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 5.24 Silberschatz, Galvin and Gagne 2003

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 Common in UNIX operating systems (Solaris, Linux, Mac OS X) 5.25 Silberschatz, Galvin and Gagne 2003

POSIX Each OS had its own thread library and style That made writing multithreaded programs difficult because: you had to learn a new API with each new OS you had to modify your code with each port to a new OS POSIX (IEEE 1003.1c-1995) provided a standard known as Pthreads Unix International (UI) threads (Solaris threads) are available on Solaris (which also supports POSIX threads) 5.26 Silberschatz, Galvin and Gagne 2003

Windows XP Threads Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) 5.27 Silberschatz, Galvin and Gagne 2003

Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) 5.28 Silberschatz, Galvin and Gagne 2003

Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface 5.29 Silberschatz, Galvin and Gagne 2003

Java Thread States 5.30 Silberschatz, Galvin and Gagne 2003

On the Scheduling of Threads Threads may be scheduled by the system scheduler (OS) or by a scheduler in the thread library (depending on the threading model). The scheduler in the thread library: will preempt currently running threads on the basis of priority does NOT time-slice (i.e., is not fair). A running thread will continue to run forever unless: a thread call is made into the thread library a blocking call is made the running thread calls sched_yield() 5.31 Silberschatz, Galvin and Gagne 2003

Chapter 5 Homework Write a multithreaded program Java, or Pthreads pg 169, 5.9, 5.10, 5.11, OR an MT program of your choice write, compile, run, and monitor your program as it runs show source code, output of run show results of monitoring the program run...execution time, memory use, thread execution write a program means read/research/understand existing code fragments and examples, prepare a source file, compile and run the program, explain the execution and output it does NOT mean simply copy/modify other's solution to the assignment 5.32 Silberschatz, Galvin and Gagne 2003