Title: Multitasking
1Multitasking Synchronization
2Introduction (1)
- There are two classes of media
- 1) Static media do not have a time dimension,
and their contents and meaning do not depend on
the presentation of time. Static media include
alphanumeric data, graphics, and still images. - 2) Dynamic media have a time dimension,
and their meanings and correctness depend on the
rate at which they are presented. Dynamic media
include animation, audio, and video.
3Introduction (2)
- Video must be played at 25 frames/s ( or
30 frames/s, depending on the system ) in order
to have perceptually smooth movement. - Similarly, only one playback rate is
sensible when we play audio. Playback at slower
or faster rates will distort the meaning /
quality of sound. - Since these media must be played back
continuously at a fixed rate, they are often
called continuous media. They are also called
isochronous media, because of the fixed
relationship between each media unit and time.
4Introduction (3)
- We define multimedia systems as capable of
handling at least one type of continuous media in
digital form as well as static media ( e.g.,
image sound)
5Classes of Multimedia Systems
- Multimedia systems can be classified into
- 1) Standalone systems use dedicated system
resources. The amount of multimedia information
may be limited and multimedia data communications
are not supported. - 2) Distributed systems share both system
resources and information resources and can
support communication among users.
6Challenges of Multimedia Computing and
Communications
- Multimedia data have a time dimension and must
be transmitted, processed and presented in a
fixed rate in most applications. So, the
multimedia computing and communications system
must meet this timeliness requirement. - Multimedia applications normally use multiple
related media simultaneously. - Multimedia data are data intensive, so they
must be compressed, and high-speed communications
networks and powerful computers are required to
handle them.
7Networked Multimedia Synchronization (1)
- Synchronization is defined as the correct or
desired temporal appearance (presentation) of the
media items.
8Networked Multimedia Synchronization (2)
Each Package contains a bit of information,
that tells at which time it needs to be handled !
9Networked Multimedia Synchronization (3)
- The aim of temporal synchronization
specification is to represent all possible
temporal relationships among media.
10Networked Multimedia Synchronization (4)
- In time-line-based temporal specification,
the presentation time of each stream is specified
to a common clock.
11Required Synchronization Accuracy (1)
- Synchronization can be measured by four
parameters - 1) Delay Measures end-to-end delay in live
applications and response time in retrieval
applications - 2) Delay jitter Measures the deviation of
presentation time of continuous media samples
from their fixed or desired presentation time - 3) Intermedia skew Measures the time shift
between related media from the desired temporal
relationship - 4) Tolerable error rate Measures the
allowable bit error rate and packet error rate
for a particular stream in a particular
application. There are two types of errors - - Bit error
- - Packet loss
12Required Synchronization Accuracy (2)
Very sensitive, since we use it every day
to Localize sounds !
13Synchronization Problems
- The clock rates at the transmitter and the
receiver might be different. If the clock rate
is faster at the transmitter than at the
receiver, the transmitter sends more data than
what the receiver can consume, causing data
overflow at the receiver. On the other hand, if
the clock rate is slower at the transmitter than
at the receiver, the receiver has a
data-starvation problem. - When connectionless transport protocol is used,
packets belonging to a stream may arrive at the
receiver out of order. - When multiple sources are involved, it may be
difficult to coordinate the stream transmission
times at different locations. - It is important to maintain synchronization
during and after user interactions. F. ex., when
a presentation is resumed after pausing for some
time, the presentation should be resumed
synchronously. Thus, synchronization is
complicated by user interactions. - Coding / decoding times for different streams may
vary.
14Mechanisms to achieve multimedia synchronization
(1)
15Mechanisms to achieve multimedia synchronization
(2)
16Mechanisms to achieve multimedia synchronization
(3)
- Measures to counter delay jitter can be
divided into two groups - Corrective measures do not do anything to the
transport protocol and underlying networks. They
try to smooth out the jitter at the destination
before media data are presented to the user. - Preventive measures try to minimize delay and
delay jitter by improving transport protocol and
underlying networks.
17Mechanisms to achieve multimedia synchronization
(4)
- Corrective measures Buffering
- Preventive Measures When delay jitter is
too large, corrective measures are not very
useful, because they require a large buffer and
long buffering time, which is inappropriate for
many applications.
18Mutual Exclusion
- The simplest way for two or more threads in a
java program to interact is via an object, whose
methods can be invoked by the set of threads.
This shared objects state can be observed and
modified by its methods. - Consequently, two threads can communicate by one
thread writing the state of the shared object and
the other thread reading that state. - Trouble !!
19Mutual Exclusion, Interference (1)
- The execution of the instructions from a set
of threads can be interleaved in an arbitrary
fashion. This interleaving can (and will ? )
result in incorrect updates to the state of a
shared object. This is called interference.
Ornamental Garden
- To simplify the problem, the garden is
designed so that people can enter but never leave.
20Mutual Exclusion, Interference (2)
- The general solution to the problem of
interference is to give methods that access a
shared object with mutually exclusive access to
that object. - This ensures that an update is not interupted
by concurrent updates !
21Mutual Exclusion, Interference (3)
- Concurrent activations of a method in Java
can be made mutually exclusive by prefixing the
method with the keyword synchronized. - Java associates a lock with every object. The
Java compiler inserts code to acquire the lock
before executing the body of a synchronized
method, and code to release the lock before the
method returns. - If an object has more than one method, all the
methods should be synchronized in order to ensure
mutually exclusive access to the state of the
object. -
Ornamental Garden
22Mutual Exclusion, Interference (4)
- Once a thread has acquired the lock on an
object by executing a synchronized method, that
method may itself call another synchronized
method from the same object without having to
wait to acquire the lock again. - The lock counts how many times it has been
acquired by the same thread and does not allow
another thread to access the object until there
has been an equivalent number of releases. - This locking strategy is termed recursive
locking, since it permits recursive synchronized
methods.
23Mutual Exclusion, Interference (5)
- Example
- public synchronized void increment(int n)
- if(n gt 0)
- value
- increment(n-1)
- else return
-
- If locking in Java was not recursive, this
example would cause a calling thread to be
blocked forever, waiting to acquire a lock which
it already holds.
24Monitors and Condition Synchronization (1)
- A monitor encapsulates data, which can only be
observed and modified by monitor access
procedures. Only a single access procedure may
be active at a time. An access procedure thus
has mutually exclusive access to the data
variables encapsulated in the monitor. - Monitors support condition synchronization in
addition to ensuring that access to the data they
encapsulate is mutually exclusive. Condition
synchronization permits a monitor to block
threads until a particular condition holds (such
as a count becoming non-zero, a buffer becoming
empty, or a new input becoming available).
A monitor is simply represented in Java as a
class that has synchronized methods.
25Monitors and Condition Synchronization (2)
- Each guarded action in the model of a monitor
is implemented as a synchronized method which
uses a while loop and wait() to implement the
guard. -
- Changes in the state of the monitor are
signalled to waiting threads using notify() or
notifyAll().
Car Park
26Semaphores (1)
- down(S) when S gt 0 do decrement S
- up(S) increment S
- Semaphores are usually implemented by
- a blocking wait() as follows
- down(S) if S gt 0
- decrement S
- else
- block execution of calling
process - up(S) if processes blocked on S
- awaken one of them
- else
- increment S
- A semaphore S is an integer variable that can
take only non-negative values. Once S has been
given an initial value, the only operations
permitted on S are up(S) and down(S).
27Semaphores (2)
- class Buffer
- protected Object buf
- protected int in 0
- protected int out 0
- protected int count 0
- protected int size
-
- public Buffer(int size)
- this.size size
- buf new Objectsize
-
-
- public synchronized void put(Object o) throws
InterruptedException - while (count size) wait()
- bufin o
- count
- in (in1) size
- notify()
-
28Semaphores (3)
- Implementations of semaphores usually manage
processes, blocked on a semaphore by down(), as a
First-In-First-Out queue (FIFO). The operation
up() awakens the process at the head of the queue.
in
FIFO
out
Semaphore
29Semaphores (4)
- The producer-consumer model with a bounded
buffer is an example of a program that handles
data items without altering them. - In addition, the behaviour of the producer,
the consumer, and the buffer itself are not
affected by the value of the items they handle.
In other words, they do not test the value of
these data items. The behaviour is said to be
data-independent.
Bounded Buffer
30Deadlock (1)
- Deadlock occurs in a system when all its
constituent processes are blocked i.e., there
are no eligible actions it can perform. - Four necessary and sufficient conditions for
the occurrence of deadlock - Serially reusable resources The processes
involved share resources which they use under
mutual exclusion - Incremental acquisition Processes hold on to
resources already allocated to them while waiting
to acquire additional resources - No preemtion Once acquired by a process,
resources cannot be preemted (forcibly
withdrawn), but are only released voluntarily - Wait-for-cycle A circular chain (or cycle) of
processes exists, such that each process holds a
resource which its successor in the cycle is
waiting to acquire
31Deadlock (2)
32Deadlock (3)
33Deadlock (4)
34Dining Philosophers
- The problem each philosopher is modelled by
the process - Phil (sit down ?right.get ?left.get ?eat
?left.put ?right.put ?rise)
Philosophers
- One solution to the dining philosophers
problem depends on ensuring that a wait-for-cycle
cannot exist. To do so, some assymmetry is
introduced into the definition of a philosopher. - Odd numbered philosopher gets right fork first
and even-numbered philosopher gets left fork
first.
Philosophers (fixed)
35Racing conditions (1)
- Racing conditions can occur when two or more
processes are reading some shared data and the
final result depends on who runs exactly when.
36Racing conditions (2)
37Racing conditions (3)
- So, put the thread to sleep !
38Threads (1)
API
39Threads (2)
- Processes are not really parallel, but
sequential !
40Threads (3)
41Threads (4)
- void interrupt()
- First the checkAccess() method of this thread is
invoked, which may cause a SecurityException to
be thrown - It the thread is blocked in an invokation of
wait(), join(), sleep() or methods of the Object
class, then it will receive an InterruptedExceptio
n.
42Threads (5)
- join()
- Waits for this thread to die, which ensures
sequential execution !
43Threads (6)
- Thread A joins thread B (waits for thread B
to finish) but gets interrupted by thread C
44Threads (7)
- Thread A interrupts itself, but the interrupt
throws first an exception when the sleep() method
is activated, since isInterrupted() does not
affect the interrupted status of the thread
45Threads (8)
- Here, the thread will enter everlasting
sleep, since the interrupted() method cleares the
interrupted status of the thread. So, unless
another thread comes and wakes the sleeping
thread with another interrupt(), it will sleep
forever The sleeping-beauty phenomenon
46Threads (9)
47Threads (10)
48Threads (11)
- Solution F.ex. use join() on one of the
threads prior to acquiring locks