0% found this document useful (0 votes)
2 views5 pages

Practical 6 OS

This document outlines a C program that demonstrates non-preemptive scheduling algorithms in operating systems, specifically First-Come, First-Served (FCFS) and Shortest Job Next (SJN). It includes code for process management, sorting by arrival time, and calculating completion, turnaround, and waiting times. The program prompts the user for process details and allows the selection of the scheduling algorithm to be used.

Uploaded by

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

Practical 6 OS

This document outlines a C program that demonstrates non-preemptive scheduling algorithms in operating systems, specifically First-Come, First-Served (FCFS) and Shortest Job Next (SJN). It includes code for process management, sorting by arrival time, and calculating completion, turnaround, and waiting times. The program prompts the user for process details and allows the selection of the scheduling algorithm to be used.

Uploaded by

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

Practical 6

Aim: Write a C program to demonstrate the concept of non-preemptive scheduling


algorithms. in operating system
Software: Virtual Machine Box, Ubuntu, Linux, C Compiler
Theory:
In non-preemptive scheduling, a running process cannot be interrupted by the operating
system; it voluntarily relinquishes control of the CPU. In this scheduling, once the resources
(CPU cycles) are allocated to a process, the process holds the CPU till it gets terminated or
reaches a waiting state.

Code:
#include <stdio.h>

// Structure for process


struct Process {
int pid; // Process ID
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};

// Function to sort processes by arrival time (FCFS)


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

// Function to implement FCFS scheduling


void FCFS(struct Process proc[], int n) {
sortByArrival(proc, n);
int current_time = 0;

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


if (current_time < proc[i].arrival_time)
current_time = proc[i].arrival_time;

proc[i].completion_time = current_time + proc[i].burst_time;


proc[i].turnaround_time = proc[i].completion_time - proc[i].arrival_time;
proc[i].waiting_time = proc[i].turnaround_time - proc[i].burst_time;

current_time = proc[i].completion_time;
}
}

// Function to implement Shortest Job Next (SJN) scheduling


void SJN(struct Process proc[], int n) {
int completed = 0, current_time = 0, min_index;
int visited[n];
for (int i = 0; i < n; i++) visited[i] = 0;
while (completed < n) {
min_index = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && proc[i].arrival_time <= current_time) {
if (min_index == -1 || proc[i].burst_time < proc[min_index].burst_time)
min_index = i;
}
}

if (min_index == -1) {
current_time++;
} else {
visited[min_index] = 1;
proc[min_index].completion_time = current_time + proc[min_index].burst_time;
proc[min_index].turnaround_time = proc[min_index].completion_time -
proc[min_index].arrival_time;
proc[min_index].waiting_time = proc[min_index].turnaround_time -
proc[min_index].burst_time;

current_time = proc[min_index].completion_time;
completed++;
}
}
}

// Function to display process details


void displayProcesses(struct Process proc[], int n) {
printf("\nPID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting
Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", proc[i].pid, proc[i].arrival_time,
proc[i].burst_time, proc[i].completion_time, proc[i].turnaround_time, proc[i].waiting_time);
}
}

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

struct Process proc[n];


printf("Enter Arrival Time and Burst Time for each process:\n");
for (int i = 0; i < n; i++) {
proc[i].pid = i + 1;
printf("Process %d: ", i + 1);
scanf("%d %d", &proc[i].arrival_time, &proc[i].burst_time);
}

printf("\nSelect Scheduling Algorithm:\n1. FCFS\n2. SJN\nEnter choice: ");


scanf("%d", &choice);

if (choice == 1) {
FCFS(proc, n);
} else if (choice == 2) {
SJN(proc, n);
} else {
printf("Invalid choice!\n");
return 0;
}
displayProcesses(proc, n);
return 0;
}

Output:

You might also like