0% found this document useful (0 votes)
7 views6 pages

24 Octwork ARIN

Uploaded by

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

24 Octwork ARIN

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>

#define MAX_PROCESSES 100

typedef struct {
int pid;
int burst_time;
int arrival_time;
int priority;
int waiting_time;
int turnaround_time;
int response_time;
} process_t;

void generate_processes(process_t processes[], int n) {


srand(time(0));
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
processes[i].burst_time = rand() % 20 + 1; // Random burst time between
1 and 20
processes[i].arrival_time = rand() % 50; // Random arrival time
between 0 and 49
processes[i].priority = rand() % 10 + 1; // Random priority between 1
and 10
}
}

void sort_by_arrival_time(process_t processes[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j + 1].arrival_time) {
process_t temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
}

void fcfs_scheduling(process_t processes[], int n) {


sort_by_arrival_time(processes, n);

int current_time = 0;
int total_waiting_time = 0, total_turnaround_time = 0, total_response_time =
0;

printf("\nFCFS Scheduling:\n");
printf("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\
tResponse Time\n");

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


if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time +
processes[i].burst_time;
processes[i].response_time = processes[i].waiting_time;

total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
total_response_time += processes[i].response_time;
current_time += processes[i].burst_time;

printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid,
processes[i].burst_time, processes[i].arrival_time, processes[i].waiting_time,
processes[i].turnaround_time, processes[i].response_time);
}

printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
printf("Average Response Time: %.2f\n", (float)total_response_time / n);
}

// Add SJF, SRTF, and Round Robin implementations similarly...


void sjf_scheduling(process_t processes[], int n) {
sort_by_arrival_time(processes, n); // Sort by arrival time first

int completed = 0, current_time = 0;


int total_waiting_time = 0, total_turnaround_time = 0, total_response_time =
0;
int is_completed[MAX_PROCESSES] = {0}; // To keep track of completed
processes

printf("\nSJF Scheduling (Non-Preemptive):\n");


printf("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\
tResponse Time\n");

while (completed < n) {


int min_index = -1;
int min_burst_time = 1e9;

// Find the process with the smallest burst time among the arrived
processes
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && is_completed[i] ==
0) {
if (processes[i].burst_time < min_burst_time) {
min_burst_time = processes[i].burst_time;
min_index = i;
}
// In case two processes have the same burst time, choose the
one that arrived first
if (processes[i].burst_time == min_burst_time &&
processes[i].arrival_time < processes[min_index].arrival_time) {
min_index = i;
}
}
}

if (min_index == -1) {
current_time++; // If no process has arrived yet, move the current
time
continue;
}

// Calculate times for the chosen process


processes[min_index].waiting_time = current_time -
processes[min_index].arrival_time;
processes[min_index].turnaround_time = processes[min_index].waiting_time
+ processes[min_index].burst_time;
processes[min_index].response_time = processes[min_index].waiting_time;

// Update the current time


current_time += processes[min_index].burst_time;
// Mark process as completed
is_completed[min_index] = 1;
completed++;

total_waiting_time += processes[min_index].waiting_time;
total_turnaround_time += processes[min_index].turnaround_time;
total_response_time += processes[min_index].response_time;

// Print the result for this process


printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[min_index].pid,
processes[min_index].burst_time, processes[min_index].arrival_time,
processes[min_index].waiting_time, processes[min_index].turnaround_time,
processes[min_index].response_time);
}

printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
printf("Average Response Time: %.2f\n", (float)total_response_time / n);
}
void srtf_scheduling(process_t processes[], int n) {
int remaining_burst_time[MAX_PROCESSES];
int is_completed[MAX_PROCESSES] = {0}; // Track completed processes
int current_time = 0, completed = 0;
int total_waiting_time = 0, total_turnaround_time = 0, total_response_time =
0;
int last_process_executed = -1;

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


remaining_burst_time[i] = processes[i].burst_time; // Initialize
remaining burst time
}

printf("\nSRTF Scheduling (Preemptive SJF):\n");


printf("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\
tResponse Time\n");

while (completed < n) {


int min_index = -1;
int min_burst_time = 1e9;

// Find the process with the smallest remaining burst time that has
arrived
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && !is_completed[i] &&
remaining_burst_time[i] < min_burst_time) {
min_burst_time = remaining_burst_time[i];
min_index = i;
}
}

if (min_index == -1) {
current_time++; // If no process has arrived yet, move the current
time
continue;
}

// If the process is being executed for the first time, calculate its
response time
if (remaining_burst_time[min_index] == processes[min_index].burst_time)
{
processes[min_index].response_time = current_time -
processes[min_index].arrival_time;
total_response_time += processes[min_index].response_time;
}

// Process is executed for 1 unit of time


remaining_burst_time[min_index]--;
current_time++;

// If the process is completed


if (remaining_burst_time[min_index] == 0) {
completed++;
is_completed[min_index] = 1;

// Calculate waiting and turnaround times


processes[min_index].turnaround_time = current_time -
processes[min_index].arrival_time;
processes[min_index].waiting_time =
processes[min_index].turnaround_time - processes[min_index].burst_time;

total_turnaround_time += processes[min_index].turnaround_time;
total_waiting_time += processes[min_index].waiting_time;

// Print the result for this process


printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[min_index].pid,
processes[min_index].burst_time, processes[min_index].arrival_time,
processes[min_index].waiting_time, processes[min_index].turnaround_time,
processes[min_index].response_time);
}
}

printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
printf("Average Response Time: %.2f\n", (float)total_response_time / n);
}
void round_robin_scheduling(process_t processes[], int n, int quantum) {
int remaining_burst_time[MAX_PROCESSES];
int waiting_time[MAX_PROCESSES] = {0}; // Keep track of waiting time for
each process
int is_completed[MAX_PROCESSES] = {0}; // Track completed processes
int current_time = 0, completed = 0;
int total_waiting_time = 0, total_turnaround_time = 0, total_response_time =
0;
int response_time[MAX_PROCESSES];
int first_time[MAX_PROCESSES] = {0}; // Check if the process has been
executed before

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


remaining_burst_time[i] = processes[i].burst_time; // Initialize
remaining burst time
response_time[i] = -1; // Initially set response time to -1 (not yet
responded)
}

printf("\nRound Robin Scheduling (Quantum = %d):\n", quantum);


printf("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\
tResponse Time\n");

// Implement a queue to keep track of processes


int queue[MAX_PROCESSES];
int front = 0, rear = 0;

// Initially, enqueue all processes that have arrived by time 0


for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time) {
queue[rear++] = i;
}
}

while (completed < n) {


if (front == rear) {
current_time++; // No process available, increment time
// Add any processes that have now arrived
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time == current_time) {
queue[rear++] = i;
}
}
continue;
}

// Dequeue the first process in the queue


int index = queue[front];
front = (front + 1) % MAX_PROCESSES;

// If this is the first time the process is executed, calculate its


response time
if (response_time[index] == -1) {
response_time[index] = current_time - processes[index].arrival_time;
total_response_time += response_time[index];
}

// Execute the process for the quantum time or until it finishes,


whichever comes first
int execution_time = (remaining_burst_time[index] < quantum) ?
remaining_burst_time[index] : quantum;
remaining_burst_time[index] -= execution_time;
current_time += execution_time;

// If process is completed
if (remaining_burst_time[index] == 0) {
completed++;

processes[index].turnaround_time = current_time -
processes[index].arrival_time;
processes[index].waiting_time = processes[index].turnaround_time -
processes[index].burst_time;

total_turnaround_time += processes[index].turnaround_time;
total_waiting_time += processes[index].waiting_time;

printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[index].pid,
processes[index].burst_time, processes[index].arrival_time,
processes[index].waiting_time, processes[index].turnaround_time,
response_time[index]);
} else {
// Re-enqueue the process if it has not finished
queue[rear] = index;
rear = (rear + 1) % MAX_PROCESSES;
}

// Enqueue any newly arrived processes during this time slice


for (int i = 0; i < n; i++) {
if (processes[i].arrival_time > current_time - execution_time &&
processes[i].arrival_time <= current_time) {
queue[rear++] = i;
}
}
}

printf("Average Waiting Time: %.2f\n", (float)total_waiting_time / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
printf("Average Response Time: %.2f\n", (float)total_response_time / n);
}
int main() {
process_t processes[MAX_PROCESSES];
int n = 80 + rand() % 21; // Randomly generate between 80-100 processes

generate_processes(processes, n);

fcfs_scheduling(processes, n); // Call FCFS


sjf_scheduling(processes, n); // Call SJF (Non-Preemptive)
srtf_scheduling(processes, n); // Call SRTF (Preemptive SJF)

// Call Round Robin with different quantum times


round_robin_scheduling(processes, n, 2);
round_robin_scheduling(processes, n, 4);
round_robin_scheduling(processes, n, 6);
round_robin_scheduling(processes, n, 8);
round_robin_scheduling(processes, n, 10);
round_robin_scheduling(processes, n, 12);
round_robin_scheduling(processes, n, 14);
round_robin_scheduling(processes, n, 16);
round_robin_scheduling(processes, n, 20);
round_robin_scheduling(processes, n, 25);

return 0;
}

You might also like