In: Computer Science
Hi Guys,
The assignment in Process Scheduling Operating System to write C code that implement FCFS & Round-Robin ( Without using any array) but we can use linkedlist
PROGRAM FOR FCFS(first come first serve)PROCESS SCHEDULING
#include <stdio.h>
#include <stdlib.h>
/*we know that fcfs is non preemptive scheduling algorithm that
means we cannot preempt the process in middle
the process will be
executed till its end */
struct fcfs
{
int at,ct,bt,tat,pid,wt;
};// making a structure
int main()
{
int n,i,j;
float awt,atat;
struct fcfs allfcfs[100],temp; //decrearing a
structure
printf("enter how many processes u want to
enter:");
scanf("%d",&n);// entering no of processes
you want to execute
for(i=0;i<n;i++)
{
printf("enter the arrival time of
%d process:",i+1);
scanf("%d",&allfcfs[i].at);//
enter arrival time of process i
printf("enter the burst time of %d
process:",i+1);
scanf("%d",&allfcfs[i].bt);//
enter burst time of process i
allfcfs[i].pid=i+1;
}// this loop is all about entering process
details
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(allfcfs[j].at>allfcfs[j+1].at)
{
temp=allfcfs[j];
allfcfs[j]=allfcfs[j+1];
allfcfs[j+1]=temp;
}
}
}// this loop is all about sorting process based on
their arrival time
int c=0;
for(i=0;i<n;i++)
{
if(i==0)
{
c=allfcfs[i].at;
}
else
{
if(allfcfs[i].at>allfcfs[i-1].ct)
{
c=allfcfs[i].at-allfcfs[i-1].ct;
}
else
{
c=0;
}
}
if(i>0)
{
allfcfs[i].ct=allfcfs[i-1].ct+allfcfs[i].bt+c;
calculating cpu time of process i
}
else
{
allfcfs[i].ct=allfcfs[i].bt+c;
}
allfcfs[i].tat=allfcfs[i].ct-allfcfs[i].at;// calculting turn
around time of process i
allfcfs[i].wt=allfcfs[i].ct-allfcfs[i].bt-allfcfs[i].at;
calculating waiting time of process i
awt=awt+allfcfs[i].wt;// for
calculating average waiting time we are finding the sum of waiting
times of all processes
atat=atat+allfcfs[i].tat;
}
printf("Average Turn Around Time is
%f\n",(float)(atat/n));//printing average turn around time
printf("Average Waiting time is
%f",(float)(awt/n));// printing average waiting time
}
PROGRAM FOR RR(round robin) PROCESS SCHEDULING
#include<stdio.h>
struct RR{
int pid,bt,bt1,ct,tat,wt,f;
}; // making a structure for rr
main()
{
struct RR rr[100];declearing a structure
int n,i,q,st=0,c=0;
float atat=0,awt=0;
printf("enter no of processes:");//entering how
many processes you want to enter
scanf("%d",&n);
for (i=0;i<n;i++){
rr[i].pid=i+1;
rr[i].f=0;
printf("enter the
bt:");
scanf("%d",&rr[i].bt);
rr[i].bt1=rr[i].bt;
printf("---------------------------\n");
} // entering the process
details
printf("enter time quantum:");
scanf("%d",&q);// entering the time
quantum(the amount time will be given to every process while
execution)
while (1)
{
for (i=0;i<n;i++)
{
if
((rr[i].bt<=q)&&(rr[i].f==0))/*checking whether the
process is completed or not and also checking whether
the
remaining burst time of process is less than quantum or not*/
{
st=st+rr[i].bt;//here we are changing the cpu time by executing
that process i
rr[i].bt=0;// we are changing the burst time to 0 because the
process is completed
rr[i].f=1;// changing flag value to 1 because process i is
completed
c++; // I used this variable to check no of processes
completed till now
rr[i].ct=st;
rr[i].tat=rr[i].ct;// changing turn around time
atat=atat+rr[i].tat; // changing average turn around time
rr[i].wt=rr[i].tat-rr[i].bt1;waiting time calculation
awt=awt+rr[i].wt;// changing average waiting time
}
else if (rr[i].bt>q)
// checking burst time is greater than quantum if true we simply
add that quantum to cpu time
{
st=st+q;
rr[i].bt=rr[i].bt-q; //we decrement quantum value from the burst
time because processi is executed q amount of time
}
}
if (c==n)
break;// break if all
processes are completly executed
}
// printing average turn around time and waiting
time
printf("average Turn Around Time is
%f\n",(float)(atat/n));// printing average turn around time
printf("average Waiting Time is
%f",(float)(awt/n));// printing average waiting time
}