Professional Documents
Culture Documents
FALLSEM2023-24 BCSE303L TH VL2023240100787 2023-06-01 Reference-Material-I
FALLSEM2023-24 BCSE303L TH VL2023240100787 2023-06-01 Reference-Material-I
Concurrency
Two variations:
(1) unbounded-buffer
(2) bounded-buffer
Unbounded-buffer: places no practical limit on the size of the buffer
Producer never waits
Consumer waits if there is no buffer to consume
Bounded-buffer: assumes that there is a fixed buffer size
Producer must wait if all buffers are full
Consumer waits if there is no buffer to consume
IPC - Shared Memory: An area of memory shared among the processes that
wish to communicate.
The communication is under the control of the users processes not the oper-
ating system.
Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.
IPC - Message Passing: Processes communicate with each other without re-
sorting to shared variables
IPC facility provides two operations:
(1) send(message)
(2) receive(message)
The message size is either fixed or variable
Logical
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
Direct Communication
Processes must name each other explicitly;
send(P, message) - send a message to process P
receive(Q, message) - receive a message from process Q
Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating processes
Between each pair there exists exactly one link
The link may be unidirectional
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Indirect Communication
Operations;
Create a new mailbox (port)
Send and receive messages through mailbox
Delete a mailbox
Primitives are defined as;
send(A, message) - send a message to mailbox A
receive(A, message) - receive a message from mailbox A
Indirect Communication
Mailbox sharing
P1 , P2 , and P3 share mailbox A
P1 , sends; P2 and P3 receive
Who gets the message?
Indirect Communication
Mailbox sharing
P1 , P2 , and P3 share mailbox A
P1 , sends; P2 and P3 receive
Who gets the message?
Solutions:
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified who the
receiver was.
Synchronization:
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send - the sender is blocked until the message is received
Blocking receive - the receiver is blocked until a message is available
Non-blocking is considered asynchronous
Non-blocking send - the sender sends the message and continue
Non-blocking receive - the receiver receives a valid message or null message
Several processes access and manipulate the same data concurrently and out-
come of the execution depends on the particular order in which the access
takes place.
To guard against the race condition, we need to ensure that processes be
synchronized.
A solution to the critical section problem must satisfy the following three re-
quirements;
Mutual Exclusion: If process Pi is executing in its critical section, then no other
processes can be executing in their critical sections.
If one process is executing inside critical section then the other process must
not enter in the critical section.
Progress: If no process is executing in its critical section and some processes wish
to enter their critical sections, then only those processes that are not executing
in their remainder sections can participate in the decision on which will enter
its critical section next, and this selection cannot be postponed.
Progress means that if one process doesn’t need to execute into critical section
then it should not stop other processes to get into the critical section.
Bounded Waiting: The number of times that other processes are allowed to
enter their critical sections after a process has made a request to enter its
critical section and before that request is granted.
The process must not be endlessly waiting for getting into the critical section.
The variable turn indicates whose turn it is to enter its critical section.
That is, if turn == i, then process Pi is allowed to execute in its critical
section.
The flag array is used to indicate if a process is ready to enter its critical
section.
That is, if flag [i] is true, this value indicates that Pi is ready to enter its critical
section.
Property 2 & 3:
The process Pi ,can be prevented from entering the critical section only if it is
stuck in the while loop with the condition
flag [j] == true and turn == j
If Pj is not ready to enter its critical section, then
flag [j] = false, and Pi can enter its critical section.
If Pj has set flag [j] to true and also executing in its while statement, then
either turn == i or turn == j
If turn == i , then Pi will enter the critical section and if turn == j then Pj
will enter the critical section.
However, once Pj exits its critical section, it will reset flag [j] to false, allowing
Pi to enter its critical section.
If Pi resets flag [j] to true, it must also set turn to i
Thus , since Pi does not change the value of the variable turn while executing
the while statement, Pi will enter the critical section(progress) after atmost
one entry by Pj (bounded waiting).
Processes numbered 0 to N − 1
num is an array of N integers.
Each entry corresponds to a process(initially 0)
Binary Semaphore: The value of a binary semaphore can range only between
0 and 1.
On some systems, binary semaphores are known as mutex locks, as they are
the locks that provide mutual exclusion.
Counting Semaphore: Its value can range over an unrestriced domain.
It is used to control access to a resource that has multiple instances.
The producer tries to insert data into an empty slot of the buffer.
The consumer tries to remove data from a filled slot in the buffer.
The producer and consumer processes share the following data structures;
semaphore chopstick[5];
where all the elements of chopstick are initialized to 1.
Timing errors can occur when either mutex locks or semaphores are used.
One strategy for dealing with such errors is to incorporate simple synchroniza-
tion tools as high-level language construct, the monitor type.
A high level abstraction that provides a convenient and effective mechanism
for process synchronization.
A monitor type presents a set of programmer defined operations that provide
mutual exclusion within the monitor.
condition x, y ;
The operation x.wait() means that the process invoking this operation is sus-
pended until another process invokes x.signal()
The x.signal() operation resumes exactly one suspended process.