Skip to main content

C++ Program For PRIORITY WITH PREEMPTIVE Scheduling Algorithm

C++ Program For PRIORITY WITH PREEMPTIVE Scheduling Algorithm




Priority Based Scheduling

  • Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems.
  • Each process is assigned a priority. Process with highest priority is to be executed first and so on.
  • Processes with same priority are executed on first come first served basis.
  • Priority can be decided based on memory requirements, time requirements or any other resource requirement.
Priority Scheduling always selects the process(es) with the highest priority currently ready to run. If there is more than one process having the currently highest priority, you need a second scheduling algorithm to choose among these processes. Non-preemptive Priority Scheduling only selects a new process to run if the running process finished its work or yields (voluntarily) to the scheduler.
Preemptive Priority Scheduling is the same algorithm but if a new process having a higher priority than the currently running process arrives, it gets selected immediately. The new process has not to wait until the currently running process finishes or yields.

Source Code

  1. //C++ Program For PRIORITY WITH PREEMPTIVE Scheduling Algorithm
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5.  {

  6.    int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
  7.    cout<<"Enter the number of process : ";
  8.    cin>>n;
  9.    cout<<"\n Enter process : time priorities \n";
  10.    for(i=0;i<n;i++)
  11.     {
  12.       cout<<"\nProcess no  : "<<i+1<<" : ";
  13.       cin>>pt[i]>>pp[i];
  14.       p[i]=i+1;
  15.     }
  16.   for(i=0;i<n-1;i++)
  17.    {
  18.      for(int j=i+1;j<n;j++)
  19.      {
  20.        if(pp[i]<pp[j])
  21.        {
  22.          x=pp[i];
  23.          pp[i]=pp[j];
  24.          pp[j]=x;
  25.          x=pt[i];
  26.          pt[i]=pt[j];
  27.          pt[j]=x;
  28.          x=p[i];
  29.          p[i]=p[j];
  30.          p[j]=x;
  31.       }
  32.    }
  33. }
  34. w[0]=0;
  35. awt=0;
  36. t[0]=pt[0];
  37. atat=t[0];
  38. for(i=1;i<n;i++)
  39.  {
  40.    w[i]=t[i-1];
  41.    awt+=w[i];
  42.    t[i]=w[i]+pt[i];
  43.    atat+=t[i];
  44.  }
  45. cout<<"\n\n Job \t Burst Time \t Wait Time \t Turn Around Time   Priority \n";
  46. for(i=0;i<n;i++)
  47.   cout<<"\n"<<"\t\t"<<p[i]<<"\t\t"<<pt[i]<<"\t\t"<<w[i]<<"\t\t"<<t[i]<<"\t\t"<<pp[i];
  48. awt/=n;
  49. atat/=n;
  50. cout<<"\n Average Wait Time : %d \n"<<awt;
  51. cout<<"\n Average Turn Around Time : %d \n"<<atat;
  52. return 0;
  53. }

dotclu
dot clu

=====Also Watch Our  Video========
SJF-Shortest Job First or SJN (shortest job Next) || C++ program of SJF with input | dot clu
https://youtu.be/zXA3oS5p1UM

Get started With C || First Program in C || C tutorial || Hello world program in C || dot clu
link: https://youtu.be/Oqyoe1nJNjE

Install Virtual BOX in your PC | create Virtual Machine | downloading link in description || dot clu
link:https://youtu.be/x9ijUzOUyiY

How to Install Hyper V in windows 7?
link: https://youtu.be/Y64Dr3YNCSw

What is Bitcoin? || Earn Bitcoin money with your Android phone || 100% working || dot clu || Hindi
link: https://youtu.be/HYQgDQrSC1I

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...

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...

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 scheduling algorithms.  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(...