WHAT IS PROCESS SYNCRONIZATION. Process Synchronization is a way to coordinate processes that use shared data Cooperating processes are processes that share resources. E xecuting 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 PROCESSA . . . . . . 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 PRODUCER CONSUMER
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 S oftware-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);
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 Waiting Key L ock 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 Waiting Key L ock F/T 1 2 3 4
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);