• No results found

PROCESS SYNCHRONIZATION TOPICS

In document Operating Systems PART -A (Page 54-57)

Thread Pools

UNIT 3 PROCESS SYNCHRONIZATION TOPICS

3.8 SYNCHRONIZATION

3.9 THE CRITICAL SECTION PROBLEM 3.10 PETERSON’S SOLUTION

3.11 SYNCHRONIZATION HARDWARE 3.12 SEMAPHORES

3.13 CLASSICAL PROBLEMS OF SYNCHRONIZATION 3.14 MONITORS

3.1 SYNCHRONIZATION

Since processes frequently needs to communicate with other processes therefore, there is a need for a well- structured communication, without using interrupts, among processes.

Race Conditions

In operating systems, processes that are working together share some common storage (main memory, file etc.) that each process can read and write. When two or more processes are reading or writing some shared data and the final result depends on who runs precisely when, are called race conditions. Concurrently executing threads that share data need to synchronize their operations and processing in order to avoid race condition on shared data. Only one ‘customer’ thread at a time should be allowed to examine and update the shared variable. Race conditions are also possible in Operating Systems. If the ready queue is implemented as a linked list and if the ready queue is being manipulated during the handling of an interrupt, then interrupts must be disabled to prevent another interrupt before the first one completes. If interrupts are not disabled than the linked list could become corrupt.

1. count++ could be implemented as

register1 = count register1 = register1 + 1 count = register1 2. count--could be implemented as register2 = count register2 = register2 – 1 count = register2

3. Consider this execution interleaving with “count = 5” initially:

S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 -1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

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

2. 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

3. 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

• Assume that each process executes at a nonzero speed • No assumption concerning relative speed of the N processes

A. Critical Section

The key to preventing trouble involving shared storage is find some way to prohibit more than one process from reading and writing the shared data simultaneously. That part of the program where the shared memory is accessed is called the Critical Section. To avoid race conditions and flawed results, one must identify codes in Critical Sections in each thread. The characteristic properties of the code that form a Critical Section are

x Codes that reference one or more variables in a “read-update-write” fashion while any of those variables is

possibly being altered by another thread. x Codes that alter one or more variables that are possibly being referenced in “read-updata-write”

fashion by another thread. x Codes use a data structure while any part of it is possibly being altered by another thread. x Codes alter any part of a data structure while it is possibly in use by another thread.

Here, the important point is that when one process is executing shared modifiable data in its critical section, no other process is to be allowed to execute in its critical section. Thus, the execution of critical sections by the processes is mutually exclusive in time.

B. Mutual Exclusion

A way of making sure that if one process is using a shared modifiable data, the other processes will be excluded from doing the same thing. Formally, while one process executes the shared variable, all other processes desiring to do so at the same time moment should be kept waiting; when that process has finished executing the shared variable, one of the processes waiting; while that process has finished executing the shared variable, one of the processes waiting to do so should be allowed to proceed. In this fashion, each

process executing the shared data (variables) excludes all others from doing so simultaneously. This is called Mutual Exclusion.

Note that mutual exclusion needs to be enforced only when processes access shared modifiable data when processes are performing operations that do not conflict with one another they should be allowed to proceed concurrently.

Mutual Exclusion Conditions

If we could arrange matters such that no two processes were ever in their critical sections simultaneously, we could avoid race conditions. We need four conditions to hold to have a good solution for the critical section problem (mutual exclusion).

x No two processes may at the same moment inside their critical sections.

x No assumptions are made about relative speeds of processes or number of CPUs.

x No process should outside its critical section should block other processes.

x No process should wait arbitrary long to enter its critical section.

3.3 PETERSON’S SOLUTION

The mutual exclusion problem is to devise a pre-protocol (or entry protocol) and a post-protocol (or exist protocol) to keep two or more threads from being in their critical sections at the same time. Tanenbaum examine proposals for critical-section problem or mutual exclusion problem.

Problem

When one process is updating shared modifiable data in its critical section, no other process should allowed to enter in its critical section.

In document Operating Systems PART -A (Page 54-57)