CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 

Slides:



Advertisements
Similar presentations
Process management Information maintained by OS for process management  process context  process control block OS virtualization of CPU for each process.
Advertisements

Concurrency: Threads, Address Spaces, and Processes Sarah Diesburg Operating Systems COP 4610.
Why Concurrency? Allows multiple applications to run at the same time  Analogy: juggling.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 12, 2005 Prof. John Kubiatowicz
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching January 30, 2006 Prof. Anthony D. Joseph
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 10, 2008 Prof. John Kubiatowicz
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)
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 11, 2006 Prof. John Kubiatowicz
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 9, 2009 Prof. John Kubiatowicz
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.
Concurrency: Threads, Address Spaces, and Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
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.
CSC139 Operating Systems Lecture 4 Thread Dispatching Adapted from Prof. John Kubiatowicz's lecture notes for CS162
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
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Chapter 4: Threads March 17 th, 2009 Instructor: Hung Q. Ngo Kyung Hee University.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
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.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Scheduler activations Landon Cox March 23, What is a process? Informal A program in execution Running code + things it can read/write Process ≠
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
10 August 2015 Charles Reiss
CS 6560: Operating Systems Design
OPERATING SYSTEMS CS3502 Fall 2017
Protection of System Resources
Scheduler activations
Mechanism: Limited Direct Execution
Lecture Topics: 11/1 Processes Process Management
Intro to Processes CSSE 332 Operating Systems
Concurrency: Threads, Address Spaces, and Processes
Process management Information maintained by OS for process management
CSCI 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 13, 2010 Prof. John Kubiatowicz
CS162 Operating Systems and Systems Programming Lecture 3 Thread Dispatching January 30, 2008 Prof. Anthony D. Joseph
Computer System Overview
CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching September 10, 2007 Prof. John Kubiatowicz
Concurrency: Threads, Address Spaces, and Processes
January 28, 2010 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching January.
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.
PROCESS MANAGEMENT Information maintained by OS for process management
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Processes and Process Management
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.)
Processes David Ferry CSCI 3500 – Operating Systems
CS510 Operating System Foundations
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment. Phone:
Concurrency: Threads, Address Spaces, and Processes
Chapter 3: Process Management
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 

Review: Modern Process with Multiple Threads Process: Operating system abstraction to represent what is needed to run a single, multithreaded program Two parts: One or Multiple Threads Each thread is a single, sequential stream of execution Protected Resources Main Memory State (contents of Address Space) I/O state (i.e. file descriptors) Why separate the concept of a thread from that of a process? The “thread” part of a process (concurrency) Separate from the “address space” (Protection) Heavyweight Process  Process with one thread

Review: Single and Multithreaded Processes Threads encapsulate concurrency “Active” component of a process Address spaces encapsulate protection “Passive” component of a process Keeps buggy program from trashing the system

Further Understanding Threads Thread dispatching Goals for Today Further Understanding Threads Thread dispatching Thread Scheduling

Single-Threaded Example Imagine the following C program: main() { ComputePI(“pi.txt”); PrintClassList(“clist.text”); } Q. Why does this program never print out class list? A: ComputePI would never finish

Time Use of Threads Version of program with Threads: main() { CreateThread(ComputePI(“pi.txt”)); CreateThread(PrintClassList(“clist.text”)); } Q. What does “CreateThread” do? Start independent thread running given procedure The behavior of this program You would actually see the class list This should behave as if there are two separate CPUs CPU1 CPU2 Time

Memory Footprint of Two-Thread Example If we stopped this program and examined it with a debugger, we would see Two sets of Stacks Two sets of CPU registers in two TCPs Code Global Data Heap Stack 1 Stack 2 Address Space

Per Thread State Each Thread has a Thread Control Block (TCB) Execution State: CPU registers, program counter, pointer to stack Scheduling info: State, priority, CPU time Accounting Info Various Pointers (for implementing scheduling queues) Pointer to enclosing process Etc (add stuff as you find a need) OS Keeps track of TCBs in protected memory in Array, or Linked List, or …

Lifecycle of a Thread (or Process) As a thread executes, it changes state: new: The thread is being created ready: The thread is waiting to run running: Instructions are being executed waiting: Thread waiting for some event to occur terminated: The thread has finished execution “Active” threads are represented by their TCBs TCBs organized into queues based on their state

Ready Queue And Various I/O Device Queues Thread not running  TCB is in some scheduler queue Separate queue for each device/signal/condition Each queue can have a different scheduler policy Head Tail Ready Queue Tape Unit 0 Disk Unit 2 Ether Netwk 0 Other State TCB9 Link Registers TCB6 TCB16 Other State TCB2 Link Registers TCB3 Other State TCB8 Link Registers

Dispatch Loop Conceptually, the dispatching loop of the operating system looks as follows: Loop { RunThread(); ChooseNextThread(); SaveStateOfCPU(curTCB); LoadStateOfCPU(newTCB); } This is an infinite loop One could argue that this is all that the OS does Q. Should we ever exit this loop? When would that be? Shut down computer Or emergency crash of operating system called “panic()”

Running a thread Consider first portion: RunThread() Q. How do I run a thread? Load its state (registers, PC, stack pointer) into CPU Load environment (memory space, etc) Jump to the PC Q. How does the dispatcher (part of kernel) get control back? Internal events: thread returns control voluntarily External events: thread gets preempted (the details of two types of events are next …)

1st type of event: Internal Events Blocking on I/O The act of requesting I/O implicitly yields the CPU Waiting on a “signal” from other thread Thread asks to wait and thus yields the CPU Thread executes a yield() Thread volunteers to give up CPU e.g. computePI() { while(TRUE) { ComputeNextDigit(); yield(); }

Context Switch Code in Kernel Switch(tCur,tNew) { /* Save old thread */ TCB[tCur].regs.r7 = CPU.r7; … TCB[tCur].regs.r0 = CPU.r0; TCB[tCur].regs.sp = CPU.sp; TCB[tCur].regs.retpc = CPU.retpc; /*return addr*/ /* Load new thread and execute it*/ CPU.r7 = TCB[tNew].regs.r7; CPU.r0 = TCB[tNew].regs.r0; CPU.sp = TCB[tNew].regs.sp; CPU.retpc = TCB[tNew].regs.retpc; return; /* Return to CPU.retpc */ } In fact, we could not even implement this procedure in C  C cannot write to register (embeded asm in C)

2nd type of event: External Events Q. What happens if thread never does any I/O, never waits, and never yields control? e.g. Could the ComputePI program grab all resources and never release the processor? Must find way that dispatcher can regain control! Answer: Utilize External Events Interrupts: signals from hardware or software that stop the running code and jump to kernel Timer: like an alarm clock that goes off every some many milliseconds

Example of External Event: Network Interrupt Raise priority Re-enable Ints Save registers Dispatch to Handler  Transfer Network Packet from hardware to Kernel Buffers Restore registers Clear current Int Disable Ints Restore priority RTI “Interrupt Handler” PC saved Disable Ints Supervisor Mode  add $r1,$r2,$r3 subi $r4,$r1,#4 slli $r4,$r4,#2 External Interrupt Pipeline Flush lw $r2,0($r4) lw $r3,4($r4) add $r2,$r2,$r3 sw 8($r4),$r2  Restore PC User Mode An interrupt is a hardware-invoked context switch Kernel runs the interrupt handler immediately

Choosing a Thread to Run Q. How does Dispatcher (part of kernel) decide what to run? Zero ready threads – dispatcher loops Alternative is to create an “idle thread” Can put machine into low-power mode Exactly one ready thread – easy More than one ready thread: use scheduling priorities Possible priorities: LIFO (last in, first out): FIFO (first in, first out): Put ready threads on back of list, pull them from front Priority queue: keep ready list sorted by TCB priority field (We will study CPU scheduling in chapter 5.)

In Conclusion … The state of a thread is contained in the TCB Registers, PC, stack pointer States: New, Ready, Running, Waiting, Terminated Multithreading provides simple illusion of multiple CPUs Switch registers and stack to dispatch new thread Provide mechanism to ensure dispatcher regains control Switch routine Can be very expensive if many registers Must be very carefully constructed! Many scheduling options Decision of which thread to run