Operating System Lab File
Operating System Lab File
In
Computer Science Engineering Core
(Session: 2024-25)
Output:-
Output:-
• MKDIR Command:-Used to create directories(folders).
Output:-
OUTPUT:
4.Commands
• ls:- Lists files and directories in the current folder.
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
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
Output
Experiment: 5
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;
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 (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
• 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]
#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 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);
// Helper functions
void enqueue(int p) {
queue[rear++] = p;
is_in_queue[p] = true;
}
int dequeue() {
return queue[front++];
}
}
}
// 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();
} 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]
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: ");
}
printf("Enter Allocation matrix (processes x resources):\n");
for (int i = 0; i < n; i++) {
printf("Process %d allocation: ", 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++) {
Finish[i] = true;
done_something = true;
}
}
}
} while (done_something);
deadlock_found = true;
}
}
if (!deadlock_found) {
printf("No deadlock detected. All processes can complete.\n");
}
return 0;
}
OUTPUT:-