Fcfs
#include <iostream>
#include <vector>
using namespace std;
struct Process {
int id;
int arrivalTime;
int burstTime;
};
void calculateWaitingTime(vector<Process>& processes,
vector<int>& waitingTimes) {
int currentTime = 0;
for (int i = 0; i < processes.size(); i++) {
if (processes[i].arrivalTime > currentTime) {
currentTime = processes[i].arrivalTime;
}
waitingTimes[i] = currentTime - processes[i].arrivalTime;
currentTime += processes[i].burstTime;
}
}
void calculateTurnaroundTime(const vector<Process>&
processes, const vector<int>& waitingTimes, vector<int>&
turnaroundTimes) {
for (int i = 0; i < processes.size(); i++) {
turnaroundTimes[i] = processes[i].burstTime +
waitingTimes[i];
}
}
void calculateAverageTimes(const vector<int>&
waitingTimes, const vector<int>& turnaroundTimes) {
double totalWaitingTime = 0;
double totalTurnaroundTime = 0;
for (int i = 0; i < waitingTimes.size(); i++) {
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
}
double averageWaitingTime = totalWaitingTime /
waitingTimes.size();
double averageTurnaroundTime = totalTurnaroundTime /
turnaroundTimes.size();
cout << "Average Waiting Time: " << averageWaitingTime
<< endl;
cout << "Average Turnaround Time: " <<
averageTurnaroundTime << endl;
cout<<"komaljangid0827cs211127"<<endl;
}
int main() {
vector<Process> processes = { {1, 0, 6}, {2, 3, 2}, {3, 5, 8}, {4,
9, 4}, {5, 10, 1} };
vector<int> waitingTimes(processes.size());
vector<int> turnaroundTimes(processes.size());
calculateWaitingTime(processes, waitingTimes);
calculateTurnaroundTime(processes, waitingTimes,
turnaroundTimes);
calculateAverageTimes(waitingTimes, turnaroundTimes);
cout<<"komaljangid0827cs211127"<<endl;
return 0;
}
Sjf
#include <iostream>
using namespace std;
int main() {
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
cout << "Enter number of process: ";
cin >> n;
cout << "Enter Burst Time:" << endl;
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
cout << "P" << i + 1 << ": ";
cin >> A[i][1];
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
cout << "P BT WT TAT" << endl;
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
cout << "P" << A[i][0] << " " << A[i][1] << " "
<< A[i][2] << " " << A[i][3] << endl;
}
avg_tat = (float)total / n;
cout << "Average Waiting Time= " << avg_wt << endl;
cout << "Average Turnaround Time= " << avg_tat <<
endl;
cout<<"komaljangid0827cs211127"<<endl;
}
Priority cpu
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int id;
int arrivalTime;
int burstTime;
int priority;
};
bool compareArrivalTime(const Process& p1, const Process&
p2) {
return p1.arrivalTime < p2.arrivalTime;
}
bool comparePriority(const Process& p1, const Process& p2)
{
return p1.priority < p2.priority;
}
void calculateWaitingTime(vector<Process>& processes,
vector<int>& waitingTimes) {
vector<Process> sortedProcesses = processes;
sort(sortedProcesses.begin(), sortedProcesses.end(),
compareArrivalTime);
int currentTime = 0;
int completedProcesses = 0;
while (completedProcesses < processes.size()) {
int highestPriority = -1;
int highestPriorityIndex = -1;
for (int i = 0; i < sortedProcesses.size(); i++) {
if (sortedProcesses[i].arrivalTime <= currentTime &&
sortedProcesses[i].priority > highestPriority) {
highestPriority = sortedProcesses[i].priority;
highestPriorityIndex = i;
}
}
if (highestPriorityIndex == -1) {
currentTime++;
continue;
}
waitingTimes[sortedProcesses[highestPriorityIndex].id] =
currentTime -
sortedProcesses[highestPriorityIndex].arrivalTime;
currentTime +=
sortedProcesses[highestPriorityIndex].burstTime;
sortedProcesses.erase(sortedProcesses.begin() +
highestPriorityIndex);
completedProcesses++;
}
}
void calculateTurnaroundTime(const vector<Process>&
processes, const vector<int>& waitingTimes, vector<int>&
turnaroundTimes) {
for (int i = 0; i < processes.size(); i++) {
turnaroundTimes[processes[i].id] =
processes[i].burstTime + waitingTimes[processes[i].id];
}
}
void calculateAverageTimes(const vector<int>&
waitingTimes, const vector<int>& turnaroundTimes) {
double totalWaitingTime = 0;
double totalTurnaroundTime = 0;
for (int i = 0; i < waitingTimes.size(); i++) {
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
}
double averageWaitingTime = totalWaitingTime /
waitingTimes.size();
double averageTurnaroundTime = totalTurnaroundTime /
turnaroundTimes.size();
cout<<"komaljangid0827cs211127"<<endl;
cout << "Average Waiting Time: " << averageWaitingTime
<< endl;
cout << "Average Turnaround Time: " <<
averageTurnaroundTime << endl;
}
int main() {
vector<Process> processes = { {0, 0, 6, 2}, {1, 2, 3, 1}, {2, 5,
1, 3}, {3, 9, 7, 2}, {4, 12, 4, 4} };
vector<int> waitingTimes(processes.size());
vector<int> turnaroundTimes(processes.size());
calculateWaitingTime(processes, waitingTimes);
calculateTurnaroundTime(processes, waitingTimes,
turnaroundTimes);
calculateAverageTimes(waitingTimes, turnaroundTimes);
cout<<"komaljangid0827cs211127"<<endl;
return 0;
}}
Round robin
// C++ program for implementation of RR scheduling
#include<iostream>
using namespace std;
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
cout<<"komaljangid0827cs211127"<<endl;
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner
// until all of them are not done.
while (1)
{
bool done = true;
// Traverse all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending
process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has
been processed
t += quantum;
// Decrease the burst_time of
current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has
been processed
t = t + rem_bt[i];
// Waiting time is current time minus
time
// used by this process
wt[i] = t - bt[i];
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == true)
break;
}
}
// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[])
{
cout<<"komaljangid0827cs211127"<<endl;
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
cout << "PN\t "<< " \tBT "
<< " WT " << " \tTAT\n";
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
// Driver code
int main()
{
// process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
// Burst time of all processes
int burst_time[] = {10, 5, 8};
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Various cpu scheduling algo
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int id;
int arrivalTime;
int burstTime;
int priority;
};
bool compareArrivalTime(const Process& p1, const Process&
p2) {
return p1.arrivalTime < p2.arrivalTime;
}
bool compareBurstTime(const Process& p1, const Process&
p2) {
return p1.burstTime < p2.burstTime;
}
bool comparePriority(const Process& p1, const Process& p2)
{
return p1.priority < p2.priority;
}
void calculateWaitingTime(const vector<Process>&
processes, vector<int>& waitingTimes) {
vector<Process> sortedProcesses = processes;
sort(sortedProcesses.begin(), sortedProcesses.end(),
compareArrivalTime);
int currentTime = 0;
int completedProcesses = 0;
while (completedProcesses < processes.size()) {
int shortestBurstTime = -1;
int shortestBurstTimeIndex = -1;
for (int i = 0; i < sortedProcesses.size(); i++) {
if (sortedProcesses[i].arrivalTime <= currentTime) {
if (shortestBurstTime == -1 ||
sortedProcesses[i].burstTime < shortestBurstTime) {
shortestBurstTime = sortedProcesses[i].burstTime;
shortestBurstTimeIndex = i;
}
} else {
break;
}
}
if (shortestBurstTimeIndex == -1) {
currentTime++;
continue;
}
waitingTimes[sortedProcesses[shortestBurstTimeIndex].id] =
currentTime -
sortedProcesses[shortestBurstTimeIndex].arrivalTime;
currentTime +=
sortedProcesses[shortestBurstTimeIndex].burstTime;
sortedProcesses.erase(sortedProcesses.begin() +
shortestBurstTimeIndex);
completedProcesses++;
}
}
void calculateTurnaroundTime(const vector<Process>&
processes, const vector<int>& waitingTimes, vector<int>&
turnaroundTimes) {
for (int i = 0; i < processes.size(); i++) {
turnaroundTimes[processes[i].id] =
processes[i].burstTime + waitingTimes[processes[i].id];
}
}
void calculateAverageTimes(const vector<int>&
waitingTimes, const vector<int>& turnaroundTimes) {
double totalWaitingTime = 0;
double totalTurnaroundTime = 0;
for (int i = 0; i < waitingTimes.size(); i++) {
totalWaitingTime += waitingTimes[i];
totalTurnaroundTime += turnaroundTimes[i];
}
double averageWaitingTime = totalWaitingTime /
waitingTimes.size();
double averageTurnaroundTime = totalTurnaroundTime /
turnaroundTimes.size();
cout << "Average Waiting Time: " << averageWaitingTime
<< endl;
cout << "Average Turnaround Time: " <<
averageTurnaroundTime << endl;
}
int main() {
vector<Process> processes = { {0, 0, 6, 2}, {1, 2, 3, 1}, {2, 5,
1, 3}, {3, 9, 7, 2}, {4, 12, 4, 4} };
vector<int> waitingTimes(processes.size());
vector<int> turnaroundTimes(processes.size());
cout << "FCFS Scheduling:" << endl;
calculateWaitingTime(processes, waitingTimes);
calculateTurnaroundTime(processes, waitingTimes,
turnaroundTimes);
calculateAverageTimes(waitingTimes, turnaroundTimes);
cout<<"komaljangid0827cs211127"<<endl;
cout << endl;
cout << "SJF Scheduling:" << endl;
sort(processes.begin(), processes.end(),
compareBurstTime);
calculateWaitingTime(processes, waitingTimes);
calculateTurnaroundTime(processes, waitingTimes,
turnaroundTimes);
calculateAverageTimes(waitingTimes, turnaroundTimes);
cout << endl;
cout << "Priority Scheduling:" << endl;
sort(processes.begin(), processes.end(), comparePriority);
calculateWaitingTime(processes, waitingTimes);
calculateTurnaroundTime(processes, waitingTimes,
turnaroundTimes);
calculateAverageTimes(waitingTimes, turnaroundTimes);
cout<<"komaljangid0827cs211127"<<endl;
cout << endl;
return 0;
}
Dining philosopher
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
pthread_mutex_t forks[NUM_PHILOSOPHERS];
void* philosopher(void* arg) {
int id = *(int*)arg;
int left_fork = id;
int right_fork = (id + 1) % NUM_PHILOSOPHERS;
while (true) {
// Think
std::cout << "Philosopher " << id << " is thinking.\n";
sleep(2);
// Pick up forks
pthread_mutex_lock(&forks[left_fork]);
pthread_mutex_lock(&forks[right_fork]);
// Eat
std::cout << "Philosopher " << id << " is eating.\n";
sleep(2);
// Put down forks
pthread_mutex_unlock(&forks[left_fork]);
pthread_mutex_unlock(&forks[right_fork]);
}
pthread_exit(NULL);
}
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopherIds[NUM_PHILOSOPHERS];
// Initialize forks
for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {
pthread_mutex_init(&forks[i], NULL);
}
// Create philosopher threads
for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {
philosopherIds[i] = i;
pthread_create(&philosophers[i], NULL, philosopher,
&philosopherIds[i]);
}
// Wait for threads to finish
for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {
pthread_join(philosophers[i], NULL);
}
// Destroy forks
for (int i = 0; i < NUM_PHILOSOPHERS; ++i) {
pthread_mutex_destroy(&forks[i]);
}
return 0;
}
Reader writers
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#define NUM_READERS 3
#define NUM_WRITERS 2
pthread_mutex_t mutex;
pthread_cond_t readerCondition, writerCondition;
int resource = 0;
int numReaders = 0;
void* reader(void* arg) {
while (true) {
// Acquire lock
pthread_mutex_lock(&mutex);
numReaders++;
// Wait for writers to finish
while (resource == -1) {
pthread_cond_wait(&readerCondition, &mutex);
}
// Read resource
std::cout << "Reader " << *((int*)arg) << " reads
resource: " << resource << "\n";
usleep(1000000);
// Release lock
numReaders--;
if (numReaders == 0) {
pthread_cond_signal(&writerCondition);
}
pthread_mutex_unlock(&mutex);
usleep(1000000);
}
pthread_exit(NULL);
}
void* writer(void* arg) {
while (true) {
// Acquire lock
pthread_mutex_lock(&mutex);
// Wait for readers to finish
while (numReaders > 0 || resource == -1) {
pthread_cond_wait(&writerCondition, &mutex);
}
// Write to resource
resource = *((int*)arg);
std::cout << "Writer writes resource: " << resource <<
"\n";
usleep(1000000);
std::cout<<"komaljangid0827cs211127"<<std::endl;
// Release lock
resource = -1;
pthread_cond_broadcast(&readerCondition);
pthread_cond_signal(&writerCondition);
pthread_mutex_unlock(&mutex);
usleep(1000000);
}
pthread_exit(NULL);
}
int main() {
pthread_t readerThreads[NUM_READERS];
pthread_t writerThreads[NUM_WRITERS];
int readerIds[NUM_READERS];
int writerIds[NUM_WRITERS];
// Initialize mutex and condition variables
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&readerCondition, NULL);
pthread_cond_init(&writerCondition, NULL);
// Create reader threads
for (int i = 0; i < NUM_READERS; ++i) {
readerIds[i] = i;
pthread_create(&readerThreads[i], NULL, reader,
&readerIds[i]);
}
// Create writer threads
for (int i = 0; i < NUM_WRITERS; ++i) {
writerIds[i] = i;
pthread_create(&writerThreads[i], NULL, writer,
&writerIds[i]);
}
// Wait for reader threads to finish
for (int i = 0; i < NUM_READERS; ++i) {
pthread_join(readerThreads[i], NULL);
}
// Wait for writer threads to finish
for (int i = 0; i < NUM_WRITERS; ++i) {
pthread_join(writerThreads[i], NULL);
}
// Destroy mutex and condition variables
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&readerCondition);
pthread_cond_destroy(&writerCondition);
std::cout<<"komaljangid0827cs211127"<<std::endl;
return 0;
}