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

Similar documents
CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

CSE 120 Principles of Operating Systems

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

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Midterm Review

CSE 153 Design of Operating Systems Fall 2018

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Scheduling

Lecture 5: Synchronization w/locks

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Advanced Memory Management

Threads Implementation. Jo, Heeseung

CSE120 Principles of Operating Systems. Architecture Support for OS

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

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

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

MCSE Training Guide: Windows Architecture and Memory

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

Maru: Hardware-Assisted Secure Cloud Computing

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

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

As Michi Henning and Steve Vinoski showed 1, calling a remote

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Synchronization: Semaphore

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

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

Processes and Threads

Bridge Talk Release Notes for Meeting Exchange 5.0

THREADS. Jo, Heeseung

What s in a process?

An Introduction to Design Patterns

Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Lecture 4. Nachos Appetizer

file://j:\macmillancomputerpublishing\chapters\in073.html 3/22/01

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

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

Outline. Parallel Numerical Algorithms. Forward Substitution. Triangular Matrices. Solving Triangular Systems. Back Substitution. Parallel Algorithm

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

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

Concurrent programming: From theory to practice. Concurrent Algorithms 2016 Tudor David

Multithreaded Programming

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Lecture 2 Process Management

CSCE 313: Intro to Computer Systems

CS 450 Operating System Week 4 Lecture Notes

OPERATING SYSTEM. Chapter 4: Threads

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Illustrated

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

Chapter 4: Multi-Threaded Programming

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

Chapter 4: Multithreaded Programming

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

Windows NT, Terminal Server and Citrix MetaFrame Terminal Server Architecture

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

index.pdf March 17,

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 5: Threads. Outline

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS307: Operating Systems

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

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

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

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

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

Chapter 4: Multithreaded

Register Allocation. Consider the following assignment statement: x = (a*b)+((c*d)+(e*f)); In posfix notation: ab*cd*ef*++x

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

Chapter 4: Threads. Chapter 4: Threads

Disciplina Sistemas de Computação

Nearest Neighbor Learning

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

Chapter 3: Processes. Operating System Concepts 8th Edition

Computer Networks. College of Computing. Copyleft 2003~2018

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

Lecture 4: Process Management

Introduction to OpenMP

CS510 Operating System Foundations. Jonathan Walpole

CISC2200 Threads Spring 2015

Chapter 4: Threads. Chapter 4: Threads

Distributed Systems Operation System Support

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

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

Chapter 3 Process Description and Control

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

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

Threads. CS3026 Operating Systems Lecture 06

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

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

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

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

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

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

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

Transcription:

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

Announcement Project 0 Due Project 1 out Homework 1 due on Thursday Submit it to Gradescope onine 2

Processes Reca that a process incudes many things An address space (defining a the code and data pages) OS resources (e.g., open fies) and accounting information Execution state (PC, SP, regs, etc.) Creating a new process is costy because of a of the data structures that must be aocated and initiaized Reca struct proc in Soaris Communicating between processes is costy because most communication goes through the OS Overhead of system cas and copying data 3

Parae Programs To execute these programs we need to Create severa processes that execute in parae Have the OS schedue these processes in parae (ogicay or physicay) This situation is very inefficient Space: PCB, page tabes, etc. Time: create data structures, fork and copy addr space, etc. Soution: possibe to have cooperating processes? 4

Rethinking Processes Key idea: Why don t we separate the concept of a process from its execution state? Process: address space, privieges, resources, etc. Execution state: PC, SP, registers Exec state aso caed thread of contro, or thread 5

Threads Thread vs Process A thread defines a sequentia execution stream within a process (PC, SP, registers) A process defines the address space and genera process attributes (everything but threads of execution) A thread is bound to a singe process A process, however, can have mutipe threads Threads become the unit of scheduing Processes are now the containers in which threads execute 6

Threads: Lightweight Processes A sequentia execution stream within a process Environment (resource) execution 7 (a) Three processes each with one thread (b) One process with three threads

The Thread Mode 8 Shared information Processor info: parent process, time, etc Memory: goba data, heap, page tabe, and stats, etc I/O and fie: communication ports, directories and fie descriptors, etc Private state State (ready, running and bocked) Registers Program counter Execution stack Why? Each thread execute separatey

Threads in a Process Stack (T1) Thread 1 Thread 2 Stack (T2) Stack (T3) Thread 3 9 PC (T2) Heap Static Data Code PC (T3) PC (T1)

Anaogy Process: 3 projects for different casses (CSE120, CSE140, CSE110) Each one has different text book, different web pages, different TAs/Instructors Threads: 3 activities in CSE120 (Homework, Lectures, Projects) Share the same concepts (textbook) Share TA/Tutors A of them are going on in parae (within one quarter) Each has their own things, too 10 10/7/18

A 2-min Expainer Video https://www.youtube.com/watch?v=dhf- DYO1K78 11

Threads: Concurrent Servers Using fork() to create new processes to hande requests in parae is overki for such a simpe task Reca our forking Web server: 12 whie (1) { } int sock = accept(); if ((chid_pid = fork()) == 0) { Hande cient request Cose socket and exit } ese { Cose socket }

Threads: Concurrent Servers Instead, we can create a new thread for each request web_server() { } whie (1) { } int sock = accept(); thread_fork(hande_request, sock); Difference from fork()? 13 hande_request(int sock) { Process request cose(sock); }

Thread Usage: Web Server 14

Thread Usage: word processor A thread can wait for I/O, whie the other threads can sti running. What if it is singe-threaded? 15

Windows Thread Lists from Performance Monitor 16

Windows Performance Anayzer 17

Mac OS Activity Monitor 18 10/7/18 CSE120 - Operating Systems, Yuanyuan Zhou

Thread Information on Linux Process information: Read /proc/[your PID]/stat fie Thread information (2.6 kerne): Read /proc/[your PID]/task/[thread ID]/stat 19

Kerne-managed 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 A thread operations are impemented in the kerne The OS schedues a of the threads in the system OS-managed threads are caed kerne-eve threads or Kerne managed threads or ightweight processes NT: threads Soaris: ightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM 20

Kerne-managed Thread Limitations Kerne-managed threads make concurrency much cheaper than processes Much ess state to aocate and initiaize However, for fine-grained concurrency, kernemanaged threads sti suffer from too much overhead Thread operations sti require system cas Ideay, want thread operations to be as fast as a procedure ca Kerne-eve threads have to be genera to support the needs of a programmers, anguages, runtimes, etc. For more fine-grained concurrency, need even cheaper threads 21

User-Leve-Managed Threads 22 To make threads cheap and fast, they need to be impemented at user eve Kerne-eve managed threads are managed by the OS User-eve managed threads are managed entirey by the runtime system (user-eve ibrary) User-eve-managed threads are sma and fast A thread is simpy represented by a PC, registers, stack, and sma thread contro bock (TCB) Creating a new thread, switching between threads, and synchronizing threads are done via procedure ca No kerne invovement User-eve managed thread operations 100x faster than kerne managed threads pthreads: PTHREAD_SCOPE_PROCESS

User eve threads Kerne eve Managed threads User eve Managed threads 23

Sma and Fast Nachos thread contro bock cass Thread { int *stack; int *stacktop; int machinestate[machinestatesize]; ThreadStatus status; char *name; <Methods> }; 24

User Leve Managed Thread Limitations But, user-eve managed threads are not a perfect soution As with everything ese, they are a tradeoff User-eve managed threads are invisibe to the OS They are not we integrated with the OS As a resut, the OS can make poor decisions Scheduing a process with ide threads Bocking a process whose thread initiated an I/O, even though the process has other threads that can execute Unscheduing a process with a thread hoding a ock Soving this requires communication between the kerne and the user-eve thread manager 25

Tradeoffs between the two Kerne-eve managed threads Integrated with OS (informed scheduing) Sow to create, manipuate, synchronize User-eve managed threads Fast to create, manipuate, synchronize Not integrated with OS (uninformed scheduing) Understanding the differences between kerne and user-eve managed threads is important For programming (correctness, performance) 26

Kerne and User Threads Or use both kerne and user-eve threads Can associate a user-eve thread with a kerne-eve thread Or, mutipex user-eve threads on top of kerne-eve threads Java Virtua Machine (JVM) (aso pthreads) Java threads are user-eve threads On oder Unix, ony one kerne thread per process Mutipex a Java threads on this one kerne thread On NT, modern Unix Can mutipex Java threads on mutipe kerne threads Can have more Java threads than kerne threads 27

Impementing Threads Impementing threads has a number of issues Interface Context switch Preemptive vs. non-preemptive Scheduing Synchronization (next ecture) Focus on user-eve managed threads Kerne-eve managed threads are simiar to origina process management and impementation in the OS What you wi be deaing with in Nachos Not ony wi you be using threads in Nachos, you wi be impementing more thread functionaity 28

Sampe Thread Interface 29 thread_fork(procedure_t) Create a new thread of contro Aso thread_create(), thread_setstate() thread_stop() Stop the caing thread; aso thread_bock thread_start(thread_t) Start the given thread thread_yied() Vountariy give up the processor thread_exit() Terminate the caing thread; aso thread_destroy Thread_join(t) or t.join() causes the current thread to pause execution unti t's thread terminates

Thread Scheduing The thread scheduer determines when a thread runs It uses queues to keep track of what threads are doing Just ike the OS and processes But it is impemented at user-eve in a ibrary Run queue: Threads currenty running (usuay one) Ready queue: Threads ready to run Are there wait queues? How woud you impement thread_seep(time)? 30

Non-Preemptive Scheduing Threads vountariy give up the CPU with thread_yied Ping Thread Pong Thread whie (1) { printf( ping\n ); thread_yied(); } whie (1) { printf( pong\n ); thread_yied(); } 31 What is the output of running these two threads?

thread_yied() Wait a second. How does thread_yied() work? The semantics of thread_yied 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_yied to return? It means that another thread caed thread_yied! Execution trace of ping/pong printf( ping\n ); thread_yied(); printf( pong\n ); thread_yied(); 32

Impementing thread_yied() thread_yied() { } thread_t od_thread = current_thread; current_thread = get_next_thread(); append_to_queue(ready_queue, od_thread); context_switch(od_thread, current_thread); return; As od thread As new thread The magic step is invoking context_switch() Why do we need to ca append_to_queue()? 33

Thread Context Switch The context switch routine does a of the magic Saves context of the currenty running thread (od_thread) Push a machine state onto its stack (not its TCB) Restores context of the next thread Pop a machine state from the next thread s stack The next thread becomes the current thread Return to the NEW thread This is a done in assemby anguage It works at the eve of the procedure caing convention, so it cannot be impemented using procedure cas See code/threads/switch.s in Nachos 34

Preemptive Scheduing Non-preemptive threads have to vountariy give up CPU A ong-running thread wi take over the CPU Ony vountary cas to thread_yied(), thread_stop(), or thread_exit() causes a context switch Preemptive scheduing causes an invountary context switch Need to regain contro of processor asynchronousy How? Use timer/aarm interrupt 35

Bocking Vs. non-bocking System Cas Bocking system ca Usuay I/O reated: read(), fread(), getc(), write() Doesn t return unti the ca competes The process/thread is switched to bocked state When the I/O competes, the process/thread becomes ready Simpe Rea ife exampe: attending a ecture Using non-bocking system ca for I/O Asynchronous I/O Compicated The ca returns once the I/O is initiated, and the caer continue Once the I/O competes, an interrupt is deivered to the caer Rea ife exampe: appy for job 36

Threads Summary 37 The operating system as a arge mutithreaded program Each process executes as a thread within the OS Mutithreading is aso very usefu for appications Efficient mutithreading requires fast primitives Processes are too heavyweight Soution is to separate threads from processes Kerne-eve managed threads much better, but sti significant overhead User-eve managed threads even better, but not we integrated with OS Now, how do we get our threads to correcty cooperate with each other? Synchronization