Semaphore for Process Synchornization

July 28, 2022, 12:38 p.m. | Operating Systems

Problem Statement:

A factory has few machines which are used for manufacturing sheet metal products. Operator, Cleaner and mechanic are the 3 users who access those machines. Operator runs the machine to produce sheet metal products; mechanic trouble shoots the machine or performs daily maintenance work on machine. Cleaner cleans the machine and fills lubrication oil on machine.

 Any user is restricted to do their work on the machine when another user doing their work on the same machine. For example Operator should not run the machine while either mechanic trouble shooting the machine or cleaner cleaning the machine.

Questions :

1.      How you synchronize the users of machine?

2.      Design an algorithm for the Synchronization Problem.

Solution:

1.      Semaphore integer variable Machine_free is used to solve the synchronization problem.

2.      If Machine_free value is equal to 1 then Machine is not used by any user, so requested user can access the machine for their work.

3.      If its value is less than 1 then some user working on machine so another requested user should wait.

Class machine_synchornize;

{

 Private int Machine_free;

 Public void machine_synchornize()

 {

  Machine_free = 1;

  }

   Public void wait()

  {

   While(this.machine_free <=0);

    Machine_free--;

   }

   Public void Signal()

   {

    Machine_free++;

    }

    Class user

    {

    Void Dowork()

    {

    Machine_synchornize M1 = new machine_synchornize();

    M1.wait();

    //Access Machine;

    M1.signal();

    }

    }


Comments