CSE 120 Principles of Operating Systems

Similar documents
CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

CS 153 Design of Operating Systems Winter 2016

CSE 153 Design of Operating Systems Fall 2018

CS 318 Principles of Operating Systems

CS 318 Principles of Operating Systems

CS 153 Design of Operating Systems Spring 18

CSE 153 Design of Operating Systems Fall 2018

Lecture 5: Synchronization w/locks

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Lecture 4: Threads

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

Threads Implementation. Jo, Heeseung

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

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

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

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

What s in a process?

Threads. CSE 410, Spring 2004 Computer Systems.

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

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

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

Advanced Operating Systems (CS 202) Scheduling (1)

Computer Systems Laboratory Sungkyunkwan University

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

SE350: Operating Systems. Lecture 3: Concurrency

CSE 153 Design of Operating Systems

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

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

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

THREADS. Jo, Heeseung

CS 153 Design of Operating Systems Winter 2016

Announcements/Reminders

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

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

Lecture 5: Concurrency and Threads

Chapter 3 Process Description and Control

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Disciplina Sistemas de Computação

CSCE 313: Intro to Computer Systems

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

CS 450 Operating System Week 4 Lecture Notes

Processes and Threads

Definition Multithreading Models Threading Issues Pthreads (Unix)

Chapter 4: Multithreaded

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

Multithreaded Programming

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

CISC2200 Threads Spring 2015

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

Chapter 5: Threads. Outline

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

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

OPERATING SYSTEM. Chapter 4: Threads

Operating Systems. Lecture 4. Nachos Appetizer

Processes and Threads

Process Description and Control

Arvind Krishnamurthy Spring Threads, synchronization, scheduling Virtual memory File systems Networking

CS5460: Operating Systems

From Processes to Threads

Chapter 4: Multi-Threaded Programming

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

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

CS 326: Operating Systems. Process Execution. Lecture 5

Distributed Systems Operation System Support

Threads and Concurrency

Chapter 4: Multithreaded Programming

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Threads. CS3026 Operating Systems Lecture 06

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

CSE 153 Design of Operating Systems

CS162 Operating Systems and Systems Programming Lecture 3. Concurrency: Processes, Threads, and Address Spaces. Review: History of OS

Lecture 4: Process Management

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

CS510 Operating System Foundations. Jonathan Walpole

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

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

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Administrivia. Nachos guide and Lab #1 are on the web.

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

Lecture 2 Process Management

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Chapter 4: Threads. Chapter 4: Threads

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

Module 5: Threads. Background Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads

Process Scheduling Queues

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

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

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

CSCC69H3. Operating Systems Sina Meraji U of T

CS153: Process 2. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

What we will learn. Multi Process Systems: Processes &Threads. Architectural support: Processor Modes. Processes (classic vs.

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

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Transcription:

CSE 120 Principles of Operating Systems Spring 2009 Lecture 4: Threads Geoffrey M. Voelker

Announcements Homework #1 due now Project 0 due tonight Project 1 out April 9, 2009 CSE 120 Lecture 4 Threads 2

Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting information Execution state (PC, SP, regs, etc.) Creating a new process is costly because of all of the data structures that must be allocated and initialized Recall struct proc in Solaris which does not even include page tables, perhaps TLB flushing, etc. Communicating between processes is costly because most communication goes through the OS Overhead of system calls and copying data April 9, 2009 CSE 120 Lecture 4 Threads 3

Parallel Programs Also recall our Web server example that forks off copies of itself to handle multiple simultaneous requests Or any parallel program that executes on a multiprocessor To execute these programs we need to Create several processes that execute in parallel Cause each to map to the same address space to share data» They are all part of the same computation Have the OS schedule these processes in parallel (logically or physically) This situation is very inefficient Space: PCB, page tables, etc. Time: create data structures, fork and copy addr space, etc. April 9, 2009 CSE 120 Lecture 4 Threads 4

Rethinking Processes What is similar in these cooperating processes? They all share the same code and data (address space) They all share the same privileges They all share the same resources (files, sockets, etc.) What don t they share? Each has its own execution state: PC, SP, and registers Key idea: Why don t we separate the concept of a process from its execution state? Process: address space, privileges, resources, etc. Execution state: PC, SP, registers Exec state also called thread of control, or thread April 9, 2009 CSE 120 Lecture 4 Threads 5

Threads Modern OSes (Mach, Chorus, NT, modern Unix) separate the concepts of processes and threads The thread defines a sequential execution stream within a process (PC, SP, registers) The process defines the address space and general process attributes (everything but threads of execution) A thread is bound to a single process Processes, however, can have multiple threads Threads become the unit of scheduling Processes are now the containers in which threads execute Processes become static, threads are the dynamic entities April 9, 2009 CSE 120 Lecture 4 Threads 6

Threads in a Process Stack (T1) Thread 1 Thread 2 Stack (T2) Stack (T3) Thread 3 PC (T2) Heap Static Data Code PC (T3) PC (T1) April 9, 2009 CSE 120 Lecture 4 Threads 7

Thread Design Space Address Space One Thread/Process One Address Space (MSDOS) One Thread/Process Many Address Spaces (Early Unix) Thread Many Threads/Process One Address Space (Pilot, Java) Many Threads/Process Many Address Spaces (Mach, Unix, NT, Chorus) April 9, 2009 CSE 120 Lecture 4 Threads 8

Process/Thread Separation Separating threads and processes makes it easier to support multithreaded applications Concurrency does not require creating new processes Concurrency (multithreading) can be very useful Improving program structure Handling concurrent events (e.g., Web requests) Writing parallel programs So multithreading is even useful on a uniprocessor April 9, 2009 CSE 120 Lecture 4 Threads 9

Threads: Concurrent Servers Using fork() to create new processes to handle requests in parallel is overkill for such a simple task Recall our forking Web server: while (1) { int sock = accept(); if ((child_pid = fork()) == 0) { Handle client request Close socket and exit } else { Close socket } } April 9, 2009 CSE 120 Lecture 4 Threads 10

Threads: Concurrent Servers Instead, we can create a new thread for each request web_server() { while (1) { int sock = accept(); thread_fork(handle_request, sock); } } handle_request(int sock) { Process request close(sock); } April 9, 2009 CSE 120 Lecture 4 Threads 11

Kernel-Level Threads We have taken the execution aspect of a process and separated it out into threads To make concurrency cheaper As such, the OS now manages threads and processes All thread operations are implemented in the kernel The OS schedules all of the threads in the system OS-managed threads are called kernel-level threads or lightweight processes NT: threads Solaris: lightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM April 9, 2009 CSE 120 Lecture 4 Threads 12

Kernel Thread Limitations Kernel-level threads make concurrency much cheaper than processes Much less state to allocate and initialize However, for fine-grained concurrency, kernel-level threads still suffer from too much overhead Thread operations still require system calls» Ideally, want thread operations to be as fast as a procedure call Kernel-level threads have to be general to support the needs of all programmers, languages, runtimes, etc. For such fine-grained concurrency, need even cheaper threads April 9, 2009 CSE 120 Lecture 4 Threads 13

User-Level Threads To make threads cheap and fast, they need to be implemented at user level Kernel-level threads are managed by the OS User-level threads are managed entirely by the run-time system (user-level library) User-level threads are small and fast A thread is simply represented by a PC, registers, stack, and small thread control block (TCB) Creating a new thread, switching between threads, and synchronizing threads are done via procedure call» No kernel involvement User-level thread operations 100x faster than kernel threads pthreads: PTHREAD_SCOPE_PROCESS April 9, 2009 CSE 120 Lecture 4 Threads 14

Small and Fast Nachos thread control block class Thread { int *stack; int *stacktop; int machinestate[machinestatesize]; ThreadStatus status; char *name; <Methods> }; April 9, 2009 CSE 120 Lecture 4 Threads 15

U/L Thread Limitations But, user-level threads are not a perfect solution As with everything else, they are a tradeoff User-level threads are invisible to the OS They are not well integrated with the OS As a result, the OS can make poor decisions Scheduling a process with idle threads Blocking a process whose thread initiated an I/O, even though the process has other threads that can execute Unscheduling a process with a thread holding a lock Solving this requires communication between the kernel and the user-level thread manager April 9, 2009 CSE 120 Lecture 4 Threads 16

Kernel vs. User Threads Kernel-level threads Integrated with OS (informed scheduling) Slow to create, manipulate, synchronize User-level threads Fast to create, manipulate, synchronize Not integrated with OS (uninformed scheduling) Understanding the differences between kernel and user-level threads is important For programming (correctness, performance) For test-taking April 9, 2009 CSE 120 Lecture 4 Threads 17

Kernel and User Threads Or use both kernel and user-level threads Can associate a user-level thread with a kernel-level thread Or, multiplex user-level threads on top of kernel-level threads Java Virtual Machine (JVM) (also pthreads) Java threads are user-level threads On older Unix, only one kernel thread per process» Multiplex all Java threads on this one kernel thread On NT, modern Unix» Can multiplex Java threads on multiple kernel threads» Can have more Java threads than kernel threads» Why? April 9, 2009 CSE 120 Lecture 4 Threads 18

User and Kernel Threads Multiplexing user-level threads on a single kernel thread for each process Multiplexing user-level threads on multiple kernel threads for each process April 9, 2009 CSE 120 Lecture 4 Threads 19

Implementing Threads Implementing threads has a number of issues Interface Context switch Preemptive vs. non-preemptive Scheduling Synchronization (next lecture) Focus on user-level threads Kernel-level threads are similar to original process management and implementation in the OS What you will be dealing with in Nachos Not only will you be using threads in Nachos, you will be implementing more thread functionality April 9, 2009 CSE 120 Lecture 4 Threads 20

Sample Thread Interface thread_fork(procedure_t) Create a new thread of control Also thread_create(), thread_setstate() thread_stop() Stop the calling thread; also thread_block thread_start(thread_t) Start the given thread thread_yield() Voluntarily give up the processor thread_exit() Terminate the calling thread; also thread_destroy April 9, 2009 CSE 120 Lecture 4 Threads 21

Thread Scheduling The thread scheduler determines when a thread runs It uses queues to keep track of what threads are doing Just like the OS and processes But it is implemented at user-level in a library Run queue: Threads currently running (usually one) Ready queue: Threads ready to run Are there wait queues? How would you implement thread_sleep(time)? April 9, 2009 CSE 120 Lecture 4 Threads 22

Non-Preemptive Scheduling Threads voluntarily give up the CPU with thread_yield Ping Thread while (1) { printf( ping\n ); thread_yield(); } Pong Thread while (1) { printf( pong\n ); thread_yield(); } What is the output of running these two threads? April 9, 2009 CSE 120 Lecture 4 Threads 23

thread_yield() Wait a second. How does thread_yield() work? The semantics of thread_yield are that it gives up the CPU to another thread In other words, it context switches to another thread So what does it mean for thread_yield to return? It means that another thread called thread_yield! Execution trace of ping/pong printf( ping\n ); thread_yield(); printf( pong\n ); thread_yield(); April 9, 2009 CSE 120 Lecture 4 Threads 24

Implementing thread_yield() thread_yield() { thread_t old_thread = current_thread; current_thread = get_next_thread(); append_to_queue(ready_queue, old_thread); context_switch(old_thread, current_thread); return; } As old thread As new thread The magic step is invoking context_switch() Why do we need to call append_to_queue()? April 9, 2009 CSE 120 Lecture 4 Threads 25

Thread Context Switch The context switch routine does all of the magic Saves context of the currently running thread (old_thread)» Push all machine state onto its stack (not its TCB) Restores context of the next thread» Pop all machine state from the next thread s stack The next thread becomes the current thread Return to caller as new thread This is all done in assembly language It works at the level of the procedure calling convention, so it cannot be implemented using procedure calls See code/threads/switch.s in Nachos April 9, 2009 CSE 120 Lecture 4 Threads 26

Preemptive Scheduling Non-preemptive threads have to voluntarily give up CPU A long-running thread will take over the machine Only voluntary calls to thread_yield(), thread_stop(), or thread_exit() causes a context switch Preemptive scheduling causes an involuntary context switch Need to regain control of processor asynchronously Use timer interrupt Timer interrupt handler forces current thread to call thread_yield» How do you do this? Nachos is non-preemptive in OS, preemptive among processes» See use of thread->yieldonreturn in code/machine/interrupt.cc April 9, 2009 CSE 120 Lecture 4 Threads 27

Threads Summary The operating system as a large multithreaded program Each process executes as a thread within the OS Multithreading is also very useful for applications Efficient multithreading requires fast primitives Processes are too heavyweight Solution is to separate threads from processes Kernel-level threads much better, but still significant overhead User-level threads even better, but not well integrated with OS Now, how do we get our threads to correctly cooperate with each other? Synchronization April 9, 2009 CSE 120 Lecture 4 Threads 28

Next time Read Chapter 6.1 6.6 April 9, 2009 CSE 120 Lecture 4 Threads 29