Page 132 - DCAP103_Principle of operating system
P. 132

Unit 4: Process Management-III



            Operations P and V are done as single, indivisible, atomic action. It is guaranteed that once a   Notes
            semaphore operations has stared, no other process can access the semaphore until operation has
            completed. Mutual exclusion on the semaphore, S, is enforced within P(S) and V(S).
            If several processes attempt a P(S) simultaneously, only process will be allowed to proceed.
            The other processes will be kept waiting, but the implementation of P and V guarantees that
            processes will not suffer indefinite postponement. Semaphores solve the lost-wakeup problem.
            4.5.1 Producer-Consumer Problem Using Semaphores

            The solution to producer-consumer problem uses three semaphores, namely, full, empty and
            mutex.
            Initialization
               •  Set full buffer slots to 0.
                 i.e., semaphore full = 0.
               •  Set empty buffer slots to N.
                 i.e., semaphore empty = N.
               •  For control access to critical section set mutex to 1.
                 i.e., semaphore mutex = 1.

            Producer ( )
            WHILE (true)

            produce-Item ( );
            P (empty);
            P (mutex);
            enter-Item ( )
            V (mutex)
            V (full);
            Consumer ( )
            WHILE (true)
            P (full)
            P (mutex);
            remove-Item ( );
            V (mutex);
            V (empty);
            consume-Item (Item)
                          The semaphore ‘full’ is used for counting the number of slots in the buffer
                          that are full. The ‘empty’ for counting the number of slots that are empty
                          and semaphore ‘mutex’ to make sure that the producer and consumer do not
                          access modifiable shared section of the buffer simultaneously.

            4.6 Deadlock Concept and Handling

            In a multiprogramming environment, several processes may compete for a finite number of
            resources. A process requests resources; if the resources are not available at that time, the process
            enters  a  wait  state.  Waiting  processes  may  never  again  change  state,  because  the  resources
            they have requested are held by other waiting processes. This situation is called a deadlock.
            Perhaps the best illustration of a deadlock  can be drawn from  a law passed  by the Kansas
            legislature early in the 20th century. It said, in part—“When two trains approach each other at a
            crossing, both shall come to a full stop and neither shall start up again until the other has gone.”


                                             LOVELY PROFESSIONAL UNIVERSITY                                   125
   127   128   129   130   131   132   133   134   135   136   137