CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Thursday, June 08, 2006 The number of UNIX installations has grown to 10, with more expected. The UNIX Programmer's Manual, 2nd Edition, June, 1972.
Processes CSCI 444/544 Operating Systems Fall 2008.
Introduction to Kernel
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Processes and Resources
CSE451 Processes Spring 2001 Gary Kimura Lecture #4 April 2, 2001.
CS533 Concepts of Operating Systems Class 3 Integrated Task and Stack Management.
Processes April 5, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Operating Systems (CSCI2413) Lecture 3 Processes phones off (please)
Process Description and Control A process is sometimes called a task, it is a program in execution.
Scheduler Activations Jeff Chase. Threads in a Process Threads are useful at user-level – Parallelism, hide I/O latency, interactivity Option A (early.
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.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
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.
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
Implementing Processes and Process Management Brian Bershad.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
1 RTOS Design Some of the content of this set of slides is taken from the documentation existing on the FreeRTOS website
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
CS 162 Discussion Section Week 2. Who am I? Haoyuan (HY) Li Office Hours: 1pm-3pm
1 Review of Process Mechanisms. 2 Scheduling: Policy and Mechanism Scheduling policy answers the question: Which process/thread, among all those ready.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
1  process  process creation/termination  context  process control block (PCB)  context switch  5-state process model  process scheduling short/medium/long.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
Review: threads and processes Landon Cox January 20, 2016.
Slides on threads borrowed by Chase Landon Cox. Thread states BlockedReady Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Embedded Real-Time Systems
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Tutorial 2: Homework 1 and Project 1
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Introduction to Kernel
Processes and threads.
Process concept.
Operating Systems CMPSC 473
Process Management Process Concept Why only the global variables?
CS 6560: Operating Systems Design
CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Scheduler activations
Lecture Topics: 11/1 Processes Process Management
CS 3305 System Calls Lecture 7.
Review: threads and synchronization
Processes A process is a running program.
Intro to Processes CSSE 332 Operating Systems
CSCI 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
More examples How many processes does this piece of code create?
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process & its States Lecture 5.
Light-weight Contexts: An OS Abstraction for Safety and Performance
Thread Implementation Issues
Lecture Topics: 11/1 General Operating System Concepts Processes
Processes Hank Levy 1.
CS510 Operating System Foundations
Foundations and Definitions
Processes Hank Levy 1.
CS703 – Advanced Operating Systems
Presentation transcript:

CPS110: Implementing threads Landon Cox

Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going

Recall, thread interactions 1.Threads can access shared data  E.g., use locks, monitors  What we’ve done so far 2.Threads also share hardware  CPU and memory  For this class, assume uni-processor  Single CPU core: one thread runs at a time  Unrealistic in the multicore era!

Hardware, OS interfaces Hardware OS Applications Memory CPU CPU, Mem Job 1 CPU, Mem Job 2 CPU, Mem Job 3 Remaining thread lecturesMemory lectures Thread lectures up to this point

The play analogy  Process is like a play performance  Program is like the play’s script  One CPU is like a one-man-show  (actor switches between roles) Threads Address space

Threads that aren’t running  What is a non-running thread?  thread=“stream of executing instructions”  non-running thread=“paused execution”  Blocked/waiting, or suspended but ready  Must save thread’s private state  Leave stack etc. in memory where it lies  Save registers to memory  Reload registers to resume thread

Private vs global thread state  What state is private to each thread?  PC (where actor is in his/her script)  Stack, SP (actor’s mindset)  What state is shared?  Code (like lines of a play)  Global variables, heap  (props on set)

Thread control block (TCB) The software that manages threads and schedules/dispatches them is the thread system or “OS” OS must maintain data to describe each thread  Thread control block (TCB)  Container for non-running thread’s private data  Values of PC, SP, other registers (“context”)  Each thread also has a stack Other OS data structures (scheduler queues, locks, waiting lists) reference these TCB objects.

Thread control block CPU Address Space TCB1 PC SP registers TCB1 PC SP registers TCB2 PC SP registers TCB2 PC SP registers TCB3 PC SP registers TCB3 PC SP registers Code Stack Code Stack Code Stack PC SP registers PC SP registers Thread 1 running Ready queue

Thread states  Running  Currently using the CPU  Ready(suspended)  Ready to run when CPU is next available  Blocked (waiting or sleeping)  Stuck in lock (), wait () or down ()

Switching threads  What needs to happen to switch threads? 1.Thread returns control to OS  For example, via the “yield” call 2.OS chooses next thread to run 3.OS saves state of current thread  To its thread control block 4.OS loads context of next thread  From its thread control block 5.Run the next thread Project 1: swapcontext

1. Thread returns control to OS  How does the thread system get control?  Voluntary internal events  Thread might block inside lock or wait  Thread might call into kernel for service  (system call)  Thread might call yield  Are internal events enough?

1. Thread returns control to OS  Involuntary external events  (events not initiated by the thread)  Hardware interrupts  Transfer control directly to OS interrupt handlers  From 104  CPU checks for interrupts while executing  Jumps to OS code with interrupt mask set  OS may preempt the running thread (force yield) when an interrupt gives the OS control of its CPU  Common interrupt: timer interrupt

2. Choosing the next thread  If no ready threads, just spin  Modern CPUs: execute a “halt” instruction  Project 1: exit if no ready threads  Loop switches to thread if one is ready  Many ways to prioritize ready threads  Will discuss a little later in the semester

3. Saving state of current thread  What needs to be saved?  Registers, PC, SP  What makes this tricky?  Self-referential sequence of actions  Need registers to save state  But you’re trying to save all the registers  Saving the PC is particularly tricky

Saving the PC  Why won’t this work?  Returning thread will execute instruction at 100  And just re-execute the switch  Really want to save address store PC in TCB 101 switch to next thread Instruction address

4. OS loads the next thread  Where is the thread’s state/context?  Thread control block (in memory)  How to load the registers?  Use load instructions to grab from memory  How to load the stack?  Stack is already in memory, load SP

5. OS runs the next thread  How to resume thread’s execution?  Jump to the saved PC  On whose stack are these steps running? or Who jumps to the saved PC?  The thread that called yield  (or was interrupted or called lock/wait)  How does this thread run again?  Some other thread must switch to it

Example 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 () print “start yield (thread %d)” swapcontext (tcb1, tcb2) print “end yield (thread %d)” swapcontext (tcb1, tcb2) save regs to tcb1 load regs from tcb2 // sp points to tcb2’s stack now! jump tcb2.pc // sp must point to tcb1’s stack! return Thread 1 output Thread 2 output start thread 1 start yield (thread 1) start thread 2 start yield (thread 2) end yield (thread 1) end thread 1 end yield (thread 2) end thread 2 Note: this assumes no pre-emptions. If OS is preemptive, then other interleavings are possible.

Thread states Blocked Ready Running Thread is scheduled Thread is Pre-empted (or yields) Thread calls Lock or wait (or makes I/O request) Another thread calls unlock or signal (or I/O completes) ? ?

Creating a new thread  Also called “forking” a thread  Idea: create initial state, put on ready queue 1.Allocate, initialize a new TCB 2.Allocate a new stack 3.Make it look like thread was going to call a function  PC points to first instruction in function  SP points to new stack  Stack contains arguments passed to function  Project 1: use makecontext 4.Add thread to ready queue

Creating a new thread Parent returncall Parent thread_create(parent work) (child work) Child

Thread join  How can the parent wait for child to finish? Parent thread_create (parent work) (child work) Child join

Thread join  Will this work?  Sometimes, assuming  Uni-processor  No pre-emptions  Child runs after parent  Never, ever assume these things!  Yield is like slowing the CPU  Program must work +- any yields child () { print “child works” } parent () { create child thread print “parent works” yield () print “parent continues” } parent works child works parent continues child works parent works parent continues

Thread join  Will this work?  No. Child can call signal first.  Would this work with semaphores?  Yes  No missed signals (increment sem value) child () { lock print “child works” signal unlock } parent () { create child thread lock print “parent works” wait print “parent continues” unlock } parent works child works parent continues child works parent works parent continues 2 1 3

How can we solve this?  Pair off for a couple of minutes child () { } parent () { } parent works child works parent continues child works parent works parent continues