SYNCHRONIZATION
HARDWARE
WHAT IS PROCESS SYNCRONIZATION.
• Process Synchronization is a way to coordinate processes that
use shared data
• Cooperating processes are processes that share resources.
• Executing many concurrent processes, process synchronization
helps to maintain shared data consistency and cooperating
process execution
• A race condition occurs when two or more operations are
executed at the same time, not scheduled in the proper
sequence, and not exited in the critical section correctly.
PROCESS SYNCHRONIZATION
• Several processes are run in operating system.
• Some of them share resources due to which problem like data
inconsistency arise
• For example :One process changing the data in memory
location where another process is trying to read the same
memory location. It is possible that the data read by the second
process will be erroneous
PROCESS
B
PROCES
SA
.
.
.
.
.
.
DATA
DATA
DATA
WRITES READS
IN PRODUCER CONSUMER PROBLEM:
(BOUNDED BUFFER PROBLEM)
The problem is to make sure that the producer won't try to add
data into the buffer if it's full and that the consumer won't try to
remove data from an empty buffer.
DATA
DATA
DATA
DATA
PRODUCE
R
CONSUME
R
A SECTION OF CODE,
• A Critical Section Environment contains:
• Entry Section Code requesting entry into
the critical section.
• Critical Section Code in which only one
process can execute at any one time.
• Exit Section The end of the critical section,
releasing or allowing others in.
• Remainder Section Rest of the code AFTER
the critical section.
THE CRITICAL SECTION MUST ENFORCE ALL
THREE OF THE FOLLOWING RULES:
• Mutual Exclusion: No more than one process can execute in its
critical section at one time.
• Progress: If no one is in the critical section and someone wants
in, then those processes not in their remainder section must be
able to decide in a finite time who should go in.
• Bounded Wait: All requesters must eventually be let into the
critical section.
WHY WE NEED HARDWARE BASE SOLUTON
• Software-based solutions such as Peterson’s are not
guaranteed to work on modern computer architectures . In the
following discussions, we explore several more solutions to the
critical-section problem.
SOLUTION TO CRITICAL-SECTION
PROBLEM USING LOCKS
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
TESTANDSET INSTRUCTION
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
SOLUTION USING TESTANDSET
• Shared boolean variable lock, initialized to FALSE
• Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
BOUNDED-WAITING MUTUAL EXCLUSION
WITH TESTANDSET()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
{
key = TestAndSet(&lock);
}
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[ j ])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
F F F F
F F F F
Waitin
g
Key
Lock F
1 2 3 4
BOUNDED-WAITING MUTUAL EXCLUSION
WITH TESTANDSET()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
{
key = TestAndSet(&lock);
}
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[ j ])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
F /T F F F
F /T F F F
Waitin
g
Key
Lock F/T
1 2 3 4
SWAP INSTRUCTION
• Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
SOLUTION USING COMPARE AND SWAP
• Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key
• Solution:
do {
key = TRUE;
while ( key == TRUE)
{
Swap (&lock, &key );
}
// critical section
lock = FALSE;
// remainder section
} while (TRUE);

Synchronization hardware

  • 1.
  • 2.
    WHAT IS PROCESSSYNCRONIZATION. • Process Synchronization is a way to coordinate processes that use shared data • Cooperating processes are processes that share resources. • Executing many concurrent processes, process synchronization helps to maintain shared data consistency and cooperating process execution • A race condition occurs when two or more operations are executed at the same time, not scheduled in the proper sequence, and not exited in the critical section correctly.
  • 3.
    PROCESS SYNCHRONIZATION • Severalprocesses are run in operating system. • Some of them share resources due to which problem like data inconsistency arise • For example :One process changing the data in memory location where another process is trying to read the same memory location. It is possible that the data read by the second process will be erroneous PROCESS B PROCES SA . . . . . . DATA DATA DATA WRITES READS
  • 4.
    IN PRODUCER CONSUMERPROBLEM: (BOUNDED BUFFER PROBLEM) The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer. DATA DATA DATA DATA PRODUCE R CONSUME R
  • 5.
    A SECTION OFCODE, • A Critical Section Environment contains: • Entry Section Code requesting entry into the critical section. • Critical Section Code in which only one process can execute at any one time. • Exit Section The end of the critical section, releasing or allowing others in. • Remainder Section Rest of the code AFTER the critical section.
  • 6.
    THE CRITICAL SECTIONMUST ENFORCE ALL THREE OF THE FOLLOWING RULES: • Mutual Exclusion: No more than one process can execute in its critical section at one time. • Progress: If no one is in the critical section and someone wants in, then those processes not in their remainder section must be able to decide in a finite time who should go in. • Bounded Wait: All requesters must eventually be let into the critical section.
  • 7.
    WHY WE NEEDHARDWARE BASE SOLUTON • Software-based solutions such as Peterson’s are not guaranteed to work on modern computer architectures . In the following discussions, we explore several more solutions to the critical-section problem.
  • 8.
    SOLUTION TO CRITICAL-SECTION PROBLEMUSING LOCKS do { acquire lock critical section release lock remainder section } while (TRUE);
  • 9.
    TESTANDSET INSTRUCTION boolean TestAndSet(boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
  • 10.
    SOLUTION USING TESTANDSET •Shared boolean variable lock, initialized to FALSE • Solution: do { while ( TestAndSet (&lock )) ; // do nothing // critical section lock = FALSE; // remainder section } while (TRUE);
  • 11.
    BOUNDED-WAITING MUTUAL EXCLUSION WITHTESTANDSET() do { waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) { key = TestAndSet(&lock); } waiting[i] = FALSE; // critical section j = (i + 1) % n; while ((j != i) && !waiting[ j ]) j = (j + 1) % n; if (j == i) lock = FALSE; else waiting[j] = FALSE; // remainder section } while (TRUE); F F F F F F F F Waitin g Key Lock F 1 2 3 4
  • 12.
    BOUNDED-WAITING MUTUAL EXCLUSION WITHTESTANDSET() do { waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) { key = TestAndSet(&lock); } waiting[i] = FALSE; // critical section j = (i + 1) % n; while ((j != i) && !waiting[ j ]) j = (j + 1) % n; if (j == i) lock = FALSE; else waiting[j] = FALSE; // remainder section } while (TRUE); F /T F F F F /T F F F Waitin g Key Lock F/T 1 2 3 4
  • 13.
    SWAP INSTRUCTION • Definition: voidSwap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }
  • 14.
    SOLUTION USING COMPAREAND SWAP • Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key • Solution: do { key = TRUE; while ( key == TRUE) { Swap (&lock, &key ); } // critical section lock = FALSE; // remainder section } while (TRUE);