in ,

Priority Scheduling Program In C

priority scheduling program in c.We know that different scheduling methods are employed to allocate resources to carry out various activities. Priority-based scheduling is one of these CPU methods.

Operating systems often use the C priority scheduling program to plan the execution of several processes as they enter the CPU.

Each process has a priority assigned to it, and the CPU is assigned to the process with the greatest priority.

Here, the term priority scheduling program in c comes in. so what is that? Let’s find out.

What is Priority Scheduling Algorithm?

Each process has a priority assigned to it in the priority scheduling method, and as each process enters the queue, it is stored according to its priority so that processes with higher priorities are handled first.

The scheduling of equal priority processes is done in FCFS order.

Every method is outlined as a Priority Number in an operating system’s priority-based scheduling algorithm. The processes are carried out based on this Priority Number.

In real-time systems, this scheduling approach is typically highly helpful. Priorities 1, 2, 3, and so forth are carried out in order starting with the process with the highest priority.

There are two types of schedules:

1.  Non-preemptive

According to this approach, the process that is now running is not interrupted if a new process with a higher priority than the one it is replacing arrives. The newly arrived process is instead elevated to the front of the ready queue.

2. Preemptive

It is a priority scheduling technique that preempts the CPU when a new process enters the system, meaning it will begin executing the new process if it has a higher priority than the one that is presently running.

Characteristics

  • According to the order of the processes, it schedules the procedure.
  • Lower number = higher priority
  • We schedule based on FCFS if two or more processes are equal in priority.
  • Starvation is a significant issue with priority scheduling.
  • Aging is a strategy for gradually raising the priority of the processes that have been waiting in the system for a long time. Aging is a solution to the problem of famine.

What criteria does the priority depend on?

Task priority can be decided either internally or externally.

Some of the internal variables include:

  • Time limits the process’s
  • memory needs
  • Time limits the process’s

While the external causes could include:

  • The task’s importance,
  • the amount allocated for computer use

The implementation

  • Enter the processes’ arrival time, burst time, and priority first.
  • The process with the lowest arrival time will be scheduled first; if there are two or more processes with the lowest arrival times, the process with the highest priority will be scheduled first.
  • Now, more procedures will be scheduled based on their arrival time and priority. Filter by process number if two processes have the priority equal.
  • We can schedule the processes based on their priority after they have all arrived.

Note: They will make it very obvious in the question which number has higher priority and which number has lesser priority.

Priority-Scheduling-Program-In-C-input-output
Priority Scheduling Program In C Priority Scheduling Program In C :

Boundaries

The issue arises when the operating system assigns a task a very low priority, causing it to wait in the queue for a longer period without being completed by the CPU.

This technique also referred to as “Starvation” or “Infinite Blocking,” could result in a very long wait if the user needs it.

Solution

Numerous operating systems employ a process known as “aging,” in which a low-priority operation gradually gains priority while waiting in the queue.

The process will be carried out even if it is not given high priority.

Also, Read For Your Career: What companies are in the consumer services field?

Priority Scheduling Program In C :

#include<stdio.h>
#include<conio.h>
void main()
 {
   int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;

   printf("Enter the number of process : ");
   scanf("%d",&n);

   printf("\n Enter process : time priorities \n");

   for(i=0;i<n;i++)
    {
      printf("\nProcess no %d : ",i+1);
      scanf("%d  %d",&pt[i],&pp[i]);
      p[i]=i+1;  //this contains process numbers
    }

//This section sorting burst time, priority and process number in ascending order using selection sort
  for(i=0;i<n-1;i++)
   {
     for(int j=i+1;j<n;j++)
     {
       if(pp[i]<pp[j])
       {
         x=pp[i];
         pp[i]=pp[j];
         pp[j]=x;
         x=pt[i];
         pt[i]=pt[j];
         pt[j]=x;
         x=p[i];
         p[i]=p[j];
         p[j]=x;
      }
   }
}


w[0]=0; //waiting time for first process is zero
awt=0;
t[0]=pt[0];
atat=t[0];

//calculate waiting time
for(i=1;i<n;i++)
 {
   w[i]=t[i-1];
   awt+=w[i];
   t[i]=w[i]+pt[i];
   atat+=t[i];
 }

printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time   Priority \n");

for(i=0;i<n;i++)

  printf("\n %d \t\t %d  \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);

awt/=n; //calculte average waiting time
atat/=n; //calculte average turnaround time

printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
getch();
}

Priority Scheduling Program In C Output:

Enter the number of process : 4

 Enter process : time priorities

Process no 1 : 6

3

Process no 2 : 2
2

Process no 3 : 14
1

Process no 4 : 6
4


 Job  Burst Time      Wait Time    Turn Around Time   Priority

 3        14               0              6               14

 2         2               14             11              16

 1         6               16             15              22

 4         6               22             18              28

 Average Wait Time : 13

 Average Turn Around Time : 20

Written by webwoob

Leave a Reply

Your email address will not be published. Required fields are marked *

Bottled-and-jarred-packaged-goods

Bottled and jarred packaged goods

what-will-be-the-output-of-the-following-python-code

what will be the output of the following python code MCQ question