Sunday, 20 October 2013

Synchronization(using Semaphores)-Producer Consumer

/*......
Before moving to the algorithm...I am writing the structutre of the program
we have created two threads p and c

pthread_create(&c,NULL,&consume,NULL); 
pthread_create(&p,NULL,&produce,NULL);
first argument is the address of the variable holding the address of the thread
second argument is priority of the thread which we have set to 0
third argument is the function name from where thread starts its execution
fourth argument is the parameter passed to the function...here we have no parameter hence we have written as NULL

NOW thread c will take handling consumer process and thread p will handle producer process


Consumer-Producer Problem
Here we have taken the bounded buffer of size 5
Three semaphores have been taken-:

mutex-Initial value is taken as 1,this has been used to implement lock ie. while producer is in 
making changes to buffer consumer cannot do and vice-versa

empty-Initial value is size of the buffer since in the beginning buffer will be empty hence number of empty slots will also be equal to buffer

 full-Initial value is 0 since at the beginning number of the filled slots are 0 

...........*/


#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
pthread_t p;
pthread_t c;
sem_t mutex,full,empty;
int bufferSize=5;
int buffer[5];
int nextProduce,nextConsume;
int in,out;

void *produce(void *p)
{

int i=0;
while(i<=10)
{
sem_wait(&empty);//if buffer is full number of empty slots will be 0 hence empty=0 and thread has to wait here untill consumer consumes some of the product
sem_wait(&mutex);//it locks the consumer function so that while p is in its critical section c cannot move to its critical section
nextProduce=(int)rand();
buffer[in]=nextProduce;
printf("\nProduced %d at %d",nextProduce,in);
in=((in+1)%bufferSize);
sem_post(&mutex);//releasing the lock
sem_post(&full);//increasing the value of the full since number of filled slots will increase
i++;
}//end of while
}//end of produce

void *consume(void *p)
{int i=0;
while(i<=10)
{

sem_wait(&full);
sem_wait(&mutex);
nextConsume=buffer[out];
printf("\nConsume %d at %d",nextConsume,out);
out=((out+1)%bufferSize);
sem_post(&mutex);
sem_post(&empty);
i++;
}//end of while
}//end of consume

int main()
{
sem_init(&mutex,0,1);
sem_init(&full,0,0);
sem_init(&empty,0,bufferSize);
pthread_create(&c,NULL,&consume,NULL);
pthread_create(&p,NULL,&produce,NULL);
pthread_join(p,NULL);
pthread_join(c,NULL);
}


For compilation gcc -pthread xyz.c


Monday, 14 October 2013

Bankers Algorithm(Deadlock Avoidance)

Bankers algorithm is Deadlock avoidance algorithm which capable of dealing with more than two or n process resource allocation.Everytime there is demand by the process for allocation algorithm is invoked by OS.
DATA STRUCTURES USED-:
Available-Vector to store resources available with the system
Max[i][j]-Max demand made by the ith process for jth resource
Allocated[i][j]-Number of instances of jth resources has been allocated to ith process
Need[][]-Max-Allocated

ALGO-:

  • The process works on lines of bank management.Whenever there is the demand of resources allocation which cannot be completed by OS, process is asked to wait until some finished process release its resources.
  • Released resources are added to Available list
  • This Available list is compared with Need list of next process
  • If Need is greater than Available, process is put on hold and same condition is checked for next process
  • If Need is less than or Equal to Available then the Allocated resources are added to Available(since if condition is true process can successfully finish itself and can release its resources)
  • All above process are repeated until all process have been completed or there is no process left for which Need <=Available. In the latter case system is not in SAFE state and in former safe sequence can be generated


#include<stdio.h>

#include<conio.h>
#include<stdlib.h>

int available[1][5];
int max[5][5];
int allocated[5][5];
int need[5][5];
int p[5]={0};
int n=5;//number of process
int m=3;
void input()
{
     int i,j;
     puts("Enter the Allocation Matrix");
     for(i=0;i<n;i++)
     {
        printf("P%d ",i);          
        for(j=0;j<m;j++)
        {
        scanf("%d",&allocated[i][j]);
     }//inner for
     printf("\n");
 }//outer for


 puts("Enter the MAX Matrix");
     for(i=0;i<n;i++)
     {
        printf("P%d ",i);          
        for(j=0;j<m;j++)
        {
        scanf("%d",&max[i][j]);
     }//inner for
     printf("\n");
 }//outer for

 puts("Enter the Available Matrix");
     for(i=0;i<m;i++)
     {
       scanf("%d",&available[0][i]);            
}


}//function end
void calc_need()
{
      int i,j;
      puts("NEED matrix");
     for(i=0;i<n;i++)
     {
             
        for(j=0;j<m;j++)
        {
        need[i][j]=max[i][j]-allocated[i][j];
         printf("%d ",need[i][j]);  
         }//inner for
     printf("\n");
     }//outer for
   
 }//end of calc_need
 int less_than_or_equal(int i)//to check if for process i need <=Available
 {
      int j;
      for(j=0;j<m;j++)
      {
                      if(need[i][j]<=available[0][j])
                      continue;
                      else
                      return 0;
      }//end of for
                      return 1;
   
  }
 void  increase_available(i)
  {
                     
      int j;
      for(j=0;j<m;j++)
      {
      available[0][j]=available[0][j]+allocated[i][j];  
                      }
}

void check()
{
     int j;
      for(j=0;j<n;j++)
      {
                      printf("%d",p[j]);
 }
}
main()
{
   
      int i;
      int f=0;
      input();//to take inputs for Allocated Max and Available
      calc_need();
      while(1)
      {
             if(f==n)
             break;
             f=0;
             for(i=0;i<n;i++)
             {
                           
                           
                              if((p[i]==0)&&(less_than_or_equal(i)))
                              {
                              increase_available(i);
                              p[i]=1;
                              printf("P%d ",i);
                              continue;
                              }
                              else
                              {
                                  f=f+1;
                                  continue;
                              }
           
             }//end of for
   
      }//end of while
      getch();
      check();
      getch();
}//end of main


OUTPUT
Safe Sequence
<P1,P3,P4,P0,P2>