#include
#include
#include
#include

//Max Number of customers
#define MAX 5
//Max number of resources
#define RESOURCES 3

//available resources
int available[RESOURCES];
//maximum demand of customers
int maximum[MAX][RESOURCES];
//allocation of customer
int allocation[MAX][RESOURCES];
//need of customer
int need[MAX][RESOURCES];

pthread_mutex_t mutex;

//Function to check for safe state
int checkSafeState(int allocated[][RESOURCES], int need[][RESOURCES], int available[])
{
int finish[MAX] = {0};
int work[RESOURCES];
int i, j;

// Initializing the work[] array
for (i = 0; i < RESOURCES; i++)
work[i] = available[i];

int count = 0;
while (count < MAX)
{
int safe = 0;
for (i = 0; i < MAX; i++)
{
if (finish[i] == 0)
{
int flag = 0;
for (j = 0; j < RESOURCES; j++)
{
if (need[i][j] > work[j])
flag = 1;
}
if (flag == 0)
{
for (j = 0; j < RESOURCES; j++)
work[j] += allocated[i][j];
safe = 1;
finish[i] = 1;
count++;
}
}
}
if (safe == 0)
return 0;
}
return 1;
}

//Function to request resources
int request_resource(int customer_id, int request[])
{
int i;

//Acquiring the mutex lock
pthread_mutex_lock(&mutex);

//Check if request can be granted
for (i = 0; i < RESOURCES; i++)
{
if (request[i] > need[customer_id][i])
{
printf("Exceeds maximum claim.\n");
pthread_mutex_unlock(&mutex);
return -1;
}
}

for (i = 0; i < RESOURCES; i++)
{
if (request[i] > available[i])
{
printf("Resources not available.\n");
pthread_mutex_unlock(&mutex);
return -1;
}
}

//Decrement available resources and increment allocated resources
for (i = 0; i < RESOURCES; i++)
{
available[i] -= request[i];
allocation[customer_id][i] += request[i];
need[customer_id][i] -= request[i];
}

//Check for safe state
if (checkSafeState(allocation, need, available))
{
printf("Request granted.\n");
pthread_mutex_unlock(&mutex);
return 0;
}
else
{
printf("Request denied. System is not in safe state.\n");
//Rollback changes
for (i = 0; i < RESOURCES; i++)
{
available[i] += request[i];
allocation[customer_id][i] -= request[i];
need[customer_id][i] += request[i];
}
pthread_mutex_unlock(&mutex);
return -1;
}
}

//Function to release resources
int release_resource(int customer_id, int release[])
{
int i;

//Acquiring the mutex lock
pthread_mutex_lock(&mutex);

//Increment available resources and decrement allocated resources
for (i = 0; i < RESOURCES; i++)
{
available[i] += release[i];
allocation[customer_id][i] -= release[i];
need[customer_id][i] += release[i];
}

printf("Resource released.\n");
pthread_mutex_unlock(&mutex);
return 0;
}

//Function to execute threads
void* thread_function(void* arg)
{
int customer_id = (int)arg;
int request[RESOURCES];
int i;

while (1)
{
printf("Customer %d\n", customer_id);
printf("Available resources:");
for (i = 0; i < RESOURCES; i++)
printf(" %d", available[i]);
printf("\n");

//Generate random request
for (i = 0; i < RESOURCES; i++)
request[i] = rand() % (need[customer_id][i] + 1);

printf("Requesting resources:");
for (i = 0; i < RESOURCES; i++)
printf(" %d", request[i]);
printf("\n");

//Request resources
int ret = request_resource(customer_id, request);
if (ret == 0)
{
//Release resources
release_resource(customer_id, request);
}
sleep(1);
}
}

int main(int argc, char** argv)
{
int i, j;

//Initializing mutex
pthread_mutex_init(&mutex, NULL);

//Taking input for available resources
printf("Enter number of available resources for each type:\n");
for (i = 0; i < RESOURCES; i++)
scanf("%d", &available[i]);

//Taking input for maximum demand of customers
printf("\nEnter maximum demand for each customer for each type:\n");
for (i = 0; i < MAX; i++)
for (j = 0; j < RESOURCES; j++)
scanf("%d", &maximum[i][j]);

//Initializing allocated and need matrix
for (i = 0; i < MAX; i++)
for (j = 0; j < RESOURCES; j++)
{
allocation[i][j] = 0;
need[i][j] = maximum[i][j];
}

//Creating customer threads
pthread_t threads[MAX];
for (i = 0; i < MAX; i++)
pthread_create(&threads[i], NULL, thread_function, (void*)i);

//Joining customer threads
for (i = 0; i < MAX; i++)
pthread_join(threads[i], NULL);

//Destroy the mutex
pthread_mutex_destroy(&mutex);
return 0;
}


Are there any questions left?
Create a Free Account
Level up your programming skills with IQCode
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Pleased to see you again

Sign up to unlock all of IQCode features:
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Looking for an answer to a question you need help with?
you have points