본문 바로가기

Operating System Design

Operating System Design - Synchronization Tools

Backgound

 

Concurrent access to shared data may result in data inconsistency

Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes

 

Race Condition

 

Unless there is mutual exclusion, the same pid could be assigned to two different processes

Critical Section

 

When one process in critical section, no other may be in its critical section

Solution : 

Mutual Exclusion : If process Pi is executing in its critical section, then no other processes can be executing in their critical sections

Progress : If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely

Bounded Waiting : A bound must exist on 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

 

Peterson’s Solution

 

The two processes share two variables: int turn; boolean flag[2]

The variable turn indicates whose turn it is to enter the critical section

The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready

Provable that the three CS requirement are met:

   1. Mutual exclusion is preserved Pi enters CS only if: either flag[j] = false or turn = i

   2. Progress requirement is satisfied

   3. Bounded-waiting requirement is met

But, in this case

If this happend,

flag = true; 

x = 100;

the operation for Thread 2 may be reordered

 

Synchronization Hardware

 

Memory barriers : the memory guarantees a computer architecture makes to application programs

Hardware instructions : 

   Test-and-Set instruction : Executed atomically

   solution : 

Compare-and-Swap instruction : 

   solution : 

Atomic variables : provides atomic updates on basic data types such as integers and booleans

 

Mutex Locks

 

Protect a critical section by first acquire() a lock then release() the lock

Calls to acquire() and release() must be atomic

 

Semaphore

 

Provides more sophisticated ways (than Mutex locks) for process to synchronize their activities

Semaphore S : integer variable

Can only be accessed via two indivisible (atomic) operations : wait() and signal()

Must guarantee that no two processes can execute the wait() and signal() on the same semaphore at the same time

Semaphore Implementation with no busy waiting : 

Each semaphore there is an associated waiting queue

Each entry in a waiting queue has two data items: value, pointer to next record in the list

Operations:

   block : place the process invoking the operation on the appropriate waiting queue

   wakeup : remove one of processes in the waiting queue and place it in the ready queue

 

Monitors

 

Abstract data type, internal variables only accessible by code within the procedure

Only one process may be active within the monitor at a time

Condition Variables

 

Operations on a condition variable :

x.wait() : a process that invokes the operation is suspended until x.signal()

x.signal() : resumes one of processes (if any) that invoked x.wait()

 

Monitor Implementation - Semaphores

Monitor Implementation – Condition Variables

 

Liveness

 

Liveness refers to a set of properties that a system must satisfy to ensure processes make progress

 

Deadlock : two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes

Starvation : indefinite blocking A process may never be removed from the semaphore queue in which it is suspended

Priority Inversion : scheduling problem when lower-priority process holds a lock needed by higher-priority process

 

Priority Inheritance Protocol : The priority of the highest thread waiting to access a shared resource to be assigned to the thread currently using the resource (The current owner of the resource is assigned the priority of the highest priority thread wishing to acquire the resource)