You are on page 1of 7

University of gondar

College of informatics
Information system

Operating system project

Name id
1. Abenezer nega gur/40679/13
2. Yohannes Berhanu gur/40191/13
3. Ejige Dessie gur/23115/13
4. Haile Nigusie gur/22852/13
5. Yonael Fikresilase gur/22949/13

SUBMITED TO :GASHAW DESALEGN


SUBMITED DATE:04-08-2015

Introduction to Priority scheduling


Priority scheduling is a CPU scheduling algorithm that is used in operating
systems to decide which process should be executed next. In this algorithm, each
process is assigned a priority value or level, based on some criteria. The process
with the highest priority value is executed first, and if two or more processes
have the same priority, then one of several other scheduling algorithms may be
used to determine which process to execute.
Priority values can be assigned to processes based on a variety of criteria,
such as the amount of CPU time the process has used, the amount of I/O the
process has performed, the amount of memory the process requires, or the
priority level assigned by the user or system administrator.
The priority levels can be either static or dynamic. In the case of static
priority levels, the priority of a process remains fixed for the duration of its
execution. In contrast, in dynamic priority levels, the priority of a process can
change over time based on the behavior of the process or the system.
There are two types of priority scheduling: preemptive and non-preemptive.
Preemptive priority scheduling can ensure that high-priority processes get
executed first, even if lower-priority processes are already running. However, it
may lead to lower-priority processes being starved of resources if high-priority
processes continuously arrive. To avoid this problem, some scheduling
algorithms may include aging, where the priority of a process increases over
time if it is waiting for a long time.
Our assignment is about non preemptive priority scheduling so we will
discuss more about this .

non-preemptive
Non-preemptive priority scheduling is a scheduling algorithm
used by operating systems to determine the order in which processes are
executed on a CPU. In non-preemptive priority scheduling, each process is
assigned a priority value, which indicates the relative importance of the process.
The scheduler selects the process with the highest priority to run next, and that
process continues to execute until it completes its execution or is blocked (e.g.,
waiting for I/O). If a process with a higher priority becomes ready to run while a
lower-priority process is running, the scheduler does not interrupt the lower-
priority process. Instead, the higher-priority process waits until the lower-
priority process completes its execution.
Non-preemptive priority scheduling is sometimes referred to as
"priority-based scheduling," "static priority scheduling," or "fixed-priority
scheduling." This scheduling algorithm has some advantages over other
scheduling algorithms, such as round-robin scheduling or first-come, first-served
scheduling, as it allows the operating system to give more processing time to
more important tasks. However, it can also lead to problems such as priority
inversion, where a low-priority process holds a resource that a high-priority
process needs to continue execution.
In non-preemptive priority scheduling, the priority of a process can be set in a
variety of ways. For example, it could be set based on the type of task the
process performs, the user who submitted the process, or the relative importance
of the process to the overall system performance. The priority value of a process
can be adjusted dynamically based on changing system conditions or user
preferences.

Here is an example of non preemptive priority in c++ language


Example 1
#include <iostream>
#include <queue>
using namespace std;
struct Process {
int id;
int burstTime;
int priority;
};

// Comparison function for priority queue


struct ComparePriority {
bool operator()(Process const& p1, Process const& p2)
{
// Higher priority processes come first
return p1.priority < p2.priority;
}
};

int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;

priority_queue<Process,vector<Process>, ComparePriority> pq;

for(int i=1; i<=n; i++) {


Process p;
cout << "Enter burst time and priority for process " << i << ": ";
cin >> p.burstTime >> p.priority;
p.id = i;
pq.push(p);
}

cout << "\nOrder of execution:\n";


int time = 0;
while(!pq.empty()) {
Process p = pq.top();
pq.pop();
cout << "Process " << p.id << " (burst time: " << p.burstTime << ", priority: " << p.priority
<< ")\n";
time += p.burstTime;
}
cout << "\nTotal execution time: " << time << "\n";

return 0;
}

Description for above code


In this implementation, we first ask the user to input the number of
processes, and then we create a priority queue to store the processes. We then
iterate over each process and ask the user to input the burst time and priority for
each process. The processes are then added to the priority queue based on their
priority.
Finally, we loop over the priority queue and execute each process one by
one. The order of execution is determined by the priority of each process, with
higher priority processes executing first. The total execution time is also
calculated and printed to the console.

Example 2:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Process {
int id;
int burstTime;
int priority;
};

bool compareProcesses(Process p1, Process p2) {


// Higher priority processes come first
return p1.priority < p2.priority;
}

int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;

vector<Process> processes(n);

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


cout << "Enter burst time and priority for process " << i+1 << ": ";
cin >> processes[i].burstTime >> processes[i].priority;
processes[i].id = i+1;
}

sort(processes.begin(), processes.end(), compareProcesses);

cout << "\nOrder of execution:\n";


int time = 0;
for(int i=0; i<n; i++) {
Process p = processes[i];
cout << "Process " << p.id << " (burst time: " << p.burstTime << ", priority: " <<
p.priority << ")\n";
time += p.burstTime;
}
cout << "\nTotal execution time: " << time << "\n";

return 0;
}

Description for above code

In this implementation, we also ask the user to input the number of


processes, and then we create a vector to store the processes. We then iterate
over each process and ask the user to input the burst time and priority for each
process. The processes are then added to the vector.
After that, we sort the processes based on their priority using the sort()
function and a custom comparison function. Finally, we loop over the sorted
vector and execute each process one by one. The order of execution is
determined by the priority of each process, with higher priority processes
executing first. The total execution time is also calculated and printed to the
console.

Overall, non-preemptive priority scheduling is a useful scheduling


algorithm that allows the operating system to prioritize the execution of
important tasks. However, it is not always the best choice, as it can lead to
problems such as priority inversion and can result in lower overall system
throughput than other scheduling algorithms.

You might also like