You are on page 1of 16

1.

Bankers Algorithm
I) Add the following functionalities in your program
a) Accept Available
b) Display Allocation, Max
c) Display the contents of need matrix
d) Display Available

// Banker's Algorithm
#include <stdio.h>
int main()
{
int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 },
{ 2, 0, 0 },
{ 3, 0, 2 },
{ 2, 1, 1 },
{ 0, 0, 2 } };

int max[5][3] = { { 7, 5, 3 },
{ 3, 2, 2 },
{ 9, 0, 2 },
{ 2, 2, 2 },
{ 4, 3, 3 } };

int avail[3] = { 3, 3, 2 };

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 < 5; 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;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
int flag = 1;
for(int i=0;i<n;i++)
{
if(f[i]==0)
{
flag=0;
printf("The following system is not safe");
break;
}
}

if(flag==1)
{
printf("Following is the SAFE Sequence\n");
for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);
}
return (0);
}

II)
Modify above program so as to include the following:
a) Accept Request for a process
b) Resource request algorithm
c) Safety algorithm Consider a system with ‘n’ processes and ‘m’ resource types.
Accept number of instances for every resource type. For each process accept the allocation and maximum
requirement matrices. Write a program to display the contents of need matrix and to check if the given
request of a process can be granted immediately or not.

#include<stdio.h>
#include<conio.h>

int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;

void input();

void show();

void cal();

int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
request();
getch();
return 0;
}

void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}

}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}

void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\t");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}

void cal() {
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}

}
if (c1 == n) {
printf("\n The system is in safe state");
} else {
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}

void request() {
int c, pid, request[100][100], B[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n *****Error encountered*****\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
getch();
} else {
exit(0);
}
}

2. File Allocation Methods


Slot-1
Write a program to simulate Sequential (Contiguous) file allocation method. Assume disk with n number
of blocks. Give value of n as input. Write menu driver program with menu options as mentioned above
and implement each option.

#include<stdio.h>
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}

Slot- II
Write a program to simulate Linked file allocation method. Assume disk with n number of blocks. Give
value of n as input. Write menu driver program with menu options.

#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}
Slot- III
Write a program to simulate Indexed file allocation method. Assume disk with n number of blocks. Give
value of n as input. Write menu driver program with menu options.

#include<stdio.h>
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block already allocated\n");
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
}
3. Disk Scheduling Algorithms

Slot-I
i) Write an OS program to implement FCFS Disk Scheduling algorithm.

include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);

// logic for FCFS disk scheduling

for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}

printf("Total head moment is %d",TotalHeadMoment);


return 0;

ii) Write an OS program to implement SSTF algorithm Disk Scheduling algorithm.

include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial,count=0;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);

// logic for sstf disk scheduling

/* loop will execute until all process is completed*/


while(count!=n)
{
int min=1000,d,index;
for(i=0;i<n;i++)
{
d=abs(RQ[i]-initial);
if(min>d)
{
min=d;
index=i;
}

}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}

printf("Total head movement is %d",TotalHeadMoment);


return 0;
}

Output:-

Enter the number of Request


8
Enter Request Sequence
95 180 34 119 11 123 62 64
Enter initial head Position
50
Total head movement is 236

Slot-II
i. Write an OS program to implement SCAN Disk Scheduling algorithm

#include <stdio.h>

#include <math.h>

int main()

int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],


queue2[20], temp1 = 0, temp2 = 0;

float avg;

printf("Enter the max range of disk\n");

scanf("%d", &max);

printf("Enter the initial head position\n");


scanf("%d", &head);

printf("Enter the size of queue request\n");

scanf("%d", &n);

printf("Enter the queue of disk positions to be read\n");

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

scanf("%d", &temp);

if (temp >= head)

queue1[temp1] = temp;

temp1++;
}

else

queue2[temp2] = temp;

temp2++;
}
}

for (i = 0; i < temp1 - 1; i++)

for (j = i + 1; j < temp1; j++)

if (queue1[i] > queue1[j])

temp = queue1[i];

queue1[i] = queue1[j];

queue1[j] = temp;
}
}
}
for (i = 0; i < temp2 - 1; i++)

for (j = i + 1; j < temp2; j++)

if (queue2[i] < queue2[j])

temp = queue2[i];

queue2[i] = queue2[j];

queue2[j] = temp;
}
}
}

for (i = 1, j = 0; j < temp1; i++, j++)

queue[i] = queue1[j];

queue[i] = max;

for (i = temp1 + 2, j = 0; j < temp2; i++, j++)

queue[i] = queue2[j];

queue[i] = 0;

queue[0] = head;

for (j = 0; j <= n + 1; j++)

diff = abs(queue[j + 1] - queue[j]);

seek += diff;

printf("Disk head moves from %d to %d with seek %d\n", queue[j],


queue[j + 1], diff);
}

printf("Total seek time is %d\n", seek);

avg = seek / (float)n;

printf("Average seek time is %f\n", avg);

return 0;
}
ii. Write an OS program to implement C-SCAN algorithm Disk Scheduling algorithm.

include <stdio.h>

#include <math.h>

int main()

int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],


queue2[20], temp1 = 0, temp2 = 0;

float avg;

printf("Enter the max range of disk\n");

scanf("%d", &max);

printf("Enter the initial head position\n");

scanf("%d", &head);

printf("Enter the size of queue request\n");

scanf("%d", &n);

printf("Enter the queue of disk positions to be read\n");

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

scanf("%d", &temp);

if (temp >= head)

queue1[temp1] = temp;

temp1++;
}

else

queue2[temp2] = temp;

temp2++;
}
}
for (i = 0; i < temp1 - 1; i++)

for (j = i + 1; j < temp1; j++)

if (queue1[i] > queue1[j])

temp = queue1[i];

queue1[i] = queue1[j];

queue1[j] = temp;
}
}
}

for (i = 0; i < temp2 - 1; i++)

for (j = i + 1; j < temp2; j++)

if (queue2[i] > queue2[j])

temp = queue2[i];

queue2[i] = queue2[j];

queue2[j] = temp;
}
}
}

for (i = 1, j = 0; j < temp1; i++, j++)

queue[i] = queue1[j];

queue[i] = max;

queue[i + 1] = 0;

for (i = temp1 + 3, j = 0; j < temp2; i++, j++)

queue[i] = queue2[j];

queue[0] = head;
for (j = 0; j <= n + 1; j++)

diff = abs(queue[j + 1] - queue[j]);

seek += diff;

printf("Disk head moves from %d to %d with seek %d\n", queue[j],


queue[j + 1], diff);
}

printf("Total seek time is %d\n", seek);

avg = seek / (float)n;

printf("Average seek time is %f\n", avg);

return 0;
}

4. Distributed and mobile OS


Slot-I
Write an MPI program to calculates sum of randomly generated 1000 numbers (stored in array) on a
cluster.

#include <cstdio>
#include <cstdlib>
#include <mpi.h>

static int rank, nodes;

int main()
{
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nodes);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status status;

int ans = 0;
int total = 0;

int start = rank * 1000;


int end = start + 999;

for(int i = start; i <= end; i++) {


ans = ans + i;
}

if(rank != 0) {
MPI_Ssend(&ans, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
} else {
total = ans;
for(int j = 1; j < 10; j++) {
MPI_Recv(&ans, 1, MPI_INT, j, 0, MPI_COMM_WORLD, &status);
total += ans;
}
printf("Total is %d\n", total);
printf("Total Nodes is %d\n", nodes);
}

MPI_Finalize();
return 0;
}

Slot-II
Write an MPI program to find the min and max number from randomly generated 1000 numbers (stored
in array) on a cluster (Hint: Use MPI_Reduce).

#include <stdio.h>
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int rows, cols, min, value, n;
int done = 0, numprocs, rank, i, j;
srand(time(NULL));
double startwtime = 0.0, endwtime;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(processor_name,&namelen);
while (!done) {
if (rank == 0) {
printf("Enter the height/width of the matrix\n");
scanf("%d",&rows);
scanf("%d",&cols);
startwtime = MPI_Wtime();
}
if (rows == 0 || cols == 0) {
done = 1;
} else {
int *arr = (int **) malloc(rows * sizeof(int)); //creating 2d array
for (i = 0; i < rows; i++) {
arr[i] = (int *) malloc(cols * sizeof(int));
}
for (i = 0; i < rows; i++) { //Array filling
for (j = 0; j < cols; j++) {
arr[i][j] = rand();
}
}
for (i = 0; i < rows; i++) { // output of the array to the screen for clarity
for (j = 0; j < cols; j++) {
printf("%d\n",arr[i][j]);
}
}
min = arr[0][0];
for (i = 0; i < rows; i++) { //find min value
for (j = 0; j < cols; j++) { //
if(min > arr[i][j]){min = arr[i][j];} //need to parallize
}
}
if (rank == 0) {
endwtime = MPI_Wtime();
printf("min = %d\n", min);
printf("wall clock time = %f\n", endwtime-startwtime);
fflush( stdout );
for(i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
}
}
}
MPI_Finalize();
return 0;
}

You might also like