Race Conditions Defined 1. Every data structure defines invariant conditions. defines the space of possible legal states of the structure defines what.

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

Threads, SMP, and Microkernels
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-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Computer Systems/Operating Systems - Class 8
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
1 CS 333 Introduction to Operating Systems Class 2 – OS-Related Hardware & Software The Process Concept Jonathan Walpole Computer Science Portland State.
Introduction to Kernel
OS Spring’03 Introduction Operating Systems Spring 2003.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Process Description and Control A process is sometimes called a task, it is a program in execution.
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.
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.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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 and Concurrency. A First Look at Some Key Concepts kernel The software component that controls the hardware directly, and implements the core.
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.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CPS 110/EE 153 Course Intro. The operating system (OS) is the interface between user applications and the hardware. An OS implements a sort of virtual.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Background: Operating Systems Brad Karp UCL Computer Science CS GZ03 / M th November, 2008.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
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
CPS110: Threads review and wrap up Landon Cox February 11, 2009.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Administrivia Nachos guide and Lab #1 are on the web. Form project teams of 2-3. Lab #1 due February 5. Synchronization.
1 Lecture 6 “Nachos” n nachos overview n directory structure n nachos emulated machine n nachos OS n nachos scheduler n nachos threads.
NachOS Threads System Road Map through the Code Lab 3.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
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.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
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?
CPS110: Threads review and wrap up
CS 6560: Operating Systems Design
Structure of Processes
More Threads and Synchronization
Structure of Processes
Threads, SMP, and Microkernels
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Lecture 4- Threads, SMP, and Microkernels
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.)
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
CS510 Operating System Foundations
CS703 – Advanced Operating Systems
Presentation transcript:

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 in Nachos

Administrative Install and build Nachos if you haven’t already. “I need more space to build Nachos.” Space will be provided soon. Until then, only build in the threads directory (needs 1.2 MB). Goodies: Today’s slides:

The operating system (OS) is the interface between the user and the hardware. An OS implements a sort of virtual machine that is easier to program than the raw hardware. Operating Systems: The Big Picture [McKinley] physical machine interface User Applications Operating System Architecture virtual machine interface

Nachos is designed to look, feel, and crash like a “real” OS. Both the Nachos “OS” and test programs run together as an ordinary process on an ordinary Unix system (Solaris). What is Nachos? User Applications Operating System Architecture Nachos “OS” Solaris OS Architecture MIPS User Applications

Nachos runs real user programs on a simulated machine. MIPS simulator in Nachos executes real user programs. The real OS is treated as part of the underlying hardware. What is Nachos? User Applications Operating System Architecture Nachos “OS” Solaris OS Architecture MIPS User Applications

Nachos Projects Labs 1-3: concurrency and synchronization race conditions with processes and threads implementing/using synchronization for safe concurrent code Lab 4: protected kernel with multiprogramming OS kernel with system calls, memory allocation, virtual address translation, protection Lab 5: I/O and inter-process communication Labs 6-7: virtual memory page faults and demand loading page replacement and page cache management

Overview of Nachos Labs 1-3 In the thread assignments, you build and test the kernel’s internal primitives for processes and synchronization. Think of your program as a “real” kernel doing “real basic” things. boot and initialize run main(), parse arguments, initialize machine state run a few tests create processes (threads) and execute kernel code in their contexts shut down...runs native, directly on the host machine. Kernel only; no user programs (until Lab 4)

The Nachos Thread Library The Nachos library implements concurrent 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 library threads are sometimes called coroutines readyList data while(1) { t = get next ready thread; scheduler->Run(t); }

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->Yield(); currentThread->Sleep();

Blocking or Sleeping An executing thread may request some resource or action that causes it to block or sleep awaiting some event. passage of a specific amount of time (a pause request) completion of I/O to a slow device (e.g., keyboard or disk) release of some needed resource (e.g., memory) In Nachos, threads block by calling Thread::Sleep. A sleeping thread cannot run until the event occurs. The blocked thread is awakened when the event occurs. E.g., Wakeup or Nachos Scheduler::ReadyToRun(Thread* t) In an OS, processes may sleep while executing in the kernel to handle a system call or fault.

Nachos Thread States and Transitions running readyblocked Scheduler::Run Scheduler::ReadyToRun Thread::Sleep (voluntary) Thread::Yield (voluntary or involuntary)

new = scheduler->FindNextToRun();/* get next ready thread */ scheduler->Run(new);/* run it */ Run calls SWITCH(currentThread, new) to suspend current thread and pass control to new thread. The Nachos Scheduler The core of Nachos is the Scheduler class: one global shared scheduler object pool of ready threads (the ready list) readyList

/* * Save context of the calling thread (old), restore registers of * the next thread to run (new), and return in context of new. */ switch/MIPS (old, new) { old->stackTop = SP; save RA in old->MachineState[PC]; save callee registers in old->MachineState restore callee registers from new->MachineState RA = new->MachineState[PC]; SP = new->stackTop; return (to RA) } A Nachos Context Switch Caller-saved registers (if needed) are already saved on the thread’s stack. Caller-saved regs restored automatically on return. Return to last procedure that called switch in new. Save current stack pointer and caller’s return address in old thread object. Switch off of old stack and back to new stack.

Questions about Nachos Context Switches Can I trace the stack of a thread that has switched out and is not active? What will I see? (a thread in the READY or BLOCKED state) What happens on the first switch into a newly forked thread? Thread::Fork (actually StackAllocate) sets up for first switch. What happens when the thread’s main procedure returns? When do we delete a dying thread’s stack? How does Nachos know what the current thread is?

Threads and Data Races can occur when threads operate on shared data. When is data shared? local variables are private addressed off the stack, e.g., not shared, since each thread has its own stack pointer (SP) never export (pass/share/store) a pointer to a local variable global variables and static objects are shared addressed with absolute addresses dynamic objects or other heap data are shared allocated from global storage with new/delete, or malloc/free

Debugging with Threads Lab 1: demonstrate races with the Nachos List class. Some will result in a crash; know how to analyze them. > gdb nachos [or ddd] (gdb) run program_arguments Program received signal SIGSEGV, Segmentation Fault. 0x10954 in function_name(function_args) at file.c:line_number (gdb) where Caution: gdb [ddd] is not Nachos-thread aware! A context switch will change the stack pointer. Before stepping: (gdb) break SWITCH