0% found this document useful (0 votes)
3 views13 pages

Osy Microproject

This document outlines a micro project focused on implementing the Priority Scheduling Algorithm in C, which manages process execution based on assigned priority levels. It covers the algorithm's aim, methodology, code implementation, and applications in various fields, emphasizing the importance of optimizing system performance while addressing challenges such as process starvation. The project also highlights the skills developed and the key performance metrics analyzed during the implementation.

Uploaded by

Pratiksha Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Osy Microproject

This document outlines a micro project focused on implementing the Priority Scheduling Algorithm in C, which manages process execution based on assigned priority levels. It covers the algorithm's aim, methodology, code implementation, and applications in various fields, emphasizing the importance of optimizing system performance while addressing challenges such as process starvation. The project also highlights the skills developed and the key performance metrics analyzed during the implementation.

Uploaded by

Pratiksha Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Write a C program to implement Priority Scheduling Algorithm

Chapter No: 1

Introduction

Priority scheduling is a method used in operating systems to manage the order in which
processes are executed based on their priority levels. Each process is assigned a priority,
and the CPU is allocated to the process with the highest priority first. This helps ensure
that important tasks are completed quickly. However, it can lead to lower-priority
processes being delayed or "starved" if there are always higher-priority tasks available.
Overall, priority scheduling is essential for improving system efficiency and
responsiveness.
This micro project focuses on implementing a Priority Scheduling algorithm, which
organizes processes based on their assigned priority levels. By allocating CPU time to
the highest-priority processes first, priority scheduling enhances responsiveness,
particularly in systems where certain tasks require immediate attention.We will
implement both preemptive and non-preemptive priority scheduling techniques. The
project will involve coding and simulating how these methods work, highlighting their
advantages, such as improved responsiveness, as well as challenges like the potential
for lower-priority processes to be delayed.
This micro project is centered around Priority Scheduling, a vital algorithm in operating
systems that manages the execution of processes based on their assigned priority levels.
By prioritizing certain tasks, this scheduling method ensures that critical processes are
executed promptly, which is especially important in environments where time-sensitive
operations are required.
Example:

Waiting Time:51 Turnaround Time:28

Department of Computer Technology 1


Write a C program to implement Priority Scheduling Algorithm

Chapter No:2
Aim of Micro project

Priority scheduling is a CPU scheduling algorithm designed to allocate system resources by


prioritizing processes based on their importance. Each process is assigned a priority value, with
higher-priority processes (usually those with lower numerical values) being executed first. The
method can be implemented in two ways: non-pre-emptive, where a running process continues
until it finishes, or pre-emptive, where the CPU can switch to a newly arrived higher-priority
process. The aim of priority scheduling is to handle critical tasks efficiently, reduce waiting
times for important processes, and improve overall system performance. Key metrics like
turnaround time, waiting time, and completion time are used to evaluate its effectiveness. While
it offers advantages such as faster handling of urgent tasks and better resource management, it
also has challenges, including the risk of starvation for lower-priority processes and increased
complexity in pre-emptive systems.

Department of Computer Technology 2


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 3

Course Outcome Achieved

Upon completion of this micro project, we achieved:

 Learned to calculate and analyse key performance metrics such as


Completion Time (CT), Turnaround Time (TAT), and Waiting Time (WT),
and understood how to optimize system efficiency.

 Enhanced programming skills in C, focusing on data structures and


algorithms.

 Represented scheduling results through tables and Gantt charts, improving


our ability to communicate technical solutions effectively.

 Evaluated the algorithm's strengths and limitations, understanding its practical


applications and areas where improvements are necessary.

Department of Computer Technology 3


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 4

Methodology Followed

 Define the Problem Scope

Efficiently schedule processes based on priority, minimizing metrics like


turnaround time, waiting time, and response time.

 Input Data Collection


o Process ID (PID).
o Arrival Time (AT) of each process.
o Burst Time (BT) or execution time.
o Priority value.

 Sorting Based on Priority

Non-preemptive Scheduling: Sort processes based on priority when they arrive.

Preemptive Scheduling: Continuously check for new processes arriving with a higher
priority. If found, preempt the current process and allocate the CPU to the higher-
priority process.

 Execute Processes

o Pick the highest-priority process from the queue.


o Allocate CPU to that process.
o For pre-emptive scheduling, switch if a higher-priority process comes in.

 Calculate Metrics

o Completion Time (CT): When the process finishes.


o Turnaround Time (TAT): TAT=CT−ATTAT = CT - ATTAT=CT−AT.
o Waiting Time (WT): WT=TAT−BTWT = TAT - BTWT=TAT−BT.

 Display Results

o Show results in a table with:


o Process ID, Priority, CT, TAT, WT.

Department of Computer Technology 4


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 5

Resources Used

Priority Queue

A priority queue or heap is used to maintain the processes, where each process has a
priority value. The priority queue ensures efficient access to the highest-priority
process (highest or lowest depending on the system's configuration).

CPU Scheduler

The CPU scheduler uses the priority queue to select the next process for execution.
The scheduler ensures that processes with higher priority values are chosen first.

Timer (for Preemption)

In a preemptive priority scheduling system, a timer is essential for periodically


interrupting the CPU to check for process priority changes or to enforce time limits on
running processes. The timer can trigger context switching when a higher-priority
process arrives.

Department of Computer Technology 5


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 6

Code and Output Screenshot

Code:

#include <stdio.h>

struct Process {
int id; // Process ID
int burst_time; // Burst Time
int priority; // Priority
int waiting_time; // Waiting Time
int turnaround_time; // Turnaround Time
};

// Function to sort processes based on priority (higher priority first)


void sort_by_priority(struct Process proc[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].priority < proc[j].priority) {
struct Process temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}

// Function to calculate Waiting Time and Turnaround Time


void calculate_times(struct Process proc[], int n) {
proc[0].waiting_time = 0; // Waiting time for the first process is 0
proc[0].turnaround_time = proc[0].burst_time; // Turnaround time = Burst time for first
process

// Calculate waiting time and turnaround time for all processes


for (int i = 1; i < n; i++) {
proc[i].waiting_time = proc[i - 1].waiting_time + proc[i - 1].burst_time;
proc[i].turnaround_time = proc[i].waiting_time + proc[i].burst_time;
}
}

// Function to print Gantt Chart

void print_gantt_chart(struct Process proc[], int n) {

Department of Computer Technology 6


Write a C program to implement Priority Scheduling Algorithm

printf("\nGantt Chart:\n");
printf(" ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < proc[i].burst_time; j++) printf("--");
printf(" ");
}

printf("\n|");

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


int len = proc[i].burst_time / 2;
for (int j = 0; j < len; j++) printf(" ");
printf("P%d", proc[i].id);
for (int j = 0; j < len; j++) printf(" ");
printf("|");
}
printf("\n ");

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


for (int j = 0; j < proc[i].burst_time; j++) printf("--");
printf(" ");
}
printf("\n0");

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


printf("%*d", proc[i].burst_time + 2, proc[i].turnaround_time);
}
printf("\n");
}

// Function to display the table of process details


void print_table(struct Process proc[], int n) {
printf("\n%-10s%-15s%-10s%-15s%-15s\n", "Process", "Burst Time", "Priority", "Waiting
Time", "Turnaround Time");
for (int i = 0; i < n; i++) {
printf("P%-9d%-15d%-10d%-15d%-15d\n", proc[i].id, proc[i].burst_time,
proc[i].priority, proc[i].waiting_time, proc[i].turnaround_time);
}
}

// Function to calculate and print average waiting and turnaround times


void calculate_and_print_averages(struct Process proc[], int n) {
float total_waiting_time = 0, total_turnaround_time = 0;
for (int i = 0; i < n; i++) {
total_waiting_time += proc[i].waiting_time;
total_turnaround_time += proc[i].turnaround_time;
}
printf("\nAverage Waiting Time: %.2f\n", total_waiting_time / n);
printf("Average Turnaround Time: %.2f\n", total_turnaround_time / n);
}

Department of Computer Technology 7


Write a C program to implement Priority Scheduling Algorithm

// Main function
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process proc[n];

// Input process details


for (int i = 0; i < n; i++) {
printf("\nEnter details for Process %d:\n", i + 1);
proc[i].id = i + 1;
printf("Burst Time: ");
scanf("%d", &proc[i].burst_time);
printf("Priority (higher value means higher priority): ");
scanf("%d", &proc[i].priority);
}

// Sort processes by priority and calculate times


sort_by_priority(proc, n);
calculate_times(proc, n);

// Display formatted table


print_table(proc, n);

// Display Gantt chart


print_gantt_chart(proc, n);

// Calculate and print average times


calculate_and_print_averages(proc, n);

return 0;
}

Department of Computer Technology 8


Write a C program to implement Priority Scheduling Algorithm

Output:

Department of Computer Technology 9


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 7

Skills Developed

• Programming Skills: Learned to write efficient code for sorting and managing
processes based on priority.

• Analytical Skills: Enhanced the ability to calculate and interpret performance


metrics such as Turnaround Time (TAT), Waiting Time (WT), and Completion Time
(CT).Time Management: Prioritized tasks and managed time efficiently to complete
the project within deadlines.

• Problem Solving: Enhanced problem-solving skills through debugging and


testing the algorithm.

Department of Computer Technology 10


Write a C program to implement Priority Scheduling Algorithm

Chapter No: 8

Applications of Microproject

• Operating Systems: Handles high-priority tasks such as interrupts and kernel


processes over user-level processes.

• Real-time Systems: Used in embedded systems, robotics, and industrial


automation where certain tasks (e.g., sensor data processing) must be completed
promptly.

• Healthcare Systems: Used in hospital management software to prioritize


emergency cases or critical patient monitoring systems.

• Military Systems: Used in mission-critical applications like missile control


systems or battlefield communication, where timely processing is vital.

Department of Computer Technology 11


Write a C program to implement Priority Scheduling Algorithm

Conclusion

Priority scheduling is a vital CPU scheduling algorithm that ensures the efficient execution of
processes based on their assigned priority levels. By giving preference to high-priority tasks, it
optimizes system performance and resource utilization, making it particularly suitable for time-
sensitive applications such as real-time systems, operating systems, and multimedia processing.
The algorithm can be implemented in both pre-emptive and non-preemptive forms, offering
flexibility depending on system requirements. While it provides significant advantages, such
as faster handling of critical tasks, it also presents challenges like potential starvation of lower-
priority processes. These can be mitigated with techniques like aging, which dynamically
adjusts priorities over time. Overall, priority scheduling is a powerful tool for managing
computational resources effectively in diverse fields, balancing system responsiveness and
fairness.

Department of Computer Technology 12


Write a C program to implement Priority Scheduling Algorithm

References

• Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating System


Concepts.
Wiley.
• Tanenbaum, Andrew S. Modern Operating Systems. Pearson.
• GeeksforGeeks. "Priority Scheduling Algorithms Operating System."
• TutorialsPoint. "Operating System – Priority Scheduling Algorithms."

Department of Computer Technology 13

You might also like