0% found this document useful (0 votes)
32 views32 pages

OS - II Pract - Sol - Set

Os

Uploaded by

Sunny Ranpise
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)
32 views32 pages

OS - II Pract - Sol - Set

Os

Uploaded by

Sunny Ranpise
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/ 32

TyBsc Comp.

Operating System -II (2019 pattern)


Practical Solution Set

Assignment No. 1:
Slot 1. Bankers Algorithm

#include<stdio.h>
#define MAX 10

int m,n,total[MAX],avail[MAX],alloc[MAX][MAX],
max[MAX][MAX],need[MAX][MAX],work[MAX],finish[MAX],
seq[MAX],request[MAX];

void accept()
{
int i,j;

printf("Enter no.of process:");


scanf("%d",&n);

printf("Enter no.of resource types:");


scanf("%d",&m);

printf("Enter total no.of resources of each resource type:\n");

for(i=0;i<m;i++)
{
printf("%c:",65+i);
scanf("%d",&total[i]);
}

1
printf("Enter no.of allocated resources of each resource type by each
process:\n");

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

printf("Enter no.of maximum resources of each resource type by each


process:\n");

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

void calc_avail()
{
int i,j,s;

for(j=0;j<m;j++)
{
s=0;
for(i=0;i<n;i++)
s+=alloc[i][j];

avail[j] = total[j] - s;
}
}
2
void calc_need()
{
int i,j;

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

void print()
{
int i,j;

printf("\tAllocation\tMax\tNeed\n\t");

for(i=0;i<3;i++)
{
for(j=0;j<m;j++)
printf("%3c",65+j);
printf("\t");
}

printf("\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<m;j++)
printf("%3d",alloc[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%3d",max[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%3d",need[i][j]);
printf("\n");
}

printf("Available\n");
for(j=0;j<m;j++)
3
printf("%3c",65+j);
printf("\n");
for(j=0;j<m;j++)
printf("%3d",avail[j]);

printf("\n");
}

int check(int s)
{
int i,j;

i = s;
do
{
if(!finish[i])
{
for(j=0;j<m;j++)
{
if(need[i][j]>work[j])
break;
}

if(j==m) return i;
}

i=(i+1)%n;
}while(i!=s);

return -1;
}

void banker()
{
int i,j,k=0;

for(i=0;i<n;i++)
finish[i]=0;

for(j=0;j<m;j++)
4
work[j] = avail[j];

i=0;
while((i=check(i))!=-1)
{
printf("Process P%d resource granted.\n",i);
finish[i] = 1;

for(j=0;j<m;j++)
work[j] += alloc[i][j];

printf("finish(");
for(j=0;j<n;j++)
printf("%d,",finish[j]);
printf("\b)\nwork(");
for(j=0;j<m;j++)
printf("%d,",work[j]);
printf("\b)\n");

seq[k++]=i;

i=(i+1)%n;
}

if(k==n)
{
printf("System is in safe state.\n");
printf("Safe sequence:");
for(j=0;j<n;j++)
printf("P%d ",seq[j]);
}
else
{
printf("System is not in safe state.");
}
printf("\n");
}

int main()
{
5
int i,j,pno;

accept();
calc_avail();
calc_need();
print();
banker();

printf("Enter process no:");


scanf("%d",&pno);

printf("Enter resource request of process P%d\n",pno);

for(j=0;j<m;j++)
{
printf("%c:",65+j);
scanf("%d",&request[j]);
}

for(j=0;j<m;j++)
{
if(request[j]>need[pno][j])
break;
}

if(j==m)
{
for(j=0;j<m;j++)
{
if(request[j]>avail[j])
break;
}

if(j==m)
{
for(j=0;j<m;j++)
{
avail[j]-=request[j];
alloc[pno][j]+=request[j];
need[pno][j]-=request[j];
6
print();
banker();
}
}
else
printf("Process P%d must wait.\n",pno);
}
else
printf("Process P%d has exceeded its maximum claim\n",pno);

return 0;
}

7
Assignment No. 1:
Slot 2. Bankers algorithm

#include<stdio.h>
#define MAX 10

int m,n,total[MAX],avail[MAX],alloc[MAX][MAX],
max[MAX][MAX],need[MAX][MAX],work[MAX],finish[MAX],
seq[MAX],request[MAX];

void accept()
{
int i,j;

printf("Enter no.of process:");


scanf("%d",&n);

printf("Enter no.of resource types:");


scanf("%d",&m);

printf("Enter total no.of resources of each resource type:\n");

for(i=0;i<m;i++)
{
printf("%c:",65+i);
scanf("%d",&total[i]);
}

printf("Enter no.of allocated resources of each resource type by each


process:\n");

for(i=0;i<n;i++)
{
printf("P%d:\n",i);
for(j=0;j<m;j++)
{
printf("%c:",65+j);
8
scanf("%d",&alloc[i][j]);
}
}

printf("Enter no.of maximum resources of each resource type by each


process:\n");

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

void calc_avail()
{
int i,j,s;

for(j=0;j<m;j++)
{
s=0;
for(i=0;i<n;i++)
s+=alloc[i][j];

avail[j] = total[j] - s;
}
}

void calc_need()
{
int i,j;

for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j]=max[i][j]-alloc[i][j];
}
9
void print()
{
int i,j;

printf("\tAllocation\tMax\tNeed\n\t");

for(i=0;i<3;i++)
{
for(j=0;j<m;j++)
printf("%3c",65+j);
printf("\t");
}

printf("\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<m;j++)
printf("%3d",alloc[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%3d",max[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%3d",need[i][j]);
printf("\n");
}

printf("Available\n");
for(j=0;j<m;j++)
printf("%3c",65+j);
printf("\n");
for(j=0;j<m;j++)
printf("%3d",avail[j]);

printf("\n");
}

int check(int s)
10
{
int i,j;

i = s;
do
{
if(!finish[i])
{
for(j=0;j<m;j++)
{
if(need[i][j]>work[j])
break;
}

if(j==m) return i;
}

i=(i+1)%n;
}while(i!=s);

return -1;
}

void banker()
{
int i,j,k=0;

for(i=0;i<n;i++)
finish[i]=0;

for(j=0;j<m;j++)
work[j] = avail[j];

i=0;
while((i=check(i))!=-1)
{
printf("Process P%d resource granted.\n",i);
finish[i] = 1;

for(j=0;j<m;j++)
11
work[j] += alloc[i][j];

printf("finish(");
for(j=0;j<n;j++)
printf("%d,",finish[j]);
printf("\b)\nwork(");
for(j=0;j<m;j++)
printf("%d,",work[j]);
printf("\b)\n");

seq[k++]=i;

i=(i+1)%n;
}

if(k==n)
{
printf("System is in safe state.\n");
printf("Safe sequence:");
for(j=0;j<n;j++)
printf("P%d ",seq[j]);
}
else
{
printf("System is not in safe state.");
}
printf("\n");
}

int main()
{
int i,j,pno;

accept();
calc_avail();
calc_need();
print();
banker();

printf("Enter process no:");


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

printf("Enter resource request of process P%d\n",pno);

for(j=0;j<m;j++)
{
printf("%c:",65+j);
scanf("%d",&request[j]);
}

for(j=0;j<m;j++)
{
if(request[j]>need[pno][j])
break;
}

if(j==m)
{
for(j=0;j<m;j++)
{
if(request[j]>avail[j])
break;
}

if(j==m)
{
for(j=0;j<m;j++)
{
avail[j]-=request[j];
alloc[pno][j]+=request[j];
need[pno][j]-=request[j];
print();
banker();
}
}
else
printf("Process P%d must wait.\n",pno);
}
else
printf("Process P%d has exceeded its maximum claim\n",pno);
13
return 0;
}

14
Assignment No. 2:
Slot 1.
Continuous allocation
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}

15
Program Output:
Files Allocated are :
Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0

16
Assignment No. 2:
Slot 2
Linked allocation

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200

typedef struct dir


{
char fname[20];
int start;
struct dir *next;
}NODE;

NODE *first,*last;

int n,fb,bit[MAX];

void init()
{
int i;

printf("Enter total no.of disk blocks:");


scanf("%d",&n);

fb = n;

for(i=0;i<10;i++)
{
int k = rand()%n;
if(bit[k]!=-2)
{
bit[k]=-2;
fb--;
}
}

17
}

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;

printf("Enter file name:");


18
scanf("%s",fname);

printf("Enter no.of blocks:");


scanf("%d",&nob);

if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}

for(i=0;i<n;i++)
{
if(bit[i]==0) 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--;
19
}
j++;
}

bit[i]=-1;
printf("File %s created successully.\n",fname);
}

void delete()
{
char fname[20];
NODE *p,*q;
int nob=0,i,j;

printf("Enter file name to be deleted:");


scanf("%s",fname);

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

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;
21
case 2:
create();
break;
case 3:
show_dir();
break;
case 4:
delete();
break;
case 5:
exit(0);
}
}

return 0;
}

ssignment No. 2:
Slot 3
/* Program to simulate indexed file allocation strategy */
Program Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the
disk : \n", ind);
scanf("%d",&n);
}
else

22
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1234
Allocated
File Indexed

23
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
78
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)

24
Assignment No. 3:
Slot 1
(i) FCFS Disk Scheduling
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ReadyQueue[100],i,n,TotalHeadMov=0,initial;
//Enter the number of requests
scanf("%d",&n);
for(i=0;i<n;i++){
//Enter the sequence of request
scanf("%d",&ReadyQueue[i]);
}
// Enter initial head position
scanf("%d",&initial);
for(i=0;i<n;i++)
{
TotalHeadMov=TotalHeadMov+abs(ReadyQueue[i]-initial);
initial=ReadyQueue[i];
}
printf("Total Head Movement=%d",TotalHeadMov);
}

25
Slot 1 (ii) SSTF
# include<stdio.h>
#include<stdlib.h>
int main()
{
int ReadyQueue[100],i,n,TotalHeadMoment=0,initial,count=0;
// The number of requests
scanf("%d",&n);
//Enter the requests sequence
for(i=0;i<n;i++)
scanf("%d",&ReadyQueue[i]);
//Enter the initial head position
scanf("%d",&initial);

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

26
TotalHeadMoment=TotalHeadMoment+min;
initial=ReadyQueue[index];
ReadyQueue[index]=1000;
count++;
}
printf("Total head movement is %d",TotalHeadMoment);
}

27
Assignment No. 3:
Slot 2 (i)
Scan Algorithm
#include<stdio.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;
}
}

28
}
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],d
iff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

29
Assignment No. 3:
Slot 2 (ii)
C-SCAN algorithm

#include<stdio.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;
}
}
30
}
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],d
iff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}

OUTPUT :

31
32

You might also like