Professional Documents
Culture Documents
Aim: - Write A Program To Implement FCFS CPU Scheduling: Algorithm
Aim: - Write A Program To Implement FCFS CPU Scheduling: Algorithm
Aim: - Write A Program To Implement FCFS CPU Scheduling: Algorithm
algorithm.
Explanation: - The simplest CPU scheduling algo. Is first-come, first-served (FCFS) scheduling
algorithm. With this scheme, the processes that request the CPU first is allocated the CPU
first.Ex.-
P1 4
P2 5
P3 2
Gantt Chart
P1 P2 P3
0 4 9 11
Calculation
P1 0-0=0 4-0=0
P2 4-0=4 9-0=9
P3 9-0=9 11-0=0
#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
_setcursortype(_NOCURSOR);
sleep(2); textcolor(15);
textcolor(15);
gotoxy(37, 13); cprintf("G"); delay(100);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
clrscr(); textcolor(15);
gotoxy(28, 13); cprintf("C");
delay(100);
clrscr();
menu_back:
textcolor(8);
gotoxy(1, 25); cprintf("Best viewed in maximize window");
textcolor(15);
menu=getch();
if(menu=='E' || menu=='e')
clrscr();
textcolor(8);
gotoxy(lo,14);cprintf("ÛÛ");
textcolor(15);
gotoxy(28,12);cprintf("initializing schedule...");
gotoxy(28,13);cprintf("%d%", lo);
gotoxy(di,14);cprintf("ÛÛ");
delay(200);
sleep(2);
goto go;
goto credits;
else
goto menu_back;
go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf("ow many processes would you like to put? (1-9)");
textcolor(15);
clrscr();
if(proc=='1')
pro=1;
else if(proc=='2')
pro=2;
else if(proc=='3')
pro=3;
else if(proc=='4')
pro=4;
else if(proc=='5')
pro=5;
else if(proc=='6')
pro=6;
else if(proc=='7')
pro=7;
else if(proc=='8')
pro=8;
else if(proc=='9')
pro=9;
else
goto back;
_setcursortype(_SOLIDCURSOR);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
y+=2;
textcolor(8);
y=5;
limits:
do{
textcolor(15);
if(proc=='1')
burst[burst2]=1;
else if(proc=='2')
burst[burst2]=2;
else if(proc=='3')
burst[burst2]=3;
else if(proc=='4')
burst[burst2]=4;
else if(proc=='5')
burst[burst2]=5;
else if(proc=='6')
burst[burst2]=6;
else if(proc=='7')
burst[burst2]=7;
else if(proc=='8')
burst[burst2]=8;
else if(proc=='9')
burst[burst2]=9;
else
textcolor(8);
goto limits; }
burst2++;
y+=2;
z++;}
while(z<=pro);
_setcursortype(_NOCURSOR);
gotoxy(26,25);cprintf("...");
textcolor(8);
textcolor(8);
gotoxy(2, 1); cprintf("he Gantt Char");
textcolor(15);
textcolor(8);
textcolor(8);
textcolor(15);
y+=7; }
textcolor(8);
y=1;
burst2=1;
{
textcolor(15);
gantt+=burst[burst2];
burst2++;
y+=7; }
gotoxy(77, 25);cprintf("...");
textcolor(8);
getch();
clrscr();
y=5;
textcolor(8);
textcolor(15);
y+=2; }
textcolor(8);
gantt=0;
burst2=1;
textcolor(15);
gantt2+=gantt;
gantt+=burst[burst2];
burst2++;
y+=2;
y=5;
burst2=1;
gantt=burst[burst2];
textcolor(15);
gantt3+=gantt;
burst2++;
gantt+=burst[burst2];
y+=2;
}
gotoxy(1, 25); cprintf("P");
gotoxy(26,25);cprintf("...");
textcolor(8);
getch();
clrscr();
textcolor(8);
textcolor(15);
gotoxy(77,25);cprintf("...");
textcolor(8);
getch();
goto end;
credits:
clrscr();
textcolor(8);
textcolor(15);
textcolor(8);
getch();
clrscr();
goto menu_back;
end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf("THE END!");
return 0;
Input: -
P1 4
P2 5
P3 2
Output:-
Gantt Chart
P1 P2 P3
0 4 9 11
P1 4
P2 5
P3 2
Gantt Chart
P1 P2 P3
0 2 6 11
Calculation
P1 2-0=2 6-0=6
P2 6-0=6 11-0=11
P3 0-0=0 2-0=2
Program
#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
_setcursortype(_NOCURSOR);
clrscr();
sleep(2);
textcolor(15);
gotoxy(44,13);cprintf("X");
delay(100);
textcolor(0);
gotoxy(44,13);cprintf(" ");
textcolor(15);
gotoxy(42,13);cprintf("O");
delay(100);
textcolor(0);
gotoxy(42,13);cprintf(" ");
textcolor(15);
gotoxy(41,13);cprintf("C");
delay(100);
textcolor(0);
gotoxy(41,13);cprintf(" ");
textcolor(15);
gotoxy(40,13);cprintf("I");
delay(100);
textcolor(0);
gotoxy(40,13);cprintf(" ");
textcolor(15);
gotoxy(39,13);cprintf("R");
delay(100);
textcolor(0);
gotoxy(39,13);cprintf(" ");
textcolor(15);
gotoxy(38,13);cprintf("N");
delay(100);
textcolor(0);
gotoxy(38,13);cprintf(" ");
textcolor(15);
gotoxy(37,13);cprintf("E");
delay(100);
textcolor(0);
gotoxy(37,13);cprintf(" ");
delay(100);
textcolor(15);
gotoxy(37,13);cprintf("E");
delay(100);
textcolor(8);
gotoxy(37,13);cprintf("E");
textcolor(15);
gotoxy(38,13);cprintf("N");
delay(100);
textcolor(8);
gotoxy(38,13);cprintf("N");
textcolor(15);
gotoxy(39,13);cprintf("R");
delay(100);
textcolor(8);
gotoxy(39,13);cprintf("R");
textcolor(15);
gotoxy(40,13);cprintf("I");
delay(100);
textcolor(8);
gotoxy(40,13);cprintf("I");
textcolor(15);
gotoxy(41,13);cprintf("C");
delay(100);
textcolor(8);
gotoxy(41,13);cprintf("C");
textcolor(15);
gotoxy(42,13);cprintf("O");
delay(100);
textcolor(8);
gotoxy(42,13);cprintf("O");
textcolor(15);
gotoxy(44,13);cprintf("X");
delay(100);
textcolor(8);
gotoxy(44,13);cprintf("X");
sleep(2);
clrscr();
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
textcolor(15);
delay(100);
gotoxy(39, 13); printf(" ");
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
gotoxy(33, 13); cprintf("C");
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
textcolor(15);
delay(100);
textcolor(8);
gotoxy(29, 13); cprintf("P");
textcolor(15);
delay(100);
textcolor(8);
sleep(2);
delay(100);
clrscr();
menu_back:
textcolor(8);
textcolor(15);
menu=getch();
if(menu=='E' || menu=='e')
clrscr();
textcolor(8);
gotoxy(lo,14);cprintf("ÛÛ");
textcolor(15);
gotoxy(28,12);cprintf("initializing schedule...");
gotoxy(28,13);cprintf("%d%", lo);
gotoxy(di,14);cprintf("ÛÛ");
delay(200);
sleep(2);
goto go;
}
goto credits;
else
goto menu_back;
go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf("ow many processes would you like to put? (1-9)");
textcolor(15);
clrscr();
if(proc=='1')
processno=1;
else if(proc=='2')
processno=2;
else if(proc=='3')
processno=3;
else if(proc=='4')
processno=4;
else if(proc=='5')
processno=5;
else if(proc=='6')
processno=6;
else if(proc=='7')
processno=7;
else if(proc=='8')
processno=8;
else if(proc=='9')
processno=9;
else
goto back;
_setcursortype(_SOLIDCURSOR);
textcolor(8);
textcolor(15);
textcolor(8);
textcolor(15);
gotoxy(32, y); cprintf("P%d", x);
y+=2;
textcolor(8);
y=5;
limits:
do
textcolor(15);
if(proc=='1')
burtym[burtymarr]=1;
else if(proc=='2')
burtym[burtymarr]=2;
else if(proc=='3')
burtym[burtymarr]=3;
else if(proc=='4')
burtym[burtymarr]=4;
else if(proc=='5')
burtym[burtymarr]=5;
else if(proc=='6')
burtym[burtymarr]=6;
else if(proc=='7')
burtym[burtymarr]=7;
else if(proc=='8')
burtym[burtymarr]=8;
else if(proc=='9')
burtym[burtymarr]=9;
else
goto limits;
burtymarr++;
y+=2;
z++;
while(z<=processno);
_setcursortype(_NOCURSOR);
gotoxy(26,25);cprintf("...");
textcolor(8);
getch();
clrscr();
y=4;
min=10000;
nodup=burtymarr;
min=burtym[burtymarr];
textcolor(8);
textcolor(15);
}
wtnttchart2[nodup]=ganttval;
wtadd+=wtnttchart2[nodup];
ganttval+=min;
textcolor(15);
y2+=7;
xg+=7;
xgv+=7;
nodup2[noduparr]=nodup;
wtnttchart[nodup]=ganttval;
ttadd+=wtnttchart[nodup];
noduparr++;
textcolor(8);
textcolor(8);
textcolor(15);
gotoxy(77, 25);cprintf("...");
textcolor(8);
getch();
clrscr();
y=5;
textcolor(8);
textcolor(15);
y+=2;
textcolor(8);
textcolor(15);
y=5;
y+=2;
}
y=5;
y+=2;
gotoxy(26,25);cprintf("...");
textcolor(8);
getch();
clrscr();
textcolor(8);
textcolor(15);
textcolor(8);
getch();
goto end;
credits:
clrscr();
textcolor(8);
textcolor(15);
getch();
clrscr();
goto menu_back
end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf("THE END!");
return 0;
Input: -
Ex.-
P1 4
P2 5
P3 2
Output:
Gantt Chart
P1 P2 P3
0 2 6 11
Aim: - Write a program to implement Round-Robin (RR) CPU
scheduling lgorithm.
Explanation: -The RR CPU Scheduling algorithm is designed especially for time sharing systems.
It is similar to FCFS scheduling, but preemption is added to switch between processes. A small
unit of time called time quantum. Ex.-
P1 4
P2 5
Gantt Chart
P1 P2 P1 P2 P2
0 2 4 6 8 9
Calculation
P1 (0-2)+(2-6)=6 6-0=6
P2 (2-4)+(4-8)+(8-9)=7 7-0=7
Program: -
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#define N 100
float burst;
float arrival;
int priority;
int num;
}p[20],temp;
struct ssss
int no;
int xx;
float arr;
float burst;
float ww;
int pri;
float wait;
}process[N];
void main()
{
void roundrobin(int);
char msg[80];
errorcode = graphresult();
getch();
scanf("%d",&n);
for(i=0;i<n;i++)
{
gotoxy(22,2);
printf("PROCESS:%d\n",i+1);
scanf("%f",&p[i].burst);
scanf("%f",&p[i].arrival);
printf("PRIORITY :\t");
scanf("%d",&p[i].priority);
p[i].num = i+1;
printf("\n\nINVALID INPUT\n");
i--;
getch();
cleardevice();
for(i=0;i<n;i++)
for(j=i;j<n;j++)
temp = p[i];
p[i] = p[j];
p[j] = temp;
cleardevice();
gotoxy(22,2);
roundrobin(n);
getch();
void roundrobin(int n)
int i,j,l=0,pr[30];float
tot[10]={1,1,1,1,1,1,1,1,1,1},s[10]={0,0,0,0,0,0,0,0,0,0},c=1,y=0;
float q,awt,total=0,wt[10]={0,0,0,0,0,0,0,0,0,0},g[400];
cleardevice();
printf("Enter the time quantum: ");
scanf("%f",&q);
for(i=0;i<n;i++)
tot[i] = p[i].burst;
g[y++] = total;
g[y++] = 0;
//time quantum
g[y++] = p[i].num;
i++;
else
i=0;
c++;
else
g[y++] = p[i].num;
i++; //process
if(total>=p[i+1].arrival)
i++;
else
i=0;
}
}
else
i++;
if(i<=n)
g[y++] = 0;
g[y++] = 0;
g[y++] = p[i-1].num;
q=0;
for(i=0;i<n;i++) //calculates average waiting time
//printf("%d. %f\n",p[i].num,wt[i]);
q = q + wt[i];
awt = q/n;
graphics(&wt,awt,n,&g,y,&pr,l);
getch();
int i,msg1=0,j=0,u=0;
char msg[30];
float left,right,top,bottom,awd=0,turn=0,d=0;
getch();
cleardevice();
setlinestyle(4, 1, 1);
setfillstyle(10,1);
bar3d(0,0,620,340,0,0);
line(2,2,2,338);
line(2,338,618,338);
line(618,338,618,2);
setlinestyle(0,1,1);
left = 0;
top = 40;
bottom = 80;
right = 15;
for(i=0;i<y;i++)
left = right;
right = *(g+i);
bar3d(left,top,right,bottom,0,0);
sprintf(msg,"%2.1f",*(g+i));
outtextxy(right,(82+msg1),msg);
i++;
setfillstyle(1,*(g+i));
if(right>=600)
right = 0;
top = 120;
bottom = 160;
i--;
outtextxy(right,164,msg);
i++;
delay(500);
}
setcolor(CYAN+BLINK);
outtextxy(20,30,msg);
for(i=0;i<n;i++)
d = d + p[i].burst;
awd = d/n;
outtextxy(300,30,msg);
setcolor(WHITE);
sprintf(msg,"PROCESS");
outtextxy(32,210,msg);
sprintf(msg,"WAITING TIME");
outtextxy(190,210,msg);
outtextxy(340,210,"COLORS");
outtextxy(410,210,"IDLE");
setfillstyle(1,0);
bar3d(455,206,515,216,0,0);
for(i=0;i<n;i++)
{
sprintf(msg,"%d.",p[i].num);
outtextxy(48,226+(i*17),msg);
sprintf(msg,"%f",*(wt+i));
outtextxy(208,226+(i*17),msg);
if(i==6)
break;
//getch();
//cleardevice();
/*for(i=7;i<n;i++)
sprintf(msg,"%d.",p[i].num);
outtextxy(48,226+((i-7)*17),msg);
sprintf(msg,"%f",*(wt+i));
outtextxy(208,226+((i-7)*17),msg);
} */
for(i=0;i<n;i++)
setfillstyle(1,p[i].num);
bar3d(330,225+(i*17),390,235+(i*17),0,0);
if(i==6)
break;
settextstyle(TRIPLEX_FONT, HORIZ_DIR,2 );
outtextxy(50,0,"GANTT CHART");
if(n>=7)
getch();
cleardevice();
settextstyle(DEFAULT_FONT,HORIZ_DIR,0);
sprintf(msg,"PROCESS");
outtextxy(32,10,msg);
sprintf(msg,"WAITING TIME");
outtextxy(190,10,msg);
outtextxy(340,10,"COLORS");
outtextxy(410,10,"IDLE");
setfillstyle(1,0);
bar3d(455,6,515,16,0,0);
for(i=7;i<n;i++)
sprintf(msg,"%d.",p[i].num);
outtextxy(48,26+((i-7)*17),msg);
sprintf(msg,"%f",*(wt+i));
outtextxy(208,26+((i-7)*17),msg);
for(i=7;i<n;i++)
setfillstyle(1,p[i].num);
bar3d(330,25+((i-7)*17),390,35+((i-7)*17),0,0);
}
}
getch();
Input: -
P1 4
P2 5
Output: -
P1 (0-2)+(2-6)=6 6-0=6
P2 (2-4)+(4-8)+(8-9)=7 7-0=7
Explanation: - - The algorithm Associates with each process the length of the letter’s next CPU
burst. When the CPU is available, it is assigned to the process that has the highest priority.
Program:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
struct process
int process_no;
int burst_time;
int priority;
int flag;
}typedef pr;
pr* create();
int i,no,choise,time_quantum,temp=0;
void main()
pr *head,*head1,*head2,*head3,*head4,*sjf;
pr *p,*q;
head=create();
head1=copy_list(head);
head2=copy_list(head);
head3=copy_list(head);
head4=copy_list(head);
clrscr();
for(q=head1;q!=NULL;q=q->next)
display(head1);
priority_scheduling(head3);
pr * create()
pr *head,*prev;
scanf("%d",&no);
p->process_no=1;
p=(pr *)malloc(sizeof(pr));
scanf("%d",&p->burst_time);
p->process_no=1;
p->flag=1;
head->prev=p;
for(i=1;i<no;i++)
p=(pr *)malloc(sizeof(pr));
scanf("%d",p->burst_time);
p->process_no=i+1;
p->flag=1;
prev->next=p;
prev=p;
p->next=NULL;
return head;
char s1[2],s3[2];
int gd=DETECT,gm,x=10;
float num=0;
initgraph(&gm,&gd,"c:\\tc\\bgi");
printf("\n\tProcess\tBurst Time");
for(q=hend1;q!=NULL;q=q->next)
settextstyle(1,0,2);
outtextxy(10,250,"GANTT CHART");
outtextxy(10,360,"0");
temp=0;
for(q=head1;q!=NULL;q=q->next)
rectangle(x,300,x+50,q->burst_time,350);
outtextxy(x+20,320,"P");
itoa(q->process_no,s1,10);
outtextxy(x+35,320,s1);
x=x+50+q->burst_time;
temp+=q->burst_time;
itoa(temp,s3,10);
outtextxy(x+10,500,s3);
if(q->next!=NULL)
sum+=temp;
getch();
closegratph();
for(q=head;q!=NULL;q=q->next)
scanf("%d",&q->priority);
for(p=head;p->next!NULL;p=p->next)
for(q=p->next;q!=NULL;q=q=->next)
if(p->priority>q->priority)
temp=p->burst_time;
p->bursttime=q->burst_time;
q->burst_time=temp;
temp=p->process_no;
p->process_no=q->process_no;
q->process_no=temp;
}
display(head);
pr *copy_list(pr *first)
pr *head1,*r,*prev;
r->burst_time=first->burst_time;
r->process_no=q->burst_time;
r->flag=1;
prev->next=r;
for(q=first->next;q!=NULL;q=q->next)
r=(pr *)malloc(sizeof(pr));
r->burst_time=q->burst_time;
r->process_no=q->process_no;
r->flag=1;
prev->next=r;
prev=r;
r->next=NULL;
return head1;
}
Input: -
GANTT CHART
P3 P2 P1
0 3 6 30
void main()
int x;
// freopen(“in.cpp”,”r”,stdin);
while(1)
{
clrscr();
printf(“\n\n\t-----MENU----“);
switch(x)
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define min 10
int ref[max],count,frame[min],n;
void input()
{
count=0;
scanf(“%d”,&n);
scanf(“%d”,&temp);
while(temp!=-1)
{
ref[count++]=temp;
scanf(“%”,&temp);
}
}
void FIFO()
{
clrscr();
for(i=0;i<n;i++)
frame[i]=-1;
for(i=0;i<count;i++)
{
for(j=0;j<n;j++)
if(frame[j]==ref[i])
break;
if(j==n)
frame[fault%n]=ref[i],fault++;
for(j=0;j<n;j++)
printf(“%d”,frame[j]);
getch();
}
getch();
void LRU()
int i,j,k,stack[min],top=0,fault=0;
clrscr();
for(i=0;i<count;i++)
{
if(top<n)
stack[top++]=ref[i],fault++;
else
{
for(j=0;j<n;j++)
if(stack[j]==ref[i])
break;
if(j<n)
{
for(k=j;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
}
else
{
for(k=0;k<n-1;k++)
stack[k]=stack[k+1];
stack[k]=ref[i];
fault++;
}
for(j=0;j<top;j++)
getch();
}//for i
getch();
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int ref[max],count,frame[min],n;
void input()
int i,temp;
clrscr();
count=0;
scanf(“%d”,&n);
scanf(“%d”,&temp(;
while(temp!=-1)
ref[count++]=temp;
scanf(“%d,”temp);
void main()
intx;
//freopen(in.cpp”,”r”,stdin);
while(1)
clrscr();
printf(\n\n\t-----MENU-----“);
switch(x)
case2:
FIFO();
break;
case4:
LRU();
break;
case0;
exit(0);