CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.

Slides:



Advertisements
Similar presentations
Operating Systems ECE344 Midterm review Ding Yuan
Advertisements

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.
Process management Information maintained by OS for process management  process context  process control block OS virtualization of CPU for each process.
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
OS Spring’03 Introduction Operating Systems Spring 2003.
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.
OS Spring’04 Introduction Operating Systems Spring 2004.
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
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.
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.
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.
Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
CS 162 Discussion Section Week 2. Who am I? Haoyuan (HY) Li Office Hours: 1pm-3pm
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
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.
Processes & Threads Introduction to Operating Systems: Module 5.
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.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
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
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.
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.
Processes and Process Management
Implementing Mutual Exclusion
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
CS510 Operating System Foundations
CS333 Intro to Operating Systems
Processes Hank Levy 1.
CS703 – Advanced Operating Systems
Presentation transcript:

CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008

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

Recall, thread interactions 1.Threads can access shared data  Use locks, monitors, and semaphores  What we’ve done so far 2.Threads also share hardware  CPU (uni-processor)  Memory

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=“sequence of executing instructions”  non-running thread=“paused execution”  Must save thread’s private state  To re-run, re-load private state  Want thread to start where it left off

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

Thread control block (TCB)  What needs to access threads’ private data?  The CPU  This info is stored in the PC, SP, other registers  The OS needs pointers to non-running threads’ data  Thread control block (TCB)  Container for non-running thread’s private data  Values of PC, code, SP, stack, registers

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 control block CPU Address Space TCB2 PC SP registers TCB2 PC SP registers TCB3 PC SP registers TCB3 PC SP registers Code Stack PC SP registers PC SP registers Thread 1 running Ready queue

Thread states  Running  Currently using the CPU  Ready  Ready to run, but waiting for the CPU  Blocked  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  Interrupts lead to pre-emption (a forced yield)  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)” switch to next thread (swapcontext) print “end yield (thread %d)” 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. Programmers must assume pre-emptions, so this output is not guaranteed.

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

Course administration  Second UTA  Jason Lee  Office hours: Thursday, 4:30-6:30, Teer lab  Lots of questions on newsgroup  Great resource

Course administration  Project 1 disk scheduler (1d)  How to use concurrency primitives  Disk scheduler should be done or almost done  Two/seven groups are done with 1d  Project 1 thread library (1t)  How concurrency primitives actually work  Best way to learn something is to build it! (no such thing as magic)  Should be able to start after class  Time management is really, really important (start soon!!)  Happy answer any Project 1 questions

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 () { print “child works” lock childDone = 1 signal unlock } parent () { childDone = 0 create child thread print “parent works” lock while (!childDone) { wait } print “parent continues” unlock } parent works child works parent continues child works parent works parent continues

Thread join  Many libraries supply “join” operation  Makes this a lot easier child () { print “child works” } parent () { childDone = 0 create child thread print “parent works” join with child print “parent continues” }