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

Disk Scheduling and Cpu Scheduling

round robin,priority scheduling preemptive,srt,fifo,priority scheduling,sjf,fcfs,lru

Uploaded by

Vishaldeep Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
211 views

Disk Scheduling and Cpu Scheduling

round robin,priority scheduling preemptive,srt,fifo,priority scheduling,sjf,fcfs,lru

Uploaded by

Vishaldeep Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include<stdio.

h>
#include<conio.h>
void roundrobin();
void prioritypre();
void srt();
void fifo();
void prioritynonpre();
void sjf();
void fcfs();
void lru();
int main()
{
int choice1,choice2,choice3,choice4,choice5;
// clrscr();
while(1)
{
//clrscr();
printf("Enter your choice : \n1 for CPU Scheduling algorithms \n
2 for Memory Management algorithms \n0 for EXIT \n Enter : ");
scanf("%d",&choice1);
if(choice1 == 1)
{
//clrscr();
printf("Enter your choice : \n1 for Pre-emptive \n2 for Non-Pree
mptive \n0 to GO BACK \n Enter : ");
scanf("%d",&choice2);
if(choice2 == 1)
{
//clrscr();
printf("Enter your choice : \n1 for Round Robin \n2 for Priorit
y (Pre-emptive)\n3 for SRT \n0 to GO BACK \n Enter : ");
scanf("%d",&choice3);
if(choice3 == 1)
{
roundrobin();
}
else if(choice3 == 2)
{
prioritypre();
}
else if(choice3 == 3)
{
srt();
}
else if(choice3 == 0)
{
break;
}
else
{
printf("\n\n\t INVALID INPUT");
printf("\n\t Press any key to continue......");
getch();
}
}
else if(choice2 == 2)
{
//clrscr();
printf("Enter your choice : \n1 for FIFO \n2 for Priority (Non Pre-empt
ive) \n3 for SJF \n0 to GO BACK \n Enter : ");
scanf("%d",&choice4);
if(choice4 == 1)
{
fifo();
}
else if(choice4 == 2)
{
prioritynonpre();
}
else if(choice4 == 3)
{
sjf();
}
else if(choice4 == 0)
{
break;
}
else
{
printf("\n\n\t INVALID INPUT");
printf("\n\t Press any key to continue......");
getch();
}
}
else if(choice2 == 0)
{
break;
}
else
{
printf("\n\n\t INVALID INPUT");
printf("\n\t Press any key to continue......");
getch();
}
}
else if(choice1 == 2)
{
//clrscr();
printf("Enter your choice : \n1 for FCFS \n2 for LRU \n0 to GO BACK \n Enter : "
);
scanf("%d",&choice5);
if(choice5 == 1)
{
fcfs();
}
else if(choice5 == 2)
{
lru();
}
else if(choice5 == 0)
{
break;
}
else
{
printf("\n\n\t INVALID INPUT");
printf("\n\t Press any key to continue......");
getch();
}
}
else if(choice1 == 0)
{
break;
}
else
{
printf("\n\n\t INVALID INPUT");
printf("\n\t Press any key to continue......");
getch();
}
}
getch();
return 0;
}
void sjf()
{
int burst[4],arrival[4],done[4],waiting[4];
int i,j,sum,min;
int temp;
sum = 0;
printf("\n\n\tEnter the details of the processes ");
for(i=0;i<4;i++)
{
printf("\n\n\tProcess %d\n",i+1);
printf("\nEnter the burst time : ");
scanf("%d",&burst[i]);
printf("Enter the arrival time : ");
scanf("%d",&arrival[i]);
done[i] = 0;
waiting[i] = 0;
}
for(i=0;i<4;i++)
{
sum = sum + burst[i];
}
printf("\nsum = %d",sum);
for(i=0;i<=sum;i++)
{
printf(" i = %d ",i);
min = sum;
for(j=0;j<4;j++)
{
if(burst[j] < min && done[j] == 0 && arrival[j] <= i)
{
min = burst[j];
}
}
temp = i - arrival[j];
i = i + burst[j];
done[j] = 1;
waiting[j] = temp;
printf("\ntemp = %d",temp);
}
for(i=0;i<4;i++)
{
printf("\n\na(%d) = %d",i+1,waiting[i]);
}
printf("\n\nPress any key to continue.....");
getch();
}
void roundrobin()
{
int burst[4],arrival[4],lefttime[4],waiting[4],last[4];
int i,j,sum;
int gap=0;
float awt=0;
sum = 0;
// clrscr();
printf("\n\n\tEnter the details of the processes ");
for(i=0;i<4;i++)
{
printf("\n\n\tProcess %d\n",i+1);
printf("\nEnter the burst time : ");
scanf("%d",&burst[i]);
printf("Enter the arrival time : ");
scanf("%d",&arrival[i]);
lefttime[i] = 0;
waiting[i] = 0;
last[i] = 0;
}
printf("Enter the interval time : ");
scanf("%d",&gap);
for(i=0;i<4;i++)
{
sum = sum + burst[i];
}
//printf("\nsum = %d",sum);
for(i=0;i<4;i++)
{
lefttime[i] = burst[i];
}
j=0;
for(i=0;i<=sum;i++)
{
if(lefttime[j] > 0 && arrival[j] < i )
{
if(burst[j] < gap)
{
//printf("\nProcessing p%d in less",j);
lefttime[j] = 0;
waiting[j] = waiting[j] + (i - last[j]);
last[j] = i;
//printf(" Waiting = %d",waiting[j]);
i = i + lefttime[j];
}
else if(burst[j] > gap)
{
//printf("\nProcessing p%d in more",j);
lefttime[j] = lefttime[j] - gap;
waiting[j] = waiting[j] + (i - last[j]);
last[j] = i;
//printf(" Waiting = %d",waiting[j]);
i = i + gap;
}
}
if(j<4)
j++;
else
j=0;
}
for(i=0;i<4;i++)
{
printf("\n\nWaiting time for p%d = %d",i+1,waiting[i]);
awt = awt + waiting[i];
}
awt = awt/4;
printf("\n\nThe average waiting time is %.3f",awt);
printf("\n\nPress any key to continue.....");
getch();
}
void lru()
{
int num,i,buf,page[100],buff[100],j,pagefault,rep,flag = 1,ind,abc[100];
int count;
int l,k,fla;
// clrscr();
printf("Enter the number of paging sequence you want to enter : ");
scanf("%d",&num);
printf("\nEnter the paging sequence : \n");
for(i=0;i<num;i++)
{
printf("\n %d. ",i+1);
scanf("%d",&page[i]);
}
printf("\n\nEnter the buffer size : ");
scanf("%d",&buf);
for(j=0;j<buf;j++)
{
buff[j] = 0;
}
pagefault = 0;
flag = 1;
count = 0;
k = 0;
for(i=0;i<num;i++)
{
flag = 1;
for(j=0;j<buf;j++)
{
if(buff[j] == page[i])
{
flag = 0;
break;
}
}
j = 0;
if(flag == 0)
{
continue;
}
else
{
printf("\ni'm here");
if(k < buf)
{
buff[k] = page[i];
k++;
pagefault++;
printf("\nNow pages are : "); //%d %d %d ",buff[0],buff[1],buff[2]);
for(l=0;l<buf;l++)
{
if(buff[l] != 0)
{
printf(" %d",buff[l]);
}
}
continue;
}
count = 0;
fla = 1;
for(j=0;j<buf;j++)
{
abc[j] = 0;
}
for(l=i;l>=0;l++)
{
for(j=buf-1;j>=0;j--)
{
if(abc[j] == page[l])
{
fla = 0;
break;
}
}
if(fla == 1)
{
abc[count] = page[l];
count++;
}
if(count == (buf-1))
{
rep = abc[buf-1];
break;
}
}
for(l=0;l<buf;l++)
{
if(rep == buff[l])
{
ind = l;
break;
}
}
printf("\nReplacement = %d",rep);
printf("\nindex = %d",ind);
buff[ind] = page[i];
pagefault++;
printf("\nNow pages are : %d %d %d ",buff[0],buff[1],buff[2]);
}
}
printf("\n\nPage faults = %d",pagefault);
printf("\n\nPress any key to continue.....");
getch();
}

void fifo()
{
int k=0,ptime[25],n,s=0,i,sum=0;
char name[25][25];
float avg;
//clrscr();
printf ("enter the no. of process: \t");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name for processes: \t");
printf("%d \t",i+1);
scanf("%s",name[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the process time: \t");
printf("%s \t",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n \n");
printf("\n process - name \t process - time \n");
for(i=0;i<n;i++)
{
printf("\t %s \t \t %d \n",name[i],ptime[i]);
}
printf("\n \n FIFO SCHEDULING \n \n");
for(i=0;i<n;i++)
{
printf("process %s from %d to %d \n", name[i],k,(k+ptime[i]));
k+=ptime[i];
}
for(i=0;i<(n-1);i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\n average waiting time: \t");
printf("%2fmsec",avg);
sum=avg=s=0.0;
for(i=0;i<n;i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n turn around time is \t");
printf("%2fmsec",avg);
printf("\n\nPress any key to continue.....");
getch();
}
void fcfs()
{
int num,i,buf,page[100],buff[100],j,pagefault,flag = 1,temp,k,l;
//clrscr();
printf("Enter the number of paging sequence you want to enter : ");
scanf("%d",&num);
printf("\nEnter the paging sequence : \n");
for(i=0;i<num;i++)
{
printf("\n %d. ",i+1);
scanf("%d",&page[i]);
}
printf("\n\nEnter the buffer size : ");
scanf("%d",&buf);
for(j=0;j<buf;j++)
{
buff[j] = 0;
}
pagefault = 0;
flag = 1;
k= 0;
for(i=0;i<num;i++)
{
flag = 1;
for(j=0;j<buf;j++)
{
if(buff[j] == page[i])
{
flag = 0;
break;
}
}
j = 0;
if(flag == 0)
{
continue;
}
else
{
if(k < buf)
{
buff[k] = page[i];
k++;
pagefault++;
printf("\nNow pages are : "); //%d %d %d ",buff[0],buff[1],buff[2]);
for(l=0;l<buf;l++)
{
if(buff[l] != 0)
{
printf(" %d",buff[l]);
}
}
continue;
}
for(j=0;j<buf-1;j++)
{
temp = buff[j+1];
buff[j+1] = buff[j];
buff[j] = temp;
}
buff[buf-1] = page[i];
pagefault++;
printf("\nNow pages are : "); //%d %d %d ",buff[0],buff[1],buff[2]);
for(l=0;l<buf;l++)
{
if(buff[l] != 0)
{
printf(" %d",buff[l]);
}
}
}
}
printf("\n\nPage faults = %d",pagefault);
printf("\nPage replacements are = %d",pagefault-buf);
printf("\n\nPress any key to continue.....");
getch();
}

You might also like