Skip to content

Commit

Permalink
Merge pull request #1415 from AshmitaBarthwal/AshmitaBarthwal-patch-1
Browse files Browse the repository at this point in the history
 FCFS.c
  • Loading branch information
pankaj-bind authored Oct 31, 2024
2 parents 471dff0 + 75072f5 commit b788265
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
90 changes: 90 additions & 0 deletions CPU Scheduling Algorithms/FCFS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <stdio.h>
#include <stdlib.h>

// Structure to hold each process's details
struct Process {
int id, bt, at, start, comp;
};

// Function to calculate the response time of each process
int* find_response(struct Process* input, int n) {
int* rt = (int*)malloc(n * sizeof(int)); // Array to hold response times
for (int i = 0; i < n; i++) {
rt[i] = abs(input[i].start - input[i].at); // Response time = start time - arrival time
}
return rt;
}

// Function to calculate the turnaround time of each process
int* find_tat(struct Process* input, int n) {
int* tat = (int*)malloc(n * sizeof(int)); // Array to hold turnaround times
for (int i = 0; i < n; i++) {
tat[i] = abs(input[i].comp - input[i].at); // Turnaround time = completion time - arrival time
}
return tat;
}

// Function to calculate the waiting time of each process
int* find_wt(struct Process* input, int* tat, int n) {
int* wt = (int*)malloc(n * sizeof(int)); // Array to hold waiting times
for (int i = 0; i < n; i++) {
wt[i] = abs(tat[i] - input[i].bt); // Waiting time = turnaround time - burst time
}
return wt;
}

// Comparison function for sorting processes by arrival time (and ID if arrival times are equal)
int comp(const void* a, const void* b) {
struct Process* p1 = (struct Process*)a;
struct Process* p2 = (struct Process*)b;
if (p1->at == p2->at)
return p1->id - p2->id; // If arrival times are the same, sort by ID
return p1->at - p2->at; // Sort by arrival time otherwise
}

// Function to schedule the processes and determine their start and completion times
void schedule(struct Process* input, int n) {
qsort(input, n, sizeof(struct Process), comp); // Sort processes by arrival time
int last_comp = 0; // Tracks the completion time of the last scheduled process

for (int i = 0; i < n; i++) {
input[i].start = last_comp; // Start time of current process is the last process's completion time
input[i].comp = last_comp + input[i].bt; // Completion time = start time + burst time
last_comp = input[i].comp; // Update last completion time
}
}

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

struct Process* input = (struct Process*)malloc(n * sizeof(struct Process)); // Array to hold process data
for (int i = 0; i < n; i++) {
printf("Enter the id, burst time and arrival time of process %d:\n", i + 1);
scanf("%d %d %d", &input[i].id, &input[i].bt, &input[i].at); // Input process details
}

schedule(input, n); // Schedule processes based on arrival time
int* response = find_response(input, n); // Calculate response times
int* turntime = find_tat(input, n); // Calculate turnaround times
int* waittime = find_wt(input, turntime, n); // Calculate waiting times

// Print table headers with appropriate column spacing
printf("%8s %8s %8s %8s %12s %10s %12s %8s\n", "Process", "Burst", "Arrival", "Start", "Completion", "Response", "Turn Around", "Waiting");

// Print each process's details in the formatted table
for (int i = 0; i < n; i++) {
printf("%8s%d %8d %8d %8d %12d %10d %12d %8d\n",
"P", input[i].id, input[i].bt, input[i].at,
input[i].start, input[i].comp, response[i],
turntime[i], waittime[i]);
}

// Free dynamically allocated memory
free(input);
free(response);
free(turntime);
free(waittime);
return 0;
}
69 changes: 69 additions & 0 deletions CPU Scheduling Algorithms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Process Scheduling Simulation in C (First-Come, First-Serve Algorithm)

## Table of Contents
- [Algorithm Overview](#algorithm-overview)
- [Features](#features)
- [First-Come, First-Serve (FCFS) Algorithm](#first-come-first-serve-fcfs-algorithm)
- [Detailed Explanation of Functions](#detailed-explanation-of-functions)
- [Requirements](#requirements)
- [Installation and Execution](#installation-and-execution)
- [Example Usage](#example-usage)
- [Output Explanation](#output-explanation)
- [Memory Management](#memory-management)
- [Code Structure and Files](#code-structure-and-files)
- [Contributing](#contributing)
- [License](#license)

## Algorithm Overview

This algorithm simulates a **First-Come, First-Serve (FCFS) scheduling** algorithm for processes in an operating system. In FCFS scheduling, the CPU processes requests in the order of their arrival, ensuring that each process is completed before the next begins. This code calculates and displays essential metrics like **response time**, **turnaround time**, and **waiting time** for each process.

The FCFS algorithm is a non-preemptive scheduling algorithm and is straightforward to implement, making it ideal for understanding basic process scheduling.

## Features

- **Sorts processes by arrival time** to ensure the First-Come, First-Serve order.
- **Calculates Response Time** (time between arrival and first execution).
- **Calculates Turnaround Time** (total time from arrival to completion).
- **Calculates Waiting Time** (time spent waiting in the queue).
- **Displays Results** in a well-formatted table, showing calculated metrics for each process.

## First-Come, First-Serve (FCFS) Algorithm

The **FCFS** scheduling algorithm follows a non-preemptive approach where processes are served in the order of their arrival. Here’s how it works in this program:

1. **Sorting by Arrival Time**: The program first sorts all processes by their arrival times. If two processes have the same arrival time, they are further sorted by their ID.
2. **Sequential Execution**: Each process begins execution as soon as the CPU is available, based on the completion of the previous process.
3. **Metric Calculation**: For each process, the program calculates:
- **Start Time**: When the process begins execution.
- **Completion Time**: When the process finishes execution.
- **Response Time**: Difference between start time and arrival time.
- **Turnaround Time**: Difference between completion time and arrival time.
- **Waiting Time**: Difference between turnaround time and burst time.

## Detailed Explanation of Functions

### 1. `find_response(struct Process* input, int n)`
- Calculates the **response time** for each process.
- Formula: **Response Time** = `start time - arrival time`

### 2. `find_tat(struct Process* input, int n)`
- Calculates the **turnaround time** for each process.
- Formula: **Turnaround Time** = `completion time - arrival time`

### 3. `find_wt(struct Process* input, int* tat, int n)`
- Calculates the **waiting time** for each process.
- Formula: **Waiting Time** = `turnaround time - burst time`

### 4. `schedule(struct Process* input, int n)`
- Sorts processes by arrival time using the FCFS approach.
- Determines **start time** and **completion time** for each process based on its burst time and order of arrival.

### 5. `comp(const void* a, const void* b)`
- Comparison function used by `qsort` to sort processes by arrival time, and by ID if arrival times are equal.

## Requirements

To run this program, you need:
- A C compiler like `gcc`.
- Basic command-line skills for compiling and executing C programs.

0 comments on commit b788265

Please sign in to comment.