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:
- #include <iostream>
- using namespace std;
- int main()
- {
- int n;
- cout<<"enter your toal no. of process ";
- cin>>n;
- float total,wait[n];
- float p[n],twaiting=0,waiting=0;
- int proc;
- int stack[n];
- float brust[n],arrival[n],sbrust,temp[n],top=n,prority[n];
- int i;
- for(i=0;i<n;i++)
- {
- p[i]=i;
- stack[i]=i;
- cout<<"\nenter arival time "<<i+1<<": ";
- cin>>arrival[i];
- cout<<"enter brust time "<<i+1<<": ";
- cin>>brust[i];
- cout<<"enter prority time "<<i+1<<": ";
- cin>>prority[i];
- cout<<"\n";
- temp[i]=arrival[i];
- sbrust=brust[i]+sbrust;
- }
- for(i=0;i<sbrust;i++)
- {
- //section 1
- proc=stack[0];
- if(temp[proc]==i)
- {
- twaiting=0;
- }
- else
- {
- twaiting=i-(temp[proc]);
- }
- temp[proc]=i+1;
- wait[proc]=wait[proc]+twaiting;
- waiting=waiting+(twaiting);
- brust[proc]=brust[proc]-1;
- if(brust[proc]==0)
- {
- for(int x=0;x<top-1;x++)
- {
- stack[x]=stack[x+1];
- }
- top=top-1;
- for(int z=0;z<top-1;z++)
- {
- if((prority[stack[0]]>prority[stack[z+1]]) && (arrival[stack[z+1]] <= i+1))
- {
- int t=stack[0];
- stack[0]=stack[z+1];
- stack[z+1]=t;
- }
- }
- }
- }
- cout<<"\nAverage waiting time is: "<<waiting/n;
- float tu=(sbrust+waiting)/n;
- cout<<endl<<"Average turnaround time is: "<<tu;
- return 0;
- }
Comments
Post a Comment