Sunday, July 17, 2011

Program For Dining Philosophers Problem

class dinningPphilosopher
 { 
   public static void main(String a[])
    {
     int Semaphore=0,i,j=1;
     String philo[]={"Gandhi","Socrates","Tagore","Einstein","Newton"};
     int fork[]=new int[5];
     for(i=0;i<5;i++)
      {
       if(Semaphore==0)
        { 
         if(i%2==0)
         { 
          System.out.println("*****************************************");
          System.out.println("Batch "+j+" has started");
         }
         System.out.println(philo[i]+" is dinning currently");
         if(i%2!=0&&i!=0)
          {
           Semaphore=1;
          }
        }
        if(Semaphore==1)
        {
         System.out.println("Sorry you will have to wait till batch "+j+" finishes          dinning");
         System.out.println("Batch "+j+" has finished dining");
         Semaphore=0;
         j+=1;
        }
      }
      System.out.println("All the philosophers have finished dinning");
    }
 }

No comments:

Post a Comment