COMP 346 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

COMP 346

Description:

critical section in which thread may be changing common variables, updating a ... The critical aspect of semaphores is that they are executed atomically. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 15
Provided by: ENCS
Category:
Tags: comp | critical

less

Transcript and Presenter's Notes

Title: COMP 346


1
COMP 346 OPERATING SYSTEMS
Tutorial 5
http//www.cs.concordia.ca/k_karthi/comp346.html
2
Topics
  • MutualExclusion abstract class
  • Semaphores
  • Java Semaphores
  • Synchronization rules
  • Monitors
  • Monitors pros and cons
  • Condition variables
  • Java Monitors

3
Mutual Exclusion
critical section in which thread may be
changing common variables, updating a table,
writing a file and so on. Mutual exclusion If
thread is executing in its critical section, then
no other threads can be executing in their
critical sections.
public abstract class MutualExclusion public
static void criticalSection() try
Thread.sleep( (int) (Math.random() TIME) )
catch (InterruptedException e)
public static void nonCriticalSection()
try Thread.sleep( (int)
(Math.random() TIME) ) catch
(InterruptedException e) public
abstract void enteringCriticalSection(int
t) public abstract void leavingCriticalSection(i
nt t) private static final int TIME 3000

4
Semaphores
a synchronization tool A semaphore S , is
accessed only through two standard operations P
and V. The definitions are P(S) While S lt 0
S-- V(S) S When one thread
modifies the semaphore value, no other thread can
simultaneously modify that same semaphore
value. The general strategy for using a semaphore
(binary) to control access to a critical section
is Semaphore S
P(S)
criticalSection() V(S)

5
Semaphores contd..
Each thread that wishes to use a resource would
perform a P operation on the semaphore (thereby
decrementing the count). When a thread releases
a resource, it performs a V operation
(incrementing the count). When the count for the
semaphore goes to 0, all resources are being
used. Subsequent threads that wish to use a
resource will block until the count becomes
greater than 0. disadvantage busy waiting
(while a process is in its critical section, any
other process that tries to enter its critical
section must loop continuously in the entry code.
Busy waiting wastes CPU cycles that some other
process might be able to use productively. This
type of semaphore is called spinlock. To overcome
this we need the process to block itself. The
block operation places a process into a waiting
queue associated with the semaphore, and the
state of the process is switched to the waiting
state. A process that is blocked, should be
restarted by a wakeup operation, which changes
the process from the waiting state to the ready
state.
6
Semaphores contd..
The semaphore operations can now be defined as
P(S) value-- if (value lt 0) add this
process to list block V(S)
value if (value lt 0) remove a process
P from list wakeup(P)
When a process must wait on a semaphore it is
added to the list of processes for that
semaphore.
A V operation removes one process from the list
of waiting processes, and awakens the process.
7
Semaphores contd..
The critical aspect of semaphores is that they
are executed atomically. We must guarantee that
no two processes can execute P and V operations
on the same semaphore at the same time.
Java Semaphores
Java does not provide a semaphore, but we can
easily construct one using standard
synchronization mechanisms. Declaring the P() and
V() methods as synchronized ensures that each
operation is performed atomically.
8
Java Semaphore Implementation
Public class Semaphore public
Semaphore( ) value 0
public Semaphore (int v)
value v public
synchronized void P( ) while
(value lt 0) try
wait( ) catch
(InterruptedException e)
value --
public synchronized void V( )
value notify ( ) private int
value
9
Synchronization Rules
The synchronized keyword is a straightforward
construct and here are few rules about its
behavior
  • A thread that owns the lock for an object can
    enter another synchronized method (or block) for
    the same object.
  • A thread can nest synchronized method
    invocations for different objects. Thus it can
    simultaneously own the lock for several
    different objects.
  • If the wait set for an object is empty, then a
    call to notify( ) or notifyAll( ) has no effect.

10
The Monitor
  • An abstract data structure
  • Uses conditionals (data members)
  • When a process runs within a monitor only one
    function is accessed at a time
  • Conditional variables represent critical regions
    and functions are critical sections, thus must be
    guaranteed to be atomic
  • In analogy to semaphore it will have its own
    signal( ) and wait ( ) or several of them

11
The Monitor Advantages and Disadvantages
Advantages
  • Reduces the number of programmer errors that
    occur when using semaphores
  • Internal implementation of the monitor may
    change, but the clients using it shouldnt be
    rewritten

Disadvantages
  • Monitor allows the programmer to release a
    resource, which was never acquired
  • It also allows the programmer to request the
    same resource without releasing it first

12
Condition Variables
  • When a thread is running within the monitor and
    certain condition is not met, it blocks
  • Ownership is transferred to another process
  • It can only be unblocked by other processes
    which satisfies the condition
  • The variables used in conditions are called
    condition variables.

Basic Operations
  • queue( ) how many processes are waiting on a
    condition
  • signal( ) let others know that condition
    doesnt hold anymore
  • wait ( ) block oneself on a condition and
    release monitor to others

13
Java Monitors
In many ways, the objects lock acts as a
monitor. Every java object has an associated
monitor. A thread can acquire an objects monitor
by either entering a synchronized method or
blocking. With monitors, the wait and signal
operations can be applied to named condition
variables, allowing a thread to wait for a
specific condition or to be notified when a
specific condition has been met. Java monitors
use the signal-and- continue approach When a
thread is signaled with the notify ( ) method, it
can acquire the lock for the monitor only when
the notifying thread exits the synchronized
method or block.
14
Javas Implementation of Monitors
  • Synchronized makes functions atomic
  • Java maintains internally objects waiting queue
    and does not provide queue( ) function
  • Javas equivalent to signal( ) notify ( ) and
    notifyAll( ) notify( ) wakes up exactly one
    thread sleeping on the condition notifyAll( )
    everyone respectively.
  • Javas equivalent to wait( ) is . wait()
Write a Comment
User Comments (0)
About PowerShow.com