CSE451 NT Synchronization Autumn 2002 Gary Kimura Lecture #9 October 18, 2002.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Operating Systems ECE344 Midterm review Ding Yuan
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.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Secure Operating Systems Lesson 5: Shared Objects.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Mid Review of Class Argument Validation and Synchronization Guidelines April 26, 2000 Instructor: Gary Kimura.
Synchronization April 14, 2000 Instructor Gary Kimura Slides courtesy of Hank Levy.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Introduction to Embedded Systems
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Nachos Phase 1 Code -Hints and Comments
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
CSE 451: Operating Systems Section 5 Midterm review.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Windows CE Overview and Scheduling Presented by Dai Kawano.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CSE 451 Section #3. Questions from Lecture Kinds of threads When threads are used How threads are implemented Scheduling.
CS703 – Advanced Operating Systems
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Outline Other synchronization primitives
Atomic Operations in Hardware
Atomic Operations in Hardware
Other Important Synchronization Primitives
Chapter 5: Process Synchronization
CSE451 Basic Synchronization Spring 2001
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Topic 6 (Textbook - Chapter 5) Process Synchronization
Threading And Parallel Programming Constructs
Lecture 2 Part 2 Process Synchronization
NT Synchronization Primitives
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
NT Executive Resources
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
CSE 451 Section 1/27/2000.
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University
Presentation transcript:

CSE451 NT Synchronization Autumn 2002 Gary Kimura Lecture #9 October 18, 2002

2 Today So far we’ve talked about masking interrupts, spinlocks, semaphores, and monitors –Semaphores and monitors can be used both in user and kernel level code. Kernel level is usual direct support, user level code often requires some kernel calls to support synchronized access to the data structures Today we’ll delve into some additional kernel level synchronization routines. Why? Because masking interrupts, spinlocks, and semaphores a nice but are not the right tool for all occasions In particular we’re going to look at these in the context of Windows 2000

3 Additional Synchronization Routines From Windows NT A brief look at spinlocks again A set of very specialized primitives –Interlocked Operations Mutex (not quite a real binary semaphore) General kernel synchronization objects –Events –Semaphores A general purpose synchronization package –Eresource Some are kernel mode only Some are implemented in the kernel and exported to the user as a system call Some are implemented in both kernel and user mode

Spinlocks A spinlock is a quick mechanism for acquiring exclusive access to a critical section of code It uses Interrupt Request Levels (IRQL) to disable other code from running In the Windows 2000 Kernel the spinlock calls are: –KeAcquireSpinLock: Enter critical section –KeReleaseSpinLock: Leave critical section On UP systems spinlocks are implemented by raising the Interrupt Request Level On MP systems spinlocks are implemented using a shared variable that contains zero or one (or the owners thread ID)

5 Interlocked Operations Interlocked operations are atomic operations that can be used to synchronize access to a single variable or as a building block for more complex synchronization routines Some of the supported interlocked operations are –InterlockedIncrement –InterlockedDecrement –InterlockedCompareExchange –and there are more

6 X86 Interlocked Examples lock prefix is MP only __inline LONG FASTCALL InterlockedIncrement( IN PLONG Addend ) { __asm { mov eax, 1 mov ecx, Addend lock xadd [ecx], eax inc eax } where xadd atomically does temp := eax + [ecx] eax := [ecx] [ecx] := temp __inline LONG FASTCALL InterlockedCompareExchange( IN OUT PLONG Destination, IN LONG Exchange, IN LONG Comperand ) { __asm { mov eax, Comperand mov ecx, Destination mov edx, Exchange lock cmpxchg [ecx], edx } where cmpxchg atomically does if eax == [ecx] [ecx] := edx else eax := [ecx]

7 Uses for Interlocked Operations With interlocked increment and decrement we can easily update global counters Compare exchange is a quick mechanism for implementing locks Compare exchange can also be used for quick linked list queue operations.

8 Executive Mutex A mutex looks like a spinlock from the programmers viewpoint but a mutex allow recursion and page faults Operations are –ExAcquireFastMutex: Enter critical region –ExReleaseFastMutex: Exit critical region –ExTryToAcquireFastMutex: Like acquire but return immediately without blocking and indicate if we acquired the mutex The executive mutex is implemented using kernel synchronization events Similar calls are also available at user level

9 Semaphores and Events The Windows NT kernel uses what are called kernel objects to define semaphores and events Semaphores in Windows 2000 are plain vanilla counted semaphores Event are something new for our class. There are two types of events –Synchronization events: used to allow one thread at a time to enter a critical region of code –Notification events: used to signal all the threads about an event

10 Events An event variable’s state is either signaled or not-signaled. Threads can wait for an event to become signaled Kernel level event calls are –KeClearEvent: Event becomes not-signaled –KePulseEvent: Atomically signal and then not-signal event –KeReadStateEvent: Returns current event state –KeResetEvent: Event becomes not-signaled and returns previous state –KeSetEvent and KeSetEventBoostPriority: Signal an event and optionally boost the priority of the waiting thread Similar calls are also available at the user level

11 Semaphores A semaphore variable is just a plain old vanilla counted semaphore, but its implementation shares much of the event logic Kernel semaphore calls are –KeReadStateSemaphore: Returns the signal state of the semaphore –KeReleaseSemaphore: Takes as input a release count and a priority boost of the newly released threads This is also a set of calls available at user level

12 Wait for routine In Windows there are many types of objects that a thread can wait on in the kernel. –Semaphores, events, timers, queues, processes, threads, etc. There are two routines that threads use to wait on these objects. In kernel mode the wait calls are –KeWaitForSingleObject: Wait for one event or semaphore to become signaled –KeWaitForMultipleObjects: Wait for one or more events or semaphores (WaitAny or WaitAll) There is an optional timeout on the wait calls that will return from the wait call even if the object is not signaled Two similar calls are also available at the user level

13 An example using events and waits KEVENT Event; KSEMAPHORE Semaphore; Main: KeInitializeEvent( &Event, SynchronizationEvent); KeInitializeSemaphore(&Semaphore, 0, MAXLONG); Thread One: Status = KeWaitForSingleObject( Event,…,Timeout); if (Status != STATUS_TIMEOUT) { // The event was signaled and its ours } Thread Two: Objects[2] = {&Event, &Semaphor}; Status = KeWaitForMultipleObjects( 2, Objects, WaitAny,…, Timeout); Switch (Status) { 0: // event went off 1: // Semaphore went off Default: // something else happened } Thread Three: KeSetEvent(Event,…)

14 Recap of what we have so far Spinlocks – in theory this can be done at the user level however for practical purposes it rarely is done at that level. Great for protecting short little critical sections. Usually we want everything in memory (i.e., we don’t really want to wait while holding a spinlock) Interlocked Operations – usable in both user and kernel mode. Sometimes difficult to use but a great building block none the less Semaphore – Both user and kernel mode. Pretty good for general synchronization Events – Both user and kernel mode. More specialized than semaphores Mutex – Both user and kernel mode. More usable than spinlocks but also more expensive to use

15 Executive Resource The Executive Resource (Eresource) package is used by Windows 2000 kernel level code to handle the multi- reader/single-writer access to protected data structures. A version is also available in user mode The package exports a typedef called an Eresource Operations are: –ExAcquireResourceShared –ExAcquireResourceExclusive –ExAcquireSharedStarveExclusive –ExReleaseResource (ExReleaseResourceForThread) –ExConvertExclusiveToShared

16 Eresource features A resource can be acquired recursively The ownership of a resource is tied to a thread ID The users get to decide whether exclusive waiters starve The package automatically handles priority inversion The routines are implemented using spinlocks, events, and semaphores More details at a later lecture if time permits

17 Next Time Deadlocks