4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Synchronization and Deadlocks
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6: Process Synchronization
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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.
1 Concurrency: Deadlock and Starvation Chapter 6.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
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 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Operating System Chapter 6. Concurrency: Deadlock and Starvation Lynn Choi School of Electrical Engineering.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Copyright © Curt Hill Concurrent Execution An Overview for Database.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
COT 4600 Operating Systems Fall 2009
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Outline Other synchronization primitives
Threads Threads.
Other Important Synchronization Primitives
Operating Systems CMPSC 473
143a discussion session week 3
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Dr. Mustafa Cem Kasapbaşı
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
“The Little Book on Semaphores” Allen B. Downey
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

4061 Session 23 (4/10)

Today Reader/Writer Locks and Semaphores Lock Files

Today’s Objectives Describe semaphores and reader/writer locks Pseudo-code a barrier using synchronization primitives Write multi-process code that coordinates file writes to avoid race conditions or interleaved output Write code that uses a lockfile to ensure that a single instance is running

Admin Scholarships – Quiz 4 Homework 4

Readers and Writers Problem Imagine a database system with many processes trying to read and write concurrently –It’s ok if several processes read data at the same time –But once a process starts to write, it needs a mutex So perhaps we want to improve on the efficiency of enforcing mutex on all operations…

Strong Reader Synchronization One solution: –Readers First reader locks the write mutex. Subsequent readers allowed in (increment counter). Last reader out releases the write mutex –Writers Lock write mutex, write, unlock Problems?

Strong Writer Synchronization Alternate solution: –Incoming readers are queued behind any waiting writers

POSIX Read-Write Locks Calls are very similar to mutex The main difference is that you declare whether you are attempting to obtain a read lock or a write lock (through different calls)

Semaphores Like an integer, but with some special properties for synchronization –Initialization: any value. Subsequent use: restricted to increment and decrement. Cannot read value. –When a thread decrements a semaphore and the result is negative, the thread blocks until the semaphore is no longer negative. –When a thread increments a semaphore, one of the waiting threads (if any) gets unblocked.

Semaphore Operations Operations have many names –decrement and increment –down and up –Dutch: p and v –Robbins Book: wait and signal –Posix: wait and post We’ll use wait and signal unless we’re discussing posix code

Semaphore as Mutex s = Semaphore(1) Thread 1: s.wait() // critical section s.signal() Thread 2: s.wait() // critical section s.signal()

Exercise: Barriers (1)

Exercise: Barriers Say we have n threads. Our requirement is that all n threads finish their work before our program moves forward. All threads must run the code: rendezvous critical point No thread can execute “critical point” until all threads have executed rendezvous There are n threads. You can use this variable. When the first n-1 threads “arrive” they should block until the nth thread arrives, at which point all threads should proceed.

Synchronizing I/O By default, Unix allows simultaneous, unrestricted access to files Recall: each process with an open file gets its own entry in the open file table, and its own offset

Concurrent File Access If two processes have the same file open for reading, there are no problems If one reads and another writes, the reader will see the results of write operations immediately If both are writing to the file, we can have race conditions

Race Condition in File Writes Process A lseek 100 (context switch) Write 50 bytes Process B lseek 100 write 100 bytes

File Locking How do we coordinate file access? –We can use system calls to lock files –We can lock whole files, or just sections of files –We can lock files for read, or for write

Mandatory vs. Advisory Locking Mandatory –Lock enforced by the OS –Prevent read() and write() from occurring when a file is locked –Works by removing the execute permission for group and setting the setgid bit –Not on by default. Need to mount the filesystem with “-o mand” Problems? –Non-privileged processes can hold locks indefinitely. Root cannot override (except by killing the process).

Advisory Locking Advisory –Not enforced by the OS –Thus, works for cooperating processes only Problems? –Non-cooperating processes may not ask for the lock Generally, this is the preferred/standard method for file locking

Locking: Read/Write Read locks can be shared, but write locks cannot –If there’s already a read lock, you can add another read lock, but not a write lock –If there’s already a write lock, you cannot add another lock of any kind

Lockfiles Open a file with O_CREAT | O_EXCL –Atomic, and fails if the file already exists For a singleton daemon, if the lockfile creation fails with EEXIST, then the singleton is already running What if the daemon crashed without removing the lockfile? –Daemon writes its pid to the file –If daemon starts up and finds the file, it reads the pid –If the process does not exist (use kill(pid, 0)) then delete the file and try again

Lockfiles (2) But, the sequence (read file, check for process, delete file) is not atomic, and therefore has a race condition –What can happen? Lock the file before reading it –Request exclusive lock –Read file –If the process does not exist, write your own pid, then release lock