OS Solution Set
OS Solution Set
OS Solution Set
a) Accept Available
b) Display Allocation, Max
c) Display the contents of need matrix
d) Display Available
//Bankers algorithm
#include<stdio.h>
int main()
{
int
m,n,i,j,all[10][10],max[10][10],need[10][10],ava[10],work[10],no=0,safe=0,seq[10];
char p[10],t='t',f='f';
printf("Enter the number of process\n");
scanf("%d",&m);
printf("Enter the number of resources\n");
scanf("%d",&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}
for(i=0;i<m;i++)
{
if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}
}
if(safe==m)
{
printf("\n system is in safe state:\n");
printf("safe sequence is:{");
for(i=0;i<m;i++)
printf("p%d",seq[i]);
printf("}");
}
else
printf("\n system is not safe:\n");
}
/*
[root@ugilinux64 Shraddha]# cc banker.c
[root@ugilinux64 Shraddha]# ./a.out
Enter the number of process
5
Enter the number of resources
3
Enter the Allocations
Enter the allocation for process p0 0 1 0
Enter the allocation for process p1 2 0 0
Enter the allocation for process p2 3 0 2
Enter the allocation for process p3 2 1 1
Enter the allocation for process p4 0 0 2
Enter the Max
Enter the Max for process p0 753
Enter the Max for process p1 322
Enter the Max for process p2 902
Enter the Max for process p3 222
Enter the Max for process p4 433
Process Allocation Max
p0 0 1 0 7 5 3
p1 2 0 0 3 2 2
p2 3 0 2 9 0 2
p3 2 1 1 2 2 2
p4 0 0 2 4 3 3
enter availables:
653
work is:
6 5 3
contents of need table is:
Process Need
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
*******************************************************
/* Consider a system with ‘m’ processes and ‘n’ 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*/
//Resource Request
#include<stdio.h>
int main()
{
int
need[10][10],all[10][10],max[10][10],ava[10],i,j,finish[10],m,n,work[10],no=0,safe=
0,seq[10];
char p[10],t='t',f='f';
int pn=0,temp=0,temp1=0,req[10],seq1[10],safe1=0;
printf("Enter the number of process\n");
scanf("%d",&m);
printf("Enter the number of resources\n");
scanf("%d",&n);
printf("enter allocations: \n");
for(i=0;i<m;i++)
{
printf("enter the process %d: ",i);
for(j=0;j<n;j++)
{
scanf("%d",&all[i][j]);
}
}
printf("enter max:\n");
for(i=0;i<m;i++)
{
printf("enter the process %d: ",i);
for(j=0;j<n;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("enter availables:\n");
for(j=0;j<n;j++)
{
scanf("%d",&ava[j]);
work[j]=ava[j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
for(i=0;i<m;i++)
{
if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
//f(no==n)
if(no==n)
{
printf("\n p%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq[safe]=i;
safe++;
}
else
p[i]=f;
no=0;
printf("\n new work is:\n");
for(j=0;j<n;j++)
printf("%d \t",work[j]);
}
}
if(safe==m)
{
printf("\n system is in safe state:\n");
printf("safe sequence is:{");
for(i=0;i<m;i++)
printf("p%d",seq[i]);
printf("}");
}
else
printf("\n system is not safe:\n");
printf("Resourse process");
printf("enter the process no");
scanf("%d",&pn);
printf("enter the request");
for(i=0;i<=n;i++)
{
scanf("%d",&req[i]);
}
for(i=0;i<=n;i++)
{
if(req[i]<=need[pn][i])
temp++;
}
if(temp==n)
{
for(i=0;i<=n;i++)
{
if(req[i]<=ava[i])
temp1++;
}
}
printf("temp1=%d",temp1);
if(temp1==n)
{
for(i=0;i<=n;i++)
{
ava[i]=ava[i]-req[i];
all[pn][i]=all[pn][i]+req[i];
need[pn][i]=need[pn][i]-req[i];
}
}
printf("\n content of need table is:\n");
for(i=0;i<=m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",need[i][j]);
printf("\n");
}
printf("%d\t",all[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
work[i]=ava[i];
for(i=0;i<m;i++)
{
printf("new work is:\n");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n p%d id true\n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq1[safe1]=i;
safe1++;
}
else
p[i]=f;
no=0;
printf("\n new work is \n");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
}
for(i=0;i<m;i++)
{
if(p[i]!=t)
{
for(j=0;j<n;j++)
{
if(need[i][j]<=work[j])
no++;
}
if(no==n)
{
printf("\n P%d is true \n",i);
p[i]=t;
for(j=0;j<n;j++)
work[j]=work[j]+all[i][j];
seq1[safe1]=i;
safe1++;
}
else
p[i]=f;
no=0;
printf("\n new work is");
for(j=0;j<n;j++)
printf("%d\t",work[j]);
}
}
if(safe1==m)
{
printf("\n sysytem is in safe state");
printf("\n safe sequece is:");
for(i=0;i<m;i++)
printf("\n p%d",seq1[i]);
}
else
printf("\n sysytem is not safe \n");
}
*******************************************************
/* Que. Write a program to simulate Linked file allocation method. Assume disk
with n number of blocks. Give value of n as input. Randomly mark some block as
allocated and accordingly maintain the list of free blocks Write menu driver
program with menu options as mentioned below and implement each option.
Show Bit Vector
Create New File
Show Directory
Exit */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
NODE *first,*last;
int n,fb,bit[MAX];
void init()
{
int i;
for(i=0;i<n;i++)
{
bit[i] =1;
fb = n;
int k = rand()%n;
if(bit[k]!=0)
{
bit[k]=1;
fb--;
}
}
}
void show_bitvector()
{
int i;
for(i=0;i<n;i++)
printf("%d ",bit[i]);
printf("\n");
}
void show_dir()
{
NODE *p;
int i;
printf("File\tChain\n");
p = first;
while(p!=NULL)
{
printf("%s\t",p->fname);
i = p->start;
while(i!=-1)
{
printf("%d->",i);
i=bit[i];
}
printf("NULL\n");
p=p->next;
}
}
void create()
{
NODE *p;
char fname[20];
int i,j,nob;
if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}
for(i=0;i<n;i++)
{
if(bit[i]==1) break;
}
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->next=NULL;
if(first==NULL)
first=p;
else
last->next=p;
last=p;
fb-=nob;
j=i+1;
nob--;
while(nob>0)
{
if(bit[j]==0)
{
bit[i]=j;
i=j;
nob--;
}
j++;
}
bit[i]=-1;
printf("File %s created successully.\n",fname);
}
void delete()
{
char fname[20];
NODE *p,*q;
int nob=0,i,j;
p = q = first;
while(p!=NULL)
{
if(strcmp(p->fname,fname)==0)
break;
q=p;
p=p->next;
}
if(p==NULL)
{
printf("File %s not found.\n",fname);
return;
}
i = p->start;
while(i!=-1)
{
nob++;
j = i;
i = bit[i];
bit[j] = 0;
}
fb+=nob;
if(p==first)
first=first->next;
else if(p==last)
{
last=q;
last->next=NULL;
}
else
q->next = p->next;
free(p);
printf("File %s deleted successfully.\n",fname);
}
int main()
{
int ch;
init();
while(1)
{
printf("1.Show bit vector\n");
printf("2.Create new file\n");
printf("3.Show directory\n");
printf("4.Delete file\n");
printf("5.Exit\n");
printf("Enter your choice (1-5):");
scanf("%d",&ch);
switch(ch)
{
case 1:
show_bitvector();
break;
case 2:
create();
break;
case 3:
show_dir();
break;
case 4:
delete();
break;
case 5:
exit(0);
}
}
return 0;
}
********************************************************
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int bit[256],n,fb;
typedef struct d
{
char fname[25];
int length,start;
struct d *next;
}NODE;
NODE *last,*first;
void init()
{
printf("Enter the no. of block\n");//10
scanf("%d",&n);
for (int i = 0; i < n; i++)
bit[i] =1;
fb=n;//free block
}
void BitVector(){
for (int i = 0; i < n; i++)
printf("%d\t\n",bit[i]);
void createFile(){
NODE *f;
int i,j;
int len;
char fname[24];
if(fb>len){
strcpy(f->fname,fname);
f->length=len;
while (len>0)
{
if(bit[j]==1){
bit[i]=0;
i=j;
len--;
}
j++;
}
bit[i]=f->start+f->length;
printf("File %s created successully.\n",fname);
else{
printf("Insufficent Space");
}
}
void show_dir(){
NODE *f;
f=first;
printf("\tFile\tStart\tLength\n");
while (f!=NULL)
{
printf("%7s\t%8d\t%9d\n",f->fname,f->start,f->length);
f=f->next;
}
}
void fdelete(){
char fname[100];
NODE *fp,*fn;
int i,j;
fp=fn=first;
while (fn!=NULL)
{
if(strcmp(fname,fn->fname)==0)
break;
fp=fn;
fn=fn->next;
}
if (fn==NULL)
{
printf("File not found\n");
}
else{
i=fn->start;
while (bit[i]!=(fn->start+fn->length)){
bit[i++]=1;
}
bit[i]=1;
fb+=fn->length;
if(fn==first){
first=first->next;
}
else if(fn==last){
last=fp;
last->next=NULL;
}
else{
fp->next=fn->next;
}
free(fn);
printf("File %s deleted successfully.\n",fname);
}
}
void main()
{
int choice;
init();
do
{
printf("1 :Show bit vector\n");
printf("2 :Create new file \n");
printf("3 :Show Directry\n");
printf("4 :Delete file\n");
printf("5 :Exit\n");
printf("\nEnter Your choice\n :");
scanf("%d",&choice);
switch (choice)
{
case 1:
BitVector();
break;
case 2:
createFile();
break;
case 3:
show_dir();
break;
case 4:
fdelete();
break;
default:
break;
}
} while (choice<5);
}
*******************************************************
/* Write a simulation program for disk scheduling using FCFS algorithm. Accept
total number of disk blocks, disk request string, and current head position from
the user. Display the list of request in the order in which it is served. Also display
the total number of head moments.
55, 58, 39, 18, 90, 160, 150, 38, 184
Start Head Position: 50
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int rq[199],initial,i,headm=0,n;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
for(i=0;i<n;i++)
{
headm +=abs(rq[i]-initial);
initial=rq[i];
}
printf("\ntotal moment %d",headm);
}
/* OUTPUT
[root@ugilinux64 Shraddha]# cc FCFS.c
[root@ugilinux64 Shraddha]# ./a.out
while (count!=n)
{
min=1000;
for(i=0;i<n;i++)
{
d=abs(rq[i]-initial);
if(min>=d)
{
min=d;
index=i;
}
}
headm+=min;
initial=rq[index];
rq[index]=1000;
count++;
}
printf("\ntotal moment %d",headm);
}
/*OUTPUT
[root@ugilinux64 Shraddha]# cc SSTF.c
[root@ugilinux64 Shraddha]# ./a.out
*/
*******************************************************
/* Write a simulation program for disk scheduling using SCAN algorithm. Accept
total number of disk blocks, disk request string, and current head position from
the user. Display the list of request in the order in which it is served. Also display
the total number of head moments.
33, 99, 142, 52, 197, 79, 46, 65
Start Head Position: 72
Direction: Right
*/
#include<stdio.h>
#include<stdlib.h>
int max=199;
void main(){
int direction,rq[max],initial,i,headm=0,n,min,m,headb;
printf("\nEnter the No Request\n");
scanf("%d",&n);
printf("\nEnter the sequence\n");
for ( i = 0; i <n; i++)
scanf("%d",&rq[i]);
printf("\nEnter the initial\n");
scanf("%d",&initial);
printf("\nEnter head direction\n1:Left\n2:Right\n");
scanf("%d",&direction);
if(direction==1){
m=rq[0];
for(i=1;i<n;i++){
if(m<=rq[i])
m=rq[i];
}
headb=initial+m;
printf("\nTotal left HeadMovement %d ",headb);
}else{
min=rq[0];
for(i=1;i<n;i++){
if(min>=rq[i])
min=rq[i];
}
headm=max-initial+max-min;
printf("\nTotal right HeadMovement %d ",headm);
}
}
/*OUTPUT
[root@ugilinux64 Shraddha]# cc SCAN.c
[root@ugilinux64 Shraddha]# ./a.out
Enter the No Request 9
Enter the sequence 55 58 39 18 90 160 150 38 184
Enter the initial 50
Enter head direction
1:Left
2:Right
1
Total left HeadMovement 234
*/
*******************************************************
/* Write a simulation program for disk scheduling using LOOK algorithm. Accept
totalnumber of disk blocks, disk request string, and current head position from the
user. Display the list of request in the order in which it is served. Also display the
total numberof head moments.
86, 147, 91, 170, 95, 130, 102, 70
Starting Head position= 125
Direction: User Defined
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,j,n,TotalHeadMoment=0,initial,size,move;
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);
printf("Enter the head movement direction for high 1 and for low 0\n");
scanf("%d",&move);
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
// if movement is towards high value
if(move==1)
{
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=index;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
*******************************************************
}
}
int index;
for(i=0;i<n;i++)
{
if(initial<RQ[i])
{
index=i;
break;
}
}
for( i=0;i<index;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
// if movement is towards low value
else
{
for(i=index-1;i>=0;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
for(i=n-1;i>=index;i--)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
}
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}
MPI_Finalize();
return 0;
}
/* OUTPUT
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}
MPI_Finalize();
return 0;
}
/*Write an MPI program to calculate sum of all odd randomly generated 1000
numbers(stored in array) on a cluster.*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Reduce(&local_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if (rank == 0) {
printf("Sum = %d\n", (int)sum);
}
MPI_Finalize();
return 0;
}
/*Write an MPI program to calculate sum and average randomly generated 1000
numbers (stored in array) on a cluster.*/
#include<mpi.h>
#include<stdio.h>
#include<stdlib.h>
double sum=0.0,local_sum=0.0;
double average=1.0;
double data[SIZE];
MPI_Init(&argc ,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
for(i=0;i<SIZE;i++)
{
data[i]=(double) rand()/RAND_MAX;
}
for(i=0;i<SIZE;i++)
{
local_sum= local_sum + data[i];
}
MPI_Reduce(&local_sum,&sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD
);
average=sum/SIZE;
if(rank==0)
{
printf("Sum=%d\n",(int)sum);
printf("average=%f\n",(double)average);
}
MPI_Finalize();
return 0;
}
/*OUTPUT
[root@ugilinux205 Shraddha]# mpicc sum.c
[root@ugilinux205 Shraddha]# mpirun -np 5 ./a.out
Sum=526
Average=0.526
[root@ugilinux205 Shraddha]#
/*Write an MPI program to find the max number from randomly generated 1000
numbers(stored in array) on a cluster (Hint: Use MPI_Reduce) */
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <limits.h>
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Finalize();
return 0;
}
/*Write an MPI program to find the min number from randomly generated 1000
numbers(stored in array) on a cluster (Hint: Use MPI_Reduce)*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <limits.h>
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
printf("Global minimum value = %d\n", global_min);
}
MPI_Finalize();
return 0;
}
/* OUTPUT
[root@ugilinux205 Shraddha]# mpicc MIN.c
[root@ugilinux205 Shraddha]# mpirun -np 5 ./a.out
Global minimum value = 8936987
[root@ugilinux205 Shraddha]#
*/