CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.

Slides:



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

Chapter 5 Threads os5.
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.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Threads CSCI 444/544 Operating Systems Fall 2008.
Threads. Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
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.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating Systems CSE 411 CPU Management Sept Lecture 11 Instructor: Bhuvan Urgaonkar.
CS 153 Design of Operating Systems Spring 2015
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, Thread management & Resource Management.
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.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
CSE 451: Operating Systems Winter 2014 Module 5 Threads Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska, Levy, Zahorjan.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Threads Overview Benefits, User and Kernel Threads.
CS 6560: Operating Systems Design
CSE 120 Principles of Operating
CSE 451: Operating Systems Winter 2011 Threads
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
CSE 153 Design of Operating Systems Winter 2018
Lecture Topics: 11/1 General Operating System Concepts Processes
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2007 Module 5 Threads
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2009 Module 5 Threads
CSE 451: Operating Systems Spring 2005 Module 5 Threads
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
CSE 451: Operating Systems Winter 2012 Threads
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Threads vs. Processes Hank Levy 1.
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 2019
CSE 153 Design of Operating Systems Winter 2019
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 2015 Lecture 4: Threads

CSE 153 – Lecture 4 – Threads2 Announcements l Project 1 is now posted u Make sure to go over it so that you can ask the TAs in lab if anything is unclear u Both design document and code due before class on Feb. 3 rd l Read scheduling and synchronization in textbook u Don’t wait for these topics to be covered in class u You especially need to understand priority donation in project l All set with project groups? u your TA today if you have not notified group or if you are looking for a partner

CSE 153 – Lecture 4 – Threads3 Processes l Recall that … u A process includes many things: »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.) u Processes are completely isolated from each other l Creating a new process is costly because of all of the data structures that must be allocated and initialized u Recall struct proc in Solaris l Communicating between processes is costly because most communication goes through the OS u Overhead of system calls and copying data P1 P2 OS

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

CSE 153 – Lecture 4 – Threads5 Rethinking Processes l What is similar in these cooperating processes? u They all share the same code and data (address space) u They all share the same privileges u They all share the same resources (files, sockets, etc.) l What don’t they share? u Each has its own execution state: PC, SP, and registers l Key idea: Separate resources from execution state l Exec state also called thread of control, or thread

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

CSE 153 – Lecture 4 – Threads7 Threads l Modern OSes (Mac OS, Windows, Linux) separate the concepts of processes and threads u The thread defines a sequential execution stream within a process (PC, SP, registers) u The process defines the address space and general process attributes (everything but threads of execution) l A thread is bound to a single process u Processes, however, can have multiple threads l Threads become the unit of scheduling u Processes are now the containers in which threads execute u Processes become static, threads are the dynamic entities

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

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

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

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

CSE 153 – Lecture 4 – Threads12 Threads: Concurrent Servers l Using fork() to create new processes to handle requests in parallel is overkill for such a simple task l 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 4 – Threads13 Threads: Concurrent Servers l 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 4 – Threads14 Kernel-Level Threads l We have taken the execution aspect of a process and separated it out into threads u To make concurrency cheaper l As such, the OS now manages threads and processes u All thread operations are implemented in the kernel u The OS schedules all of the threads in the system l OS-managed threads are called kernel-level threads or lightweight processes u Windows: threads u Solaris: lightweight processes (LWP) u POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM

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

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

CSE 153 – Lecture 4 – Threads17 User and Kernel Threads Multiplexing user-level threads on a single kernel thread for each process OS Multiplexing user-level threads on multiple kernel threads for each process P1P2P1P2

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

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

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

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

CSE 153 – Lecture 4 – Threads22 Sample Thread Interface l thread_fork(procedure_t) u Create a new thread of control u Also thread_create(), thread_setstate() l thread_stop() u Stop the calling thread; also thread_block l thread_start(thread_t) u Start the given thread l thread_yield() u Voluntarily give up the processor l thread_exit() u Terminate the calling thread; also thread_destroy

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

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

CSE 153 – Lecture 4 – Threads25 thread_yield() l The semantics of thread_yield are that it gives up the CPU to another thread u In other words, it context switches to another thread l So what does it mean for thread_yield to return? l Execution trace of ping/pong u printf(“ping\n”); u thread_yield(); u printf(“pong\n”); u thread_yield(); u …

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

CSE 153 – Lecture 4 – Threads27 Thread Context Switch l The context switch routine does all of the magic u Saves context of the currently running thread (old_thread) »Push all machine state onto its stack (not its TCB) u Restores context of the next thread »Pop all machine state from the next thread’s stack u The next thread becomes the current thread u Return to caller as new thread l This is all done in assembly language u It works at the level of the procedure calling convention, so it cannot be implemented using procedure calls

CSE 153 – Lecture 4 – Threads28 Preemptive Scheduling l Non-preemptive threads have to voluntarily give up CPU u A long-running thread will take over the machine u Only voluntary calls to thread_yield(), thread_stop(), or thread_exit() causes a context switch l Preemptive scheduling causes an involuntary context switch u Need to regain control of processor asynchronously u Use timer interrupt (How do you do this?) u Timer interrupt handler forces current thread to “call” thread_yield

CSE 153 – Lecture 4 – Threads29 Threads Summary l Processes are too heavyweight for multiprocessing u Time and space overhead l Solution is to separate threads from processes u Kernel-level threads much better, but still significant overhead u User-level threads even better, but not well integrated with OS l Scheduling of threads can be either preemptive or non- preemptive l Now, how do we get our threads to correctly cooperate with each other? u Synchronization…

CSE 153 – Lecture 4 – Threads30 Next time… l Read u Chapter 5.1—5.3 in book l Make sure to project group/need for partner to your TA