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

Similar documents
! 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

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

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

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

CS 450 Operating System Week 4 Lecture Notes

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Chapter 4: Threads. Chapter 4: Threads

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

Chapter 4: Multithreaded

Chapter 4: Multithreaded Programming

Chapter 3: Processes. Operating System Concepts 8th Edition

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

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

Chapter 4 Multithreaded Programming

OPERATING SYSTEM. Chapter 4: Threads

Processes and Threads

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

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

Chapter 4: Multi-Threaded Programming

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

Threads. CS3026 Operating Systems Lecture 06

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

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

Chapter 4: Threads. Chapter 4: Threads

CS307: Operating Systems

Multithreaded Programming

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

Lecture 4 Threads. (chapter 4)

Process Description and Control

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

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

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

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

Chapter 4: Multithreaded Programming

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

Lecture 2 Process Management

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

CS333 Intro to Operating Systems. Jonathan Walpole

What s in a process?

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

CHAPTER 2: PROCESS MANAGEMENT

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

CISC2200 Threads Spring 2015

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

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

SMD149 - Operating Systems

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

CSE 120 Principles of Operating Systems

3.1 Introduction. Computers perform operations concurrently

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Lecture 4: Threads; weaving control flow

Chapter 4: Processes. Process Concept

Chapter 4: Multithreaded Programming

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

Chapter 4: Multithreaded Programming

Chapter 4: Processes

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

Last class: Today: Thread Background. Thread Systems

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

Chapter 5: Threads. Outline

Chapter 3: Process Concept

Chapter 3: Process Concept

CSE 120 Principles of Operating Systems

Chapter 3: Processes. Operating System Concepts 8 th Edition,

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

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

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

CS370 Operating Systems

Chapter 3 Process Description and Control

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

CS510 Operating System Foundations. Jonathan Walpole

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

4. Concurrency via Threads

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

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

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

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

CSC 539: Operating Systems Structure and Design. Spring 2006

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

THREADS. Jo, Heeseung

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts 8th Edition,

What s in a traditional process? Concurrency/Parallelism. What s needed? CSE 451: Operating Systems Autumn 2012

Threads. Today. Next time. Why threads? Thread model & implementation. CPU Scheduling

CSE 4/521 Introduction to Operating Systems

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

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Chapter 5: Threads. Single and Multithreaded Processes

CSE 120 Principles of Operating Systems

Concurrency, Thread. Dongkun Shin, SKKU

Transcription:

Chapter 2: Threads: Questions CSCI [4 6]730 Operating Systems Threads!! How is a thread different from a process?!! Why are threads useful?!! How can OSIX threads be useful?!! What are user-level and kernel-level threads?!! What are problems with threads? 2 Course hilosophy Review: What is a rocess?!! Textbook / Course difference»! Threads»! Scheduling»! Synchronization»! Interrupt (I/O)!! ractice/ractical Considerations!! Incremental rogramming (HW) 3 A thread have (1) an execution stream and (2) a context!! Execution stream»! stream of instructions»! sequential sequence of instructions»! thread of control!! rocess context (seen picture of this already)»! Everything needed to run (restart) the process»! Registers! program counter, pointer, general purpose»! Address space! Everything the process can access in memory! Heap,, code A process is a program in execution Running on a thread code data files 4 Review: What Makes up a rocess? What are are problem s with rocesses?!! rogram code (text)!! Data»! global variables»! heap (dynamically allocated memory)!! rocess»! function parameters»! return addresses»! local variables and functions!! OS Resources!! Registers»! program counter, pointer heap text data User Mode Address Space var1 var2 main routine2 arraya arrayb!! How do processes (independent memory space) communicate?»! Not really that simple (seen it, tried it and you have too):!! roblems:! Message passing (send and receive)! Shared Memory: Set up a shared memory area (easier)?»! Overhead: Both methods add some kernel overhead lowering performance»! Complicated: IC is not really that natural! increases the complexity of your code address space are the shared resources of a(ll) thread(s) in a program 5 6

rocesses versus Threads What Makes up a Thread? Solution: A thread is a lightweight process (LW)!! An execution stream that shares an address space»! Overcome data flow over a file descriptor»! Overcome setting up `tighter memory space!! Multiple threads within a single process Examples: main() { i = 55; fork(); // what is i!! Two processes (copies of each other) examining memory address 0xffe84264 see different values (i.e., different contents)»! same frame of reference!! Two threads examining memory address 0xffe84264 see same value (i.e., same contents)!! Illustrate: i-threading.c, i-forking.c 7!! Own (necessary?)!! Own (necessary?)»! Own program counter»! Own pointer!! State (running, sleeping)!! Signal mask heap text data User Mode Address Space var1 var2 main routine2 arraya arrayb address space are the shared resources of a(ll) thread(s) in a program rogram Counter Stack ointer var1 var2 8 Single and Multithreaded rocess Why Support Threads? code data files code data files!! Divide large task across several cooperative threads!! Multi-threaded task has many performance benefits 9!! Examples:»! Web Server: create threads to:! Get network message from client! Get URL data from disk! Compose response! Send a response»! Word processor: create threads to:! Display graphics! Read keystrokes from users! erform spelling and grammar checking in background 10 Why Support Threads? Why Threads instead of a rocesses?!! Divide large task across several cooperative threads!! Multi-threaded task has many performance benefits!! Adapt to slow devices»! One thread waits for device while other threads computes!! Defer work»! One thread performs non-critical work in the background, when idle!! arallelism»! Each thread runs simultaneously on a multiprocessor 11 of Threads:»! Thread operations cheaper than corresponding process operations! In terms of: Creation, termination, (context) switching»! IC cheap through shared memory! No need to invoke kernel to communicate between threads of Threads:»! True Concurrent programming is a challenge (what does this mean? True concurrency?)»! Synchronization between threads needed to use shared variables (more on this later this is HARD). 12

Why are Threads Challenging? pthread1 Example: Output? Why are Threads Challenging? main()! {!!pthread_t t1, t2;!!char *msg1 = Thread 1 ; char *msg2 = Thread 2 ;!!int ret1, ret2;!!ret1 = pthread_create( &t1, NULL, print_fn, (void *)msg1 );!!ret2 = pthread_create( &t2, NULL, print_fn, (void *)msg2 );!!if( ret1 ret2 )!!{!!!fprintf(stderr, ERROR: pthread_created failed.\n );!!!exit(1);!!}!!pthread_join( t1, NULL );!!pthread_join( t2, NULL );!!printf( Thread 1 and thread 2 complete.\n );! }! void print_fn(void *ptr)! {!!printf( %s\n, (char *)ptr);! }! 13!! Example: Transfer $50.00 between two accounts and output the total balance of the accounts:!! Tasks: M = Balance in Maria s account (begin $100) T = Balance in Tucker s account (begin $50) B = Total balance T = 50, M = 100 Idea: on distributing the tasks: (1)!One thread debits and credits (2)! Another Totals Does that work 14 Why are Threads Challenging? Common rogramming Models!! Tasks: T = 50, M = 100 One thread debits & credits One thread totals!! Manager/worker»! Single manager handles input and assigns work to the worker threads!! roducer/consumer»! Multiple producer threads create data (or work) that is handled by one of the multiple consumer threads!! ipeline»! Task is divided into series of subtasks, each of which is handled in series by a different thread B = $150 B = $100 B = $150 15 16 Thread Support Latencies!! Three approaches to provide thread support»! User-level threads»! Kernel-level threads»! Hybrid of User-level and Kernel-level threads!! Comparing user-level threads, kernel threads, and processes.!! Null fork: the time to create, schedule, execute, and complete the entity that invokes the null procedure (overhead of creating a thread)!! Signal-Wait: the time for an entity to signal a waiting entity and then wait on a condition (overhead of synchronization) rocedure call = 7 us Kernel Trap = 17 us User Level Threads Kernel Level Threads rocesses Null fork 34 948 11,300 Signal-wait 37 441 1,840 17 18

User-Level Threads Blocked UL Threads: Jacketing!! Many-to-one thread mapping»! Implemented by user-level runtime libraries! Create, schedule, synchronize threads at user-level»! OS is not aware of user-level threads! OS thinks each process contains only a single thread of control»! Does not require OS support; ortable»! Can tune scheduling policy to meet application (user level) demands»! Lower overhead thread operations since no system calls!! Avoids blocking on system calls that block (e.g., I/O)!! Solution:»! Instead of calling a blocking system call call an application level I/O jacket routine (a nonblocking call)»! Jacket routine provides code that determines whether I/O device is busy or available (idle).»! Busy:! Thread enters the ready state and passes control to another thread! Control returns to thread it retries»! Idle:! Thread is allowed to make system call.»! Cannot leverage multiprocessors (no true parallelism)»! Entire process blocks when one thread blocks 19 20 Kernel-Level Threads Two-Level Model!! One-to-one thread mapping»! OS provides each user-level thread with a kernel thread»! Each kernel thread scheduled independently»! Thread operations (creation, scheduling, synchronization) performed by OS»! Each kernel-level thread can run in parallel on a multiprocessor»! When one thread blocks, other threads from process can be scheduled»! Higher overhead for thread operations»! OS must scale well with increasing number of threads 21!! one-one & (strict) many-to-many»! OS provides each user-level thread with a kernel thread»! Supports both bound an unbound threads! Bound threads - permanently bound to a single kernel level thread! Unbound threads may move to other kernel threads»! Flexible, best of two worlds»! More complicated 22 Hybrid of Kernel & User -Level Threads Thread Models!! m - n thread mapping (many to many)»! Application creates m threads»! OS provides pool of n kernel threads»! Few user-level threads mapped to each kernel-level thread»! Can get best of user-level and kernel-level implementations»! Works well given many short-lived user threads mapped to constant-size pool»! Complicated!! Kernel Level: Windows 95/98/NT/2000, Solaris, Linux!! User Level: OSIX threads, Mach, C-threads, Solaris threads!! Hybrids: IRIX, H-UX, True 64 UNIX, Older Solaris models»! How to select mappings?»! How to determine the best number of kernel threads?! User specified! OS dynamically adjusts number depending on system load 23 24

Threading Issues: fork() & exec() Threading Issues: Cancellation!! fork()»! Duplicate all threads?»! Duplicate only the thread that performs the fork»! Resulting new process is single threaded?»! -> solution provide two different forks!! exec()»! Replaces the process - including all threads?»! If exec is after fork then replacing all threads is unnecessary.!! Example 1: User pushes top button on a web browsers - while other threads are images (one thread per image).!! Example 2: Several threads concurrently searches data base and one thread finds target data.!! Asynchronous Cancellation: Immediate (OS need to reclaim resources)!! Deferred Cancellation: Thread terminates it self when notices it is scheduled for termination. 25 26 Threading Issues: Threads and Signals Other Thread Issues!! roblem: To which thread should OS deliver signal?!! Option 1: Require sender to specify thread ID (instead of process id)»! Sender may not know about individual threads!! Option 2: OS picks destination thread»! OSIX: Each thread has signal mask (disable specified signals)»! OS delivers signal to all threads without signal masked»! Application determines which thread is most appropriate for handing signal!! Synchronous - delivered to the same process that caused the signal!! Asynchronous - event is external to running process.!! Creating thread is costly!! No bound of number of threads 27 28 Thread ools IC: Shared Memory!! Create a number of threads in a pool where a number of threads await work :»! Usually slightly faster to service a request with an existing thread than waiting to create a new thread»! Allows the number of threads in the application(s) to be bound to the size of the pool!! The number of threads can be set heuristically based on the hardware and can even be dynamically adjusted taking into account user statistics.!! rocesses»! Each process has private address space»! Explicitly set up shared memory segment within each address space!! Threads»! Always share address space (use heap for shared data)»! Fast and easy to share data»! Must synchronize data accesses; error prone (later) 29 30

IC: Message assing IC: Signals!! Message passing most commonly used between processes»! Explicitly pass data between sender (src) + receiver (destination)»! Example: Unix pipes :»! Makes sharing explicit»! Improves modularity (narrow interface)»! Does not require trust between sender and receiver :»! erformance overhead to copy messages!! 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 31!! Signal»! Software interrupt that notifies a process of an event»! Examples: SIGFE, SIGKILL, SIGUSR1, SIGSTO, SIGCONT!! 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 32 Old Quiz Scheduler Activations Notes (Open Slot) 1.! What resources (context) within a process are shared between threads?»! (5) Address space 2.! What resources (context) cannot be shared among threads within the same process?»! (3) A thread need: (3) Own and (5) [ missing one -2] 3.! What happens to other threads within the same process when a thread reads from disk?»! (5) Blocks (3) if user level (2) 4.! Name a user level thread package?»! (5) -threads (posix threads) 5.! Do Java threads use kernel or user level threads (Justify your answer)? 1.! (4) 3 for a guess (4) for correct!! rovides better OS support for user level threading»! Dynamic adjustment of number of kernel level threads to user level threads:! E.g. Two level and the m:n thread models need to maintain appropriate ratios»! Key Idea: Kernel notifies thread scheduler of all kernel events via upcalls 33 34 Scheduler Activations Scheduler Activations!! Use an intermediate data structure between user/kernel level threads.!! Details: User level threads run and are scheduled (by the user level scheduler) on virtual processor»! A data structure or light-weigh process (LW) that is between the kernel thread and the user thread.»! Each LW is attached to a kernel thread and kernel threads are what the OS schedules to run on physical processors. User Level Thread LW Kernel Level Thread!! An application may require any number of LWs to run efficiently.»! Example: A CU-bound application on a single processor.! Needs only one LW.»! Example: An I/O-bound application! May need many LWs- one for each concurrent blocking system since if there are not enough LWs, the unassigned threads must wait for one of the LWs to return from the kernel. 35 36

Scheduler Activations!! Why not a user level thread scheduler that spawns a kernel thread for blocking operations?»! Forget spawning, use a pool of kernel threads.»! But how do we know if an operation will block?! read might block, or data might be in page cache.! Any memory reference might cause a page fault to disk.!! Scheduler Activations!! Kernel tells user when a thread is going to block, via an upcall.»! Kernel can provide a kernel thread to run the user-level upcall handler (or preempt user thread).»! User-level scheduler suspends blocking thread and can give back kernel thread it was running on. 37