Sie sind auf Seite 1von 11

Drill 4 1

PROCESS SCHEDULING ALGORITHMS


Drill 4 2
PROCESS SCHEDULING ALGORITHMS

School of Electrical Electronics and Computer


Engineering
Department of Computer Engineering
COE127L
(Operating System, Laboratory)
Laboratory Report Rubric

NAME OF STUDENT: DATE SUBMITTED:

EXPERIMENT TITLE EVALUATOR:

Criteria Poor Fair Good Very Good Excellent


Score
(Weight) (1) (2) (3) (4) (5)
The laboratory report The laboratory report The laboratory report The laboratory report The laboratory report
A. Completeness and is untidy, did not is untidy, followed the is neat, followed the is neat, followed the is very neat, well
Organization of the follow the given given format, some given format, some given format, no presented, followed
Experiment format, some parts parts are missing, parts are missing, missing parts, and the given format,
Laboratory Report are missing, most most questions are mostly questions are only a few questions organized, and the
questions are not not answered. not answered. are not answered. required content is
answered. complete.
The result, gathered The result is correct, The result and The result and The result, gathered
B. Correctness of the data, and answers to however, gathered gathered data were gathered data were all data, and answers to
gathered data and questions were all data, and answers to correct, however, correct, and most of questions are all
answers to questions incorrect. questions were all answers to questions the answers to correct.
incorrect. were all incorrect. questions are all
correct.

The interpretation of The interpretation of The interpretation of The interpretation of The interpretation of
data and discussion data and discussion data and discussion data and discussion data and discussion
C. Interpretation of data were not based on the were based on the were based on the were based on the were based on the
result and data result and data result and data result and data result and data
and discussion gathered during the gathered during the gathered during the gathered during the gathered during the
experiment. experiment, but does experiment, and experiment, and experiment and are
not present clarity. somehow presents mostly presents presented very
clarity. clarity. clearly.
Development tools Development tools Development tools All the development All the development
features such as the features such as the features such as the tools features are tools features are
debugger are not debugger is used. debugger is used. used. used.
D. Use of Development used. Program output Program output is not
Tool is not illustrated nor illustrated nor Program output is Program output is Program output is
explained. explained. illustrated but not illustrated and illustrated and
explained. explained but with explained.
minor errors.
The conclusion was The conclusion was The conclusion was The conclusion was The conclusion was
E. Conclusion not based on the based on the based on the based on the based on the
objectives and all objectives but not all objectives and few of objectives and most of objectives and all of
ideas are neither of the ideas are the ideas are coherent the ideas are coherent the ideas are coherent
coherent nor clear. coherent or clear. but not too clear. and clear. and presented very
clearly.
The words used were The words used were The words used were The words used were The words used were
not appropriate, had somehow appropriate, appropriate, had good appropriate, had very appropriate, had
F. Use of Language poor grammar, had had good grammar, grammar, had good good grammar, had excellent grammar,
bad sentence had good sentence sentence construction very good sentence had excellent
construction and construction and not and few of the ideas construction and sentence construction
ideas were not clearly all ideas were clearly were clearly almost all of the ideas and all of the ideas
expressed. expressed. expressed. were clearly were clearly
expressed. expressed.
G. Promptness The laboratory report The laboratory report The laboratory report The laboratory report The laboratory report
was submitted two or was submitted one was submitted three was submitted one to was submitted on
more weeks late. week late. to six days late two days late. time.
Drill 4 3
PROCESS SCHEDULING ALGORITHMS

OBJECTIVES
1. To implement, using C++ programming language, process scheduling
algorithms employed by operating systems.
2. To distinguish preemptive from non-preemptive process scheduling algorithms.
3. To gain an in depth understanding on the algorithms used by the process
scheduler.

DISCUSSION
Process scheduling policies fall under two types such as preemptive and
nonpreemptive scheduling. Preemptive scheduling is where the highest priority should
always be the process that must be currently utilized; it uses time-sharing environment
and transfer CPU to another job. Non-preemptive scheduling is where it cannot process
another job until the first one has not yet finished its service time.

FIRST-COME-FIRST-SERVED SCHEDULING ALGORITHM

FCFS, also termed as First-In-First-Out, allocate the CPU in order in which the process
arrive. When the CPU is free, it is allowed to process, which is occupying the front of
the queue. This algorithm is non-preemptive.

Advantage

Suitable for batch system.


It is simple to understand and code.

Disadvantage

Waiting time can be large if short request wait behind the long process.
It is not suitable for time sharing system where it is important that each user
should get the CPU for equal amount of time interval.

SHORTEST JOB NEXT SCHEDULING ALGORITHM

Shortest Job Next (SJN), also known as Shortest Job First (SJF) or Shortest Process
Next (SPN), is a scheduling policy that selects the waiting process with the smallest
execution time to execute next. When the CPU is available, it is assigned to the process
Drill 4 4
PROCESS SCHEDULING ALGORITHMS

that has the smallest next CPU burst. If the next CPU bursts of two processes are the
same, FCFS scheduling is used.

The SJF algorithm can be either pre-emptive or nonpreemptive. When a new process
arrives at the ready queue while a previous process is still executing, the type of SJF
algorithm can be determined. The next CPU burst of the newly arrived process may be
shorter than what is left of the currently executing process. A pre-emptive SJF
algorithm will preempt the currently executing process, whereas a nonpreemptive SJF
algorithm will allow the currently running process to finish its CPU burst.

SHORTEST REMAINING TIME SCHEDULING ALGORITHM

Shortest remaining time scheduling is the preemptive counter part of SJF and is useful
in time sharing system. In SRT, process with the smallest estimated run time to
completion is run next, in SJF, once a job begin executing, it runs to completion. In
SRT, a running process may be preempted by a user process with a shorter estimated
run time.

ROUND ROBIN

It is one of the oldest, simplest, fairest and most widely used scheduling algorithms,
designed especially for time-sharing systems. It is similar to FCFS scheduling, but pre-
emption is added to switch between processes. A small unit of time, called a time
quantum or time slice, is defined. A time quantum is generally from 10 to 100
milliseconds. The ready queue is treated as a circular queue. 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.

The CPU scheduler picks the first process from the queue, sets a timer to interrupt after
one quantum, and dispatched the process.

If the process is still running at the end of the quantum, the CPU is preempted and the
process is added to the tail of the queue. If the process finishes before the end of the
quantum, the process itself releases the CPU voluntarily. In either case, the CPU
scheduler assigns the CPU to the next process in the ready queue. Every time a process
is granted the CPU, a context switch occurs, which adds overhead to the process
execution time.
Drill 4 5
PROCESS SCHEDULING ALGORITHMS

QUESTION AND ANSWER


1. How does a first-come-first-served scheduling algorithm works?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

2. How does a pre-emptive and nonpreemptive SJF algorithm finish its CPU burst?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

3. In using round robin algorithm, if a process finishes early before its quantum
expires, what will the next process do?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

4. Shortest-remaining-time scheduling algorithm is the pre-emptive counterpart of


shortest job next scheduling algorithm. In SRT, explain how a process is chosen
to run next.
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________

5. In round robin scheduling algorithm, what will happen to the process/job if it has
not yet done its function before its CPU time expires?
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
Drill 4 6
PROCESS SCHEDULING ALGORITHMS

PARTIAL CODE (SAMPLE PROGRAM)


#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define TIMEQUANTUM 1;
#define MAXPROCTIME 100;
struct JobList {
int pid;
int ct_arrival; //job arrival time
int ct_service; //job CPU cyclce burst time
int vt_served;
int vt_residence; //turnaround time, completion time - arrival time
int vt_wait; //waiting time, turnaround time - cpu burst
int vt_completion; //completion time, the time when the process
JobList *nxt; //ends based on the timeline
};
bool initJobList(JobList *&,int *,int);
bool deinitJobList(JobList *&);
bool doSomething (JobList *&);
bool printProcDetails (JobList *);
bool sortJob (JobList *& jobs); //for SJN only
int _tmain(int argc, _TCHAR* argv[])
{

JobList *ptrJobStart;
// arrivaltime , bursttime - cpu cyle
int arrJobs[][2] = {
{0,7},
{2,4},
{4,1},
{5,4}};

initJobList (ptrJobStart,(int *)&arrJobs, (sizeof(arrJobs) /


(sizeof(int)*2)));
printf("\ndoFCFS\nCPU\t\tPROCESS\n\n");
doFCFS (ptrJobStart);
printProcDetails(ptrJobStart);
deinitJobList(ptrJobStart);

return 0;
}
Drill 4 7
PROCESS SCHEDULING ALGORITHMS

bool printProcDetails (JobList * ptrJobStart)


{
do {
printf("\nP%d:\t>Tc=%d\t>Tw=%d\t>Tr=%d\t\n",
ptrJobStart->pid,ptrJobStart->vt_completion,ptrJobStart->vt_wait,
ptrJobStart->vt_residence);
ptrJobStart = ptrJobStart->nxt;
}while (ptrJobStart != NULL);
return true;
}

bool deinitJobList (JobList *& ptrJobStart)


{
JobList *ptrJobDel = NULL;
do {
ptrJobDel = ptrJobStart;
ptrJobStart = ptrJobStart->nxt;
delete ptrJobDel;
}while (ptrJobStart != NULL);
return true;
}

bool initJobList(JobList*& ptrJobStart,int *pJob,int nJobs)


{
//put jobs and their corresponding cpu cycle in the list
JobList * ptrJobs = NULL;
int i;
int j=1;

for (i=0; i<nJobs*2; i+=2) {


if (ptrJobs == NULL) {
ptrJobs = new JobList; //create new list
memset(ptrJobs,0,sizeof(JobList)); //set the value 0 to its
//memory location
ptrJobStart = ptrJobs;
}
else {
ptrJobs->nxt = new JobList;
memset(ptrJobs->nxt,0,sizeof(JobList));
ptrJobs = ptrJobs->nxt;
}
ptrJobs->ct_arrival = *(pJob+i);
ptrJobs->ct_service = *(pJob+(i+1));
ptrJobs->pid = j;
ptrJobs->nxt = NULL;
j++;
}
return true;
}
Drill 4 8
PROCESS SCHEDULING ALGORITHMS

bool doSomething (JobList *&ptrJob) {


JobList *ptrJobStart=ptrJob;
int procTime = 0;
int MAXPROCTIME_ = MAXPROCTIME;
if (ptrJob == NULL) return false;

for (; procTime<MAXPROCTIME_ ; procTime++) {


do {
if (ptrJobStart->vt_served != ptrJobStart->ct_service)
{
ptrJobStart->vt_served++;
break;
}
ptrJobStart->vt_completion = procTime;
ptrJobStart->vt_residence=ptrJobStart->vt_completion-ptrJobStart->ct_arrival;
ptrJobStart->vt_wait = ptrJobStart->vt_residence-ptrJobStart->ct_service;
}while((ptrJobStart = ptrJobStart->nxt)!=NULL);//go to next structure

if (ptrJobStart == NULL) break;


}
return true;
}

6. What process scheduling algorithm is being performed by the function


doSomething?
Drill 4 9
PROCESS SCHEDULING ALGORITHMS

PROGRAMMING EXERCISE
1. Round Robin is a preemptive process scheduling algorithm that isnt based on the job
characteristics but on a predetermined slice of time thats given to each job. This time
slice is called a time quantum and its size is crucial to the performance of the system. It
usually varies from 100 milliseconds to 1 or 2 seconds. Jobs are placed in the READY
queue using a first-come, first-served scheme and the Process Scheduler selects the
first job from the front of the queue, sets the timer to the time quantum, and allocates
the CPU to this job. If processing isnt finished when time expires, the job is preempted
and put at the end of the READY queue and its information is saved in its PCB. In the
event that the jobs CPU cycle is shorter than the time quantum, (1) If this is the jobs
last CPU cycle and the job is finished, then all resources allocated to it are released and
the completed job is returned to the user. Your task is to create a function that
performs the round robin algorithm scheduling based on the sample program as a
template.
Drill 4 10
PROCESS SCHEDULING ALGORITHMS

2. Shortest Job Next is a nonpreemptive scheduling algorithm that handles jobs based on the
length of their CPU cycle time.

Assumptions:

If all the jobs are put in READY queue, the time of arrival can be ignored. Create a function
using the same template. (Note: If the jobs are sorted according to CPU cycle, then it acts as a
FCFS algorithm).
Drill 4 11
PROCESS SCHEDULING ALGORITHMS

REMARKS AND CONCLUSION


_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________

_____________________________________________________________________________________