ISP – 5 th Recitation Mutexes Code example. Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent.

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

Synchronization with Eventcounts and Sequencers
Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo)
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Deadlocks, Message Passing Brief refresh from last week Tore Larsen Oct
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
CS444/CS544 Operating Systems Synchronization 2/28/2006 Prof. Searleman
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Modified from Silberschatz, Galvin and Gagne Lecture 13 Chapter 7: Deadlocks.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples.
Chapter 2.3 : Interprocess Communication
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
1 Concurrency: Deadlock and Starvation Chapter 6.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
1 Concurrency: Deadlock and Starvation Chapter 6.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
CS510 Concurrent Systems Introduction to Concurrency.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Solution to Dining Philosophers. Each philosopher I invokes the operations pickup() and putdown() in the following sequence: dp.pickup(i) EAT dp.putdown(i)
1 Confidential Enterprise Solutions Group Process and Threads.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
Games Development 2 Concurrent Programming CO3301 Week 9.
Cpr E 308 Spring 2004 Real-time Scheduling Provide time guarantees Upper bound on response times –Programmer’s job! –Every level of the system Soft versus.
Multithreading GSP-420.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Windows Thread Management
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
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.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Windows CE Overview and Scheduling Presented by Dai Kawano.
Distributed Mutual Exclusion Synchronization in Distributed Systems Synchronization in distributed systems are often more difficult compared to synchronization.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
pThread synchronization
Window Threads Chapter 7 Windows Thread Management.
Chapter 3: Windows7 Part 5.
Background on the need for Synchronization
Process Synchronization
Applied Operating System Concepts -
SECTION 6 Performance Tuning.
Critical sections, locking, monitors, etc.
O.S. Programming Assignment
Chapter 3: Windows7 Part 5.
Waiting and Synchronization
Lecture 2 Part 2 Process Synchronization
Review: Readers-Writers Problem
Concurrency: Mutual Exclusion and Process Synchronization
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Thread Synchronization including Mutual Exclusion
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
26.
, Part II Process Synchronization
Window Application Development
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
Presentation transcript:

ISP – 5 th Recitation Mutexes Code example

Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.

What’s the idea? We have multiple threads/processes running all at the same (pseudo) time. Some of them want to access a common resource, say, print to the screen. In order to avoid creating a mess, we need to make sure that only one thread/process can access the resource for a specific piece of code.

Creating a Mutex Creating a new mutex Syntax : HANDLE CreateMutex( LPSECURITY_ATTRIBUTES MutexAttributes, BOOL InitialOwner, char* Name ); MutexAttributes can be set to NULL InitialOwner determines whether the thread calling CreateProcess would also be its first owner.

Creating a Mutex – Cont. Name field identifies the mutex in the system/process. Name is unique in the OS and can be used to gain access to a mutex that was opened by others. Unlike threadID/ProcessID, here we get to set it. If name already exists, a handle to the mutex is returned and GetLastError() shows ERROR_ALREADY_EXISTS. It’s possible to use NULL instead of the name but then we have to make sure we never lose the handle.

Opening an Existing Mutex Opening a mutex Syntax : HANDLE OpenMutex( DWORD DesiredAccess, BOOL InheritHandle, char* Name ); DesiredAccess requires a minimum permission of SYNCHRONIZE. InheritHandle allows processes, created by your program, to access the handle to the mutex

WaitForSingleObject() - Mutex WaitForSingleObject() is used for waiting for an object to become “signaled”. Syntax : DWORD WaitForSingleObject( HANDLE hObject, DWORD dwMilliseconds ); For mutexes, “Signaled” means that the mutex was released and is now owned by the thread that called for the wait function. Additional return values : WAIT_ABANDONED ( 80h) - the specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.

ReleaseMutex() Releases ownership of the specified mutex object. Syntax : BOOL ReleaseMutex( HANDLE hMutex ); Returns 0 if failed, nonzero if successful NOTE : Not releasing a mutex might result in a deadlock. Recall dining phillosophers.