EECS 482 Introduction to Operating Systems

Similar documents
EECS 482 Introduction to Operating Systems

Sample Questions. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

POSIX Threads: a first step toward parallel programming. George Bosilca

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

SMD149 - Operating Systems

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

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

EECS 482 Introduction to Operating Systems

CSE 153 Design of Operating Systems

SE350: Operating Systems. Lecture 3: Concurrency

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

Lecture 5: Synchronization w/locks

Lecture 9: Midterm Review

Lecture 5: Concurrency and Threads

CSE 120 Principles of Computer Operating Systems Fall Quarter, 2002 Halloween Midterm Exam. Instructor: Geoffrey M. Voelker

ECE 574 Cluster Computing Lecture 8

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

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

Dr. Rafiq Zakaria Campus. Maulana Azad College of Arts, Science & Commerce, Aurangabad. Department of Computer Science. Academic Year

CS-537: Midterm Exam (Spring 2001)

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

Timers 1 / 46. Jiffies. Potent and Evil Magic

OS 1 st Exam Name Solution St # (Q1) (19 points) True/False. Circle the appropriate choice (there are no trick questions).

r ~ c.. Q.) 0\ 7 < - \1") ::J - ::r 3 ::J,... ::J Q.) 0!:t. !:t. ::J ::J (/') C

Processes. Dr. Yingwu Zhu

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

Threads and concurrency

Threads and concurrency

Operating Systems Comprehensive Exam. Spring Student ID # 3/20/2013

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

CMSC421: Principles of Operating Systems

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Jan 20, 2005 Lecture 2: Multiprogramming OS

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Scheduling - Overview

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CSC 4320 Test 1 Spring 2017

CS 322 Operating Systems Practice Midterm Questions

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

CS355 Hw 4. Interface. Due by the end of day Tuesday, March 20.

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

Threads and Concurrency

Mutex Implementation

Threads and Concurrency

Lecture 4: Process Management

Concurrency: a crash course

1 Introduction. 2 Managing contexts. Green, green threads of home. Johan Montelius HT2018

CSE 153 Design of Operating Systems

Chapter 4: Multithreaded Programming

EECS 482 Introduction to Operating Systems

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Lecture #7: Implementing Mutual Exclusion

Virtual Machine Design

EECS 482 Introduction to Operating Systems

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

EECS 482 Introduction to Operating Systems

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

What s in a process?

Lecture 5 Threads and Pthreads II

Chapter 5: Synchronization 1

The Kernel Abstraction. Chapter 2 OSPP Part I

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

CS533 Concepts of Operating Systems. Jonathan Walpole

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Tasks. Task Implementation and management

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Threads Questions Important Questions

More on Synchronization and Deadlock

Concurrency. Glossary

Predictable Interrupt Management and Scheduling in the Composite Component-based System

THREADS & CONCURRENCY

Exam Guide COMPSCI 386

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Process Scheduling Queues

Operating Systems Comprehensive Exam. Fall Student ID # 10/31/2013

آنستیتیوت تکنالوجی معلوماتی و مخابراتی ICTI

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

W4118 Operating Systems. Junfeng Yang

Today: Synchronization. Recap: Synchronization

CS510 Operating System Foundations. Jonathan Walpole

Threads Implementation. Jo, Heeseung

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

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

Java Threads. Written by John Bell for CS 342, Spring 2018

Concurrency: Deadlock and Starvation

Operating Systems, Assignment 2 Threads and Synchronization

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

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

CS 111. Operating Systems Peter Reiher

CS 318 Principles of Operating Systems

3.1 Introduction. Computers perform operations concurrently

Real-Time Programming

Processes and Threads

VEOS high level design. Revision 2.1 NEC

CSE 120 Principles of Operating Systems

Transcription:

EECS 482 Introduction to Operating Systems Winter 2018 Harsha V. Madhyastha

Monitors vs. Semaphores Monitors: Custom user-defined conditions Developer must control access to variables Semaphores: Access to value is thread-safe Only condition is (value == 0) How to implement custom waiting condition with semaphores? January 29, 2018 EECS 482 Lecture 7 2

Implementing condition variables with semaphores queue waiters = {}; // list of semaphores inserted by waiting threads wait(mutex m) { // create a new semaphore semaphore s = 0; } // add new semaphore to // waiting list waiters.insert(&s); m.up(); // go to sleep s.down(); m.down(); Not atomic signal() { // nothing to do if no waiters if (waiters.empty()) { return; } // wake up one of the waiters semaphore s = waiters.front(); s.up(); // remove waiter from queue waiters.pop(); January 29, 2018 EECS 482 Lecture 7 3 }

Exercise to try Given implementations of mutex and condition variable, how to implement a semaphore? January 29, 2018 EECS 482 Lecture 7 4

Interactions between threads Threads must synchronize access to shared data High-level synchronization primitives: Locks Condition variables Monitors Semaphores Threads share the same CPU January 29, 2018 EECS 482 Lecture 7 5

States of a Thread New Create thread Running Thread completes execution Terminated Wait on lock, wait, or down Another thread calls unlock, signal, or up Blocked What if there are more threads than CPUs? January 29, 2018 EECS 482 Lecture 7 6

States of a Thread Why no transition from Ready to Blocked? New Create thread Another thread calls unlock, signal, or up Ready CPU to spare Switch CPU to another thread Terminated Thread completes execution Blocked Wait on lock, wait, or down Running January 29, 2018 EECS 482 Lecture 7 7

Ready threads What to do with thread while it s not running? Essentially, a paused execution Must save its private state somewhere Thread control block (TCB) Per-thread OS data structure for thread info Store thread context when not running What context should be stored in TCB? January 29, 2018 EECS 482 Lecture 7 8

Process Address Space Stack (T1) Thread 1 Thread 2 Stack (T2) Stack (T3) Thread 3 Data Segment Heap Static Data PC (T2) Code PC (T3) PC (T1) January 29, 2018 EECS 482 Lecture 7 9

Thread context To save space in TCB Share code among all threads and store only PC Use multiple stacks and copy only SP Also need to store general-purpose registers Keep track of ready threads (e.g., queue of TCBs) Now, any thread can be Running on the CPU Ready with TCB on ready queue Blocked with TCB in waiting queue of lock, CV, etc. January 29, 2018 EECS 482 Lecture 7 10

Project 2 is out Implement a thread library Create threads Switch between threads Manage interactions (locks and CVs) Schedule threads on CPUs Due February 17 th Start right away! Everyone should now be in a group January 29, 2018 EECS 482 Lecture 7 11

Differences compared to P1 Much harder! 15% of grade Test cases will be graded Hand grading to check for good coding practices, efficiency, no duplication of code, etc. January 29, 2018 EECS 482 Lecture 7 12

Two Perspectives to Execution Thread view: Running à (Paused) à Resume CPU view: Thread 1 à Thread 2 à Thread 1 January 29, 2018 EECS 482 Lecture 7 13

Context switch 1. Current thread returns control to OS 2. OS chooses new thread to run 3. OS saves current thread state: CPU to TCB 4. OS loads context of next thread: TCB to CPU 5. OS runs next thread How does thread return control back to OS? January 29, 2018 EECS 482 Lecture 7 14

Returning control to OS Three types of internal events: Thread calls wait(), lock(), etc. Thread requests OS to do some work (e.g., I/O) Thread voluntarily gives up CPU with yield() Are these enough? Also need external events: Interrupts (e.g., timer, I/O) are hardware events that transfer control from thread to OS interrupt handler January 29, 2018 EECS 482 Lecture 7 15

Interrupts Hardware events (implemented by CPU) Stop current execution (e.g., thread function) Start running OS interrupt handler OS registers handlers in interrupt vector table Example: timer interrupt OS may set timer to go off every 10 ms Guarantees that it will get control back in <= 10 ms January 29, 2018 EECS 482 Lecture 7 16

Context switch 1. Current thread returns control to OS 2. OS chooses new thread to run 3. OS saves current thread state: CPU to TCB 4. OS loads context of next thread: TCB to CPU 5. OS runs next thread January 29, 2018 EECS 482 Lecture 7 17

Choosing next thread to run 1 ready thread: just run it What if only thread that exists calls yield? >1 ready thread: need to make a decision CPU s scheduling policy Lots of options: FIFO, priority, round robin, etc. What should CPU do if no ready threads? Modern CPUs suspend their execution and resume on an interrupt interrupt_enable_suspend() in Project 2 January 29, 2018 EECS 482 Lecture 7 18

Context switch 1. Current thread returns control to OS 2. OS chooses new thread to run 3. OS saves current thread state: CPU to TCB 4. OS loads context of next thread: TCB to CPU 5. OS runs next thread January 29, 2018 EECS 482 Lecture 7 19

Saving state of current thread Save registers, PC, stack pointer Tricky to get right! Why won t the following code work? 100 save PC 101 switch to next thread Involves tricky assembly-language code In Project 2, we ll use Linux s swapcontext() January 29, 2018 EECS 482 Lecture 7 20

Context switch 1. Current thread returns control to OS 2. OS chooses new thread to run 3. OS saves current thread state: CPU to TCB 4. OS loads context of next thread: TCB to CPU 5. OS runs next thread January 29, 2018 EECS 482 Lecture 7 21

Load context and run How to load registers? How to load stack? How to resume execution? Who is carrying out these steps? How does thread that gave up control run again? January 29, 2018 EECS 482 Lecture 7 22

Example of thread switching Thread 1 print start thread 1 yield() print end thread 1 Thread 2 print start thread 2 yield() print end thread 2 yield() Thread 1 output start thread 1 start yield: thread 1 end yield: thread 1 end thread 1 Thread 2 output start thread 2 start yield: thread 2 end yield: thread 2 end thread 2 print start yield: thread %d switch to next thread (swapcontext) print end yield: thread %d January 29, 2018 EECS 482 Lecture 7 23

Creating a new thread Create a running thread? Seems challenging Instead, create paused thread Key idea: pretend it was running, put on readyq Then just wait for it to be scheduled! Implication: Construct TCB as if it were paused at thread start January 29, 2018 EECS 482 Lecture 7 24

Recipe for creating a thrad 1. Allocate and initialize TCB Set PC to start of thread function Set general-purpose registers to func parameters 2. Allocate and initialize stack What goes on stack? Set TCB stack pointer to stack top getcontext() and makecontext() in Project 2 3. Add TCB to ready queue Note: fork() creates a process, discuss later January 29, 2018 EECS 482 Lecture 7 25

How to use new thread Creating a thread is like an asynchronous procedure call parent call return parent create parent works child works January 29, 2018 EECS 482 Lecture 7 26

Synchronizing with child What if parent wants to work for a while, then wait for child to finish? parent create parent works parent continues child works January 29, 2018 EECS 482 Lecture 7 27

Synchronizing with child parent() create child thread print parent works print parent continues child() print child is done Desired output parent works child is done parent continues OR child is done parent works parent continues When would this work? January 29, 2018 EECS 482 Lecture 7 28

Synchronizing with child parent() create child thread print parent works yield() print parent continues child() print child is done Desired output parent works child is done parent continues OR child is done parent works parent continues Does this work? January 29, 2018 EECS 482 Lecture 7 29

Synchronizing with join parent() create child thread print parent works childthread.join() print parent continues child() print child is done Desired output parent works child is done parent continues OR child is done parent works parent continues How to make do without join? January 29, 2018 EECS 482 Lecture 7 30

Synchronizing with monitors parent() childdone = 0 create child thread print parent works lock() while (!childdone) wait() unlock() print parent continues child() print child is done lock() childdone = 1 signal() unlock() Desired output parent works child is done parent continues OR child is done parent works parent continues January 29, 2018 EECS 482 Lecture 7 31

Project 2 update You can now do a substantial part of project 2 Thread create Context switch Thread join Next topic: implementing synchronization Need to protect OS data structures (ready queue) Need to block without (much) busy waiting January 29, 2018 EECS 482 Lecture 7 32