Skip to main content

C++ Program For PRIORITY WITH NON - PREEMPTIVE Scheduling Algorithm || dot clu

C++ Program For PRIORITY WITH  NON - PREEMPTIVE Scheduling Algorithm



It is important to distinguish preemptive from non-preemptive schedulingalgorithms. Preemption means the operating system moves a process from running to ready without the process requesting it. Without preemption, the system implements ``run to completion (or yield or block)''.

Non-Preemptive Scheduling
Non-Preemptive Scheduling means once a process starts its execution or the CPU is processing a specific process it cannot be halted or in other words we cannot preempt (take control) the CPU to some other process.
A computer system implementing this cannot support the execution of process in a multi task fashion. It executes all the processes in a sequential manner.
It is not practical as all processes are not of same priority and are not always known to the system in advance.
Source Code:
  1. #include <iostream>
  2. using namespace std;

  3. int main()
  4. {   
  5.  int n;
  6.     cout<<"enter your toal no. of process ";
  7.     cin>>n;
  8.     float total,wait[n];
  9.     float p[n],twaiting=0,waiting=0;
  10.     int proc;
  11.     int stack[n];
  12.     float brust[n],arrival[n],sbrust,temp[n],top=n,prority[n];
  13.     int i;
  14.     for(i=0;i<n;i++)
  15.     {
  16.         p[i]=i;
  17.         stack[i]=i;
  18.         cout<<"\nenter arival time "<<i+1<<": ";
  19.         cin>>arrival[i];
  20.         cout<<"enter brust time "<<i+1<<": ";
  21.         cin>>brust[i];
  22.         cout<<"enter prority time "<<i+1<<": ";
  23.         cin>>prority[i];
  24.         cout<<"\n";      
  25.         temp[i]=arrival[i];
  26.         sbrust=brust[i]+sbrust;              
  27.     }
  28.     for(i=0;i<sbrust;i++)
  29.     {    
  30.         //section 1
  31.         proc=stack[0];
  32.         if(temp[proc]==i)
  33.         {
  34.           
  35.             twaiting=0;
  36.         }
  37.         else
  38.         {    
  39.             twaiting=i-(temp[proc]);
  40.          }
  41.            temp[proc]=i+1;    
  42.            wait[proc]=wait[proc]+twaiting;
  43.             waiting=waiting+(twaiting);
  44.             brust[proc]=brust[proc]-1;
  45.                            
  46.         if(brust[proc]==0)
  47.         {
  48.             for(int x=0;x<top-1;x++)
  49.             {
  50.                 stack[x]=stack[x+1];   
  51.             }
  52.             top=top-1;       
  53.         for(int z=0;z<top-1;z++)
  54.         {   
  55.             if((prority[stack[0]]>prority[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
  56.             {
  57.                    int t=stack[0];
  58.             stack[0]=stack[z+1];
  59.             stack[z+1]=t;
  60.             }
  61.                 }
  62.         }  
  63.     }
  64. cout<<"\nAverage waiting time is: "<<waiting/n;
  65. float tu=(sbrust+waiting)/n;
  66. cout<<endl<<"Average turnaround time is: "<<tu;
  67. return 0;
  68. }


Comments