R20 OS Programs
R20 OS Programs
1d.
INPUT
Enter the number of processes -- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
OUTPUT
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
11101
42516
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19
Average Waiting Time is --- 8.200000
Average Turnaround Time is --- 12.000000
2a. MFT MEMORY MANAGEMENT TECHNIQUE
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentation is %d",ef);
getch();
14
}
INPUT
Enter the total memory available (in Bytes) -- 1000
Enter the block size (in Bytes)-- 300
Enter the number of processes – 5
Enter memory required for process 1 (in Bytes) -- 275
Enter memory required for process 2 (in Bytes) -- 400
Enter memory required for process 3 (in Bytes) -- 290
Enter memory required for process 4 (in Bytes) -- 293
Enter memory required for process 5 (in Bytes) -- 100
No. of Blocks available in memory -- 3
OUTPUT
PROCESS MEMORY REQUIRED ALLOCATED INTERNAL FRAGMENTATION
1 275 YES 25
2 400 NO -----
3 290 YES 10
4 293 YES 7
Memory is Full, Remaining Processes cannot be accommodated
Total Internal Fragmentation is 42
Total External Fragmentation is 100
2b. MVT MANAGEMENT TECHNIQUE MEMORY
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
15
getch();
}
INPUT
Enter the total memory available (in Bytes) -- 1000
Enter memory required for process 1 (in Bytes) -- 400
Memory is allocated for Process 1
Do you want to continue(y/n) -- y
Enter memory required for process 2 (in Bytes) -- 275
Memory is allocated for Process 2
Do you want to continue(y/n) -- y
Enter memory required for process 3 (in Bytes) -- 550
OUTPUT
Memory is Full
Total Memory Available -- 1000
PROCESS MEMORY ALLOCATED
1 400
2 275
Total Memory Allocated is 675
Total External Fragmentation is 325
3a. A program to simulate FIFO Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
q++;
if(q1<3) { q1=q; } }
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{ printf("-->%c",f); m++; }
p=0; for(k=0;k<q1;k++)
{ if(b[i+1]==a[k]) p=1; } }
printf("\nNo of faults:%d",m);
getch();
}
OUTPUT:
Input: Enter the Number of Pages: 12
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 531-> F 2 521-> F 4 524-> F 5 524 3 324-> F 2 324 5
354-> F 2 352-> F No of faults: 9
#include<stdio.h>
#include<conio.h>
void main() {
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{ q1=q; //g=1; } }
printf("\n%d",b[i]);
printf("\t"); for(h=0;h<q1;h++)
printf("%d",a[h]); if((p==0)&&(q<=3))
{ printf("-->%c",f); m++; }
{ for(k=0;k<q1;k++) {
if(b[i+1]==a[k]) p=1; }
if(u==0) q=j; } }
else { for(k=0;k<q;k++)
{ if(b[i+1]==a[k]) p=1; } } }
printf("\nNo of faults:%d",m);
getch();
}
OUTPUT:
Input: Enter the Number of Pages: 12 Enter 12 Page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 251-> F 2 251 4 254-> F 5 254 3 354-> F 2 352-> F 5 352
2 352 No of faults: 7
PROGRAM:
#include<stdio.h>
int main()
int f,p;
int pages[50],frame[10],hit=0,count[50],time[50];
int i,j,page,flag,least,minTime,temp;
printf("Enter no of frames : ");
scanf("%d",&f);
scanf("%d",&p);
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<50;i++)
count[i]=0;
for(i=0;i<p;i++)
scanf("%d",&pages[i]);
printf("\n");
for(i=0;i<p;i++)
count[pages[i]]++;
time[pages[i]]=i;
flag=1;
least=frame[0];
for(j=0;j<f;j++)
{
if(frame[j]==-1 || frame[j]==pages[i])
if(frame[j]!=-1)
hit++;
flag=0;
frame[j]=pages[i];
break;
if(count[least]>count[frame[j]])
least=frame[j];
if(flag)
minTime=50;
for(j=0;j<f;j++)
temp=j;
minTime=time[frame[j]];
}
count[frame[temp]]=0;
frame[temp]=pages[i];
for(j=0;j<f;j++)
printf("%d ",frame[j]);
printf("\n");
return 0;
OUTPUT:
Input: Enter the Number of Pages: 12 Enter 12 Page Numbers: 2 3 2 1 5 2 4 5 3 2 5 2
Output: 2 2-> F 3 23-> F 2 23 1 231-> F 5 251-> F 2 251 4 254-> F 5 254 3 354-> F 2 352-> F 5 352
2 352 No of faults: 7
Output:
Student@ubuntu:~$gcc shm.c
Student@ubuntu:~$ ./a.out
Shared memory ID=65537 child pointer 3086680064
Child value =1
Shared memory ID=65537 child pointer 3086680064
Parent value=1
Parent value=42
Child value=42
PROGRAM
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
printf(“\nEnter your choice: ”);
scanf(“%d”, &choice);
switch(choice) {
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
Break;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT
1. Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit
Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit
Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit
Enter your choice: 3
PROGRAM
void main()
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
clrscr();
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf(“Enter the no of processes:\n”);
scanf(“%d”,&p);
scanf(“%d”,&r);
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
scanf(“%d”,&clm[i][j]);
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
scanf(“%d”,&alloc[i][j]);
for(j=1;j<=r;j++)
scanf(“%d”,&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
do
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
req[i][j]=clm[i][j]-alloc[i][j];
for(j=1;j<=r;j++)
{ printf(” “,avail[j]); }
for(i=1;i<=p;i++)
for(j=1;j<=r;j++)
printf(“%d”,clm[i][j]);
}
printf(“\t\t\t”);
for(j=1;j<=r;j++)
printf(“%d”,alloc[i][j]);
printf(“\n”);
prc=0;
for(i=1;i<=p;i++)
prc=i;
for(j=1;j<=r;j++)
if(avail[j]
prc=0;
break;
if(prc!=0)
break;
if(prc!=0)
{
count++;
for(j=1;j<=r;j++)
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
while(count!=p&&prc!=0);
if(count==p)
else
getch();
OUT PUT:
Available resource is :
245 123
253 234
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
int cl[10][10],al[10][10],av[10],i,j,k,m,n,ne[10][10],flag=0;
clrscr();
for(i=0;i<m;i++) { for(j=0;j<n;j++) {
scanf("%d",&cl[i][j]); } }
for(i=0;i<m;i++) { for(j=0;j<n;j++) {
scanf("%d",&al[i][j]); } }
for(i=0;i<m;i++) { for(j=0;j<n;j++)
{ ne[i][j]=cl[i][j]-al[i][j];
printf("\t%d",ne[i][j]); } printf("\n"); }
for(i=0;i<n;i++)
scanf("%d",&av[i]);
printf("Claim matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ printf("\t%d",cl[i][j]); }
printf("\n"); }
for(i=0;i<m;i++) { for(j=0;j<n;j++)
{ printf("\t%d",al[i][j]); } printf("\n"); }
printf("Available matrix:\n");
for(i=0;i<n;i++) { printf("%d\t",av[i]); } /
/for(k=0;k<m;k++) for(i=0;i<m;i++)
}
OUTPUT:
Allocated matrix: 1 0 0 5 1 1 2 1 1 0 0 2
Available matrix: 1 1 2
Safe State - 21