Threads and Concurrency. A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
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.
Critical Sections and Race Conditions. Administrivia Install and build Nachos if you haven’t already.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
OS Spring’03 Introduction Operating Systems Spring 2003.
Threads CSCI 444/544 Operating Systems Fall 2008.
Threads. Announcements CSUGLab accounts are ready Fixed homework 1 submissions using CMS.
CS533 Concepts of Operating Systems Class 3 Integrated Task and Stack Management.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Process Concept An operating system executes a variety of programs
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
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.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Welcome to the World of Nachos CPS 110 Spring 2004 Discussion Session 1.
Protection and the Kernel: Mode, Space, and Context.
CS 153 Design of Operating Systems Spring 2015
Threads, Thread management & Resource Management.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
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.
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
ITFN 3601 Introduction to Operating Systems Lecture 3 Processes, Threads & Scheduling Intro.
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Introduction to Operating Systems and Concurrency.
Department of Computer Science and Software Engineering
Processes & Threads Introduction to Operating Systems: Module 5.
CPS110: Threads review and wrap up Landon Cox February 11, 2009.
Administrivia Nachos guide and Lab #1 are on the web. Form project teams of 2-3. Lab #1 due February 5. Synchronization.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Introduction to Operating Systems Concepts
Process concept.
CPS110: Threads review and wrap up
CS 6560: Operating Systems Design
Chapter 4 Threads.
More Threads and Synchronization
Chapter 4: Threads.
Operating System Concepts
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Mid Term review CSC345.
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads and Concurrency
Threads Chapter 4.
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.)
Threads vs. Processes Hank Levy 1.
CS510 Operating System Foundations
CS703 – Advanced Operating Systems
Threads.
Presentation transcript:

Threads and Concurrency

A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core privileged OS functions. Modern hardware has features that allow the OS kernel to protect itself from untrusted user code. thread An executing stream of instructions and its CPU register context. virtual address space An execution context for thread(s) that provides an independent name space for addressing some or all of physical memory. process An execution of a program, consisting of a virtual address space, one or more threads, and some OS kernel state.

Threads A thread is a schedulable stream of control. defined by CPU register values (PC, SP) suspend: save register values in memory resume: restore registers from memory Multiple threads can execute independently: They can run in parallel on multiple CPUs... - physical concurrency …or arbitrarily interleaved on a single CPU. - logical concurrency Each thread must have its own stack.

Two Threads Sharing a CPU reality concept context switch

A Peek Inside a Running Program 0 high code library your data heap registers CPU R0 Rn PC “memory” x x your program common runtime stack address space (virtual or physical) SP y y

A Program With Two Threads 0 high code library data registers CPU R0 Rn PC “memory” x x program common runtime stack address space SP y y stack running thread “on deck” and ready to run

Thread Context Switch 0 high code library data registers CPU R0 Rn PC “memory” x x program common runtime stack address space SP y y stack 1. save registers 2. load registers switch in switch out

Context Switches: Voluntary and Involuntary On a uniprocessor, the set of possible execution schedules depends on when context switches can occur. Voluntary: one thread explicitly yields the CPU to another. E.g., a Nachos thread can suspend itself with Thread::Yield. It may also block to wait for some event with Thread::Sleep. Involuntary: the system scheduler suspends an active thread, and switches control to a different thread. Thread scheduler tries to share CPU fairly by timeslicing. Suspend/resume at periodic intervals (e.g., nachos -rs) Involuntary context switches can happen “any time”.

A Nachos Thread Thread* t machine state name/status, etc. “fencepost” 0xdeadbeef Stack low high stack top unused region thread object or thread control block int stack[StackSize] t = new Thread(name); t->Fork(MyFunc, arg); currentThread->Sleep(); currentThread->Yield();

Why Threads Are Important 1. There are lots of good reasons to use threads. “easy” coding of multiple activities in an application e.g., servers with multiple independent clients parallel programming to reduce execution time 2. Threads are great for experimenting with concurrency. context switches and interleaved executions race conditions and synchronization can be supported in a library (Nachos) without help from OS 3. We will use threads to implement processes in Nachos. (Think of a thread as a process running within the kernel.)

Concurrency Working with multiple threads (or processes) introduces concurrency: several things are happening “at once”. How can I know the order in which operations will occur? physical concurrency On a multiprocessor, thread executions may be arbitrarily interleaved at the granularity of individual instructions. logical concurrency On a uniprocessor, thread executions may be interleaved as the system switches from one thread to another. context switch (suspend/resume) Warning: concurrency can cause your programs to behave unpredictably, e.g., crash and burn.

The Dark Side of Concurrency With interleaved executions, the order in which processes execute at runtime is nondeterministic. depends on the exact order and timing of process arrivals depends on exact timing of asynchronous devices (disk, clock) depends on scheduling policies Some schedule interleavings may lead to incorrect behavior. Open the bay doors before you release the bomb. Two people can’t wash dishes in the same sink at the same time. The system must provide a way to coordinate concurrent activities to avoid incorrect interleavings.

Example: A Concurrent Color Stack InitColorStack() { push(blue); push(purple); } PushColor() { if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); }

Interleaving the Color Stack #1 PushColor() { if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); } ThreadBody() { while(1) PushColor(); }

Interleaving the Color Stack #2 if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); }

Interleaving the Color Stack #3 if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); } Consider a yield here on blue’s first call to PushColor(). X

Interleaving the Color Stack #4 if (s[top] == purple) { ASSERT(s[top-1] == blue); push(blue); } else { ASSERT(s[top] == blue); ASSERT(s[top-1] == purple); push(purple); } Consider yield here on blue’s first call to PushColor(). X

Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what it means for the structure to be “well-formed” 2. Operations depend on and preserve the invariants. The invariant must hold when the operation begins. The operation may temporarily violate the invariant. The operation restores the invariant before it completes. 3. Arbitrarily interleaved operations violate invariants. Rudely interrupted operations leave a mess behind for others. 4. Therefore we must constrain the set of possible schedules.

Threads vs. Processes 1. The process is a kernel abstraction for an independent executing program. includes at least one “thread of control” also includes a private address space (VAS) - requires OS kernel support (but some use process to mean what we call thread) 2. Threads may share an address space threads have “context” just like vanilla processes - thread context switch vs. process context switch every thread must exist within some process VAS processes may be “multithreaded” data Thread::Fork

User-level Threads The Nachos library implements user-level threads. no special support needed from the kernel (use any Unix) thread creation and context switch are fast (no syscall) defines its own thread model and scheduling policies readyList data while(1) { t = get next ready thread; scheduler->Run(t); }

Kernel-Supported Threads Most newer OS kernels have kernel-supported threads. thread model and scheduling defined by OS Nachos kernel can support them: extra credit in Labs 4 and 5 NT, advanced Unix, etc. data New kernel system calls, e.g.: thread_fork thread_exit thread_block thread_alert etc... Threads can block independently in kernel system calls. Kernel scheduler (not a library) decides which thread to run next. Threads must enter the kernel to block: no blocking in user space