NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

1. TO SHOW THE IMPLEMENTATION OF FORK ( ) ALGORITHM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main () {
pid_t pid;
int status;
pid = fork(); // Create a child process
if (pid < 0) { // Error occurred
fprintf(stderr, "Fork failedn");
exit(1);
} else if (pid == 0) { // Child process
printf("Child process - pid: %dn", getpid());
exit(0);
} else { // Parent process
printf("Parent process - pid: %dn", getpid());
wait(&status); // Wait for child to terminate
printf("Child exited with status %dn", status);
}
return 0;
}

2. A program to implement First Come First Served (FCFS) algorithm.

#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("nEnter Process Burst Timen");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("nProcessttBurst Timet Waiting Timet Turnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d]tt%dtt%dtt%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("nnAverage Waiting Time:%d",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0;
}

3. A program to implement Shortest Job First (SJF) algorithm.

#include<stdio.h>
#include<conio.h>
int main()
{
int n, BT[10], WT, TAT=0, i, j, temp;
float AWT, TWT=0, ATAT, TTAT=0;
printf("Enter total number of process:t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Burst time of process %d:t",i+1);
scanf("%d",&BT[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(BT[i]>BT[j])
{
temp=BT[i];
BT[i]=BT[j];
BT[j]=temp;}
}
}
printf("nProcessttBTttWTttTAT");
for(i=0;i<n;i++)
{
TAT=BT[i]+WT;
printf("n%dtt%dtt%dtt%d",i+1,BT[i],WT,TAT);
TWT=TWT+WT;
TTAT=TTAT+TAT;
WT=WT+BT[i];
}
AWT=TWT/n;
ATAT=TTAT/n;
printf("nAverage waiting time:%f",AWT);
printf("nAverage Turn around time:%f",ATAT);
return 0;
}

4. Write a program to show the implementation of Round Robin (RR) algorithm.

#include<stdio.h>
#include<conio.h>
int main()
{
int count, n, time, remain, flag=0, time_quantum;
int wait_time=0, turnaround_time=0, at[10], bt[10], rt[10];
printf("Enter total process:t");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival time for process %d:t",count+1);
scanf("%d",&at[count]);
printf("Enter Burst time for process %d:tt",count+1);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter quantum time:t");
scanf("%d",&time_quantum);
printf("nProcessttTurn Around TimettWaiting Timen");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum&&rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0&&flag==1)
{
remain--;
printf("P[%d]tt%dtttt%dn",count+1, time-at[
count], time- at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else count=0;
}
printf("nAverage waiting time=%fn",wait_time*1.0/n);
printf("Average turnaround time=%f",turnaround_time*1.0/n);
return 0;
}

5. WAP to find the page fault that can occur in a given reference string using FIFO page replacement algorithm.

#include<stdio.h>
#include<stdlib.h>
int n,ref[100],fs,frame[100],count=0;
int f=-1, r=-1;
void input();
void show();
void cal();
void queue(int k);
int main()
{
printf("*********FIFO Page Replacement Algorithm*********n");
input();
cal();
show();
return 0;
}void input()
{
int i;
printf("Enter the no. of pages in Reference string:-t");
scanf("%d",&n);
printf("Enter Reference string:-n");
for(i=0;i<n;i++)
{
scanf("%d",&ref[i]);
}
printf("Enter the frame size:-t");
scanf("%d",&fs);
}
void cal()
{
int i;
for(i=0;i<fs;i++)
{
frame[i]=-1;
}
for(i=0;i<n;i++)
{
queue(ref[i]);
}
}

void queue(int k)
{
int i;
for(i=0;i<fs;i++)
{
if(frame[i]==k)
{
return;
}
}
if(r==fs-1)
{
r=0;
}
else if(r==-1)
{
f=0;
r=0;
}
else
{
r=r+1;
}
frame[r]=k;
count++;
}
void show()
{
printf("Page faults=%d",count);
}


6. WAP to find the page fault that can occur in a given reference string using LRU page replacement algorithm.
#include<stdio.h>
#include<conio.h>
int n, ref[100], fs, frame[100], count=0;
void input();
void show();
void cal();
int main()
{
printf("*****************LRU Page Replacement Algorithm*******************n");
input();
cal();
show();
getch();
return 0;
}
void input()
{
int i;
printf("Enter no. of pages in reference string:-t");
scanf("%d",&n);
printf("Enter the reference string:-n");
for(i=0;i<n;i++)
scanf("%d",&ref[i]);
printf("Enter the frame size:-t");
scanf("%d",&fs);
}
void cal()
{
int i, j, k=0, c1, c2[100], r, temp[100], t;
frame[k]=ref[k];
count++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<fs;j++)
{
if(ref[i]!=frame[j])
c1++;
}
if(c1==fs)
{
count++;
if(k<fs)
{
frame[k]=ref[i];
k++;
}
else
{
for(r=0;r<fs;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(frame[r]!=ref[j])
c2[r]++;
else
break;
}
}
for(r=0;r<fs;r++)
temp[r]=c2[r];
for(r=0;r<fs;r++)
{
for(j=r;j<fs;j++)
{
if(temp[r]<temp[j])
{
t=temp[r];
temp[r]=temp[j];
temp[j]=t;
}
}
}
for(r=0;r<fs;r++)
{
if(c2[r]==temp[0])
frame[r]=ref[i];
}
}
}
}
}

void show()
{
printf("Page faults = %d",count);
}


7. WAP to find the page fault that can occur in a given reference string using Optimal page replacement algorithm.

#include<stdio.h>
#include<conio.h>
int n;
int findmax(int a[])
{
int max, i, k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
int main()
{
int seq[30], fr[5], pos[5], find, flag, max, i, j, m, k, t, s;
int count=1, pf=0, p=1;
printf("Length of reference string:-t");
scanf("%d",&max);
printf("nThe reference string:-n");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("nNumber of frames:-t");
scanf("%d",&n);

fr[0]=seq[0];
pf++;
printf("%dt",fr[0]);
i=1;

while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%dt",fr[count]);
count++;
pf++;
}
i++;
}
printf("n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k;
break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%dt",fr[k]);
pf++;
printf("n");
}
}
printf("nThe no. of page faults are %d",pf);
getch();
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.