0% found this document useful (0 votes)
13 views19 pages

Os Asst 3

Uploaded by

Nithura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

Os Asst 3

Uploaded by

Nithura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Vellore Institute of Technology

School of Computer Science and Engineering


(SCOPE)

Assessment 3

Registration Number : 22BCE3311


Name : Vishwakanth G
Course Name : Operating Systems Lab
Course Code : BCSE303P
Slot : L39 + L40
Faculty Name : Dr. Mohankumar B
Faculty Emp. ID : 20159
1
1. Consider a system with 5 processes (P0, P1, P2, P3, P4) and 4 resource types (A, B, C, D). The
Allocation, Max, and Available matrices are given as follows:

Using the Banker's Algorithm, answer the following questions:

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.

- Verifying if the request is less than or equal to the available resources.

- 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.

(b) Compare the theoretical values with simulated results.

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);

int alloc[n][m], max[n][m], avail[m];

printf("Enter the allocation matrix:\n");


for (i = 0; i < n; i++)
2

{
printf("Process %d: ", i);
for (j = 0; j < m; j++)
{
scanf("%d", &alloc[i][j]);
}
}

printf("Enter the max matrix:\n");


for (i = 0; i < n; i++)
{
printf("Process %d: ", i);
for (j = 0; j < m; j++)
{
scanf("%d", &max[i][j]);
}
}

printf("Enter the available resources vector of size %d: ", m);


for (i = 0; i < m; i++)
{
scanf("%d", &avail[i]);
}

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++)
{
f[k] = 0;
}

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);

int alloc[n][m], max[n][m], avail[m], need[n][m];

printf("Enter the allocation matrix:\n");


for (i = 0; i < n; i++)
{
printf("Process %d: ", i);
5

for (j = 0; j < m; j++)


{
scanf("%d", &alloc[i][j]);
}
}

printf("Enter the max matrix:\n");


for (i = 0; i < n; i++)
{
printf("Process %d: ", i);
for (j = 0; j < m; j++)
{
scanf("%d", &max[i][j]);
}
}

printf("Enter the available resources vector of size %d: ", m);


for (i = 0; i < m; i++)
{
scanf("%d", &avail[i]);
}

for (i = 0; i < n; i++)


{
for (j = 0; j < m; j++)
{
need[i][j] = max[i][j] - alloc[i][j];
}
}

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;
}
}

for (i = 0; i < m; i++)


{
if (request[i] > avail[i])
{
can_grant = 0;
printf("\nRequest cannot be granted. Not enough resources
available.\n");
return 0;
}
}

for (i = 0; i < m; i++)


{
avail[i] -= request[i];
alloc[process][i] += request[i];
need[process][i] -= request[i];
}

int f[n], ans[n], ind = 0;


for (k = 0; k < n; k++)
{
f[k] = 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

a. Bounded Buffer Problem

Code:

#include <stdio.h>
#include <stdlib.h>

int mutex = 1, full = 0, empty = 10, x = 0;

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");

for (i = 1; i > 0; i++)


{

printf("\n\nEnter your choice:");


scanf("%d", &n);
12

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:

b. Reader Writer Program

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 monitor_init(struct monitor *m)


{
m->rcnt = 0;
m->wcnt = 0;
m->waitr = 0;
m->waitw = 0;
pthread_cond_init(&m->canread, NULL);
pthread_cond_init(&m->canwrite, NULL);
pthread_mutex_init(&m->condlock, NULL);
}

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

printf("Writer %d is writing\n", i);


pthread_mutex_unlock(&M.condlock);
}

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);
}

void *reader(void *id)


{
int c = 0;
int i = *(int *)id;
while (c < 5)
{
usleep(1);
beginread(i);
endread(i);
c++;
}
return NULL;
}

void *writer(void *id)


{
int c = 0;
int i = *(int *)id;
while (c < 5)
{
usleep(1);
beginwrite(i);
endwrite(i);
c++;
}
return NULL;
}

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]);
}

for (int i = 0; i < 5; i++)


{
pthread_join(r[i], NULL);
}

for (int i = 0; i < 5; i++)


{
pthread_join(w[i], NULL);
}

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];

void *dine(void *arg)


{
int philosopher_id = *(int *)arg;

for (int i = 0; i < 5; i++)


{
printf("Philosopher %d is thinking.\n", philosopher_id);
sleep(1);

int left_fork = philosopher_id;


int right_fork = (philosopher_id + 1) % 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);

printf("Philosopher %d has finished eating and put down forks %d and


%d.\n", philosopher_id, left_fork, right_fork);

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];

for (int i = 0; i < NUM_PHILOSOPHERS; i++)


{
pthread_mutex_init(&forks[i], NULL);
philosopher_ids[i] = i;
}

for (int i = 0; i < NUM_PHILOSOPHERS; i++)


{
pthread_create(&philosophers[i], NULL, dine, &philosopher_ids[i]);
}

for (int i = 0; i < NUM_PHILOSOPHERS; i++)


{
pthread_join(philosophers[i], NULL);
}

for (int i = 0; i < NUM_PHILOSOPHERS; i++)


{
pthread_mutex_destroy(&forks[i]);
}

return 0;
}

Output:
19

You might also like