CSE 153 Design of Operating Systems Winter 2018

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Advertisements

Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
Processes CSCI 444/544 Operating Systems Fall 2008.
Chapter 4: Threads. 2 What’s in a process? A process consists of (at least): –an address space –the code for the running program –the data for the running.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Threads. Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CS 153 Design of Operating Systems Spring 2015
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Implementing Processes and Process Management Brian Bershad.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
OPERATING SYSTEM CONCEPT AND PRACTISE
Processes and threads.
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
CSE 120 Principles of Operating
CSE 451: Operating Systems Winter 2011 Threads
Day 12 Threads.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
CSE 451: Operating Systems Spring 2013 Module 5 Threads
Process Management Presented By Aditya Gupta Assistant Professor
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Threads & multithreading
Chapter 4: Threads.
CSE 451: Operating Systems Winter 2010 Module 5 Threads
ICS 143 Principles of Operating Systems
CSE 451: Operating Systems Winter 2007 Module 5 Threads
CSE 451: Operating Systems Autumn 2004 Module 5 Threads
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Spring 2008 Module 5 Threads
Lecture Topics: 11/1 General Operating System Concepts Processes
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
Threads Chapter 4.
CSE 451: Operating Systems Winter 2007 Module 5 Threads
Processes Hank Levy 1.
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
Threads Chapter 5 2/17/2019 B.Ramamurthy.
Threads Chapter 5 2/23/2019 B.Ramamurthy.
CSE 451: Operating Systems Winter 2009 Module 5 Threads
CSE 451: Operating Systems Spring 2005 Module 5 Threads
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
CSE 451: Operating Systems Winter 2012 Threads
CSE 451: Operating Systems Autumn 2009 Module 5 Threads
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Threads vs. Processes Hank Levy 1.
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
Processes Hank Levy 1.
CS703 – Advanced Operating Systems
CSE451 Introduction to Operating Systems Spring 2007
CSE 451: Operating Systems Winter 2006 Module 5 Threads
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Threads.
Presentation transcript:

CSE 153 Design of Operating Systems Winter 2018 Lecture 6: Threads

Processes Recall that … But… A process includes: OS Recall that … A process includes: An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting info Execution state (PC, SP, regs, etc.) PCB to keep track of everything Processes are completely isolated from each other But… Struct proc does not even include page tables, perhaps TLB flushing, etc. CSE 153 – Lecture 6 – Threads

Some issues with processes Creating a new process is costly because of new address space and data structures that must be allocated and initialized Recall struct proc in xv6 or Solaris Communicating between processes is costly because most communication goes through the OS Inter Process Communication (IPC) – we will discuss later Overhead of system calls and copying data CSE 153 – Lecture 6 – Threads

Parallel Programs Also recall our Web server example that 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 This situation is very inefficient (CoW helps) Space: PCB, page tables, etc. Time: create data structures, fork and copy addr space, etc. Aside: Copy on Write. To save the unnecessary cost of making a complete copy of the address when much of it may stay identical, we can just share the actual pages (details of implementing CoW will come in memory management). When either process writes to a page of memory (the two copies of the page diverge), this causes a trap to the OS which creates a copy at that point. This way, we avoid the unnecessary copies for memory that remains the same, or in case we do exec right after the fork(). CSE 153 – Lecture 6 – Threads

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: Separate resources from execution state Exec state also called thread of control, or thread CSE 153 – Lecture 6 – Threads

Recap: Process Components A process is named using its process ID (PID) A process contains all of the state for a program in execution An address space The code for the executing program The data for the executing program A set of operating system resources Open files, network connections, etc. An execution stack encapsulating the state of procedure calls The program counter (PC) indicating the next instruction A set of general-purpose registers with current values Current execution state (Ready/Running/Waiting) Per-Process State Per-Thread State CSE 153 – Lecture 6 – Threads

Threads Separate execution and resource container roles The thread defines a sequential execution stream within a process (PC, SP, registers) The process defines the address space, resources, and general process attributes (everything but threads) Threads become the unit of scheduling Processes are now the containers in which threads execute Processes become static, threads are the dynamic entities Mach, Chorus, NT, modern Unix CSE 153 – Lecture 6 – Threads

Recap: Process Address Space 0xFFFFFFFF Stack SP Heap (Dynamic Memory Alloc) Address Space Static Data (Data Segment) Code (Text Segment) PC 0x00000000 CSE 153 – Lecture 6 – Threads

Threads in a Process Stack (T1) Thread 1 Stack (T2) Thread 2 Heap Static Data PC (T3) PC (T2) Code PC (T1) CSE 153 – Lecture 6 – Threads

Thread Design Space One Thread/Process One Thread/Process One Address Space (MSDOS) Many Address Spaces (Early Unix) Address Space Thread Many Threads/Process Many Threads/Process One Address Space (Pilot, Java) Many Address Spaces (Mac OS, Unix, Windows) CSE 153 – Lecture 6 – Threads

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 CSE 153 – Lecture 6 – Threads

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 } CSE 153 – Lecture 6 – Threads

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); CSE 153 – Lecture 6 – Threads

Implementing threads Kernel Level Threads All thread operations are implemented in the kernel The OS schedules all of the threads in the system Don’t have to separate from processes OS-managed threads are called kernel-level threads or lightweight processes Windows: threads Solaris: lightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM CSE 153 – Lecture 6 – Threads

Alternative: User-Level Threads Implement threads using user-level library ULTs 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 CSE 153 – Lecture 6 – Threads

Summary KLT vs. ULT Kernel-level threads User-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  CSE 153 – Lecture 6 – Threads

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 CSE 153 – Lecture 6 – Threads

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)? CSE 153 – Lecture 6 – Threads

Non-Preemptive Scheduling Threads voluntarily give up the CPU with thread_yield What is the output of running these two threads? Ping Thread Pong Thread while (1) { printf(“ping\n”); thread_yield(); } while (1) { printf(“pong\n”); thread_yield(); } CSE 153 – Lecture 6 – Threads

thread_yield() 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? Execution trace of ping/pong printf(“ping\n”); thread_yield(); printf(“pong\n”); … It means that another thread called thread_yield! CSE 153 – Lecture 6 – Threads

Implementing 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; } The magic step is invoking context_switch() Why do we need to call append_to_queue()? As old thread As new thread CSE 153 – Lecture 6 – Threads

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 CSE 153 – Lecture 6 – Threads

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 (How do you do this?) Timer interrupt handler forces current thread to “call” thread_yield CSE 153 – Lecture 6 – Threads

Threads Summary Processes are too heavyweight for multiprocessing Time and space overhead 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 Scheduling of threads can be either preemptive or non-preemptive Now, how do we get our threads to correctly cooperate with each other? Synchronization… CSE 153 – Lecture 6 – Threads