Description: This programming example in c Linux for Round Robin algorithm for process scheduling in Operating Systems first takes total number of processes as input. Then iterating for total number of processes, it takes input the process ID, arrival time (arrival hour and arrival minute) and total execution time. After that, it schedules those processes using Round Robin scheduling algorithm in operating systems like Linux.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<stdbool.h>
struct Node{
int p_id;
int arr_hr;
int arr_min;
int exe_time;
int sum;
};
int main(){
int i,j,k,no_of_processes;
bool isFinish=false;
struct Node *Queue,temp;
printf("\n\t---> Round Robin Scheduling <---\n\n");
printf("Enter no. of Processes:");
scanf("%d",&no_of_processes);
Queue=malloc(no_of_processes);
for(i=0;i<no_of_processes;i++){
printf("Give Process#%d id :\n",i+1);
scanf("%d",&Queue[i].p_id);
printf("Give Arrival Hour
(0~23):\n");
scanf("%d",&Queue[i].arr_hr);
printf("Give Arrival Minute :\n");
scanf("%d",&Queue[i].arr_min);
printf("Give Execution Time (in
secs.):\n");
scanf("%d",&Queue[i].exe_time);
Queue[i].sum=0;
}
for(i=0;i<no_of_processes;i++){
for(j=0;j<no_of_processes-i-1;j++)
if(Queue[j].arr_hr>Queue[j+1].arr_hr || (Queue[j].arr_hr==Queue[j+1].arr_hr && Queue[j].arr_min>Queue[j+1].arr_min)){
temp=Queue[j];
Queue[j]=Queue[j+1];
Queue[j+1]=temp;
}
}
int count=0,time=0;
i=0,j=0,k=0;
while(count<no_of_processes){
if(Queue[i].exe_time>0){
printf("Process#%d Turn\t(Total Time Elapsed: %dsecs.)\n",i+1,time);
printf("--> %d\n",++Queue[i].sum);
sleep(1);
time++;
Queue[i].exe_time--;
if(Queue[i].exe_time==0)
isFinish=true;
else{
printf("--> %d\n",++Queue[i].sum);
sleep(1);
time++;
Queue[i].exe_time--;
if(Queue[i].exe_time==0)
isFinish=true;
}
printf("-> Processes currently in the Queue <-\n");
for(j=1,k=(i+1)%no_of_processes;j<=(no_of_processes-count-1);j++,k=(k+1)%no_of_processes){
if(Queue[k].exe_time!=0)
printf("-> Process#%d\t(%d sec(s) Remaining)\n",k+1,Queue[k].exe_time-Queue[k].sum);
}
if(j==1)
printf("-> There is no process in the Queue\n");
if(isFinish==true)
count++;
}
i=(i+1)%no_of_processes;
isFinish=false;
}
return 0;
return 0;
}
No comments
Post a Comment