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

Popular posts from this blog

Shortest Remaining Time First (SRTF) Program in C++ || dot clu

Shortest Remaining Time First (SRTF) Program in C++ || dot clu Shortest Remaining Time First (SRTF) Shortest Remaining Time First (SRTF) Algorithm is preemptive version of Shortest Job First Algorithm. In this current process is executed until it is completed or a new process is added having lower burst time compare to the the remaining time for current process. SRTF algorithm may lead to starvation, if processes with lower burst time continues to add to cpu scheduler then the current process will never get a chance to get executed. For example consider the following table Process Arrival Time Burst Time P1 0 10 P2 1 6 P3 2 9 P4 3 4 At time t=0, Process P1 will start get executing as it is only the process present at that time. Then at t=1, Process P2 added to the CPU scheduler, at this time remaining time(Burst time) for Process P1 gets 9, as Burst time of P2 is less than the remaining time of other processes (for now there is only process P1) therefore pro...

First Fit Program and Algorithm in C++ || dot clu

First Fit Program and Algorithm in C++ || dot clu First Fit Algorithm in C and C++ Here you will learn about first fit algorithm in C and C++ with program examples. There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme. Suggestion: 1. First Fit 2.  Best Fit 3.  Worst Fit What is First Fit Memory Management Scheme? In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on. First Fit Algorithm Get no. of Processes and no. of blocks. After that get the size of each block and process requests. Now allocate processes if(block size >= process size) //allocate the process else //move on to next block Display the processes with the blocks that are allocated to a respective...

Round Robin Scheduling Program in C++ || dot clu

Round Robin Scheduling Program in C++ Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished. Round Robin Scheduling Algorithm 1. The queue structure in ready queue is of First In First Out (FIFO) type. 2. A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum. 3. The first process that arrives is selected and sent to the processor for execution. If it is not able to  complete its execution within the time quantum provided, then an interrupt is generated using an automated timer. 4. The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted. 5. The scheduler selects another process from the ready queue and dispa...