Asssignment Project
Asssignment Project
Task 1:
Create a program that simulates the FCFS scheduling algorithm and SJF
scheduling in a simple operating system environment to demonstrate its
behaviour and advantages. Provide clear output that shows the execution
order of processes, waiting times, turnaround times, and average statistics.
Compare the response time of each algorithm.
PROGRAM (CPP)
#include <iostream>
#include <vector>
#include <algorithm>
struct Process {
int pid; // Process ID
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
calculateWaitingTime(processes);
calculateTurnaroundTime(processes);
}
// Function to simulate Shortest Job First (SJF) scheduling
void simulateSJF(std::vector<Process>& processes) {
// Sort processes based on burst time (shortest job first)
std::sort(processes.begin(), processes.end(), [](const Process& a, const
Process& b) {
return a.burst_time < b.burst_time;
});
calculateWaitingTime(processes);
calculateTurnaroundTime(processes);
}
int main() {
int num_processes;
std::cout << "Enter the number of processes: ";
std::cin >> num_processes;
std::vector<Process> processes(num_processes);
// Calculate and display average waiting time and turnaround time for SJF
total_waiting_time = 0;
total_turnaround_time = 0;
for (const auto& process : processes) {
total_waiting_time += process.waiting_time;
total_turnaround_time += process.turnaround_time;
}
std::cout << "Average Waiting Time: " << total_waiting_time /
num_processes << "\n";
std::cout << "Average Turnaround Time: " << total_turnaround_time /
num_processes << "\n";
return 0;
}
OUTPUT
PROGRAM EXPLANATION
Program Structure:
2. Functions
- `calculateWaitingTime`: Calculates waiting time for each process.
- `calculateTurnaroundTime`: Calculates turnaround time for each process.
- `simulateFCFS`: Simulates FCFS scheduling algorithm.
- `simulateSJF`: Simulates SJF scheduling algorithm.
3. Main Function:
- Takes input for the number of processes and their arrival and burst times.
- Simulates FCFS scheduling and displays the order of process execution,
average waiting time, and average turnaround time.
- Simulates SJF scheduling and displays similar results for this algorithm as
well.
Algorithms:
Overall Flow:
1. input
- Number of processes and their arrival times along with burst times are
taken as input.
2. FCFS Simulation
- Processes are sorted based on arrival times.
- FCFS is simulated, and execution order, waiting time, and turnaround time
are calculated and displayed.
3. SJF Simulation
- Processes are sorted based on burst times.
- SJF is simulated, and execution order, waiting time, and turnaround time are
calculated and displayed.
4. Output
- The program outputs the execution order for both algorithms and the
average waiting time and turnaround time for each algorithm.
Presentation Points
1. Introduction
- Explain the purpose of the program: Simulating scheduling algorithms to
manage process execution in an operating system environment.
2. FCFS Explanation
- Detail how FCFS operates by executing processes in the order of their
arrival.
- Highlight the calculation of waiting and turnaround times for each process.
3. SJF Explanation
- Explain SJF, where processes with shorter burst times are prioritized.
- Discuss the impact on waiting and turnaround times.
4. Comparison
- Compare the execution order, waiting time, and turnaround time between
FCFS and SJF.
- Discuss the advantages and disadvantages of each algorithm.
5. Conclusion
- Summarize the behavior and advantages of FCFS and SJF.
- Mention their real-world applications and scenarios where each algorithm
performs well.
Task 2:
Write a program to implement the Dining Philosophers problem using threads
or processes for
synchronization. You need to create a solution that allows multiple
philosophers to share a
limited number of forks (resources) while avoiding deadlock and contention
issues.
Program 2 (CPP)
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
#define LEFT_FORK (philosopher_id + NUM_PHILOSOPHERS - 1) %
NUM_PHILOSOPHERS
#define RIGHT_FORK (philosopher_id + 1) % NUM_PHILOSOPHERS
pthread_mutex_t forks[NUM_PHILOSOPHERS];
pthread_t philosophers[NUM_PHILOSOPHERS];
int main() {
int ids[NUM_PHILOSOPHERS];
int i;
return 0;
}
OUTPUT
PROGRAM EXPLANATION
Elements Used:
2. Mutex Locks: Used to represent forks. Mutex locks ensure exclusive access
to the forks, allowing only one philosopher to pick up a fork at a time.
Algorithm Overview:
1. Mutex Locks (Forks): The mutex locks ensure that only one philosopher can
pick up a fork at a time. This prevents multiple philosophers from accessing the
same fork simultaneously, avoiding a race condition.
2. Resource Allocation: Philosophers pick up the left and right forks (mutex
locks) adjacent to them when they want to eat. The algorithm ensures that a
philosopher can only start eating if both the left and right forks are available.
3. Deadlock Avoidance: To avoid deadlock, each philosopher follows a strategy
to pick up forks. If a philosopher cannot acquire both forks, it puts down the
already picked fork and tries again after some time.
Workflow:
4. **Thread Joining and Cleanup:** Once all philosophers finish their tasks, the
threads are joined, and mutex locks are destroyed to release system resources.
Conclusion: