CS 318 Principles of Operating Systems

Similar documents
CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

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 153 Design of Operating Systems Spring 18

CSE 153 Design of Operating Systems Fall 2018

Lecture 5: Synchronization w/locks

Intro to Threads. Two approaches to concurrency. Threaded multifinger: - Asynchronous I/O (lab) - Threads (today s lecture)

Section 4: Threads and Context Switching

Administrivia. Section scheduled at 12:30pm in Skilling. Lab 1 is ready. Ask cs140-staff for extension if you can t finish

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Processes (Intro) Yannis Smaragdakis, U. Athens

SE350: Operating Systems. Lecture 3: Concurrency

Threads Implementation. Jo, Heeseung

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

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

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

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

CS 4284 Systems Capstone. Processes and Threads Godmar Back

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

CS 537 Lecture 2 - Processes

CSC369 Lecture 2. Larry Zhang, September 21, 2015

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

Lecture 5: Concurrency and Threads

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

Yi Shi Fall 2017 Xi an Jiaotong University

CSC369 Lecture 2. Larry Zhang

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

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

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

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

CSE 153 Design of Operating Systems Fall 18

Assembly Language: Function Calls

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

Misc. Notes. In BVT, switch from i j only if E j E i C/w i. In BVT, Google is example of W better then L. For lab, recall PDE and PTE bits get ANDed

Multithreaded Programming

Assembly Language: Function Calls" Goals of this Lecture"

Computer Systems Laboratory Sungkyunkwan University

Assembly Language: Function Calls" Goals of this Lecture"

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

Threads and Concurrency

What s in a process?

Threads and Concurrency

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

- gcc file_a.c compiler running on file A - gcc file_b.c compiler running on file B - emacs text editor - firefox web browser

THREADS. Jo, Heeseung

From Processes to Threads

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

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

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CS5460: Operating Systems

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

CSE 153 Design of Operating Systems

CS 326: Operating Systems. Process Execution. Lecture 5

W4118: PC Hardware and x86. Junfeng Yang

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

Advanced Operating Systems (CS 202) Scheduling (1)

CS 5460/6460 Operating Systems

Processes and Threads

CS 153 Design of Operating Systems Winter 2016

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

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

Chapter 4: Multi-Threaded Programming

Administrivia. Section scheduled at 2:15pm in Gates B03 (next door)

X86 Stack Calling Function POV

Definition Multithreading Models Threading Issues Pthreads (Unix)

Announcements/Reminders

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Processes. Johan Montelius KTH

A process. the stack

CS 450 Operating System Week 4 Lecture Notes

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

Operating Systems and Protection CS 217

CSE 153 Design of Operating Systems

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

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Stack Discipline Jan. 19, 2018

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

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

Pebbles Kernel Specification September 26, 2004

Chapter 3 Process Description and Control

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS5460: Operating Systems

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

3. Process Management in xv6

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

CSCE 313: Intro to Computer Systems

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Transcription:

CS 318 Principles of Operating Systems Fall 2017 Lecture 5: Thread Ryan Huang

Administrivia HW1 solution released on Piazza resources Lab 0 grading - In progress - Cheating policy Lab 1 review session by Guoye - today 3pm in Malone 228 Group request 9/19/17 CS 318 Lecture 5 Thread 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 Communicating between processes is also costly - because most communication goes through the OS overhead of system calls and copying data 9/19/17 CS 318 Lecture 5 Thread 3

Concurrent Programs Recall our Web server example (or any parallel program) - forks off copies of itself to handle multiple simultaneous requests 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. 9/19/17 CS 318 Lecture 5 Thread 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 9/19/17 CS 318 Lecture 5 Thread 5

Threads Modern OSes 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 9/19/17 CS 318 Lecture 5 Thread 6

Threads in a Process What about heap? 9/19/17 CS 318 Lecture 5 Thread 7

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) 9/19/17 CS 318 Lecture 5 Thread 8

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, Windows, OS X) 9/19/17 CS 318 Lecture 5 Thread 9

Process/Thread Separation Easier to support multithreaded applications - Concurrency does not require creating new processes Concurrency (multithreading) can be very useful - Improving program structure - Allowing one process to use multiple CPUs/cores - Handling concurrent events (e.g., Web requests) - Allowing program to overlap I/O and computation So multithreading is even useful on a uniprocessor - Although today even cell phones are multicore But, brings a whole new meaning to Spaghetti Code - Forcing OS students to learn about synchronization 9/19/17 CS 318 Lecture 5 Thread 10

Threads: Concurrent Servers Using fork() to create new processes to handle requests is overkill 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 } } 9/19/17 CS 318 Lecture 5 Thread 11

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); } 9/19/17 CS 318 Lecture 5 Thread 12

Thread Package API tid thread_create (void (*fn) (void *), void *); - Create a new thread, run fn with arg void thread_exit (); - Destroy current thread void thread_join (tid thread); - Wait for thread thread to exit See [Birrell] for good introduction 9/19/17 CS 318 Lecture 5 Thread 13

Implementing Threads thread_create(fun, args) - Allocate thread control block - Allocate stack - Build stack frame for base of stack - Put func, args on stack - Put thread on ready list 9/19/17 CS 318 Lecture 5 Thread 14

Kernel-Level Threads All thread operations are implemented in the kernel The OS schedules all of the threads in the system Also known as lightweight processes - Windows: threads - Solaris: lightweight processes (LWP) - POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM 9/19/17 CS 318 Lecture 5 Thread 15

Kernel Thread Limitations Every thread operation must go through kernel - create, exit, join, synchronize, or switch for any reason - On my laptop: syscall takes 100 cycles, fn call 5 cycles - Result: threads 10x-30x slower when implemented in kernel One-size fits all thread implementation - Kernel threads must please all people - Maybe pay for fancy features (priority, etc.) you don t need General heavy-weight memory requirements - e.g., requires a fixed-size stack within kernel - other data structures designed for heavier-weight processes 9/19/17 CS 318 Lecture 5 Thread 16

Alternative: User-Level Threads Implement as user-level library (a.k.a. green threads) - One kernel thread per process - thread_create, thread_exit, etc., just library functions - library does thread context switch User-level threads are small and fast - pthreads: PTHREAD_SCOPE_PROCESS - Java: Thread 9/19/17 CS 318 Lecture 5 Thread 17

Small and Fast Pintos thread class struct thread { tid_t tid; /* Thread identifier. */ enum thread_status status; /* Thread state. */ char name[16]; /* Name (for debugging purposes). */ uint8_t *stack; /* Saved stack pointer. */ int priority; /* Priority. */ struct list_elem allelem; /* List element for all threads list. */ struct list_elem elem; /* List element. */ unsigned magic; /* Detects stack overflow. */ }; 9/19/17 CS 318 Lecture 5 Thread 18

U/L Thread Limitations Can t take advantage of multiple CPUs or cores 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 - A blocking system call blocks all threads Can replace read to handle network connections, but usually OSes don t let you do this for disk - Unscheduling a process with a thread holding a lock How to solve this? - communication between the kernel and the user-level thread manager (Windows 8) [Scheduler Activation] 9/19/17 CS 318 Lecture 5 Thread 19

Kernel vs. User Threads Kernel-level threads - Integrated with OS (informed scheduling) - Slower to create, manipulate, synchronize User-level threads - Faster to create, manipulate, synchronize - Not integrated with OS (uninformed scheduling) Understanding their differences is important - Correctness, performance 9/19/17 CS 318 Lecture 5 Thread 20

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 C#, others) - 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 modern OSes Can multiplex Java threads on multiple kernel threads Can have more Java threads than kernel threads Why? 9/19/17 CS 318 Lecture 5 Thread 21

User Threads on Kernel Threads User threads implemented on kernel threads - Multiple kernel-level threads per process - thread_create, thread_exit still library functions as before Sometimes called n : m threading - Have n user threads per m kernel threads (Simple user-level threads are n : 1, kernel threads 1 : 1) 9/19/17 CS 318 Lecture 5 Thread 22

Implementing User-Level Threads Allocate a new stack for each thread_create Keep a queue of runnable threads Replace networking system calls (read/write/etc.) - If operation would block, switch and run different thread - Schedule periodic timer signal (setitimer) - Switch to another thread on timer signals (preemption) Multi-threaded web server example - Thread calls read to get data from remote web browser - Fake read function makes read syscall in non-blocking mode - No data? schedule another thread - On timer or when idle check which connections have new data 9/19/17 CS 318 Lecture 5 Thread 23

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 might you implement sleep(time)? 9/19/17 CS 318 Lecture 5 Thread 24

Non-Preemptive Scheduling Threads voluntarily give up the CPU with yield Ping Thread Pong Thread while (1) { printf( ping\n ); yield(); } while (1) { printf( pong\n ); yield(); } What is the output of running these two threads? 9/19/17 CS 318 Lecture 5 Thread 25

yield() Wait a second. How does yield() work? The semantics of 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 yield to return? - It means that another thread called yield! Execution trace of ping/pong - printf( ping\n ); - yield(); - printf( pong\n ); - yield(); - 9/19/17 CS 318 Lecture 5 Thread 26

Implementing yield() 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()? 9/19/17 CS 318 Lecture 5 Thread 27

Preemptive Scheduling Non-preemptive threads have to voluntarily give up CPU - A long-running thread will take over the machine - Only voluntary calls to yield, sleep, or finish cause 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 yield 9/19/17 CS 318 Lecture 5 Thread 28

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 - 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 9/19/17 CS 318 Lecture 5 Thread 29

Background: Calling Conventions Registers divided into 2 groups - caller-saved regs: callee function free to modify on x86, %eax [return val], %edx, & %ecx - callee-saved regs: callee function must restore to original value upon return on x86, %ebx, %esi, %edi, plus %ebp and %esp save active caller registers call foo (pushes pc) save used callee registers...do stuff... restore callee saved registers jump back to calling function restore caller registers 9/19/17 CS 318 Lecture 5 Thread 30

Pintos Thread Implementation Per-thread state in thread control block structure struct thread {... uint8_t *stack; /* Saved stack pointer. */... }; uint32_t thread_stack_ofs = offsetof(struct thread, stack); C declaration for asm thread-switch function: - struct thread *switch_threads (struct thread *cur, struct thread *next); Also thread initialization function to create new stack: - void thread_create (const char *name, thread_func *function, void *aux); 9/19/17 CS 318 Lecture 5 Thread 31

i386 switch_threads pushl %ebx; pushl %ebp # Save callee-saved regs pushl %esi; pushl %edi mov thread_stack_ofs, %edx # %edx = offset of stack field # in thread struct movl 20(%esp), %eax # %eax = cur movl %esp, (%eax,%edx,1) # cur->stack = %esp movl 24(%esp), %ecx # %ecx = next movl (%ecx,%edx,1), %esp # %esp = next->stack popl %edi; popl %esi # Restore calle-saved regs popl %ebp; popl %ebx ret # Resume execution This is actual code from Pintos switch.s (slightly reformatted) - See Thread Switching in documentation 9/19/17 CS 318 Lecture 5 Thread 32

i386 switch_threads This is actual code from Pintos switch.s (slightly reformatted) - See Thread Switching in documentation 9/19/17 CS 318 Lecture 5 Thread 33

i386 switch_threads This is actual code from Pintos switch.s (slightly reformatted) - See [Thread Switching] in documentation 9/19/17 CS 318 Lecture 5 Thread 34

i386 switch_threads This is actual code from Pintos switch.s (slightly reformatted) - See [Thread Switching] in documentation 9/19/17 CS 318 Lecture 5 Thread 35

i386 switch_threads This is actual code from Pintos switch.s (slightly reformatted) - See [Thread Switching] in documentation 9/19/17 CS 318 Lecture 5 Thread 36

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 9/19/17 CS 318 Lecture 5 Thread 37

Next Time Read Chapters 28, 29 9/19/17 CS 318 Lecture 5 Thread 38