SlideShare a Scribd company logo
OPERATING
SYSTEM
October 10
2012
PROJECT REPORT : READER/WRITER SOLUTION
Submitted to
Sir Raazi
Made by
Shahzeb Pirzada
Bariq Qadri
2 | P a g e
Introduction:
The .NET Framework provides several threading locking primitives. The Reader Writer Solution
is one of them.
The Reader Writer Solution class is used to synchronize access to a resource. At any given time,
it allows concurrent read access to multiple (essentially unlimited) threads, or it allows write
access for a single thread. In situations where a resource is read frequently but updated
infrequently, a Reader Writer Solution will provide much better throughput than the exclusive
Monitor lock.
Namespace: System.Threading
This namespace provides all the class methods and properties required for implementation of
the Reader Writer Solution class.
Assembly:mscorlib (in mscorlib.dll)
Constructors:
ReaderWriterLock: The constructor used to initialize a new instance of the Reader Writer
Solution class.
Properties:
 Reader Lock Held: This property gets a value that point to whether the current thread
holds a reader lock.
 Writer Lock Held: This property gets a value that points to whether the current thread
holds the writer lock.
 Writer Sequence Num: Gets the current sequence number.
Method
 Acquire Reader Lock (Int32): Acquires a reader lock, using an Int32 value for the time-
out.
 Acquire Writer Lock (Int32): Acquires the writer lock, using an Int32 value for the time-
out.
 Release Lock: Releases the lock, regardless of the number of times the thread acquired
the lock.
 Release Reader Lock: Decrements the lock count.
 Release Writer Lock: Decrements the lock count on the writer lock.
3 | P a g e
 Upgrade to Writer Lock (Int32): Upgrades a reader lock to the writer lock, using an
Int32 value for the time-out.
 Restore Lock: Restores the lock status of the thread to what it was before calling
Release Lock.
The problem with ReaderWriterLock is with its implementation. Several experts have slammed
this technique and found that outside of limited scenarios, it is actually far slower than the
Monitor. Enter method used to get an exclusive lock. ReaderWriterLock gives higher priority to
reader threads then writers. This makes sense if you have many readers and only a few writers.
So a lot of readers are able to read the resource while the writer has to wait longer to get the
lock. But what If you have equal or more writers. The process of favoring readers make writer
threads queue up and take a very long time to complete.
ReaderWriterLock intelligently handles recursive lock requests from the same thread. That is, if
the thread calls Acquire Reader Lock recursively (i.e., it already holds a reader lock), the lock is
granted immediately and the lock count is increased. The thread must still call Release Reader
Lock as many times as it called Acquire Reader Lock. Or, it can call Release Lock to reduce the
lock count to zero immediately. Be careful with Release Lock, though. If you subsequently call
Release Writer Lock or Release Reader Lock, the runtime will throw an exception.
A thread can hold a reader lock or a writer lock, but not both at the same time. Instead of
releasing a reader lock in order to acquire the writer lock, you can use Upgrade To Writer Lock
and Downgrade from Writer Lock.
In some situations, you might find that you're holding a reader lock and you need to upgrade to
a writer lock. In that situation, call Upgrade to Writer Lock, but understand that you don't get
the writer lock immediately. Your code will have to wait for any other readers in the queue to
release their locks, and will also have to wait behind any other writers that are already in the
write queue.
A thread should not call Acquire Writer Lock while it holds a reader lock. Doing so will cause the
thread to block while it holds the reader lock, and will lead to a deadlock if you use an infinite
timeout. You can call the Is Reader Lock Held method to determine if your thread currently
holds a reader lock before you attempt to acquire a writer lock.
Note that the opposite — calling Acquire Reader Lock while holding a writer lock — is just
fine. Since the thread has an exclusive lock on the resource, granting the lock is okay. However,
if you need to know whether your thread is currently holding a writer lock, you can call Is
Writer Lock Held.
The ReaderWriterLock class supports recursion; due to this it causes performance loss. in this
case the class needs to maintain a record of the number of times each thread acquires the lock
and increment and decrement the counter. When multiple reader threads acquire the same
lock (remember ReaderWriterLock class allows simultaneous reads), a counter is maintained for
4 | P a g e
each thread. This overhead is what causes the ReaderWriterLock to pale in comparison to the
Monitor class. It is approximately 6 times slower.
Tools:
 MICROSOFT VISUAL STUDIO (2010)

More Related Content

PPTX
Reader Writer problem
PPTX
Reader/writer problem
PPTX
Multiprocessing -Interprocessing communication and process sunchronization,se...
PPTX
Semaphore
PDF
Get Multicore Differentiation and Great Integrated Graphics Performance
DOCX
Process synchronization
PPT
Slot03 concurrency2
PPT
Classic synchronization
Reader Writer problem
Reader/writer problem
Multiprocessing -Interprocessing communication and process sunchronization,se...
Semaphore
Get Multicore Differentiation and Great Integrated Graphics Performance
Process synchronization
Slot03 concurrency2
Classic synchronization

What's hot (16)

PPTX
Metrics lightning talk
PPTX
Multi-Threading
PPT
Types of flows in mulesoft
PPT
MULTITHREADING CONCEPT
PDF
Operating Systems 1 (8/12) - Concurrency
PPTX
Thread presentation
PPTX
Introduction to LTspice iv
PPTX
PPTX
Multithreading in java
PPT
Threads and Synchronization in c#
PPTX
Real time operating systems (rtos) concepts 7
PDF
Usp message queues
PPTX
PPTX
Java Multi Thead Programming
PPTX
Architectural patterns part 4
Metrics lightning talk
Multi-Threading
Types of flows in mulesoft
MULTITHREADING CONCEPT
Operating Systems 1 (8/12) - Concurrency
Thread presentation
Introduction to LTspice iv
Multithreading in java
Threads and Synchronization in c#
Real time operating systems (rtos) concepts 7
Usp message queues
Java Multi Thead Programming
Architectural patterns part 4
Ad

Viewers also liked (18)

PPT
PDF
Dining Philosopher's Problem
PPT
Ch7 OS
 
PDF
Operating systems
PDF
The bounded buffer
PDF
Database File System
PPTX
Process synchronization in operating system
PPTX
Producer Consumer
PPTX
Producer and consumer classical problem
PDF
The Dining Philosophers problem in Bangla
PPT
File system-and-database-chapter01-connoly
PPT
Classical problem of synchronization
PPTX
Producer consumer
PPT
Chapter 11 - File System Implementation
PPTX
Application Layer
PPTX
Process synchronization in Operating Systems
PPT
Chapter 6 - Process Synchronization
PDF
SlideShare 101
Dining Philosopher's Problem
Ch7 OS
 
Operating systems
The bounded buffer
Database File System
Process synchronization in operating system
Producer Consumer
Producer and consumer classical problem
The Dining Philosophers problem in Bangla
File system-and-database-chapter01-connoly
Classical problem of synchronization
Producer consumer
Chapter 11 - File System Implementation
Application Layer
Process synchronization in Operating Systems
Chapter 6 - Process Synchronization
SlideShare 101
Ad

Similar to READER/WRITER SOLUTION (20)

PPTX
PPTX
Concurrency in Java
PDF
Thread Dump Analysis
PPTX
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
PPTX
Locks
PPTX
Securing docker containers
PDF
Introduction+To+Java+Concurrency
PPTX
Lec 1.10 Object Oriented Programming
PPTX
Threading in java - a pragmatic primer
PPTX
Multithreading in java
PPTX
Concurrency
PPTX
Slide 7 Thread-1.pptx
DOC
Concurrency Learning From Jdk Source
PPTX
Java-7 Concurrency
ODP
Multithreading 101
PDF
Java threading
PPT
Java threading 2
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
PDF
MultiThreading in Python
PPT
Java Multithreading and Concurrency
Concurrency in Java
Thread Dump Analysis
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Locks
Securing docker containers
Introduction+To+Java+Concurrency
Lec 1.10 Object Oriented Programming
Threading in java - a pragmatic primer
Multithreading in java
Concurrency
Slide 7 Thread-1.pptx
Concurrency Learning From Jdk Source
Java-7 Concurrency
Multithreading 101
Java threading
Java threading 2
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
MultiThreading in Python
Java Multithreading and Concurrency

More from Shahzeb Pirzada (20)

PDF
Sigma Society Inauguration
PDF
Newton's Ninja - Combat'13
DOCX
The Open Systems Interconnect (OSI) Model - IS AUDIT
DOCX
RAID - IS AUDIT
DOCX
Process Improvement Techniques - IS AUDIT
DOCX
Project Management Certifications - IS AUDIT
DOCX
Computer Assisted Audit Techniques (CAATS) - IS AUDIT
DOCX
Certifications on Security - IS AUDIT
DOCX
Google Advance Searching- Information System Audit & Control Report
TXT
Prolog Code [Family Tree] by Shahzeb Pirzada
DOCX
What is Fuzzy Logic?
DOCX
Elements of Quality Management System
DOCX
City School Network- Routing & Switching Final Report
PPT
THE SEVEN HABITS OF HIGHLY EFFECTIVE TEENS
DOCX
Entrepreneurial Leadership-FINAL REPORT
DOCX
SP STUDENT MANAGEMENT SYSTEM-Human Computer Interaction Final Report
PPTX
White collar vs Blue collar
DOC
Story Narration Competition -Script
DOC
Declamation Contest Script for Moc
PPTX
BOOTABLE OPERATING SYSTEM PPT
Sigma Society Inauguration
Newton's Ninja - Combat'13
The Open Systems Interconnect (OSI) Model - IS AUDIT
RAID - IS AUDIT
Process Improvement Techniques - IS AUDIT
Project Management Certifications - IS AUDIT
Computer Assisted Audit Techniques (CAATS) - IS AUDIT
Certifications on Security - IS AUDIT
Google Advance Searching- Information System Audit & Control Report
Prolog Code [Family Tree] by Shahzeb Pirzada
What is Fuzzy Logic?
Elements of Quality Management System
City School Network- Routing & Switching Final Report
THE SEVEN HABITS OF HIGHLY EFFECTIVE TEENS
Entrepreneurial Leadership-FINAL REPORT
SP STUDENT MANAGEMENT SYSTEM-Human Computer Interaction Final Report
White collar vs Blue collar
Story Narration Competition -Script
Declamation Contest Script for Moc
BOOTABLE OPERATING SYSTEM PPT

Recently uploaded (20)

PPTX
Next-Generation Airline Network & Schedule Planning
PDF
The Cost of Neglect How Skipping Fire Safety Maintenance Can Lead to Disaster...
PDF
Blush & Brown Modern Minimalist eBook Workbook.pdf
PPTX
Your Trusted SEO Company in San Antonio – Studio0413
PPTX
Struggles of Blind Individuals and How We Can Help..pptx
PDF
Blush & Brown Modern Minimalist eBook Workbook.pdf
PDF
Threat Intelligence Services in Abu Dhabi
PDF
Legacy Application Modernisation Services.pdf
PDF
Leveraging Earth Observation Data to Improve Wildfire Prevention and Manageme...
PDF
Green minimalist professional Business Proposal Presentation.pdf
PDF
5 Best Sites to Buy Snapchat Accounts (Aged & Pva).pdf
PDF
Top 7 Cybersecurity Companies in Abu Dhabi
PDF
Environmental Impact Assessment of Quarrying Plants An IRF Kriging Solution t...
PDF
Why Infotrench Stands Out as the Best SEO Agency in Noida.pdf
PPTX
AI-Powered-Mobile-App-Development-The-Future-of-Intelligent-Applications.pptx
PPTX
Telecom Expense Management & Optimization – The ITE Group.pptx
PPTX
Social Media Marketing Services in USA | Boost Your Brand
PPTX
Unlocking-Business-Potential-Power-BI-Development-Services.pptx
PDF
Future of Digital Marketing Trends & Insights.pdf
DOC
SAS毕业证学历认证,利兹三一大学毕业证国外毕业证
Next-Generation Airline Network & Schedule Planning
The Cost of Neglect How Skipping Fire Safety Maintenance Can Lead to Disaster...
Blush & Brown Modern Minimalist eBook Workbook.pdf
Your Trusted SEO Company in San Antonio – Studio0413
Struggles of Blind Individuals and How We Can Help..pptx
Blush & Brown Modern Minimalist eBook Workbook.pdf
Threat Intelligence Services in Abu Dhabi
Legacy Application Modernisation Services.pdf
Leveraging Earth Observation Data to Improve Wildfire Prevention and Manageme...
Green minimalist professional Business Proposal Presentation.pdf
5 Best Sites to Buy Snapchat Accounts (Aged & Pva).pdf
Top 7 Cybersecurity Companies in Abu Dhabi
Environmental Impact Assessment of Quarrying Plants An IRF Kriging Solution t...
Why Infotrench Stands Out as the Best SEO Agency in Noida.pdf
AI-Powered-Mobile-App-Development-The-Future-of-Intelligent-Applications.pptx
Telecom Expense Management & Optimization – The ITE Group.pptx
Social Media Marketing Services in USA | Boost Your Brand
Unlocking-Business-Potential-Power-BI-Development-Services.pptx
Future of Digital Marketing Trends & Insights.pdf
SAS毕业证学历认证,利兹三一大学毕业证国外毕业证

READER/WRITER SOLUTION

  • 1. OPERATING SYSTEM October 10 2012 PROJECT REPORT : READER/WRITER SOLUTION Submitted to Sir Raazi Made by Shahzeb Pirzada Bariq Qadri
  • 2. 2 | P a g e Introduction: The .NET Framework provides several threading locking primitives. The Reader Writer Solution is one of them. The Reader Writer Solution class is used to synchronize access to a resource. At any given time, it allows concurrent read access to multiple (essentially unlimited) threads, or it allows write access for a single thread. In situations where a resource is read frequently but updated infrequently, a Reader Writer Solution will provide much better throughput than the exclusive Monitor lock. Namespace: System.Threading This namespace provides all the class methods and properties required for implementation of the Reader Writer Solution class. Assembly:mscorlib (in mscorlib.dll) Constructors: ReaderWriterLock: The constructor used to initialize a new instance of the Reader Writer Solution class. Properties:  Reader Lock Held: This property gets a value that point to whether the current thread holds a reader lock.  Writer Lock Held: This property gets a value that points to whether the current thread holds the writer lock.  Writer Sequence Num: Gets the current sequence number. Method  Acquire Reader Lock (Int32): Acquires a reader lock, using an Int32 value for the time- out.  Acquire Writer Lock (Int32): Acquires the writer lock, using an Int32 value for the time- out.  Release Lock: Releases the lock, regardless of the number of times the thread acquired the lock.  Release Reader Lock: Decrements the lock count.  Release Writer Lock: Decrements the lock count on the writer lock.
  • 3. 3 | P a g e  Upgrade to Writer Lock (Int32): Upgrades a reader lock to the writer lock, using an Int32 value for the time-out.  Restore Lock: Restores the lock status of the thread to what it was before calling Release Lock. The problem with ReaderWriterLock is with its implementation. Several experts have slammed this technique and found that outside of limited scenarios, it is actually far slower than the Monitor. Enter method used to get an exclusive lock. ReaderWriterLock gives higher priority to reader threads then writers. This makes sense if you have many readers and only a few writers. So a lot of readers are able to read the resource while the writer has to wait longer to get the lock. But what If you have equal or more writers. The process of favoring readers make writer threads queue up and take a very long time to complete. ReaderWriterLock intelligently handles recursive lock requests from the same thread. That is, if the thread calls Acquire Reader Lock recursively (i.e., it already holds a reader lock), the lock is granted immediately and the lock count is increased. The thread must still call Release Reader Lock as many times as it called Acquire Reader Lock. Or, it can call Release Lock to reduce the lock count to zero immediately. Be careful with Release Lock, though. If you subsequently call Release Writer Lock or Release Reader Lock, the runtime will throw an exception. A thread can hold a reader lock or a writer lock, but not both at the same time. Instead of releasing a reader lock in order to acquire the writer lock, you can use Upgrade To Writer Lock and Downgrade from Writer Lock. In some situations, you might find that you're holding a reader lock and you need to upgrade to a writer lock. In that situation, call Upgrade to Writer Lock, but understand that you don't get the writer lock immediately. Your code will have to wait for any other readers in the queue to release their locks, and will also have to wait behind any other writers that are already in the write queue. A thread should not call Acquire Writer Lock while it holds a reader lock. Doing so will cause the thread to block while it holds the reader lock, and will lead to a deadlock if you use an infinite timeout. You can call the Is Reader Lock Held method to determine if your thread currently holds a reader lock before you attempt to acquire a writer lock. Note that the opposite — calling Acquire Reader Lock while holding a writer lock — is just fine. Since the thread has an exclusive lock on the resource, granting the lock is okay. However, if you need to know whether your thread is currently holding a writer lock, you can call Is Writer Lock Held. The ReaderWriterLock class supports recursion; due to this it causes performance loss. in this case the class needs to maintain a record of the number of times each thread acquires the lock and increment and decrement the counter. When multiple reader threads acquire the same lock (remember ReaderWriterLock class allows simultaneous reads), a counter is maintained for
  • 4. 4 | P a g e each thread. This overhead is what causes the ReaderWriterLock to pale in comparison to the Monitor class. It is approximately 6 times slower. Tools:  MICROSOFT VISUAL STUDIO (2010)