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

Operating System Codes

The document contains multiple programming assignments related to operating systems, specifically focusing on process scheduling algorithms such as First Come First Serve, Non-preemptive Shortest Job First, Preemptive Shortest Job First, and process creation using fork. Each section includes C++ or C code implementations that calculate waiting time, turnaround time, and average times for processes. Additionally, the document lists group members responsible for the assignments.

Uploaded by

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

Operating System Codes

The document contains multiple programming assignments related to operating systems, specifically focusing on process scheduling algorithms such as First Come First Serve, Non-preemptive Shortest Job First, Preemptive Shortest Job First, and process creation using fork. Each section includes C++ or C code implementations that calculate waiting time, turnaround time, and average times for processes. Additionally, the document lists group members responsible for the assignments.

Uploaded by

audecendamiye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Operating System Assignment

Group members
Mugabo Vedaste: 223003861
Keza Uwineza Keren: 223019964

Answers
1. The program that executes the First Come First Serve Scheduling

#include<iostream>
using namespace std;

int main(){

//declare the needed variables


int z;
cout<<"Enter number of processes to execute";
cin>>z;
int processes[z];//={1,2,3};
int BT[z];//={24,3,3};
int n = sizeof processes/ sizeof processes[0];

//enter values in std::array<T, N> ;


for(int j = 0;j<n;j++){
cout<<"Enter process"<<j+1<<": ";
cin>>processes[j];
}
//enter burst time
for(int j = 0;j<n;j++){
cout<<"Enter burst time of process "<<j+1<<": ";
cin>>BT[j];
}

//waiting time for first processes is 0;


int WT[n];
WT[0] = 0;

// finding waiting time for other processes


for(int i=1;i<=n;i++){
WT[i] = BT[i-1] + WT[i-1];
}

//calculate turnaround time


int TAT[n];
for(int i=0;i<n;i++){
TAT[i] = WT[i] + BT[i];
}

//displaying the data


cout<<"Processes " << "Burst_time " << "Waiting_time " << "Turnaround_time "<<"\n";
for(int i=0;i<n;i++){
cout<< processes[i] <<"\t\t" << BT[i]<<"\t\t"<< WT[i]<<"\t\t"<< TAT[i]<<"\t\t";
cout<<"\n";
}

//Calculate the average turnaround and waiting time


float avgTAT; float avgWT; int sumTAT = 0; int sumWT= 0;
for(int i = 0;i<n; i++){
sumWT = sumWT + WT[i];
sumTAT = sumTAT + TAT[i];
}
avgWT = sumWT / n;
avgTAT = sumTAT /n ;

cout<<"The average waiting time is: "<< avgWT<<"\n";


cout<<"The average turnaround time is: "<<avgTAT<<"\n";
}

2. Program to execute Non-preemptive Shortest Job First Scheduling.

#include <iostream>
using namespace std;

int main() {

// Matrix for storing Process Id, Burst


// Time, Average Waiting Time & Average
// Turn Around Time.
int BT[100][4];
int i, j, n, total = 0, index, temp;
float avgWT, avgTAT;

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 >> BT[i][1];
BT[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 (BT[j][1] < BT[index][1])
index = j;
temp = BT[i][1];
BT[i][1] = BT[index][1];
BT[index][1] = temp;

temp = BT[i][0];
BT[i][0] = BT[index][0];
BT[index][0] = temp;
}

BT[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
BT[i][2] = 0;
for (j = 0; j < i; j++)
BT[i][2] += BT[j][1];
total += BT[i][2];
}

avgWT = (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++) {
BT[i][3] = BT[i][1] + BT[i][2];
total += BT[i][3];
cout << "P" << BT[i][0] << " " << BT[i][1] << " " << BT[i][2] << " " << BT[i]
[3] << endl;
}

avgTAT = (float)total / n;
cout << "Average Waiting Time= " << avgWT << endl;
cout << "Average Turnaround Time= " << avgTAT << endl;
}

3. Program to execute preemptive Shortest Job First

#include <stdio.h>
#define MAX 100
// find the waiting time for each process
void findWaitingTime(int n, int BT[], int AT[], int WT[], int CT[]) {
int rem_bt[MAX];
int complete = 0, t = 0, minm = 100000;
int shortest = 0, finish_time;
int check = 0;
// Inputing burst time
for (int i = 0; i < n; i++)
rem_bt[i] = BT[i];
// Process until all processes are complete
while (complete != n) {
// Find the process with the smallest remaining time
for (int j = 0; j < n; j++) {
if ((AT[j] <= t) && (rem_bt[j] < minm) && rem_bt[j] > 0) {
minm = rem_bt[j];
shortest = j;
check = 1;
}
}
if (check == 0) {
t++;
continue;
}
// Reduce remaining burst time by one
rem_bt[shortest]--;
// Update minimum burst time
minm = rem_bt[shortest];
if (minm == 0)
minm = 100000;
// If a process gets completely executed
if (rem_bt[shortest] == 0) {
complete++;
check = 0;
// Calculate finish time of current process
finish_time = t + 1;
CT[shortest] = finish_time;
// Calculate waiting time
WT[shortest] = finish_time - BT[shortest] - AT[shortest];
if (WT[shortest] < 0)
WT[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate turnaround time
void findTurnAroundTime(int n, int BT[], int AT[], int WT[], int TAT[], int CT[]) {
// Turnaround time = Completion time - Arrival time
for (int i = 0; i < n; i++)
TAT[i] = CT[i] - AT[i];
}
// Function to calculate average time
void findAvgTime(int n, int BT[], int AT[]) {
int WT[MAX], TAT[MAX], CT[MAX];
int totalWT = 0, totalTAT = 0;
// Calculate waiting time of each process
findWaitingTime(n, BT, AT, WT, CT);
// Calculate turnaround time for each process
findTurnAroundTime(n, BT, AT, WT, TAT, CT);
// Print process details
printf(" P AT BT WT TAT\n");
for (int i = 0; i < n; i++) {
totalWT += WT[i];
totalTAT += TAT[i];
printf(" %d \t\t %d \t\t %d \t\t %d \t\t %d\n", i + 1, AT[i], BT[i],WT[i], TAT[i]);
}
// Calculate and print average waiting and turnaround times
printf("\nAverage waiting time = %.2f", (float)totalWT / n);
printf("\nAverage turnaround time = %.2f\n", (float)totalTAT / n);
}
int main() {
int n;
int BT[MAX], AT[MAX];
// Input the number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
// Input the arrival time and burst time for each process
for (int i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of processes%d: ", i + 1);
scanf("%d%d", &AT[i], &BT[i]);
}
// Calculate average waiting time and average turnaround time
findAvgTime(n, BT, AT);
return 0;
}

4. Program to execute fork child

#include <stdio.h>
#include <unistd.h> // For fork()
#include <sys/types.h> // For pid_t
#include <sys/wait.h> // For wait()
int main() {
pid_t pid;
// Create a new child process
pid = fork();
// Check if fork() failed
if (pid < 0) {
printf("Fork failed!\n");
return 1;
}
// Child process (fork() returns 0 to the child)
if (pid == 0) {
printf("This is the child process. PID: %d\n", getpid());
printf("Child's parent PID: %d\n", getppid());
}
// Parent process (fork() returns child's PID to the parent)
else {
printf("This is the parent process. PID: %d\n", getpid());
printf("Parent waiting for the child to finish...\n");
wait(NULL); // Wait for the child process to finish
printf("Child process finished.\n");
}
return 0;
}

You might also like