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

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

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