0% found this document useful (0 votes)
33 views

Narasimha Reddy

This document describes a scheduling algorithm that uses two levels of scheduling: fixed priority preemptive scheduling for the first queue and round-robin scheduling for the second queue. Processes are assigned to queues based on their priority. The algorithm is implemented using C++ with two queues, one priority queue for the first level, and one regular queue for the second level. Sample code is provided to demonstrate how the algorithm works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Narasimha Reddy

This document describes a scheduling algorithm that uses two levels of scheduling: fixed priority preemptive scheduling for the first queue and round-robin scheduling for the second queue. Processes are assigned to queues based on their priority. The algorithm is implemented using C++ with two queues, one priority queue for the first level, and one regular queue for the second level. Sample code is provided to demonstrate how the algorithm works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CSE-316

OPERATING SYSTEMS

Submitted by:

Name:Asam Narasimha Reddy


Reg_No:12103515
Roll_No:13
Section:k21BE

Page | 1
The methodology adopted to solve the problem:
To implement a scheduling program with two levels, we will use a
combination of fixed priority preemptive scheduling and round-robin scheduling.
We will have two queues, Queue 1 and Queue 2, where Queue 1 will have higher
priority than Queue 2.

When a new process arrives, we will check if Queue 1 is empty or not. If


Queue 1 is empty, we will schedule the process in Queue 1. If Queue 1 is not
empty, we will check the priority of the arriving process. If the priority of the
arriving process is higher than the process currently running in Queue 1, we will
preempt the process running in Queue 1 and move it to Queue 2. We will then
schedule the new process in Queue 1.

For Queue 1, we will use fixed priority preemptive scheduling, where the process
with the highest priority (priority 0) will be executed first. If two processes have
the same priority, we will use round-robin scheduling to allocate the CPU time.
Each process in Queue 1 will be allocated CPU time in multiples of 2.

For Queue 2, we will use round-robin scheduling, where each process will be
allocated CPU time for a fixed time slice. Once a process finishes its time slice, it
will be moved to the end of the queue, and the next process will be scheduled.

We will continue this process until both Queues are empty.

To solve this problem, we can create two queues. The first queue will be used for
fixed priority preemptive scheduling and the second queue will be used for round-
robin scheduling. We can use a priority queue to implement the first queue where
priority 0 is the highest priority. If a process P1 is running and another process P2
with higher priority comes, then P2 preempts P1 and P1 will go to the second level
queue. The time for which a process will strictly execute must be considered in
multiples of 2.

Once Queue 1 becomes empty, Queue 2 will be processed. The priority of Queue
2 has lower priority than in Queue 1.

Here’s a sample code in C++ that implements the above methodology:

Page | 2
Implementation of Program :

#include<iostream>
#include<queue>
#include<vector>
using namespace std;

struct Process {
int pid;
int arrivalTime;
int burstTime;
int priority;
int remainingTime;
int quantum;
};

bool operator<(const Process& p1, const Process& p2) {


return p1.priority < p2.priority;
}

void execute(Process& p, int& currentTime) {


cout << "Process " << p.pid << " starts execution at time " <<
currentTime << endl;
currentTime += p.quantum;
p.remainingTime -= p.quantum;
if (p.remainingTime <= 0) {
currentTime += p.remainingTime;
cout << "Process " << p.pid << " finished execution at time " <<
currentTime << endl;
} else {
cout << "Process " << p.pid << " is preempted at time " <<
currentTime << endl;
}
}

void schedule(vector<Process>& processes, int quantum1, int quantum2)


Page | 3
{
priority_queue<Process> q1;
queue<Process> q2;
int n = processes.size();
int currentTime = 0;
for (int i = 0; i < n; i++) {
q1.push(processes[i]);
}
while (!q1.empty() || !q2.empty()) {
if (!q1.empty()) {
Process p = q1.top();
q1.pop();
p.quantum = min(quantum1, p.remainingTime);
execute(p, currentTime);
if (p.remainingTime > 0) {
q2.push(p);
}
} else {
Process p = q2.front();
q2.pop();
p.quantum = min(quantum2, p.remainingTime);
execute(p, currentTime);
if (p.remainingTime > 0) {
q2.push(p);
}
}
}
}

int main() {
vector<Process> processes = {
{1, 0, 8, 1, 8},
{2, 1, 4, 2, 4},
{3, 2, 9, 3, 9},
{4, 3, 5, 4, 5},
{5, 4, 2, 5, 2},
Page | 4
};
int quantum1 = 2;
int quantum2 = 2;
schedule(processes, quantum1, quantum2);
return 0;
}

OUTPUT SNAPSHOT:

Page | 5
Page | 6
Page | 7

You might also like