Os Asst 3
Os Asst 3
Assessment 3
1. Safety Check: Determine if the system is currently in a safe state. Show the sequence of
processes and work vectors used to verify this.
2. Resource Request: Suppose process (P1) makes a request for resources: [1, 0, 1, 0]. Check if this
request can be granted by:
- Verifying if the request is less than or equal to the process's remaining need.
- Simulating the allocation and then performing a safety check to ensure the system remains in a
safe state
(a) Write a C program to check whether the system is in safe state or not using Banker’s algorithm?
Also generate the safe sequence state.
Code:
a. Safety Check:
#include <stdio.h>
int main()
{
int n, m, i, j, k;
printf("Vishwakanth G\n22BCE3311\n\n");
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resources: ");
scanf("%d", &m);
{
printf("Process %d: ", i);
for (j = 0; j < m; j++)
{
scanf("%d", &alloc[i][j]);
}
}
int need[n][m];
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
need[i][j] = max[i][j] - alloc[i][j];
}
}
int y = 0;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
3
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
for (y = 0; y < m; y++)
{
avail[y] += alloc[i][y];
}
f[i] = 1;
}
}
}
}
int flag = 1;
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
flag = 0;
printf("\nThe system is not in safe state.\n");
break;
}
}
if (flag == 1)
{
printf("\nThe system is in safe state.\n");
printf("Sequence: ");
for (i = 0; i < n - 1; i++)
{
printf("P%d -> ", ans[i]);
}
printf("P%d\n", ans[n - 1]);
}
return 0;
}
4
Output:
Code:
b. Resource Request:
#include <stdio.h>
int main()
{
int n, m, i, j, k, process;
printf("Vishwakanth G\n22BCE3311\n\n");
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resources: ");
scanf("%d", &m);
printf("Enter the process number (0 to %d) making the request: ", n - 1);
scanf("%d", &process);
int request[m];
printf("Enter the resource request vector of size %d for process P%d: ",
m, process);
for (i = 0; i < m; i++)
{
scanf("%d", &request[i]);
}
int can_grant = 1;
for (i = 0; i < m; i++)
{
if (request[i] > need[process][i])
{
can_grant = 0;
printf("Request cannot be granted. It exceeds the process's
maximum need.\n");
6
return 0;
}
}
int y = 0;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
int flag = 0;
for (j = 0; j < m; j++)
{
if (need[i][j] > avail[j])
{
flag = 1;
break;
}
}
if (flag == 0)
{
ans[ind++] = i;
7
for (y = 0; y < m; y++)
{
avail[y] += alloc[i][y];
}
f[i] = 1;
}
}
}
}
int safe = 1;
for (i = 0; i < n; i++)
{
if (f[i] == 0)
{
safe = 0;
printf("\nRequest cannot be granted. The system would not be in a
safe state.\n");
break;
}
}
if (safe)
{
printf("\nRequest can be granted. The system remains in a safe
state.\n");
printf("Safe Sequence: ");
for (i = 0; i < n - 1; i++)
{
printf("P%d -> ", ans[i]);
}
printf("P%d\n", ans[n - 1]);
}
return 0;
}
8
Output:
9
Manual Calculation:
10
11
2. Write a C program and execute the implementation of classical problem of process
synchronization using semaphores / monitors.
• Bounded-Buffer Problem
• Reader-Writer Problem
• Dining philosopher’s problem
Code:
#include <stdio.h>
#include <stdlib.h>
void producer()
{
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces item %d", x);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item %d", x);
x--;
++mutex;
}
int main()
{
int n, i;
printf("Vishwakanth G\n22BCE3311\n\n");
printf("1. Press 1 for Producer\n2. Press 2 for Consumer\n3. Press 3 for
Exit");
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
{
producer();
}
else
{
printf("Buffer is full");
}
break;
case 2:
if ((mutex == 1) && (full != 0))
{
consumer();
}
else
{
printf("Buffer is empty");
}
break;
case 3:
exit(0);
break;
}
}
}
13
Output:
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
struct monitor
{
int rcnt;
int wcnt;
int waitr;
int waitw;
pthread_cond_t canread;
14
pthread_cond_t canwrite;
pthread_mutex_t condlock;
} M;
void beginread(int i)
{
pthread_mutex_lock(&M.condlock);
if (M.wcnt == 1 || M.waitw > 0)
{
M.waitr++;
pthread_cond_wait(&M.canread, &M.condlock);
M.waitr--;
}
M.rcnt++;
printf("Reader %d is reading\n", i);
pthread_mutex_unlock(&M.condlock);
pthread_cond_broadcast(&M.canread);
}
void endread(int i)
{
pthread_mutex_lock(&M.condlock);
if (--M.rcnt == 0)
pthread_cond_signal(&M.canwrite);
pthread_mutex_unlock(&M.condlock);
}
void beginwrite(int i)
{
pthread_mutex_lock(&M.condlock);
if (M.wcnt == 1 || M.rcnt > 0)
{
M.waitw++;
pthread_cond_wait(&M.canwrite, &M.condlock);
M.waitw--;
}
M.wcnt = 1;
15
void endwrite(int i)
{
pthread_mutex_lock(&M.condlock);
M.wcnt = 0;
if (M.waitr > 0)
pthread_cond_signal(&M.canread);
else
pthread_cond_signal(&M.canwrite);
pthread_mutex_unlock(&M.condlock);
}
int main()
{
printf("Vishwakanth G\n22BCE3311\n\n");
pthread_t r[5], w[5];
int id[5];
16
monitor_init(&M);
for (int i = 0; i < 5; i++)
{
id[i] = i;
pthread_create(&r[i], NULL, reader, &id[i]);
pthread_create(&w[i], NULL, writer, &id[i]);
}
return 0;
}
Output:
17
c. Dining Philosopher Problem
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
pthread_mutex_t forks[NUM_PHILOSOPHERS];
pthread_mutex_lock(&forks[left_fork]);
printf("Philosopher %d picked up left fork %d.\n", philosopher_id,
left_fork);
pthread_mutex_lock(&forks[right_fork]);
printf("Philosopher %d picked up right fork %d and is eating.\n",
philosopher_id, right_fork);
sleep(1);
pthread_mutex_unlock(&forks[right_fork]);
pthread_mutex_unlock(&forks[left_fork]);
}
return NULL;
}
int main()
{
18
printf("Vishwakanth G\n22BCE3311\n\n");
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
return 0;
}
Output:
19