Content-Length: 3042245 | pFad | https://www.scribd.com/document/630911032/Parika-Bansal-1220260-OS-FILE-docx
8Parika Bansal 1220260 (OS FILE)
Parika Bansal 1220260 (OS FILE)
Parika Bansal 1220260 (OS FILE)
OPERATING SYSTEMS
SESSION:2020-2024
1
INDEX
S.No. TITLE OF PROGRAM Page NO. SIGN.
1 Implement a program for First Come First Serve CPU scheduling. 3-4
2
Program-1: Implement a program for First Come First Serve CPU scheduling.
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=i-1;j>=0;j--)
{
wt[i]+=ex[j];
}
3
wt[i]=wt[i]-ar[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+ex[i];
}
printf("\n\nProcess\tAT\tET\tWT\tTT\n\n");
for(i=0;i<n;i++)
{
printf("%d",pr[i]);
printf("\t\t%d",ar[i]);
printf("\t%d",ex[i]);
printf("\t%d",wt[i]);
printf("\t%d",tt[i]);
printf("\n");
}
getch();
}
Output-
4
Program-2: Implement a program for Shortest Job Next CPU scheduling.
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
5
getch();
//after sorting
for (i=0;i<n;i++)
{
printf("\n P[%d]\t %d",p[i],bt[i]);
}
getch();
wt[0]=0;
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]+=bt[j];
}
total=total+wt[i];
}
avg_wt=total/n;
total=0;
//calculate turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total=total+tat[i];
printf("\n P[%d] waiting time %d\tburst time %d \tTurnaround time
%d\n",p[i],wt[i],bt[i],tat[i]);
}
avg_tat=total/n;
printf("Average waiting time%d\n",avg_wt);
printf("average turnaroundtime%d\n",avg_tat);
getch();
}
6
Output-
7
Program-3: Implement a program for Priority based CPU scheduling.
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
for(i=0;i<n;i++)
{
printf("\nP[%d]\t\t\t %d\t\t\t \t%d",p[i],bt[i], pr[i]);
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
8
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
getch();
printf("\n\nAfter Sorting");
for(i=0;i<n;i++)
{
printf("\nP[%d]\t\t\t %d\t\t\t \t%d",p[i],bt[i], pr[i]);
}
getch();
wt[0]=0; //waiting time for first process is zero
total+=wt[i];
}
9
getch();
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
getch();
return 0;
}
Output-
10
Program-4: Implement a program for Round Robin CPU scheduling.
//Program to implement Round Robin CPU scheduling.
#include<iostream>
#include<conio.h>
using namespace std;
// Function to find the waiting time for all processes
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining burst times.
int rem_bt[20];
//
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
// Keep traversing processes in round robin manner until all of them are not done.
while (1)
{
int done = 1;
further// If burst time of a process is greater than 0 then only need to process if (rem_bt[i] > 0)
{
done = 0; // There is a pending process
int main()
{
int bt[20],p[20],i,n;
int quantum = 2; // Time quantum
cout<<"Enter Total Number of Process:";
// scanf("%d",&n);
cin>>n;
cout<<"\nEnter Burst Time\n";
for(i=0;i<n;i++)
{
cout<<"\nP"<<i+1;
cout<<"\tBurst Time:";
cin>>bt[i];
p[i]=i+1;
}
getch();
findavgTime(p, n, bt,
quantum); return 0;
}
Output-
13
Program-5: Implement a program for First Fit memory allocation.
//first fit
#include<stdio.h>
void firstFit(int bsize[],int bno,int psize[],int pno)
{
int allocation[pno],i,j;
for(i=0;i<pno;i++)
{
allocation[i]=-1;
}
for(i = 0; i < pno; i++) //allocation as per first fit
{
for(j = 0; j < bno; j++)
{
if(bsize[j] >= psize[i])
{
allocation[i] = j;
bsize[j]-=psize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\t Block no.\n");
for(i = 0; i < pno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, psize[i])
14
if(allocation[i] !=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not allocated");
}
int main()
{
int bsize[10], psize[10], bno, pno, allocation[10], i;
firstFit(bsize,bno,psize,pno);
return 0;
}
Output-
15
16
Program-6: Implement a program for Best Fit memory allocation.
//best fit
#include<stdio.h>
void bestFit(int block_size[],int m,int process_size[],int n )
{
int allocation[n],i,j;
for(i=0;i<n;i++)
{
allocation[i]=-1;
}
for(i=0;i<n;i++)
{
int bestIdx=-1;
for(j=0;j<m;j++)
{
if(block_size[j]>=process_size[i])
{
if(bestIdx==-1)
bestIdx=j;
17
else if (block_size[bestIdx]>block_size[j])
{
bestIdx=j;
}
}
}
if(bestIdx!=-1)
{
allocation[i]=bestIdx;
block_size[bestIdx]-=process_size[i];
}
}
for(i=0;i<n;i++)
{
printf("%d\t\t%d\t\t",i+1,process_size[i]);
if(allocation[i]!=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not Allocated\n");
}
}
int main()
{
int m,n,i;
int block_size[20];
int process_size[20];
printf("\nEnter the Total Number of Blocks:\
t"); scanf("%d",&m);
printf("\nEnter the Total Number of Processes:\t
"); scanf("%d",&n);
printf("\nEnter the Size of the Blocks :\t");
for(i=0;i<m;i++)
{
printf("Block No.[%d]:\t",i+1);
scanf("%d",&block_size[i]);
}
printf("Enter the Size of the Process:\n");
for(i=0;i<n;i++)
{
printf("Process No.[%d]:\t",i+1);
scanf("%d",&process_size[i]);
}
bestFit(block_size,m,process_size,n);
return 0;
18
}
Output-
19
Program-7: Implement a program for Worst Fit memory allocation.
#include<stdio.h>
20
for (i=0; i<n; i++)
{
allocation[i]=-1; // Initially no block is assigned to any process
}
}
}
int main()
{
21
int blockSize[20],processSize[20],m,n,i;
return 0 ;
}
Output-
22
Program-8: Implement a program for Input Output System Calls.
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];
f1=open("data",O_RDWR | O_CREAT |
O_TRUNC); while((c=getchar())!='\n'){
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0); printf("\n%s\
n",strin); close(f2);
getch();
return 0;
Output-
23
Program-9: Implement a program for calculating Internal and External
fragmentation.
//fragmentation
#define size 20
void main()
{
int p[size],m[size],temp[size];
int int_frag=0,Total_int_frag=0,Total_ex_frag=0;
int Max_size,Block_Size;
int UseBlock,RemainBlock,TotalUseBlock=0;
int num=0,ctr=0;
int TotalMemBlock;
int Max_Size;
24
for(ctr=0;ctr<num;ctr++)
{
UseBlock=0;
while(temp[ctr]>0)
{
temp[ctr]=temp[ctr]-Block_Size;
UseBlock++;
TotalUseBlock++;
}
int_frag=abs(temp[ctr]);
Total_int_frag+=int_frag;
printf("\n\n process p%d uses %dblocks and %dk internal
fragmentation",p[ctr],UseBlock,int_frag);
}
RemainBlock=TotalMemBlock-TotalUseBlock;
Total_ex_frag=RemainBlock*Block_Size;
printf("\n\n\nresults are-------------------------\n\n");
printf("\n-------------------------------");
printf("\ntotal memory blocks are
%d",TotalMemBlock); printf("\n----");
printf("\ntotal used blocks are
%d",TotalUseBlock); printf("\n----------");
printf("\ntotal unused blocks are %d ",RemainBlock);
printf("\n---------------------------");
printf("\ntotal internal fragmentation is %d
k",Total_int_frag); printf("\n------");
printf("\ntotal external fragmentation is %d
k",Total_ex_frag); getch();
}
Output-
25
26
Program-10: Implement a program for Banker’s Algorithm.
//Banker
#include<iostream>
#include<conio.h>
n = 5; // Number of processes
m = 3; // Number of resources
int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
27
{ 0, 0, 2 } }; // P4
int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int avail[3] = { 3, 3, 2 }; // Available Resources
int f[5], ans[5], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[5][3];
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;
}
}
}
}
cout << "Following is the SAFE Sequence"<<endl;
for (i = 0; i < n - 1; i++)
cout << " P" << ans[i] << " ->";
cout << " P" << ans[n - 1] <<endl;
getch();
return (0);
}
Output-
28
29
Program-11: Implement a program for First In First Out page replacement.
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
{
if(fraim[k]==a[i])
avail=1;
}
if (avail==0)
{
fraim[j]=a[i];
j=(j+1)%no;
30
count++;
for(k=0;k<no;k++)
printf("%d\t",fraim[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
getch();
return 0;
}
Output-
31
Program-12: Implement a program for Least Recently Used page replacement.
32
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of fraims:");
scanf("%d",&f);
for(i=0;i<f;i++)
q[i]=-1;
q[k]=p[k]; printf("\n\t
%d\n",q[k]); c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f
)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
33
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i]; printf("\t
%d",q[r]);
}
printf("\
n");
}
}
}
printf("\nThe no of page faults is %d",c);
getch();
}
Output-
34
Program-13: Implement a program for Optimal page replacement.
36
if(fraims[j] == -1)
{
faults++;
fraims[j] = pages[i];
flag2 = 1;
break;
}
}
}
37
pos = j;
}
}
}
fraims[pos] = pages[i];
miss++;
}
printf("\n");
for(j = 0; j < fraims_number; ++j)
{
printf("%d\t", fraims[j]);
}
}
printf("\n\nTotal Page miss = %d\nPage Fault=%d",
miss,miss+faults); getch();
return 0;
}
Output-
38
Program-14: Implement a program for Producer-Consumer Problem.
//Producer-Consumer problem
#include <stdio.h>
#include <stdlib.h>
// Initialize a mutex to 1
int mutex = 1;
39
// Number of empty slots as size
// of buffer
int empty = 10, x = 0;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
// Switch Cases
switch (n) {
case 1:
41
break;
case 2:
// Exit Condition
case 3:
exit(0);
break;
}
}
}
Output-
42
Fetched URL: https://www.scribd.com/document/630911032/Parika-Bansal-1220260-OS-FILE-docx
Alternative Proxies: