OS Sem-II Slip Solutions
OS Sem-II Slip Solutions
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 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);
}
}
#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);
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
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);
}
TotalHeadMoment=TotalHeadMoment+min;
initial=RQ[index];
// 1000 is for max
// you can use any number
RQ[index]=1000;
count++;
}
Output:-
Slot-II
i. Write an OS program to implement SCAN Disk Scheduling algorithm
#include <stdio.h>
#include <math.h>
int main()
float avg;
scanf("%d", &max);
scanf("%d", &n);
scanf("%d", &temp);
queue1[temp1] = temp;
temp1++;
}
else
queue2[temp2] = temp;
temp2++;
}
}
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
for (i = 0; i < temp2 - 1; i++)
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
queue[i] = queue1[j];
queue[i] = max;
queue[i] = queue2[j];
queue[i] = 0;
queue[0] = head;
seek += diff;
return 0;
}
ii. Write an OS program to implement C-SCAN algorithm Disk Scheduling algorithm.
include <stdio.h>
#include <math.h>
int main()
float avg;
scanf("%d", &max);
scanf("%d", &head);
scanf("%d", &n);
scanf("%d", &temp);
queue1[temp1] = temp;
temp1++;
}
else
queue2[temp2] = temp;
temp2++;
}
}
for (i = 0; i < temp1 - 1; i++)
temp = queue1[i];
queue1[i] = queue1[j];
queue1[j] = temp;
}
}
}
temp = queue2[i];
queue2[i] = queue2[j];
queue2[j] = temp;
}
}
}
queue[i] = queue1[j];
queue[i] = max;
queue[i + 1] = 0;
queue[i] = queue2[j];
queue[0] = head;
for (j = 0; j <= n + 1; j++)
seek += diff;
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <mpi.h>
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;
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;
}