Bachelor Of Technology
In
Computer Science Engineering Core
(Session: 2024-25)
Lab File for Operating System
R1UC403B
School of Computing Science & Engineering Galgotias
University, Greater Noida
Name : ABHISHEK PAL
Admission No : 23SCSE1410095
Semester: IV
Section : 40
Submitted to: SARVESH SIR
Experiment: 1
Aim:-Execution of basic Linux commands and hands on experience in Command Line
Interface(CLI) in Linux.
1.Date and Calendar command
Date Command:-Display the current date and time.
Output:-
• Calendar Command:-Displays the calendar of a specific year or a month.
Output:-
2.Directory Related Commands
• Present Working Directory Command:-Shows your current directory path.
Output:-
• MKDIR Command:-Used to create directories(folders).
Output:-
3.File Related Commands
• touch:-Creates a new empty file.
OUTPUT:
• cat:-Displays the content of a file.
• cp:- Copies files or directories from one location to another
4.Commands
• ls:- Lists files and directories in the current folder.
• rm:- Deletes files or directories permanently from the filesystem.
• cd:- Changes the current working directory to the specified path.
Experiment: 2
Aim:-Write a shell program to check the given number is even or odd.
CODE:-
echo -n "Enter a number: "
read number
if [ $((number % 2)) -eq 0 ]; then
echo "Even"
else
echo "Odd"
fi
Output
Experiment: 3
Aim:- Write a Shell program to swap the two integers.
CODE:-
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2
echo "Before swapping: num1 = $num1, num2 = $num2"
# Swapping using a temporary variable
temp=$num1
num1=$num2
num2=$temp
echo "After swapping: num1 = $num1, num2 = $num2"
Output
Experiment: 4
Aim:- Write a Shell program to find the factorial of a number.
CODE:-
echo -n "Enter an element: "
read number
fact=1
while [ $number -gt 0 ]
do
fact=$((fact * number))
number=$((number - 1))
done
echo "Factorial: $fact"
Output
Experiment: 5
Aim:- write a shell program to generate fibonacci series.
CODE:-
echo -n "Enter the number of Fibonacci terms: "
read n
a=0
b=1
echo "Fibonacci series:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
c=$((a + b))
a=$b
b=$c
done
Output
Experiment: 6
Aim:- Write a C program using the following system calls (fork, exec).
CODE:-
#include <stdio.h>
#include <unistd.h>
int main() {
int pid = fork();
if (pid < 0) {
printf("Fork failed\n");
} else if (pid == 0) {
printf("Child process: PID = %d\n", getpid());
} else {
printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);
}
return 0;
}
Output
Experiment: 7
Aim:- Write a C program using the following system calls (get_pid, exit).
CODE:-
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
printf("Process ID: %d\n", getpid());
printf("Exiting the program...\n");
exit(0);
}
Output
Experiment: 8
Aim:- Write a C program to simulate CPU scheduling algorithms: a) FCFS b) SJF c) Round
Robin
a) FCFS Algo:-
1. Input the number of processes, their arrival times, and burst times.
2. Sort the processes by arrival .
3. Initialize with current_time = 0 and arrays for CT, TAT, WT
4. For each process in sorted order:
i) If current_time < arrival_time, set current_time = arrival_time ii) CT[i] = current_time +
BT[i] iii) TAT[i] = CT[i] - AT[i] iv) WT[i] = TAT[i] - BT[i] v) Update current_time = CT[i]
5. Compute average TAT and WT.
CODE:-
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], tat[n], wt[n];
int i, j, time = 0;
float total_tat = 0, total_wt = 0;
for (i = 0; i < n; i++) {
printf("Enter Process ID, Arrival Time, Burst Time: ");
scanf("%d %d %d", &pid[i], &at[i], &bt[i]);
}
// Sort by Arrival Time
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (at[i] > at[j]) {
int temp;
temp = at[i]; at[i] = at[j]; at[j] = temp;
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = pid[i]; pid[i] = pid[j]; pid[j] = temp;
}
}
}
// Calculate TAT and WT
for (i = 0; i < n; i++) {
if (time < at[i]) {
time = at[i];
}
time += bt[i];
tat[i] = time - at[i];
wt[i] = tat[i] - bt[i];
total_tat += tat[i];
total_wt += wt[i];
}
// Output table
printf("\nPID\tAT\tBT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], tat[i], wt[i]);
}
// Averages
printf("\nAverage TAT = %.2f\n", total_tat / n);
printf("Average WT = %.2f\n", total_wt / n);
return 0;
}
Output
b) SJF Algo-
1. Input process information: pid[], bt[], at[].
2. Initialize: • time = 0 → Current system time • completed = 0 → Count of completed
processes • is_completed[i] = false for all i
3. Repeat until all processes are completed:
• Find the process p such that: o at[p] <= time o is_completed[p] == false o Has the shortest
burst time among such processes
• If no such process is found: o Increment time++ (CPU is idle)
• Else: o tat[p] = (time + bt[p]) - at[p] o wt[p] = tat[p] - bt[p] o time = time + bt[p] o
is_completed[p] = true o completed++
4. Output pid[i], bt[i], at[i], tat[i], wt[i] for all processes
CODE:-
#include <stdio.h>
#include <stdbool.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], bt[n], at[n], tat[n], wt[n];
bool is_completed[n];
for (int i = 0; i < n; i++) {
printf("Enter Process ID, Arrival Time and Burst Time for process %d: ", i + 1);
scanf("%d %d %d", &pid[i], &at[i], &bt[i]);
is_completed[i] = false;
}
int time = 0, completed = 0;
while (completed < n) {
int idx = -1;
int min_bt = 1e9;
for (int i = 0; i < n; i++) {
if (at[i] <= time && !is_completed[i] && bt[i] < min_bt) {
min_bt = bt[i];
idx = i;
}
if (idx == -1) {
time++;
} else {
time += bt[idx];
tat[idx] = time - at[idx];
wt[idx] = tat[idx] - bt[idx];
is_completed[idx] = true;
completed++;
}
}
printf("\nPID\tAT\tBT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], tat[i], wt[i]);
}
return 0;
}
Output
c) Round Robin Algo-
1. Initialize: • remaining_bt[i] = bt[i] for each process
• time = 0 (current time) • wt[i] = 0 for all i
• A queue for ready
2.Add processes to the ready queue when they arrive (at[i] <= time).
3.While there are processes with remaining_bt > 0:
a. Pick the next process from the ready queue.
b. If remaining_bt[i] > time_quantum,
• execute for time_quantum units: t ime += time_quantum remaining_bt[i] -=
time_quantum
• add newly arrived processes to the ready queue (arrival time ≤ current t ime)
• append this process back to the end of the ready queue (since it’s not f inished)
c. Else (process can finish within the time quantum):
• execute for remaining_bt[i] units: t ime += remaining_bt[i]
• calculate turnaround time: tat[i] = time - at[i]
• calculate waiting time: wt[i] = tat[i] - bt[i]
• set remaining_bt[i] = 0 (process finished)
• add newly arrived processes to the ready queue
4.Repeat until all processes finish (remaining_bt[i] == 0 for all).
#include <stdio.h>
#include <stdbool.h>
#define MAX 100
int main() {
int n, time_quantum;
int at[MAX], bt[MAX], remaining_bt[MAX], wt[MAX], tat[MAX];
bool is_in_queue[MAX] = {false};
printf("Enter number of processes: ");
scanf("%d", &n);
// Input AT and BT
for (int i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for process %d: ", i + 1);
scanf("%d %d", &at[i], &bt[i]);
remaining_bt[i] = bt[i];
wt[i] = 0;
tat[i] = 0;
}
printf("Enter Time Quantum: ");
scanf("%d", &time_quantum);
int time = 0, completed = 0;
int queue[MAX];
int front = 0, rear = 0;
// Helper functions
void enqueue(int p) {
queue[rear++] = p;
is_in_queue[p] = true;
}
int dequeue() {
return queue[front++];
}
// Enqueue processes that have arrived at time 0
for (int i = 0; i < n; i++) {
if (at[i] <= time && !is_in_queue[i]) {
enqueue(i);
}
}
// Round Robin Execution
while (completed < n) {
if (front == rear) {
time++;
for (int i = 0; i < n; i++) {
if (at[i] <= time && !is_in_queue[i] && remaining_bt[i] > 0) {
enqueue(i);
}
}
continue;
}
int i = dequeue();
if (remaining_bt[i] > time_quantum) {
time += time_quantum;
remaining_bt[i] -= time_quantum;
} else {
time += remaining_bt[i];
remaining_bt[i] = 0;
completed++;
tat[i] = time - at[i];
wt[i] = tat[i] - bt[i];
// Enqueue new arrivals during execution
for (int j = 0; j < n; j++) {
if (at[j] <= time && !is_in_queue[j] && remaining_bt[j] > 0) {
enqueue(j);
}
}
// If process is not completed, add it back to the queue
if (remaining_bt[i] > 0) {
enqueue(i);
} else {
is_in_queue[i] = false; // Prevent it from being enqueued again
}
}
printf("\nProcess\tAT\tBT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], tat[i], wt[i]);
}
return 0;
}
OUTPUT:-
Experiment: 9
Aim:- Write a C program to simulate Bankers Algorithm for Deadlock Detection.
Algo-
1.Initialize:
• Work[m] = Available[m]
• Finish[n] = {false, false, ..., false}
2.Find a process i such that:
• Finish[i] == false
• Request[i][j] <= Work[j] for all j If such a process is found:
• Work[j] = Work[j] + Allocation[i][j] for all j (release all resources held by process i back to
Work)
• Finish[i] = true (process i can finish)
• Repeat Step 2 to find next such process
3.If no such process exists (no process can proceed),
stop.
4.Processes for which Finish[i] == false are deadlocked (cannot proceed).
Those with Finish[i] == true can complete safely.
CODE:-
#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES 10
#define MAX_RESOURCES 10
int main() {
int n, m;
int Available[MAX_RESOURCES];
int Allocation[MAX_PROCESSES][MAX_RESOURCES];
int Request[MAX_PROCESSES][MAX_RESOURCES];
bool Finish[MAX_PROCESSES];
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resource types: ");
scanf("%d", &m); // Fixed missing semicolon
printf("Enter Available resources vector (length %d):\n", m);
for (int i = 0; i < m; i++) {
scanf("%d", &Available[i]);
}
printf("Enter Allocation matrix (processes x resources):\n");
for (int i = 0; i < n; i++) {
printf("Process %d allocation: ", i);
for (int j = 0; j < m; j++) {
scanf("%d", &Allocation[i][j]);
}
}
printf("Enter Request matrix (processes x resources):\n");
for (int i = 0; i < n; i++) {
printf("Process %d request: ", i);
for (int j = 0; j < m; j++) {
scanf("%d", &Request[i][j]);
}
}
for (int i = 0; i < n; i++) {
Finish[i] = false;
}
int Work[MAX_RESOURCES];
for (int i = 0; i < m; i++) {
Work[i] = Available[i];
}
bool done_something;
do {
done_something = false;
for (int i = 0; i < n; i++) {
if (!Finish[i]) {
bool can_allocate = true;
for (int j = 0; j < m; j++) {
if (Request[i][j] > Work[j]) {
can_allocate = false;
break;
}
}
if (can_allocate) {
for (int j = 0; j < m; j++) {
Work[j] += Allocation[i][j];
}
Finish[i] = true;
done_something = true;
}
}
}
} while (done_something);
printf("\nDeadlock Detection Result:\n");
bool deadlock_found = false;
for (int i = 0; i < n; i++) {
if (!Finish[i]) {
printf("Process %d is deadlocked.\n", i);
deadlock_found = true;
}
}
if (!deadlock_found) {
printf("No deadlock detected. All processes can complete.\n");
}
return 0;
}
OUTPUT:-