Beispiel #1
0
/**
 * Attempts to allocate a request vector to a process. 
 *
 * @param process number
 * @param request vector 
 * @return true if successfully allocated
 */
int allocate(int process, int *Request) {
	 
	for(i=0; i<resources; i++) {
		pthread_mutex_lock( &mutexAllocation );
		Allocation[process][i] += Request[i]; 
		pthread_mutex_unlock( &mutexAllocation );
		
		pthread_mutex_lock( &mutexAvailable );
		Available[i] -= Request[i];
		pthread_mutex_unlock( &mutexAvailable );
	} 
	
	if( bankers() == FALSE) {
		for(i=0; i<resources; i++) { 
			pthread_mutex_lock( &mutexAllocation );
			Allocation[process][i] -= Request[i]; 
			pthread_mutex_unlock( &mutexAllocation );
			
			pthread_mutex_lock( &mutexAvailable );
			Available[i] += Request[i]; 
			pthread_mutex_unlock( &mutexAvailable );
		} 
		return FALSE; 
	} 
	return TRUE; 
} 
Beispiel #2
0
int main() {

	for(i=0;i<np;i++){
		for(j=0;j<nr;j++){
			max[i][j]=0;
			alloc[i][j]=0;
			need[i][j]=0;
		}
		av[i]=0;
	
	}
	
	printf("Enter the number of processess\n");
	scanf("%d",&np);
	
	printf("Enter the number of resources\n");
	scanf("%d",&nr);
	
	printf("Enter the max resources needed\n");
	for(i=0;i<np;i++)
		for(j=0;j<nr;j++)
			scanf("%d",&max[i][j]);
	 
	printf("Enter the allocated resources\n");
        for(i=0;i<np;i++)
		for(j=0;j<nr;j++){
			scanf("%d",&alloc[i][j]);
			need[i][j]=max[i][j]-alloc[i][j];
		}
		
	 printf("The need matrix is \n");		
	 for(i=0;i<np;i++){
	 	printf("\n");
		for(j=0;j<nr;j++){
			printf("%d ",need[i][j]);
		}
	}
	 printf("\n\nEnter the available resources\n");
	 for(i=0;i<nr;i++)
	 	scanf("%d",&av[i]);
	 
	 bankers();
	 
	 return -1;
}
main()
{

   int i,j,instanc,k=0,count1=0,count2=0;
   printf("\n\t Enter No. of Process(max 15):-\n");
   printf("\t\t");
   scanf("%d",&process);
//Entering No. of Processes
   for(i=0;i<process;i++)
   {
       p[i]=i;
   }

   printf("\n\tEnter No. of Resources(max 10):-\n");
   printf("\t\t");
   scanf("%d",&resource);
//No. of Resources
   for(i=0;i<process;i++)
   completed[i]=0;
//Setting Flag for uncompleted Process
   printf("\n\tEnter No. of Available Instances\n");
   for(i=0;i<resource;i++)
   {
     printf("\t\t");
     scanf("%d",&instanc);
     avail[i]=instanc;                        // Storing Available instances
   }

   printf("\n\tEnter Maximum No. of instances of resources that a Process need:\n");
   for(i=0;i<process;i++)
   {
        printf("\n\t For P[%d]\n",i);
        for(j=0;j<resource;j++)
        {
            printf("\t");
            scanf("%d",&instanc);
            fflush(stdin);
            max[i][j]=instanc;
        }
   }

 //Entering burst time for all processes
   for(i=0;i<process;i++)
   {
        printf("Enter the burst time of %d process:\t",i+1);
        scanf("%d",&ser[i]);
        arrv[i] =0;
   }

   printf("\n\t Enter no. of instances already allocated to process of a resource:\n");
   for(i=0;i<process;i++)
   {
        printf("\n\t For P[%d]\t\n",i);
        for(j=0;j<resource;j++)
        {
            printf("\t\t");
            scanf("%d",&instanc);
            allot[i][j]=instanc;
            need[i][j]=max[i][j]-allot[i][j];       //calculating Need of each process
        }
   }

    int t;
    for(t=0;t<3;t++)
    {
        int var1, var2;

        var1= 1 + rand() / (RAND_MAX / (process - 1 + 1) + 1);
        var2 = 1 + rand() / (RAND_MAX / (process - 1 + 1) + 1);

        swapRow(var1, var2,max);
        swapRow(var1,var2,allot);
        swapRow(var1,var2,need);
        swapCol(var1,var2,completed);
        swapCol(var1,var2,p);
        swapCol(var1,var2,ser);
        bankers(t);
        //setting complete array to 0
        for(i=0;i<process;i++)
            completed[i]=0;
    }
}