Get Source Code for Priority scheduling



Priority Scheduling:

In operating systems, tasks that are queued to be processed go through different scheduling algorithms in different operating systems. Priority Scheduling is one of them.

Priority scheduling is like shortest job first except that the next job to process is selected not by the amount of time it will take, but by the priority of the job. So in this priority scheduling each job has a priority value associated with it. The task with shortest priority value gets preferred. 

However, I explained priority scheduling in my another post earlier and as many of my readers wanted to have the source code, here I am publishing the source code of the program. I am not currently explaining the source code, please try to play with it to understand it. I am removing comments intentionally.


Source Code:


#include<iostream.h>
#include<stdio.h>

int main(){
     int i,j,k,p,s=0, got=0, idle=0, temp_burst, temp_row, pre_process_row, done=0;
     float sum=0;

     cout<<"Please enter the number of proces : ";
     cin>>p;

     int a[p][5];
     int b[p][5];

     cout<<"\n\nProcess\tArrival\tBurst\tPriority\n-------\t-------\t-----\t--------\n";

     for(i=0;i<p;i++){
          for(j=0;j<5;j++){
               if(j!=3){
                    cin>>a[i][j];
                    }
               }
          a[i][3]=a[i][2];
          }


     cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";

     i=a[0][1];                            

     while(done!=p){
          got=0;
          k=0;
          while(k<p){
               if(a[k][1]<=i){
                    if(a[k][2]!=0){
                         got=1;          
                         temp_burst=a[k][4];
                         temp_row=k;
                         idle=0;
                         break;
                         }
                    else
                         k++;             
                    }
               else{
                    if(idle==0)
                         printf("%5d-----------\n        |Idle  |\n",i);
                    idle=1;
                    break;
                    }
               }
          if(got!=0){
          k=0;
          while(a[k][1]<=i && k<p){
               if(a[k][2]!=0){
                    if(temp_burst>a[k][4]){
                         temp_burst=a[k][4];
                         temp_row=k;
                         }
                    }
               k++;
               }

          a[temp_row][2]-=1;
          
          if(i==a[0][1])   
               printf("%5d-----------\n        |p-%-4d|\n",i,a[temp_row][0]);
          else{
               if(pre_process_row!=temp_row)
                    printf("%5d-----------\n        |p-%-4d|\n",i,a[temp_row][0]);                   
               }

          pre_process_row=temp_row;

          if(a[temp_row][2]==0){
               done++;
               b[s][0]=a[temp_row][0];
               b[s][1]=a[temp_row][1];
               b[s][2]=i;
               b[s][3]=a[temp_row][3];
               b[s][4]=((i-a[temp_row][1])-a[temp_row][3])+1;        
               sum+=((i-a[temp_row][1])-a[temp_row][3])+1;
               s++;
               }
          }
     i++;
     }
     printf("%5d-----------\n",i); 

     cout<<endl<<endl;
     cout<<"Table of processes with completion record as they were completed\n\n";
     cout<<"\n\nProcess\tArrival\tFinish\tTotal\tWait\n------------------------------------\n";

     for(i=0;i<s;i++)
          cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i][3]<<"\t"<<b[i][4]<<"\n";

     cout<<"\n\nAvg. Wait time = "<<sum/p<<endl<<endl;
     cout<<"__________________________________________________________\nProgrammed by : Tanmay Chakrabarty, UITS, Rajshahi Campus.\n\n";

     system("pause");
     return 0;
     }


Following is a screenshot of the program with input and output.



I hope that you can now study and experiment the source code to write yours and that would be much better, more efficient and strict than this one.

Recommended Recommends

Comments

Contact Us