Sunday, March 11, 2012

SIMULATION OF CPU SCHEDULING ALGORITHMS - PRIORITY SCHEDULING





Ex. No.: 6(b)
SIMULATION OF CPU SCHEDULING ALGORITHMS  - PRIORITY SCHEDULING
 
AIM
               To simulate CPU scheduling, using Priority Scheduling (PS) algorithm.
 
Description:
 
            A priority is assigned to each process and CPU is allocated to the process with highest priority. Priorities may be based on internally measurable quantities like time limits, memory requirements, number of open files etc. and some external quantities like importance of the process, type and amount spent for each process etc. . It depicts the order in which the processes are executed, calculates the turnaround time and average waiting time of the processes
 
Algorithm:
 
1.      Start of algorithm.
2.      Get the process name, arrival time, burst time, and priority for each process.
3.      Sort the processes according to priority of each process.
4.      Calculate the average waiting time from the waiting time of the processes as
         Average Waiting Time = (wt1+wt2+…+wtn)/n where 
wt - waiting time of each process in the ready queue. Wt is the previous process’s arrival time and burst time.
         n  - Number of processes.     
5.      Calculate the average turn around time as 
         ((wt1+wt2+…+wtn)+ (bt1+bt2+…+btn))/n where
         wt - waiting time of each process in the ready queue
         bt – Burst time of each process.
         n  - Number of processes.     
6.      Print the Process name, arrival time, burst time, waiting time for each process and average waiting time and turnaround time.        
7.      Stop the algorithm.

Program:     To implement priority Scheduling Algorithm

#include<stdio.h>
main()
{
 int n,i,j,avgw=0,avgt=0,temp;
 int burst[10],wait[10],priority[10],process[10];
 printf("\nProcess Scheduling(Priority) :\n");
 printf("\nEnter the number of process : ");
 scanf("%d",&n);
 printf("Enter the Burst time and priority\n");
 for(i=0;i<n;i++)
 {
   printf("Burst time P%d : ",i+1);
   scanf("%d",&burst[i]);
   printf("Priority P%d : ",i+1);
   scanf("%d",&priority[i]);
   process[i] = i;
   printf("\n");
 }
 for(i=0;i<n;i++)
 {
   for(j=i;j<n;j++)
   {
      if(priority[i]>priority[j])
      {
            temp = priority[i];
            priority[i] = priority[j];
            priority[j] = temp;
            temp = burst[i];
            burst[i] = burst[j];
            burst[j] = temp;
            temp = process[i];
            process[i] = process[j];
            process[j] = temp;
      }
   }
 }
 printf("\nWaiting time | Turn around time \n");
 j=0;
 for(i=0;i<n;i++)
 {
   wait[i] = j;
   j = burst[i] + j;
   printf("\nP%d : %d\t\t%d",process[i]+1,wait[i],wait[i]+burst[i]);
   avgw+=wait[i];
   avgt+=wait[i]+burst[i];
 }
 printf("\n\n");
 printf("Average waiting time : %f\nAverage Turn around time : %f\n\n"
       ,avgw/n,avgt/n);
}

/*OUTPUT:
======
Enter the number of processes:
3

Enter the burst time and priority:
Burst time P1:3
Priority P1:5

Burst time P2:3
Priority P2:2

Burst time P3:3
Priority P3:6

Waiting time | Turn around time

p2: 0           3
p1: 3           6
p3: 6           9

Average waiting time is: 3.00
Average turn around time is: 6.98

No comments:

Post a Comment