0% found this document useful (0 votes)
7 views

Operating System Codes

Hi

Uploaded by

shamim20517
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Operating System Codes

Hi

Uploaded by

shamim20517
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 32

1.

non preemptive priority scheduling program in c


#include<stdio.h>

int process[1000],priority[1000],arr[1000],burst[1000];

void sort(int n)

for(int i=0;i<n-1;i++)

for(int j=0;j<n-i-1;j++)

if(arr[j]>arr[j+1])

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

temp=process[j];

process[j]=process[j+1];

process[j+1]=temp;

temp=priority[j];

priority[j]=priority[j+1];

priority[j+1]=temp;

temp=burst[j];

burst[j]=burst[j+1];

burst[j+1]=temp;
}

int main()

int n,i;

printf("Enter number of process : ");

scanf("%d",&n);

printf("enter process number , priority , arrival time , burst time\n");

for(i=0;i<n;i++)

scanf("%d%d%d%d",&process[i],&priority[i],&arr[i],&burst[i]);

sort(n);

int vis[n+1];

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

int com[n],turn[n],wt[n];

com[0]=arr[0]+burst[0];

vis[0]=-1;

int prev=com[0];

for(i=1;i<n;i++)

{
int mx=1000000;

int pos=-1;

for(int j=0;j<n;j++)

if(vis[j]==-1)continue;

if(prev>=arr[j])

if(priority[j]<mx)

mx=priority[j];

pos=j;

if(pos!=-1)

com[pos]=prev+burst[pos];

vis[pos]=-1;

prev=com[pos];

else

for(int j=0;j<n;j++)

{
if(vis[j]==0)

com[j]=arr[j]+burst[j];

prev=com[j];

break;

double avet=0.0;

double avew=0.0;

for(i=0;i<n;i++)

turn[i]=com[i]-arr[i];

avet+=turn[i];

wt[i]=turn[i]-burst[i];

avew+= wt[i];

printf("process\t\tturnaround time\t\twaiting time\n");

for(i=0;i<n;i++)

printf("%d\t\t\t%d\t\t\t%d\n",process[i],turn[i],wt[i]);

}
printf("average turn around time : %.2lf\n",avet);

printf("average waiting time : %.2lf\n",avew);

return 0;

2. Round Robin scheduling algorithm with arrival time using c

#include<stdio.h>

int queue1[1000];

int process[1000+1],arr[1000+1],burst[1000+1];

int vis[1000+1];

void sort(int n)

for(int i=1; i<=n-1; i++)

for(int j=0; j<n-1-i; j++)

if(arr[j]>arr[j+1])

int temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

temp=process[j];

process[j]=process[j+1];

process[j+1]=temp;

temp=burst[j];

burst[j]=burst[j+1];
burst[j+1]=temp;

int main()

int timequan,n,i;

int queuepoint=0;

int queuepoindex=-1;

int starttime=0;

printf("Enter time quantum : ");

scanf("%d",&timequan);

printf("Enter number of process : ");

scanf("%d",&n);

printf("Enter process no , arrival time , burst time \n");

int comp[n+1];

int b[n+1];

for(i=1; i<=n; i++)

vis[i]=0;

scanf("%d%d%d",&process[i],&arr[i],&burst[i]);

b[i]=burst[i];

sort(n);
int count=n;

while(count>0)

for(i=1; i<=n; i++)

if(vis[i]==0)

if(starttime>=arr[i]&&burst[i]>0)

// printf("%d ",i);

vis[i]=-1;

queuepoindex++;

queue1[queuepoindex]=i;

if(queuepoint>queuepoindex)

starttime++;

continue;

int take=queue1[queuepoint];

queuepoint++;

if(burst[take]<=timequan&&burst[take]>0)

starttime+=burst[take];
//printf("%d %d %d\n",take,starttime,burst[take]);

comp[take]=starttime;

// printf("%d %d\n",comp[take],take);

burst[take]=0;

count--;

else

burst[take]-=timequan;

starttime+=timequan;

for(i=1; i<=n; i++)

if(vis[i]==0)

if(starttime>=arr[i]&&burst[i]>0)

vis[i]=-1;

queuepoindex++;

queue1[queuepoindex]=i;

queuepoindex++;

queue1[queuepoindex]=take;

int turn[n+1],wt[n+1];
double avet=0;

double avew=0;

for(i=1;i<=n;i++)

turn[i]=comp[i]-arr[i];

avet+=(double)turn[i];

wt[i]=turn[i]-b[i];

avew+=(double)wt[i];

printf("\nprocess\t\tturn arount time\t\twaiting time\n");

for(i=1;i<=n;i++)

printf("%d\t\t\t%d\t\t\t%d\n",process[i],turn[i],wt[i]);

avet/=n;

avew/=n;

printf("avarage turn around time :%d\n",avet);

printf("avarage waiting time :%d\n",avew);

return 0;

3. next fit algorithm using c


#include<stdio.h>

int main()

int n,m,i,j;

printf("Enter number of process : ");

scanf("%d",&n);

printf("enter number of block : ");


scanf("%d",&m);

printf("enter process : ");

int arr[n+1],brr[m+1];

int p[n+1];

for(int i=1;i<=n;i++)

scanf("%d",&arr[i]);

p[i]=-1;

printf("Enter blocks : ");

for(int i=1;i<=m;i++)

scanf("%d",&brr[i]);

int use[n+1];

for(i=1;i<=n;i++)

use[i]=-1;

int lastpos=1;

for(i=1;i<=n;i++)

int cnt=0;

j=lastpos;

while(1)

if(cnt>m)break;

if(j>m)j=1;
cnt++;

//printf("%d %d\n",i,j);

if(brr[j]>=arr[i]&&p[j]==-1)

p[i]=j;

brr[j]-=arr[i];

lastpos=j;

break;

j++;

for(i=1;i<=n;i++)

if(p[i]==-1)

printf("%d th is not allocated\n",i);

else

printf("%d th is allocated at %d \n",i,p[i]);

}
4. worst fit algorithm using c
#include<stdio.h>
int main()

int n,m,i,j;

printf("Enter number of process : ");

scanf("%d",&n);

printf("enter number of block : ");

scanf("%d",&m);

printf("enter process : ");

int arr[n+1],brr[m+1];

int p[n+1];

for(int i=1;i<=n;i++)

scanf("%d",&arr[i]);

p[i]=-1;

printf("Enter blocks : ");

for(int i=1;i<=m;i++)

scanf("%d",&brr[i]);

for(i=1;i<=n;i++)

int mi=-100000;

int pos=-1;

for(j=1;j<=m;j++)

if(brr[j]>=arr[i]&&p[j]==-1)

int x=brr[j]-arr[i];
if(x>mi)

pos=j;

mi=x;

brr[j]-=arr[i];

if(pos!=-1)

p[i]=pos;

pos=-1;

for(i=1;i<=n;i++)

if(p[i]==-1)

printf("%d th is not allocated\n",i);

else

{
printf("%d th is allocated at %d \n",i,p[i]);

return 0;

}
5, best fit algorithm using c
#include<stdio.h>

int main()

int n,m,i,j;

printf("Enter number of process : ");

scanf("%d",&n);

printf("enter number of block : ");

scanf("%d",&m);

printf("enter process : ");

int arr[n+1],brr[m+1];

int p[n+1];

for(int i=1;i<=n;i++)

scanf("%d",&arr[i]);

p[i]=-1;

printf("Enter blocks : ");

for(int i=1;i<=m;i++)

scanf("%d",&brr[i]);

}
for(i=1;i<=n;i++)

int mi=100000;

int pos=-1;

for(j=1;j<=m;j++)

if(brr[j]>=arr[i]&&p[j]==-1)

int x=brr[j]-arr[i];

if(x<mi)

pos=j;

mi=x;

brr[j]-=arr[i];

if(pos!=-1)

p[i]=pos;

pos=-1;

for(i=1;i<=n;i++)

{
if(p[i]==-1)

printf("%d th is not allocated\n",i);

else

printf("%d th is allocated at %d \n",i,p[i]);

return 0;

6. first fit algorithm using c

#include<stdio.h>

int main()

int n,m,i,j;

printf("Enter number of process : ");

scanf("%d",&n);

printf("enter number of block : ");

scanf("%d",&m);

printf("enter process : ");

int arr[n+1],brr[m+1];

int p[n+1];
for(int i=1;i<=n;i++)

scanf("%d",&arr[i]);

p[i]=-1;

printf("Enter blocks : ");

for(int i=1;i<=m;i++)

scanf("%d",&brr[i]);

int use[n+1];

for(i=1;i<=n;i++)

use[i]=-1;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

if(brr[j]>=arr[i]&&p[j]==-1)

p[i]=j;

brr[j]-=arr[i];

break;

for(i=1;i<=n;i++)
{

if(p[i]==-1)printf("Not allocated ");

else printf("%d ",p[i]);

}
7. round robin scheduling algorithm without arrival time using c

#include<stdio.h>

int main()

int quntum,i;

printf("Enter time quantum : ");

scanf("%d",&quntum);

printf("Enter number of process : ");

int n;

scanf("%d",&n);

int process[n],burst[n];

int queue[10000];

int posc[100000];

printf("enter process and burst time : \n");

for(i=0;i<n;i++)

scanf("%d%d",&process[i],&burst[i]);

queue[i]=burst[i];

posc[i]=process[i];

int pos=0;

int rem=n;
int com[n];

int anspos[n];

int k=0;

int time=0;

while(1)

if(rem<=0)break;

if(queue[pos]<=quntum&&queue[pos]>0)

anspos[k]=posc[pos];

time+=queue[pos];

com[k]=time;

queue[pos]=-1;

k++;

rem--;

pos++;

else

time+=quntum;

queue[pos]-=quntum;

queue[pos+rem]=queue[pos];

posc[pos+rem]=posc[pos];

pos++;

}
}

int turn[n];

int wt[n];

double avet=0.0;

double avew=0.0;

printf("position\t\tturnaround time\t\twaiting time : \n");

for(i=0;i<n;i++)

// printf("%d %d\n",anspos[i],com[i]);

turn[i]=com[i];

avet+=turn[i];

wt[i]=turn[i]-burst[anspos[i]-1];

avew+=wt[i];

printf("%d\t\t\t%d\t\t\t%d\n",anspos[i],turn[i],wt[i]);

avet/=n;

avew/=n;

printf("average turn around time : %.2lf\n",avet);

printf("average waiting time : %.2lf\n",avew);

8. SJF with arrival time

#include<stdio.h>

int process[100],arr[100],burst[100];

void sort(n)

{
for(int i=0;i<n-1;i++)

for(int j=0;j<n-1-i;j++)

if(arr[j]>arr[j+1])

int temp=burst[j];

burst[j]=burst[j+1];

burst[j+1]=temp;

temp=process[j];

process[j]=process[j+1];

process[j+1]=temp;

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

int main()

printf("enter number of process : ");

int n,i;

scanf("%d",&n);

int check[n];
int brr[n];

int burstemp[n];

printf("enter process , arrival time burst time : \n");

for(i=0;i<n;i++)

scanf("%d%d%d",&process[i],&arr[i],&burst[i]);

check[i]=process[i];

brr[i]=arr[i];

burstemp[i]=burst[i];

sort(n);

int j=0;

int time=0;

int com[n],turn[n],wt[n];

int ansproces[n];

int prev=0;

while(j<n)

int indx=-1;

int ans=0;

for(i=0;i<n;i++)

if(check[i]==-1)

continue;

if(arr[i]>time)

{
continue;

if(indx==-1)

ans=burst[i];

indx=i;

else

if(ans>burst[i])

ans=burst[i];

indx=i;

if(indx==-1)

time++;

prev=time;

continue;

ansproces[j]=process[indx];

com[j]=prev+burst[indx];
prev=com[j];

time=prev;

check[indx]=-1;

j++;

double averageturn=0.0;

double averagewaiting=0.0;

for(i=0;i<n;i++)

turn[i]=com[i]-brr[ansproces[i]-1];

// printf("%d \n",brr[i]);

averageturn+=(double)turn[i];

wt[i]=turn[i]-burstemp[ansproces[i]-1];

averagewaiting+=(double)wt[i];

printf("Process\t\tturn around time\t\t\waiting time\n");

for(i=0;i<n;i++)

printf("%d\t\t\t%d\t\t\t\t%d\n",ansproces[i],turn[i],wt[i]);

averageturn/=n;

averagewaiting/=n;

printf("average turn around time : %.2lf\n",averageturn);

printf("average waiting around time : %.2lf\n",averagewaiting);


return 0;

9. 2.FCFS with arrival time and burst time.c

#include<stdio.h>

int process[100];
int ar[100];
int burst1[100];
void sort(int n)
{
int i,j;
for(i=0;i<n-1;i++)
{

for(j=0;j<n-1-i;j++)
{
if(ar[j]>ar[j+1])
{

int temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;

temp=process[j];
process[j]=process[j+1];
process[j+1]=temp;

temp=burst1[j];
burst1[j]=burst1[j+1];
burst1[j+1]=temp;

}
}

}
int main()
{
printf("enter number of process : ");
int n;
scanf("%d",&n);
printf("\nenter process : \n");
for(int i=0;i<n;i++)
{
scanf("%d",&process[i]);
}
printf("\nenter arrival time : \n");
for(int i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("\nenter burst time : \n");
for(int i=0;i<n;i++)
{
scanf("%d",&burst1[i]);
}
// for(int i=0;i<n;i++)
// {
// printf("%d %d %d \n",process[i],ar[i],burst1[i]);
// }
// printf("\n");
sort(n);
// for(int i=0;i<n;i++)
// {
// printf("%d %d %d \n",process[i],ar[i],burst1[i]);
// }

int comp[n];
int turn[n];
int wt[n];
comp[0]=ar[0]+burst1[0];
for(int i=1;i<n;i++)
{
if(comp[i-1]<ar[i])
{
comp[i]=ar[i]+burst1[i];
}
else
{
comp[i]=comp[i-1]+burst1[i];
}
}
// calculation of turnaround time and waiting time
double avgturn=0.0;
double avgwt=0.0;
for(int i=0;i<n;i++)
{
turn[i]=comp[i]-ar[i];
wt[i]=turn[i]-burst1[i];
avgturn+=turn[i];
avgwt+=wt[i];
}

printf("Process\tturn_time\twaiting_time\n\n");
for(int i=0;i<n;i++)
{
printf("%d\t %d\t\t%d\n",process[i],turn[i],wt[i]);
}
avgturn/=n;
avgwt/=n;
printf("average turnaround time : %.2lf\n",avgturn);
printf("average waiting time : %.2lf\n",avgwt);

}
10. SJF(Without arrival time) implemention using C
#include<stdio.h>

int process[100],burst[100];
void sort(n)

for(int i=0;i<n-1;i++)

for(int j=0;j<n-1-i;j++)

if(burst[j]>burst[j+1])

int temp=burst[j];

burst[j]=burst[j+1];

burst[j+1]=temp;

temp=process[j];

process[j]=process[j+1];

process[j+1]=temp;

int main()

printf("enter number of process : ");

int n,i,j;

scanf("%d",&n);

printf("Enter process and burst time : \n");

for(i=0;i<n;i++)
{

scanf("%d%d",&process[i],&burst[i]);

sort(n);

int turn[n];

turn[0]=burst[0];

int wt[n];

wt[0]=0;

double avet=0.0;

double avew=0.0;

for(i=1;i<n;i++)

turn[i]=turn[i-1]+burst[i];

avet+=(double)(turn[i]);

wt[i]=turn[i]-burst[i];

avew+=(double)wt[i];

printf("process\t\tturn around time\t\t\twaiting time\n");

for(i=0;i<n;i++)

printf("%d\t\t\t%d\t\t\t\t\t%d\n",process[i],turn[i],wt[i]);

printf("\naverage turn around time is : %.2lf\n",avet);

printf("\naverage waiting time is : %.2lf\n",avew);


return 0;

11. 1.FCFS without arrival time


#include<stdio.h>

int main()
{
printf("Enter number of process : ");
int n;
scanf("%d",&n);
int burst[n];
printf("enter burst time : ");
for(int i=0;i<n;i++)
{
scanf("%d",&burst[i]);

}
int tur[n];
tur[0]=burst[0];
double avgtur=0.0,avgwt=0.0;
printf("turnaround time : \n");
printf("%d ",tur[0]);
avgtur+=tur[0];
for(int i=1;i<n;i++)
{
tur[i]=tur[i-1]+burst[i];
avgtur=avgtur+(double)tur[i];
printf("%d ",tur[i]);
}
int wt[n];
printf("\nwaiting time : \n");
for(int i=0;i<n;i++)
{
wt[i]=tur[i]-burst[i];
avgwt=avgwt+(double)wt[i];
printf("%d ",wt[i]);
}
avgtur/=(double)n;
avgwt/=(double)n;
printf("\naverage turnaround time : %.2lf\n",avgtur);
printf("average waiting time : %.2lf\n",avgwt);

return 0;
}

12. FIFO Page Replacement Algorithm in C

#include < stdio.h >


int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf(" Incoming \ t Frame 1 \ t Frame 2 \ t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}

13. optimal page replacement algorithm in c


#include <stdio.h>

int findOptimal(int pages[], int n, int frame[], int num_frames, int index) {
int farthest = index, pos = -1;
for (int i = 0; i < num_frames; i++) {
int j;
for (j = index; j < n; j++) {
if (frame[i] == pages[j]) {
if (j > farthest) {
farthest = j;
pos = i;
}
break;
}
}
if (j == n) {
return i; // If the page is not going to be used again
}
}
return (pos == -1) ? 0 : pos;
}

int optimalPageReplacement(int pages[], int n, int num_frames) {


int frame[num_frames], page_faults = 0;
for (int i = 0; i < num_frames; i++) {
frame[i] = -1; // Initialize frame array with -1 indicating empty frames
}

for (int i = 0; i < n; i++) {


int j;
for (j = 0; j < num_frames; j++) {
if (frame[j] == pages[i]) {
// Page is already in the frame, no page fault
break;
}
}

if (j == num_frames) {
// Page fault occurs
page_faults++;
if (i < num_frames) {
frame[i] = pages[i]; // Fill the empty frames first
} else {
// Find the optimal page to replace
int pos = findOptimal(pages, n, frame, num_frames, i + 1);
frame[pos] = pages[i];
}
}

// Print the current state of frames


printf("\nFrame after processing page %d: ", pages[i]);
for (int k = 0; k < num_frames; k++) {
if (frame[k] == -1)
printf("- ");
else
printf("%d ", frame[k]);
}
}

return page_faults;
}

int main() {
int n, num_frames;

printf("Enter number of pages: ");


scanf("%d", &n);

int pages[n];
printf("Enter the page reference string:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &pages[i]);
}

printf("Enter number of frames: ");


scanf("%d", &num_frames);
int page_faults = optimalPageReplacement(pages, n, num_frames);
printf("\n\nTotal number of page faults: %d\n", page_faults);

return 0;
}

You might also like