CS 153 Design of Operating Systems Spring 2015

Slides:



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

Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Process Concept An operating system executes a variety of programs
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
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.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
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.
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.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, Thread management & Resource Management.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
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 OS Review Processes and Threads Chi Zhang
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Threads Overview Benefits, User and Kernel Threads.
CS 6560: Operating Systems Design
CSE 120 Principles of Operating
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4 Threads.
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Threads & multithreading
Operating System Concepts
CSE 153 Design of Operating Systems Winter 2018
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
Threads Chapter 4.
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.
Still Chapter 2 (Based on Silberchatz’s text and Nachos Roadmap.)
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
Threads vs. Processes Hank Levy 1.
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
CSE451 Introduction to Operating Systems Spring 2007
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Threads.
Presentation transcript:

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

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

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

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 }

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);

A Word Process w/3 Threads Display thread Backup thread Input thread

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 Windows: threads Solaris: lightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM

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

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

User and Kernel Threads P1 P2 P1 P2 OS OS Multiplexing user-level threads on a single kernel thread for each process Multiplexing user-level threads on multiple kernel threads for each process

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

Kernel vs. User Threads 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

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) 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? Walk through example.

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 Pintos Not only will you be using threads in Pintos, you will be implementing more thread functionality

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

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 Implemented at user-level in a library for user-level threads Run queue: Threads currently running (usually one) Ready queue: Threads ready to run Are there wait queues? How would you implement thread_sleep(time)?

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(); }

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!

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

Thread Context Switch The context switch routine does all of the magic Saves context of the currently running thread (old_thread) Restores context of the next thread 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

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

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…

Next time… Read Chapter 5.1—5.3 in book Make sure to email project group/need for partner to your TA