Yi Shi Fall 2017 Xi an Jiaotong University

Similar documents
From Processes to Threads

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

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

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

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

From Processes to Threads

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

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

Lecture 4: Threads; weaving control flow

CSE 120 Principles of Operating Systems

Processes, Threads and Processors

OPERATING SYSTEM. Chapter 4: Threads

Chapter 5: Threads. Outline

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

CS 450 Operating System Week 4 Lecture Notes

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

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

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Threads. CSE 410, Spring 2004 Computer Systems.

Chapter 4: Threads. Chapter 4: Threads

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

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

Chapter 4: Multi-Threaded Programming

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

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

Chapter 4: Multithreaded

CS 153 Design of Operating Systems Winter 2016

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

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

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

Chapter 4: Multithreaded Programming

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

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

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

Threads. CS3026 Operating Systems Lecture 06

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

CS307: Operating Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Processes and Threads

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

THREADS. Jo, Heeseung

Chapter 3 Process Description and Control

Chapter 4: Multithreaded Programming

Process Description and Control

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

Chapter 4: Multithreaded Programming

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

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

Concurrent Programming with Threads: Why you should care deeply

Last class: Today: Thread Background. Thread Systems

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

What s in a process?

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

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

CSE 153 Design of Operating Systems Fall 2018

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

Computer Systems Laboratory Sungkyunkwan University

COMP 530: Operating Systems Concurrent Programming with Threads: Why you should care deeply

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

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

CS 318 Principles of Operating Systems

Processes and Non-Preemptive Scheduling. Otto J. Anshus

ICS Principles of Operating Systems

Threads (light weight processes) Chester Rebeiro IIT Madras

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

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

Threads Chapter 5 1 Chapter 5

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

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

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

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Disciplina Sistemas de Computação

Threads. CSE Computer Systems November 16, 2001

Chapter 3: Processes. Operating System Concepts 8th Edition

Distributed Systems Operation System Support

W4118 Operating Systems. Junfeng Yang

Lecture 2 Process Management

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

CSE 153 Design of Operating Systems Fall 2018

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

CS 318 Principles of Operating Systems

CISC2200 Threads Spring 2015

CS 31: Intro to Systems Threading & Parallel Applications. Kevin Webb Swarthmore College November 27, 2018

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

CSE 4/521 Introduction to Operating Systems

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

Chapter 4 Threads, SMP, and

Threads, SMP, and Microkernels

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Process Characteristics

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

Transcription:

Threads Yi Shi Fall 2017 Xi an Jiaotong University

Goals for Today Case for Threads Thread details

Case for Parallelism main() read_data() for(all data) compute(); write_data(); endfor main() read_data() for(all data) compute(); CreateProcess(write_data()); endfor

Case for Parallelism Consider the following code fragment on a dual core CPU for(k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k]; Instead: CreateProcess(fn, 0, n/2); CreateProcess(fn, n/2, n); fn(l, m) for(k = l; k < m; k++) a[k] = b[k] * c[k] + d[k] * e[k];

Case for Parallelism Consider a Web server create a number of processes, and for each process do get network message from client get URL data from disk compose response send response Server connections are fast, but client connections may not be (grandma s modem connection) Takes server a loooong time to feed the response to grandma While it s doing that it can t service any more requests

Parallel Programs To build parallel programs, such as: Parallel execution on a multiprocessor Web server to handle multiple simultaneous web requests We will need to: Create several processes that can execute in parallel Cause each to map to the same address space because they re part of the same computation Give each its starting address and initial parameters The OS will then schedule these processes in parallel

Processes Overheads A full process includes numerous things: an address space (defining all the code and data pages) OS resources and accounting information a thread of control, defines where the process is currently executing That is the PC and registers Creating a new process is costly all of the structures (e.g., page tables) that must be allocated Communicating between processes is costly most communication goes through the OS

Need Lightweight Processes What s similar in these processes? They all share the same code and data (address space) They all share the same privileges They share almost everything in the process What don t they share? Each has its own PC, registers, and stack pointer Idea: why don t we separate the idea of process (address space, accounting, etc.) from that of the minimal thread of control (PC, SP, registers)?

Threads and Processes Most operating systems therefore support two entities: the process, which defines the address space and general process attributes the thread, which defines a sequential execution stream within a process A thread is bound to a single process. For each process, however, there may be many threads. Threads are the unit of scheduling Processes are containers in which threads execute

Multithreaded Processes

Threads vs. Processes A thread has no data segment or heap A thread cannot live on its own, it must live within a process There can be more than one thread in a process, the first thread calls main & has the process s stack Inexpensive creation Inexpensive context switching If a thread dies, its stack is reclaimed A process has code/data/heap & other segments There must be at least one thread in a process Threads within a process share code/data/heap, share I/O, but each has its own stack & registers Expensive creation Expensive context switching If a process dies, its resources are reclaimed & all threads die

Separating Threads and Processes Makes it easier to support multithreaded applications Different from multiprocessing, multiprogramming, multitasking Concurrency (multithreading) is useful for: improving program structure handling concurrent events (e.g., web requests) building parallel l programs Resource sharing Multiprocessor utilization Is multithreading useful even on a single processor?

Cooperative Threads A cooperative thread runs until it decides to give up the CPU main() { tid t1 = CreateThread(fn, arg); Yield(t1); } fn(int arg) { Yield(any); }

Cooperative Threads Cooperative threads use non pre-emptive scheduling Advantages: Simple Scientific apps Disadvantages: For badly written code Scheduler gets invoked only when Yield is called A thread could yield the processor when it blocks for I/O

Non-Cooperative Threads No explicit control passing among threads Rely on a scheduler to decide which thread to run A thread can be pre-empted at any point Often called pre-emptive threads Most modern thread packages use this approach

Multithreading models There are actually 2 level of threads: Kernel threads: Supported and managed directly by the kernel. User threads: Supported above the kernel, and without kernel knowledge.

Kernel Threads Kernel threads still suffer from performance problems Operations on kernel threads are slow because: a thread operation still requires a system call kernel threads may be overly general to support needs of different users, languages, etc. the kernel doesn t trust the user there must be lots of checking on kernel calls

User-Level Threads For speed, implement threads at the user level A user-level thread is managed by the run-time system user-level code that is linked with your program Each thread is represented simply by: PC Registers Stack Small control block All thread operations are at the user-level: Creating a new thread switching between threads synchronizing between threads

User-Level Threads User-level threads the thread scheduler is part of a library, outside the kernel thread context switching and scheduling is done by the library Can either use cooperative or pre-emptive threads cooperative threads are implemented by: CreateThread(), DestroyThread(), Yield(), Suspend(), etc. pre-emptive threads are implemented with a timer (signal) where the timer handler decides which thread to run next

Example User Thread Interface t = thread_fork(initial context) create a new thread of control thread_stop() stop the calling thread, sometimes called thread_block thread_start(t) start the named thread thread_yield() voluntarily give up the processor thread_exit() terminate the calling thread, sometimes called thread_destroy

Key Data Structures your process address space your program: for i (1, 10, I++) thread_fork(i);. user-level thread code: proc thread_fork() your data (shared by all your threads): queue of thread control blocks proc thread_block() proc thread_exit()... per-thread stacks

User-Level vs. Kernel Threads User-Level Managed by application Kernel not aware of thread Context switching cheap Create as many as needed Must be used with care Kernel-Level Managed by kernel Consumes kernel resources Context switching expensive Number limited by kernel resources Simpler to use Key issue: kernel threads provide virtual processors to user-level threads, but if all of kthreads block, then all user-level threads will block even if the program logic allows them to proceed

Multiplexing User-Level Threads The user-level thread package sees a virtual processor(s) it schedules user-level threads on these virtual processors each virtual processor is implemented by a kernel thread The big picture: Create as many kernel threads as there are processors Create as many user-level threads as the application needs Multiplex user-level threads on top of the kernel-level threads Why not just create as many kernel-level l l threads as app needs? Context switching Resources

Many-to-One Model user-level threads K Thread creation, scheduling, synchronization done in user space. Mainly used in language systems, portable libraries Fast - no system calls required Few system dependencies; portable No parallel execution of threads - can t exploit multiple CPUs All threads block when one uses synchronous I/O

One-to-one one Model user-level threads K K K Thread creation, scheduling, synchronization require system calls Used in Linux Threads, Windows NT, Windows 2000, OS/2 More concurrency Better multiprocessor performance Each user thread requires creation of kernel thread Each thread requires kernel resources; limits number of total threads

Many-to-Many Model user-level threads K K K If U < L? No benefits of multithreading If U > L, some threads may have to wait for an LWP to run Active thread - executing on an LWP Runnable thread - waiting for an LWP A thread gives up control of LWP under the following: A thread gives up control of LWP under the following: synchronization, lower priority, yielding, time slicing

Two-level Model user-level threads K K K K Combination of one-to-one + strict many-to-many models Supports both bound and unbound threads Bound threads - permanently mapped to a single, dedicated LWP Unbound threads - may move among LWPs in set Thread creation, scheduling, synchronization done in user space Flexible approach, best of both worlds Used in Solaris implementation of Pthreads and several other Unix implementations (IRIX, HP-UX)

Multithreading Issues Semantics of fork() and exec() system calls Thread cancellation Asynchronous vs. Deferred Cancellation Signal handling Which thread to deliver it to? Thread pools Creating new threads, unlimited number of threads Thread specific data Scheduler activations Maintaining the correct number of scheduler threads

Thread Hazards int a = 1, b = 2, w = 2; main() { CreateThread(fn, 4); CreateThread(fn, 4); while(w) ; } fn() { int v = a + b; w--; }

Concurrency Problems A statement like w-- in C (or C++) is implemented by several machine instructions: ld r4, #w add r4, r4, -1 st r4, #w Now, imagine the following sequence, what is the value of w? ld r4, #w add r4, r4, -1 st r4, #w ld r4, #w add r4, r4, -1 st r4, #w

Summary Threads increase parallelism Balance between kernel and user thread Synchronization next month