Program - 1: Write A Program To Implement First Come First Serve CPU Scheduling Algorithm

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

PROGRAM -1

Write a program to implement First Come First Serve CPU Scheduling Algorithm

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main(){
int i=1,j,n,a[10];
clrscr();
printf(Enter the no. of processes you want to execute);
scanf(%d,&n);
while(i!=n+1){
printf(\n Enter the duration for %dth process,i);
scanf(%d,&a[i-1]);
i++;
}
clrscr();
for(j=0;j<n;j++){
printf(\n \n %d process is in execution for %d sec,j+1,a[j]);
delay(a[j]*1000);
printf(\n\n%dth process has completed,j+1);
}
delay(1000);
}

1
PROGRAM -2
Write a program to implement Round Robin Algorithm

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main(){
int i=1,j,n,a[10],c[10],k,b=1;
clrscr();
printf(Enter the no. of processes you want to execute);
scanf(%d,&n);
while(i!=n+1){
printf(Enter the duration for %dh process,i);
scanf(%d,&a[i-1]);
i++;
)
while(b==1){
for(k=0;k<n;k++){
if(a[k]!=0){
b=1;
break;
}
else
b=0;
}
if(b==1){
for(j=0;j<n;j++){
if(a[j]!=0){
printf(\n %d process is in execution for 1 sec j+1);
delay(1000);
a[j]-=1;
}
}
}
k=0;
}
printf(\n\nAll the processes have completed);
delay(2000);
}

2
PROGRAM -3
Write a program to implement Priority CPU Scheduling Algorithm

/Assumption :Low value of priority means high priority of process

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include<iostream.h>
struct node
{ int pid;
int cpu_burst;
int priority;
node *next;
// node *prev;
};
void main()
{
node *start;
start->next=NULL;
// start->prev=start;
node *tracer=start;
int num;
clrscr();
cout<<"\nEnter the nunber of processes : ";
cin>>num;
cout <<"\nProcess\t CPU Burst\t Priority";
int row=5;
for(int i=0;i<num;i++)
{ node *temp=new node;
int col=1;
row++;
gotoxy(col,row);
cout<<"p"<<i;
temp->pid=i;
for(int j=0;j<2;j++)
{ col=col+15;
gotoxy(col,row);
if(j==0) cin>>temp->cpu_burst;
else cin>> temp->priority;
}
temp->next=NULL;
tracer->next=temp;
// temp->prev=tracer;
tracer=tracer->next;
}
tracer=start->next;
3
/* while(tracer!=NULL)
{
cout<<tracer->cpu_burst;
tracer=tracer->next;
}*/
//sort according to Priority value
//getch();
cout<<"\n\nHere is the GANTT CHART for Priority Scheduling.......\n\n\n\n\n";
while(start->next!=NULL)
{
tracer=start->next;
int i=0;
int smallval=tracer->priority;
int smallindex=i;
tracer=tracer->next;
while(tracer!=NULL)
{
i++;
if(tracer->priority<smallval)
{
smallval=tracer->priority;
smallindex=i;
}
tracer=tracer->next;
}
node *prev=start;
tracer=start->next;
for( i=0;i<smallindex;i++)
{
prev=prev->next;
tracer=tracer->next;
}
if(tracer!=NULL)
{
cout<<"|p"<<tracer->pid;
for(int k=0;k<tracer->cpu_burst;k++) cout<<" -";
// cout<<"\nDEL\t"<<tracer->pid<<" "<<tracer->arrival<<" "<<tracer->cpu_burst;
prev->next=tracer->next;
}
else
{
start->next=NULL;
}
}
cout<<"|";
getch();
}

4
PROGRAM- 4
Write a program to implement Shortest Job First CPU Scheduling Algorithm

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main(){

int i=1,j,n,a[10],c[10],k,b;
clrscr();
printf(Enter the no. of processes you want to execute);
scanf(%d,&n);
while(i!=n+1){
printf(Enter the duration for %dh process,i);
scanf(%d,&a[i-1]);
i++;
)
clrscr();
for(i=0;i<n;i++)
c[i]=a[i];
for(i=0;i<n-1;i++)){
for(k=i+1;k<=n-1;k++){
if(a[i]>a[k]){
b=a[i];
a[i]=a[k];
a[k]=b;
}
}
}
for(i=0;i<n;i++)){
for(j=0;j<n;j++){
if(a[i]==c[j])
break;
}
printf(\n %d process is in execution for %d sec j+1,a[i]);
delay(a[i]*1000);
printf(\n\n%dth process has completed,j+1);
}
delay(1000);
}

5
PROGRAM- 5

Write a program to implement FCFS Disk Scheduling Algorithm

#include<graphics.h>
#include<conio.h>
void main(){
int gd=DETECT,gm,h,a[5],i,s;
flaot x=10,y;
initgraph(&gd,&gm, );
printf(Enter head Location (cylinder size =0 to 199));
scanf(%d,&h);
printf(Enter the sequence of sectorts);
for(i=0;i<5;i++)
scanf(%d,&a[i]);
cleardevice();
line(20,10,20,410);
moveto(20,410);
lineto(420,410);
moveto(20,410-h*2);
s=abs(h-a[0]);
for(i=0;i<5;i++){
y=a[i];
if(i!=4)
s=s+abs(a[i+1]-a[i]);
x=x+y/2;
setcolor(i+3);
lineto(x,410-a[i]*2);
moveto(x,410-[i]*2);
}
gotoxy(40,25);
print(SEEK TIME=%d,s);
getch();
}

6
PROGRAM- 6

Write a Program to implement Shortest Seek Time First disk scheduling algorithm

#include<graphics.h>
#include<conio.h>
void main(){
int gd=DETECT,gm,h,a[5],i,j,s,z=200,t,b[5],k,p;
float x=10,y;
initgraph(&gd,&gm, );
printf(Enter head Location (cylinder size =0 to 199));
scanf(%d,&h);
s=h;
printf(Enter the sequence of sectorts);
for(i=0;i<5;i++)
scanf(%d,&a[i]);
cleardevice();
line(20,10,20,410);
moveto(20,410);
lineto(420,410);
moveto(20,410-h*2);
for(k=0;k<4;k++){
z=200;
for(i=0;i<=4;i++){
if(z>abs(s-a[i])){
z=abs(s-a[i]);
b[k]=a[i];
}}
for(p=0;p<5;p++){
if(a[p]==b[k])
a[p]=0;
}
s=b[k];
}
for(i=0;i<5;i++)
if(a[i]!=0)
b[k]=a[i];
t=abs(h-b[0]);
for(i=0;i<5;i++){
y=b[i];
if(i!=4)
t=t+abs(b[i+1]-b[i]);
x=x+y/2;
setcolor(i+3);
lineto(x,410-b[i]*2);
moveto(x,410-b[i]*2);
}
gotoxy(40,25);
printf(SEEK TIME =%d,t);
getch();}

7
PROGRAM -7

Write a program to implement SCAN disk scheduling algorithm

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#include<process.h>
struct process
{
int p;
}
void main(){
int start,in,I,j,k,t,l=210,gd=DETECT,gm;
clrscr();
struct process pr[10];
for(i=0;i<5;i++){
printf(\n enter the time for process);
scanf(%d,&pr[i].p);
}
for(i=0,i<5;i++){
for(j=0;j<4-i;j++){
if(pr[j].p>pr[j+1].p){
t=pr[j].p;
pr[j].p=pr[j+1].p;
pr[j+1].p=t;
}
}
}
for(i=0;i<5;i++){
if(pr[i].p==start){
in=i;
break;
}
}
printf(\n\n the order of execution of process are\n);
for(i=in;i>0;i--)
printd(\t %d,pr[i].p);
for(i=in+1;i<5;i++)
printf(\r %d,pr[i].p);
getch();
clrscr();
initgraph(&gd,&gm,C:\\tc\\bgi);
line(10,10,10,400);
line(10,400,400,400);
moveto((start*10),200);
setcolor(RED):
circle(start*10,200,5);
for(i=in-1;i>=0;i--){

8
setcolor(WHITE);
lineto(pr[i].p*10,1);
setcolor(RED);
circle(pr[i].p*10,1,5);
l+=10;
}
setcolor(WHITE);
lineto(10,l);
setcolor(RED);
circle(10,1,5);
l+=10;
for(i=in+1;i<5l;i++){
setclor(WHITE);
lineto(pr[i].p*10,l;
setcolor(RED);
circle(pr[i].p*!00,1,5):
l+=10;
}
getch();
}

9
PROGRAM -8

Write a program to implement C-SCAN disk scheduling algorithm

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include<iostream.h>
#include<process.h>
void main()
{
clrscr();
int req[50],num,i;
cout<<".......This algorithm assumes the disk has tracks range form 0-199........";
cout<<"\n\nEnter the number of requests to be serviced : ";
cin>>num;
cout<<"\n\n Enter the Requests \n\n";
int row=10,col=-2;
for(i=0;i<num;i++)
{
col+=6;
gotoxy(col,row);
cin>>req[i];
if(req[i]<0 || req[i]>=200)
{
cout<<"\n\nSystem has encountered invalid request .....\n TRY LATER !!!!!!quitting
...";
cout<<"Press any key....";
getch();
exit(0);
}
}
int head;
cout<<"Enter Currrent location of head : <0-199>\n\n";
cin>>head;
if(head<0 || head>=200)
{
cout<<"\n\nSystem has encountered invalid request .....\n TRY LATER !!!!!!quitting
...";
cout<<"Press any key....";
getch();
exit(0);
}

cout<<"\n\n Requests serviced by SCAN algorithm \n\n";


cout<<"||";
for(i=head;i<=199;i++)
{
for(int j=0;j<num;j++)
{

10
if(req[j]==i && req[j]!=200)
{
cout<<"<--->" <<i;
req[j]=200;
}
}
}
for(i=0;i,head;i++)
{
for(int j=0;j<num;j++)
{
if(req[j]==i && req[j]!=200)
{
req[j]=200;
cout<<"<--->" <<i;
}
}
}

cout<<"||";

//}
getch();
}

11
PROGRAM-9

Write a program to implement C-LOOK disk scheduling algorithm

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include<iostream.h>
#include<process.h>
void main()
{
clrscr();
int req[50],num,i;
cout<<".......This algorithm assumes the disk has tracks range form 0-199........";
cout<<"\n\nEnter the number of requests to be serviced : ";
cin>>num;
cout<<"\n\n Enter the Requests \n\n";
int row=10,col=-2;
for(i=0;i<num;i++)
{
col+=6;
gotoxy(col,row);
cin>>req[i];
if(req[i]<0 || req[i]>=200)
{
cout<<"\n\nSystem has encountered invalid request .....\n TRY LATER !!!!!!quitting
...";
cout<<"Press any key....";
getch();
exit(0);
}
}
int head;
cout<<"Enter Currrent location of head : <0-199>\n\n";
cin>>head;
if(head<0 || head>=200)
{
cout<<"\n\nSystem has encountered invalid request .....\n TRY LATER !!!!!!quitting
...";
cout<<"Press any key....";
getch();
exit(0);
}

cout<<"\n\n Requests serviced by SCAN algorithm \n\n";


cout<<"||";
//Find the lowest and highest request
int high=0,low=0;
for(int k=1;k<num;k++)
{

12
if(req[k]<req[low]) low=k;
if(req[k]>req[high]) high=k;
}
// cout<<"\n==="<<req[low]<<"==="<<req[high]<<"\n";

for(i=head;i<=req[high];i++)
{
for(int j=0;j<num;j++)
{
if(req[j]==i && req[j]!=200)
{
cout<<"<--->" <<i;
req[j]=200;
}
}
}
for(i=req[low];i<head;i++)
{
for(int j=0;j<num;j++)
{
if(req[j]==i && req[j]!=200)
{
req[j]=200;
cout<<"<--->" <<i;
}
}
}

cout<<"||";

//}
getch();
}

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy