In this lab exercise you are asked to implement a scheduler. Specially, you need
ID: 3687001 • Letter: I
Question
In this lab exercise you are asked to implement a scheduler. Specially, you need to develop
a scheduler that creates N processes and schedules these processes in a Round Robin way. Each
process will be allowed to have access to the CPU for a time quantum (qt). After this time quantum
is completed, the process should stop and the scheduler must select the next one. The qt must be
equal for all processes and it will be de ned by the user. The processes should be initialized and
wait for the parent to send them a signal in order to be activated.
Structure of program:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <wait.h>
Struct node
{
//Struct for the node of the queue
};
Struct queue
{
//Struct for the queue};
Void enqueue(…){
}
Int dequeue(…){
}
Void term_child(…){
//Signal hanlder, the termination of a process
….
}
Int main (int argc,char const *argv[])
{
//Variable initialization
If (argc>2)
{
qt = 1000*atoi(&argv[1][0]; // Here you need the qt
}
else
{
printf(“%s qt prog1 [prog2] ….. [prog[N} ”,argv[0]);
exit(-1)
}
….for (…)
{
//Fork and execl processes
}
sleep(1); //Small safe dealy
…
while(…) //Scheduling loop
{
…}
}
Instruction for Programs:
#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<wait.h>
#include<sys/resource.h>
Void cont(int sig_num) {
signal(SIGCONT,cout);
printf(“Proccess %d starts…. ”,getpid());
}
void stop (int sig_num)
{
signal (SIGUSR1,stop);
printf(“Process %d ends…. ”,getpid());
pause();}
Int main (int argc,char const *argv[])
{
Int I, num;
num = atoi(&argv[0][1]);
signal(SIGUSR1,stop);
signal(SIGUSRT,cont);
setpriority(PRIO_PROCESS,0,-20);
pause();
For (I = -5;i<= num; i++)
{
printf(“This is program %s and it prints for the %d time …. ”,argv[0],i+6);
unsleep(10000);
}
}
YOU NEED TO COMPILE THE CODE IN ORDER TO PRODUCE DIFFERENT EXECUTABLES:
$ gcc p.c –o p1
$ gcc p.c –o p3
$ gcc p.c –o p6
$ gcc p.c –o p9
$ gcc p.c –o p12
Explanation / Answer
ROUND ROBIN SCHEDULING ALGORITHM:
#include<stdio.h>
int ttime,i,j,temp;
main()
{
int pname[10],btime[10],pname2[10],btime2[10];
int n,x,z;
printf("Enter the no. of process:");
scanf("%d",&n);
printf("Enter the process name and burst time for the process ");
for(i=0;i<n;i++)
{
printf("Enter the process name:");
scanf("%d",&pname2[i]);
printf("Enter burst time for the process %d:",pname2[i]);
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME BURST TIME ");
for(i=0;i<n;i++)
printf("%d %d ",pname2[i],btime2[i]);
z=1;
while(z==1)
{
ttime=0;
for(i=0;i<n;i++)
{
pname[i]=pname2[i];
btime[i]=btime2[i];
}
printf ("PRESS 1.ROUND ROBIN 2.EXIT ");
scanf("%d",&x);
switch(x)
{
case 1:
rrobin(pname,btime,n);
break;
case 2:
exit(0);
break;
default:
printf("Invalid option");
break;
}
printf(" If you want to continue press 1:");
scanf("%d",&z);
}
}
rrobin(int pname[],int btime[],int n)
{
int tslice;
j=0;
printf(" ROUND ROBIN SCHEDULING ");
printf("Enter the time slice: ");
scanf("%d",&tslice);
printf("PROCESS NAME REMAINING TIME TOTAL TIME");
while(j<n)
{
for(i=0;i<n;i++)
{
if(btime[i]>0)
{
if(btime[i]>=tslice)
{
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf(" %d %d %d",pname[i],btime[i],ttime);
if(btime[i]==0)
j++;
}
else
{
ttime+=btime[i];
btime[i]=0;
printf(" %d %d %d",pname[i],btime[i],ttime);
}
}
}
}
}
CPU Schedular using round robin:
The CPU scheduler goes around this queue, allocating the CPU to each process for a time interval of one quantum. New processes are added to the tail of the queue.
#include<stdio.h>
void main()
{
int k,j,q,i,n,ts,temp;
int aw; float awt;
int bt[10],wt[10],te[10],rt[10],at[10];j=0; clrscr();
printf("enter number of process : ");
scanf("%d",&n);
printf(" enter brust time and arriavl time ");
for(i=0;i<n;i++)
{
printf("P%d brust time ",i+1);
scanf("%d",&bt[i]);
printf("ariavl time : ");
scanf("%d",&at[i]);
te[i]=0; wt[i]=0;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp=at[i]; //sorting according to arrival time
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
}
}
printf(" enter time slice ");
scanf("%d",&ts); q=0; clrscr();
printf(" process :") ;
for(i=0;i<n;i++)
{
printf(" %d",i+1);
}
printf(" Brust time :");
for(i=0;i<n;i++)
{
printf(" %d",bt[i]); rt[i]=bt[i];
}
printf(" Arrival time :");
for(i=0;i<n;i++)
{
printf(" %d",at[i]);
}
printf(" Gaint chart ");
j=0;
while(j<=n)
{
j++;
for(i=0;i<n;i++)
{
if(rt[i]==0) continue;
if(rt[i]>ts)
{
printf(" %d P%d",q,i+1);
q=q+ts;
rt[i]=rt[i]-ts;
te[i]=te[i]+1;
}
else
{
printf(" %d P%d",q,i+1);
wt[i]=q-te[i]*ts;
q=q+rt[i];
rt[i]=rt[i]-rt[i];
}
}
} //end of while
awt=0;
printf(" Process Waitnig time");
for(i=0;i<n;i++)
{
wt[i]=wt[i]-at[i];
printf(" P%d : %d",i+1,wt[i]); awt=awt+wt[i];
}
aw=awt;
printf(" total waiting time %d",aw);
printf(" Avg wainting time %f ",awt/n);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.