You are on page 1of 14

FIRST COME FIRST SERVE (FCFS

):

First-come, first-served (FCFS) means there will be no preferential treatment and a service will be provided to those that arrive first. First-come, first-served (FCFS) – sometimes first-in, first-served and first-come, first choice – is a service policy whereby the requests of customers or clients are attended to in the order that they arrived, without other biases or preferences. The policy can be employed when processing sales orders, in determining restaurant seating, on a taxi stand, etc. In Western society, it is the standard policy for the processing of most queues in which people wait for a service. The practice is also common among some airlines which do not permit seat reservations either in advance or at check-in. These airlines allow passengers to board in small groups based upon their order of check-in and sit in whatever seat on the aircraft they wish to. On the basis of first come, first served, the earlier they check in, the earlier they board the aircraft to get the seat they want. Passengers are sequentially (on a first come, first served basis) assigned into one of several "boarding groups." The passengers are then boarded onto the plane in group order.

ALGORITHM
1. Start the process 2. Declare the array size 3. Get the number of processes to be inserted 4. Get the value 5. Start with the first process from it’s initial position let other process to be in queue 6. Calculate the total number of burst time 7. Display the values 8. Stop the process

PROGRAM:
#include<stdio.h> #include<conio.h> #include<string.h> main() { int n,a[10],b[10],t[10],w[10],g[10],i,m; float att=0,awt=0; for(i=0;i<10;i++) { a[i]=0; b[i]=0; w[i]=0; g[i]=0;

} printf("enter the number of process"); scanf("%d",&n); printf("enter the burst times"); for(i=0;i<n;i++) scanf("%d",&b[i]); printf("\nenter the arrival times"); for(i=0;i<n;i++) scanf("%d",&a[i]); g[0]=0; for(i=0;i<10;i++) g[i+1]=g[i]+b[i]; for(i=0;i<n;i++) { w[i]=g[i]-a[i]; t[i]=g[i+1]-a[i]; awt=awt+w[i]; att=att+t[i]; } awt =awt/n; att=att/n; printf("\n\tprocess\twaiting time\tturn arround time\n"); for(i=0;i<n;i++) { printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]); } printf("the average waiting time is %f\n",awt); printf("the average turn around time is %f\n",att); }

OUTPUT:
enter the number of process 4 enter the burst times 4 9 8 3 enter the arrival times 0 2 4 3 process p0 p1 p2 p3 waiting time 0 2 9 18 turn arround time 4 11 17 21

the average waiting time is 7.250000 the average turn around time is 13.250000

SHORTEST JOB FIRST:
Shortest Job First (SJF) scheduling is a priority and Non Preventive. Non Preventive mean here is when the allotted time a processor then the processor cannot be taken the other, until the process is completed in the execution. This assumes scheduling time to complete the process known in advance. The mechanism is to schedule the process with the shortest time to completion first, thus providing high efficiency and low turnaround time. In terms of time spent in the current program (job) began to enter into the system until the process is completed the system, requiring a short time. Shortest Job First (SJF) scheduling algorithm can be said to be optimal with an average waiting time is minimal. For example, there are four processes by CPU Burst in milliseconds.

Scheduling processes with the SJF algorithm (non-preventive) Gantt chart can be seen in the following:

Waiting time for P1 is 0, P2 is 26, P3 and P4 is 3 is 7 so that the average waiting time is (0 + 6 + 3+7)/4=4milliseconds. Another example for the algorithm Shortest Job First (SJF), for example, there are four processes (job), namely A, B, C, D with the time course of each is 8,4,4 and 4 minutes. If the processes are executed, then the turnaround time for A is 8 minutes, for B is 12, for C is 16 and D is 20. If the four process using shortest job scheduling fisrt, then turn around time for B is 4, to C is 8, for D is 12 and for A is 20. Since SJF always pay attention to the average response time is the smallest, it is very good for the interactive process. Generally interactive process has a pattern that is waiting for orders, run the command, wait command and run the command, and so on. Problems arise when using this algorithm does not know the size of the job when the job log. To determine the size of the job is to make estimates or estimates based on previous behavior. The process does not come together, so that its adoption should be dynamic. Another issue that emerged in this algorithm is that it can be as certain conditions, a job may never complete execution. Let me give an example, for example, there is a process with the elapsed time 1 hour to arrive at time 0. But at the same time every one minute and the next come the short elapsed time of 2 minutes. As a result, it could be the A never gets a small executable.

Program: #include<stdio.h> #include<conio.h> #include<dos.h> void main() { int n,b[10],w[10],i,j,h,t,tt; int stime[10],a[10]; float avg=0; clrscr(); printf("\n\tJOB SCHEDULING ALGORITHM[SJF]"); printf("\n\t*****************************************************\n"); printf("\nEnter howmany jobs:"); scanf("%d",&n); printf("\nEnter burst time for corresponding job....\n"); for(i=1;i<=n;i++) { printf("\nProcess %d:",i); scanf("%d",&b[i]); a[i]=i; }

for(i=1;i<=n;i++) for(j=i;j<=n;j++) if(b[i]>b[j]) { t=b[i]; tt=a[i]; b[i]=b[j]; a[i]=a[j]; b[j]=t; a[j]=tt; } w[1]=0; printf("\nprocess %d waiting time is 0",a[1]); for(i=2;i<=n;i++) { w[i]=b[i-1]+w[i-1]; printf("\nProcess %d waiting time: %d",a[i],w[i]); avg+=w[i]; } printf("\ntotal waiting time:%f",avg); printf("\n\nthe average waiting time is:%f\n",avg/n); printf("\nGaunt Chart\n***************************************\n\n\t"); h=22; for(i=1;i<=n;i++) { printf("%d",b[i]); for(j=1;j<=b[i];j++) printf("%c",h); } printf("\n\t"); for(i=1;i<=n;i++) { printf("%d",w[i]); for(j=1;j<=w[i];j++) printf("%c",h); delay(1000); } getch(); }

Output:
Enter howmany jobs:3 Enter burst time for corresponding job.... Process 1:5 Process 2:2 Process 3:3

process 2 waiting time is 0 Process 3 waiting time: 2 Process 1 waiting time: 5 total waiting time:7.000000 the average waiting time is:2.333333

PRIORITY SCHEDULING
Each process is assigned a priority. Process with highest priority is to be executed first and so on. Processes with same priority are executed on first come first serve basis. Priority can be decided based on memory requirements, time requirements or any other resource requirement.

Priority scheduling is a more general case of SJF, in which each job is assigned a priority and the job with the highest priority gets scheduled first. ( SJF uses the inverse of the next expected burst time as its priority - The smaller the expected burst, the higher the priority. ) Note that in practice, priorities are implemented using integers within a fixed range, but there is no agreed-upon convention as to whether "high" priorities use large numbers or small numbers. This book uses low number for high priorities, with 0 being the highest possible priority. For example, the following Gantt chart is based upon these process burst times and priorities, and yields an average waiting time of 8.2 ms:

Two types of priority scheduling are given below: Non-preemptive Priority Scheduling In this type of scheduling the CPU is allocated to the process with the highest priority after completing the present running process.

Advantage • Good response for the highest priority processes. Disadvantage • Starvation may be possible for the lowest priority processes. Preemptive Priority Scheduling In this type of scheduling the CPU is allocated to the process with the highest priority immediately upon the arrival of the highest priority process. If the equal priority process is in

running state, after the completion of the present running process CPU is allocated to this even though one more equal priority process is to arrive.

Advantage • Very good response for the highest priority process over non-preemptive version of it. Disadvantage • Starvation may be possible for the lowest priority processes. Wait time of each process is following Process P0 0-0=0 P1 3-1=2 P2 8-2=6 P3 16 - 3 = 13 Wait Time : Service Time - Arrival Time

Average Wait Time: (0+2+6+13) / 4 = 5.25

Program:
#include<stdio.h> #include<conio.h> void main() { char p[10][5], temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j])

{ temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); }}} wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]); } printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }

OUTPUT: enter no of processes: 5 enter process1 name: aaa enter process time: 4 enter priority:5 enter process2 name: bbb enter process time: 3 enter priority:4 enter process3 name: ccc enter process time: 2 enter priority:3 enter process4 name: ddd enter process time: 5 enter priority:2 enter process5 name: eee enter process time: 1 enter priority:1 p_name P_time priority w_time eee 1 1 0

ddd 5 2 1 ccc 2 3 6 bbb 3 4 8 aaa 4 5 11 total waiting time=26 avg waiting time=5.20

ROUND ROBIN SCHEDULING:
Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system. As the term is generally used, time slices are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive). Round-robin scheduling is simple, easy to implement, and starvation-free. Round-robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in computer networks. Each process is provided a fix time to execute called quantum. Once a process is executed for given time period. Process is preempted and other process executes for given time period. Context switching is used to save states of preempted processes.
• •

Round robin scheduling is similar to FCFS scheduling, except that CPU bursts are assigned with limits called time quantum. When a process is given the CPU, a timer is set for whatever value has been set for a time quantum.
o

If the process finishes its burst before the time quantum timer expires, then it is swapped out of the CPU just like the normal FCFS algorithm. If the timer goes off first, then the process is swapped out of the CPU and moved to the back end of the ready queue.

o • •

The ready queue is maintained as a circular queue, so when all processes have had a turn, then the scheduler gives the first process another turn, and so on. RR scheduling can give the effect of all processors sharing the CPU equally, although the average wait time can be longer than with other scheduling algorithms. In the following example the average wait time is 5.66 ms. Process P1 P2 P3 Burst Time 24 3 3

The performance of RR is sensitive to the time quantum selected. If the quantum is large enough, then RR reduces to the FCFS algorithm; If it is very small, then each process gets 1/nth of the processor time and share the CPU equally.

BUT, a real system invokes overhead for every context switch, and the smaller the time quantum the more context switches there are. ( See Figure 5.4 below. ) Most modern systems use time quantum between 10 and 100 milliseconds, and context switch times on the order of 10 microseconds, so the overhead is small relative to the time quantum

The round-robin (RR) scheduling algorithm is 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. To implement RR scheduling:• we keep the ready queue as a FIFO queue of processes. • New processes are added to the tail of the ready queue.
• •

The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. The process may have a CPU burst of less than 1 time quantum.
o o

In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue.

Otherwise, if the CPU burst of the currently running process is longer than 1 time quantum,
o o

the timer will go off and will cause an interrupt to the OS. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.

o

Thus, we want the time quantum to be large with respect to the context-switch time.

If the context-switch time is approximately 10 percent of the time quantum, then about 10 percent of the CPU time will be spent in context switching.

• •

In practice, most modern systems have time quanta ranging from 10 to 100 milliseconds. The time required for a context switch is typically less than 10 microseconds; thus, the context-switch time is a small fraction of the time quantum.

Setting the quantum too short causes too many process switches and lowers the CPU efficiency, but setting it too long may cause poor response to short interactive requests. Poor average waiting time when job lengths are identical; Imagine 10 jobs each requiring 10 time slices, all complete after about 100 time slices, even FCFS is better! In general, the average turnaround time can be improved if most processes finish their next CPU burst in a single time quantum. If context-switch time is added in, the average turnaround time increases for a smaller time quantum, since more context switches are required. Although the time quantum should be large compared with the context-switch time, it should not be too large. If the time quantum is too large, RR scheduling degenerates to FCFS policy.

Process Wait Time : Service Time - Arrival Time P0 (0-0) + (12-3) = 9 P1 (3-1) = 2 P2 6-2) + (15-9) = 10 P3 (9-3) + (18-12) = 12 Average Wait Time: (9+2+10+12) / 4 = 8.25 Advantages • Round-robin is effective in a general-purpose, times-sharing system or transactionprocessing system. • Fair treatment for all the processes.
• • •

Overhead on processor is low. Overhead on processor is low. Good response time for short processes.

Disadvantages • Care must be taken in choosing quantum value. • Processing overhead is there in handling clock interrupt.

Throughput is low if time quantum is too small.

Performance of RR Scheduling • If there are n processes in the ready queue and time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. • No process waits for more than (n-1)*q time units until the next time quantum.

The performance of RR depends on time slice.If it is large then it is the same as FCFS. If q is small then overhead is too high.

Program:
#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> void main() { char p[10][5]; int et[10],wt[10],timer=3,count,pt[10],rt,i,j,totwt=0,t,n=5,found=0,m; float avgwt; clrscr(); for(i=0;i<n;i++) { printf("enter the process name : "); scanf("%s",&p[i]); printf("enter the processing time : "); scanf("%d",&pt[i]); } m=n; wt[0]=0; i=0; do { if(pt[i]>timer) { rt=pt[i]-timer; strcpy(p[n],p[i]); pt[n]=rt; et[i]=timer; n++; } else { et[i]=pt[i]; } i++; wt[i]=wt[i-1]+et[i-1]; }while(i<n); count=0; for(i=0;i<m;i++) { for(j=i+1;j<=n;j++) { if(strcmp(p[i],p[j])==0) { count++; found=j;

}} if(found!=0) { wt[i]=wt[found]-(count*timer); count=0; found=0; }} for(i=0;i<m;i++) { totwt+=wt[i]; } avgwt=(float)totwt/m; for(i=0;i<m;i++) { printf("\n%s\t%d\t%d",p[i],pt[i],wt[i]); } printf("\ntotal waiting time %d\n",totwt); printf("total avgtime %f",avgwt); } getch(); }

INPUT : enter the process name : aaa enter the processing time : 4 enter the process name : bbb enter the processing time : 3 enter the process name : ccc enter the processing time : 2 enter the process name : ddd enter the processing time : 5 enter the process name : eee enter the processing time : 1 OUTPUT: p_name p_time w_time aaa 4 9 bbb 3 3 ccc 2 6 ddd 5 10 eee 1 11

total waiting time : 39 average waiting time : 7.8000