Threads Chapter 5 1 Chapter 5

Similar documents
Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Threads, SMP, and Microkernels

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

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

Process Characteristics

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

ECE519 Advanced Operating Systems

Chapter 3 Process Description and Control

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

Chapter 4 Threads, SMP, and

Threads. CS3026 Operating Systems Lecture 06

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

DISTRIBUTED COMPUTER SYSTEMS

operating system Spring 2017 Prof.Dr. Hasan Balik Student Name : Walid.W. Ramadan mansour Student ID :

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

4. Concurrency via Threads

CSE 306/506 Operating Systems Threads. YoungMin Kwon

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

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

Process Description and Control

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

Chapter 4: Multi-Threaded Programming

Threads. studykorner.org

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

Threads, SMP, and Microkernels. Chapter 4

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

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

Operating Systems. Lecture 4. Nachos Appetizer

Processes and Threads

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

2017 Pearson Educa2on, Inc., Hoboken, NJ. All rights reserved.

IT 540 Operating Systems ECE519 Advanced Operating Systems

Module 1. Introduction:

Processes and Non-Preemptive Scheduling. Otto J. Anshus

SMD149 - Operating Systems

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

3.1 Introduction. Computers perform operations concurrently

CSE 153 Design of Operating Systems Fall 2018

Processes, Threads, SMP, and Microkernels

Process Description and Control. Chapter 3

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Process Description and Control. Chapter 3

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Definition Multithreading Models Threading Issues Pthreads (Unix)

Multithreaded Programming

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

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

Yi Shi Fall 2017 Xi an Jiaotong University

Operating Systems Overview. Chapter 2

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

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

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

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

Operating Systems. Chapter 4 Threads. Lei Duan

Process Description and Control

Introduction to Processes in Computer Systems SEEM

Operating System 4 THREADS, SMP AND MICROKERNELS

Course: Operating Systems Instructor: M Umair. M Umair

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors


Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

Process Description and Control. Major Requirements of an Operating System

Major Requirements of an Operating System Process Description and Control

Threads Assistant Professor DCS Operating System Concepts

Process Management Rab Nawaz Jadoon

CSC 539: Operating Systems Structure and Design. Spring 2006

Sistemi in Tempo Reale

Implementation of Processes

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

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

What s in a process?

Process Description and Control

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

Learning Outcomes. Processes and Threads. Major Requirements of an Operating System. Processes and Threads

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Operating System Architecture. CS3026 Operating Systems Lecture 03

Major Requirements of an OS

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Process- Concept &Process Scheduling OPERATING SYSTEMS

System Call. Preview. System Call. System Call. System Call 9/7/2018

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

CSCE 313: Intro to Computer Systems

Chapter 4: Threads. Chapter 4: Threads

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

! The Process Control Block (PCB) " is included in the context,

OPERATING SYSTEM. Chapter 4: Threads

Process Description and Control

Transcription:

Threads Chapter 5 1 Chapter 5

Process Characteristics Concept of Process has two facets. A Process is: A Unit of resource ownership: a virtual address space for the process image control of some resources (files, I/O devices...) A Unit of execution - process is an execution path through one or more programs may be interleaved with other processes execution state (Ready, Running, Blocked...) and dispatching priority 2

Process Characteristics These two characteristics are treated separately by some recent operating systems: The unit of resource ownership is usually referred to as a process or task The unit of execution is usually referred to a thread or a lightweight process 3

Multithreading vs. Single threading Multithreading: The OS supports multiple threads of execution within a single process Single threading: The OS does not recognize the separate concept of thread MS-DOS supports a single user process and a single thread Traditional UNIX supports multiple user processes but only one thread per process Solaris and Windows 2000 support multiple threads 4

Threads and Processes Single Threading Multi-Threading 5

In a Multithreaded Environment, Processes Have: A virtual address space which holds the process image Protected access to processors, other processes (inter-process communication), files, and other I/O resources 6

While Threads... Have execution state (running, ready, etc.) Save thread context (e.g. program counter) when not running Have private storage for local variables and execution stack Have shared access to the address space and resources (files etc.) of their process when one thread alters (non-private) data, all other threads (of the process) can see this threads communicate via shared variables a file opened by one thread is available to others 7

Single Threaded and Multithreaded Process Models 8 Thread Control Block contains a register image, thread priority and thread state information

Benefits of Threads vs Processes Far less time to create a new thread than a new process Less time to terminate a thread than a process Less time to switch between two threads within the same process than to switch between processes Threads can communicate via shared memory processes have to rely on kernel services for IPC 9

Application benefits of threads Consider an application that consists of several independent parts that do not need to run in sequence Each part can be implemented as a thread Whenever one thread is blocked waiting for I/O, execution could switch to another thread of the same application (instead of switching to another process) 10

Benefits of Threads Example 1: File Server on a LAN Needs to handle many file requests over a short period Threads can be created (and later destroyed) for each request If multiple processors: different threads could execute simultaneously on different processors Example 2: Spreadsheet on a single processor machine: One thread displays menu and reads user input while the other executes the commands and updates display 11

Thread States Three key states: Running, Ready, Blocked No Suspend state because all threads within the same process share the same address space (same process image) Suspending implies swapping out the whole process, suspending all threads in the process Termination of a process terminates all threads within the process Because the process is the environment the thread runs in 12

Thread Operations 13 Spawn: Process starts with one thread. That thread can spawn another thread, placing the new thread on the Ready queue Block (yield, suspend): Save PC, registers, etc. and allow other thread(s) to run Could block whole process if making system call which requires kernel service, otherwise it s a single thread being suspended. Unblock (wake): IO finishes, or another relinquishes control, thread moves to Ready queue Finish (terminate): Deallocate context (stacks etc.)

User-Level Threads (ULT) (ex. Java) Kernel not aware of the existence of threads Thread management handled by thread library in user space No mode switch (kernel not involved) But I/O in one thread could block the entire process! Many-to-One model 14

Threads library Contains code for: creating and destroying threads passing messages and data between threads scheduling thread execution pass control from one thread to another saving and restoring thread contexts ULT s can be be implemented on any Operating System, because no kernel services are required to support them 15

Kernel Role for ULTs (None!) The kernel is not aware of thread activity it only manages processes If a thread makes an I/O call, the whole process is blocked Note: in the thread library that thread is still in running state, and will resume execution when the I/O is complete So thread states are independent of process states 16

Advantages and disadvantages of ULT 17 Advantages Thread switching does not involve the kernel: no mode switching Therefore fast Scheduling can be application specific: choose the best algorithm for the situation. Can run on any OS. We only need a thread library Disadvantages Most system calls are blocking for processes. So all threads within a process will be implicitly blocked The kernel can only assign processors to processes. Two threads within the same process cannot run simultaneously on two processors

Kernel-Level Threads (KLT) Ex: Windows NT, Windows 2000, OS/2 All thread management is done by kernel No thread library; instead an API to the kernel thread facility Kernel maintains context information for the process and the threads Switching between threads requires the kernel Kernel does Scheduling on a thread basis One-to-One model 18

Advantages and disadvantages of KLT Advantages The kernel can schedule multiple threads of the same process on multiple processors Blocking at thread level, not process level If a thread blocks, the CPU can be assigned to another thread in the same process Even the kernel routines can be multithreaded Disadvantages Thread switching always involves the kernel. This means 2 mode switches per thread switch So it is slower compared to User Level Threads (But faster than a full process switch) 19

Combined ULT/KLT Approaches (e.g. Solaris) Thread creation done in the user space Bulk of thread scheduling and synchronization done in user space ULT s mapped onto KLT s The programmer may adjust the number of KLTs KLT s may be assigned to processors Combines the best of both approaches Many-to-Many model 20

Solaris Process includes the user s address space, stack, and process control block User-level threads (threads library) invisible to the OS are the interface for application parallelism Kernel threads the unit that can be dispatched on a processor Lightweight processes (LWP) each LWP supports one or more ULTs and maps to exactly one KLT 21

Solaris Threads bound thread 22 Task 2 is equivalent to a pure ULT approach ( = Old Unix) Tasks 1 and 3 map one or more ULT s onto a fixed number of LWP s (&KLT s) Note how task 3 maps a single ULT to a single LWP bound to a CPU

Solaris: Kernel Level Threads Only objects scheduled within the system May be multiplexed on the CPU s or tied to a specific CPU Each LWP is tied to a kernel level thread 23

Solaris: User Level Threads Share the execution environment of the task Same address space, instructions, data, file (any thread opens file, all threads can read). Can be tied to a LWP or multiplexed over multiple LWPs Represented by data structures in address space of the task but kernel knows about them indirectly via LWPs 24

Solaris: versatility We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching) Ex: Multiple windows but only one is active at any one time If ULT threads can block then we can add more LWPs to avoid blocking the whole application Note versatility of SOLARIS that can operate like Windows-NT or like conventional Unix 25

Solaris: Light-Weight Processes A UNIX process consists mainly of an address space and a set of LWPs that share the address space Each LWP is like a virtual CPU and the kernel schedules the LWP by the KLT that it is attached to 26

Combination of ULT and KLT Run-time library (RTL) ties together Multiple threads handled by RTL If 1 thread makes system call, LWP makes call, LWP will block, all threads tied to LWP will block Any other thread in same task will not block. 27

Why both threads and LWPs? Don t want kernel to know about the ULTs if knows it has to allocate data structure for it and be involved in context switching among ULTs Lots of work, allocate instead to RTL But kernel knows about LWPs and the ULTs can communicate with kernel through the LWPs 28

Trouble spots: Files File descriptors are shared Thread A opens file, all can read Thread B closes file, all threads lose access Read/write/seek file only has 1 offset pointer, each read/write/seek changes position of OP for each thread 29

Trouble spots: Global variables How to fix? No global variables Not practical May not work if OS sets value Keep private globals on a stack Thread library handles Special library procedures to set and read global variables Thread library deals w/ errno s and globals 30

Trouble spots: directory Only one working directory for a process If 1 thread changes it, changed for entire process (all other threads) Only 1 set of user and group Ids Every thread has equal access to all files and priviledges Potential security problems 31