DELHI TECHNOLOGICAL UNIVERSITY
Operating System (MC202)
LAB REPORT
SUBMITTED BY:
DINKAR SHARMA
23/MC/050
SUBMITTED TO:
DR. MANISHA KUMARI RAJORIYA
: INDEX :
S no. Title Date Signature
1 To copy the contents of a le to another le 07/01/2025
using system calls.
2 To use fork( ) system call to create child 21/01/2025
process and get their ids
3 Write a C program that creates a new child 28/01/2025
process. The child process should be
assigned to nd the length of your name
using execlp system call.
4 Write a program that creates another 04/02/2025
process. The child process
should prints the contents of any text on
the screen using cat command.
The parent process should exit only after
the child process has
completed its execution using wait system
call.
5 WAP to implement Banker's Algorithm and 11/02/2025
print a safe sequence if it exists.
6 WAP to implement the preemptive version 25/02/2025
of the Shortest Job First Algorithm. Find the
Average Waiting Time and Average
Turnaround time.
7 Write a menu-driven program to 18/03/2025
demonstrate non-preemptive process
scheduling using FCFS, SJF, and Priority
Based Scheduling and calculate the
average waiting and turnaround times.
8 WAP to implement Round Robin 18/03/205
Scheduling Algorithm.
9 WAP to implement LRU page replacement 25/03/2025
algorithm.
10 WAP to implement following Disk 01/04/2025
Scheduling Algorithms:
a) FCFS (First Come First Serve)
b) Elevator Algorithm (SCAN)
Print the Seek time for each algorithm.
fi
fi
fi
Practical - 1
Aim: Write a C program to read a le and copy the contents into a new le using
system calls like read( ),write),open( ) and close( ).
Code :
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#de ne BUFFER_SIZE 1024
int main() {
int sourceFile, destFile;
char bu er[BUFFER_SIZE];
ssize_t bytesRead, bytesWritten;
// Open the source le (read-only)
sourceFile = open("source.txt", O_RDONLY);
if (sourceFile < 0) {
perror("Error opening source le");
return 1;
}
// Create (or overwrite) the destination le (write-only)
destFile = open("destination.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (destFile < 0) {
perror("Error creating destination le");
close(sourceFile);
return 1;
}
// Read from source and write to destination
while ((bytesRead = read(sourceFile, bu er, BUFFER_SIZE)) > 0) {
bytesWritten = write(destFile, bu er, bytesRead);
if (bytesWritten != bytesRead) {
perror("Error writing to destination le");
close(sourceFile);
close(destFile);
return 1;
}
}
if (bytesRead < 0) {
perror("Error reading from source le");
}
fi
ff
fi
fi
fi
ff
fi
fi
fi
ff
fi
fi
// Close both les
close(sourceFile);
close(destFile);
printf("File copied successfully.\n");
return 0;
}
Output :
fi
Practical - 2
Aim: Write a program to create a child process using fork( ). Print the process id of
child and the parent process. Also Explain the behavior of forkin parent and child
process.
Code :
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork(); // Create a new process
if (pid < 0) {
// fork failed
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("This is the child process.\n");
printf("Child PID: %d\n", getpid());
printf("Parent PID (from child): %d\n", getppid());
} else {
// Parent process
printf("This is the parent process.\n");
printf("Parent PID: %d\n", getpid());
printf("Child PID (from parent): %d\n", pid);
}
return 0;
}
Output :
Practical - 3
Aim: Write a C program that creates a new child process. The child process
should be assigned to nd the length of your name using execlp system call.
Code :
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
// Replace with your actual name
char *name = "YourNameHere";
pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process: use echo and wc to get length
printf("Child: Finding length of the name using execlp...\n");
execlp("bash", "bash", "-c",
"echo -n YourNameHere | wc -c", NULL);
perror("execlp failed");
return 1;
} else {
// Parent process
wait(NULL);
printf("Parent: Child process has completed.\n");
}
return 0;
}
Output :
fi
Practical - 4
Aim: Write a program that creates another process. The child process
should prints the contents of any text on the screen using cat command.
The parent process should exit only after the child process has
completed its execution using wait system call.
Code :
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process: use 'cat' to print le content
printf("Child: Displaying contents of the le using 'cat'...\n");
execlp("cat", "cat", "sample.txt", NULL);
perror("execlp failed");
return 1;
} else {
// Parent process
wait(NULL);
printf("Parent: Child process has nished.\n");
}
return 0;
}
Output :
fi
fi
fi
Practical - 5
Aim: Write a program to implement Banker's Algorithm. The program should either
print the safe sequence of execution of given processes (if anyexists) or print.
”There is a deadlock in the system.".
Code :
#include <stdio.h>
#include <stdbool.h>
#de ne MAX_PROCESSES 10
#de ne MAX_RESOURCES 10
int main() {
int n, m; // n = number of processes, m = number of resources
int alloc[MAX_PROCESSES][MAX_RESOURCES];
int max[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int avail[MAX_RESOURCES];
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter number of resource types: ");
scanf("%d", &m);
printf("Enter allocation matrix:\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &alloc[i][j]);
printf("Enter maximum matrix:\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &max[i][j]);
printf("Enter available resources:\n");
for (int i = 0; i < m; i++)
scanf("%d", &avail[i]);
// Calculate need matrix
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
bool nish[MAX_PROCESSES] = {0};
fi
fi
fi
int safeSeq[MAX_PROCESSES];
int count = 0;
while (count < n) {
bool found = false;
for (int p = 0; p < n; p++) {
if (! nish[p]) {
bool canRun = true;
for (int r = 0; r < m; r++) {
if (need[p][r] > avail[r]) {
canRun = false;
break;
}
}
if (canRun) {
for (int r = 0; r < m; r++)
avail[r] += alloc[p][r];
safeSeq[count++] = p;
nish[p] = true;
found = true;
}
}
}
if (!found) {
printf("There is a deadlock in the system.\n");
return 1;
}
}
// Safe sequence found
printf("System is in a safe state.\nSafe sequence: ");
for (int i = 0; i < n; i++)
printf("P%d ", safeSeq[i]);
printf("\n");
return 0;
}
fi
fi
Output :
Practical - 6
Aim: WAP to implement the preemptive version of the Shortest Job First
Algorithm. Find the Average Waiting Time and Average Turnaround time.
Code :
#include <stdio.h>
#de ne MAX 100
int main() {
int n, remaining_time[MAX], burst_time[MAX], arrival_time[MAX],
waiting_time[MAX], turnaround_time[MAX];
int complete = 0, time = 0, shortest = 0, minm = 1e9;
int nish_time;
oat avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for Process %d: ", i + 1);
scanf("%d %d", &arrival_time[i], &burst_time[i]);
remaining_time[i] = burst_time[i];
}
int check = 0;
while (complete != n) {
shortest = -1;
minm = 1e9;
for (int j = 0; j < n; j++) {
if ((arrival_time[j] <= time) &&
(remaining_time[j] < minm) && remaining_time[j] > 0) {
minm = remaining_time[j];
shortest = j;
check = 1;
}
}
if (shortest == -1) {
time++;
continue;
}
fl
fi
fi
remaining_time[shortest]--;
if (remaining_time[shortest] == 0) {
complete++;
nish_time = time + 1;
waiting_time[shortest] = nish_time - burst_time[shortest] -
arrival_time[shortest];
if (waiting_time[shortest] < 0)
waiting_time[shortest] = 0;
}
time++;
}
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
avg_waiting_time += waiting_time[i];
avg_turnaround_time += turnaround_time[i];
printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, arrival_time[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nAverage Waiting Time = %.2f", avg_waiting_time);
printf("\nAverage Turnaround Time = %.2f\n", avg_turnaround_time);
return 0;
}
Output :
fi
fi
Practical - 7
Aim: To write a menu-driven program to implement:
a) First Come First Serve scheduling Algorithm (FCFS)
b) Shortest Time First algorithm(non-preemptive)
Code : (FCFS+STFS)
#include <stdio.h>
#include <stdlib.h>
#de ne MAX 100
typedef struct {
int pid, bt, wt, tat;
} Process;
void fcfs(Process p[], int n) {
p[0].wt = 0;
p[0].tat = p[0].bt;
for (int i = 1; i < n; i++) {
p[i].wt = p[i-1].wt + p[i-1].bt;
p[i].tat = p[i].wt + p[i].bt;
}
printf("\nFCFS Scheduling:\n");
printf("PID\tBT\tWT\tTAT\n");
oat totalWT = 0, totalTAT = 0;
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", p[i].pid, p[i].bt, p[i].wt, p[i].tat);
totalWT += p[i].wt;
totalTAT += p[i].tat;
}
printf("Average Waiting Time: %.2f\n", totalWT / n);
printf("Average Turnaround Time: %.2f\n", totalTAT / n);
}
void sjf(Process p[], int n) {
// Sort by burst time (non-preemptive)
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (p[j].bt > p[j + 1].bt) {
Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
fl
fi
p[0].wt = 0;
p[0].tat = p[0].bt;
for (int i = 1; i < n; i++) {
p[i].wt = p[i-1].wt + p[i-1].bt;
p[i].tat = p[i].wt + p[i].bt;
}
printf("\nSJF Scheduling (Non-preemptive):\n");
printf("PID\tBT\tWT\tTAT\n");
oat totalWT = 0, totalTAT = 0;
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", p[i].pid, p[i].bt, p[i].wt, p[i].tat);
totalWT += p[i].wt;
totalTAT += p[i].tat;
}
printf("Average Waiting Time: %.2f\n", totalWT / n);
printf("Average Turnaround Time: %.2f\n", totalTAT / n);
}
int main() {
int n, choice;
Process p[MAX];
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("Enter Burst Time for Process P%d: ", i + 1);
scanf("%d", &p[i].bt);
}
do {
printf("\nCPU Scheduling Menu:\n");
printf("1. First Come First Serve (FCFS)\n");
printf("2. Shortest Job First (Non-preemptive)\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(p, n);
break;
case 2:
sjf(p, n);
fl
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 3);
return 0;
}
Output : (FCFS+STFS)
Practical - 8
Aim: Write a menu-driven program to implement Round Robin Algorithm.
Code :
#include <stdio.h>
typedef struct {
int pid;
int bt;
int remaining;
int wt;
int tat;
} Process;
int main() {
int n, tq;
printf("Enter number of processes: ");
scanf("%d", &n);
Process p[n];
for (int i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("Enter Burst Time for Process P%d: ", i + 1);
scanf("%d", &p[i].bt);
p[i].remaining = p[i].bt;
p[i].wt = 0;
p[i].tat = 0;
}
printf("Enter Time Quantum: ");
scanf("%d", &tq);
int time = 0, done;
printf("\nRound Robin Scheduling:\n");
do {
done = 1;
for (int i = 0; i < n; i++) {
if (p[i].remaining > 0) {
done = 0;
if (p[i].remaining > tq) {
printf("P%d executes from %d to %d\n", p[i].pid, time, time + tq);
time += tq;
p[i].remaining -= tq;
} else {
printf("P%d executes from %d to %d\n", p[i].pid, time, time +
p[i].remaining);
time += p[i].remaining;
p[i].wt = time - p[i].bt;
p[i].tat = time;
p[i].remaining = 0;
}
}
}
} while (!done);
oat totalWT = 0, totalTAT = 0;
printf("\nPID\tBT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", p[i].pid, p[i].bt, p[i].wt, p[i].tat);
totalWT += p[i].wt;
totalTAT += p[i].tat;
}
printf("Average Waiting Time: %.2f\n", totalWT / n);
printf("Average Turnaround Time: %.2f\n", totalTAT / n);
return 0;
}
Output :
fl
Practical - 9
Aim: WAP to implement LRU page replacement algorithm.
Code :
#include <stdio.h>
int ndLRU(int time[], int n) {
int i, minimum = time[0], pos = 0;
for (i = 1; i < n; ++i) {
if (time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}
int main() {
int frames, pages[100], temp[100], time[100];
int total_pages, counter = 0, page_faults = 0, ag1, ag2, pos;
printf("Enter number of frames: ");
scanf("%d", &frames);
printf("Enter number of pages: ");
scanf("%d", &total_pages);
printf("Enter the page reference string:\n");
for (int i = 0; i < total_pages; ++i) {
scanf("%d", &pages[i]);
}
for (int i = 0; i < frames; ++i) {
temp[i] = -1;
}
printf("\nPage\tFrames\n");
for (int i = 0; i < total_pages; ++i) {
ag1 = ag2 = 0;
for (int j = 0; j < frames; ++j) {
fi
fl
fl
fl
fl
if (temp[j] == pages[i]) {
counter++;
time[j] = counter;
ag1 = ag2 = 1;
break;
}
}
if (! ag1) {
for (int j = 0; j < frames; ++j) {
if (temp[j] == -1) {
counter++;
page_faults++;
temp[j] = pages[i];
time[j] = counter;
ag2 = 1;
break;
}
}
}
if (! ag2) {
pos = ndLRU(time, frames);
counter++;
page_faults++;
temp[pos] = pages[i];
time[pos] = counter;
}
printf("%d\t", pages[i]);
for (int j = 0; j < frames; ++j) {
if (temp[j] != -1)
printf("%d ", temp[j]);
else
printf("- ");
}
printf("\n");
}
printf("\nTotal Page Faults = %d\n", page_faults);
return 0;
}
fl
fl
fl
fl
fi
fl
Output :
Practical - 10
Aim: Write a program to implement following Disk Scheduling Algorithms:
a) FCFS (First Come First Serve)
b) Elevator Algorithm (SCAN)
Print the Seek time for each algorithm.
Code :
#include <stdio.h>
#include <stdlib.h>
void fcfs(int requests[], int n, int head) {
int seek = 0;
printf("\nFCFS Disk Scheduling:\n");
printf("Sequence: %d", head);
for (int i = 0; i < n; i++) {
seek += abs(head - requests[i]);
head = requests[i];
printf(" -> %d", head);
}
printf("\nTotal Seek Time: %d\n", seek);
}
void scan(int requests[], int n, int head, int disk_size) {
int seek = 0;
int direction, i;
int sorted[100];
for (i = 0; i < n; i++)
sorted[i] = requests[i];
sorted[n++] = head;
sorted[n++] = 0;
sorted[n++] = disk_size - 1;
// Sort the requests
for (i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (sorted[i] > sorted[j]) {
int temp = sorted[i];
sorted[i] = sorted[j];
sorted[j] = temp;
}
// Find head index
for (i = 0; i < n; i++)
if (sorted[i] == head) break;
printf("\nSCAN Disk Scheduling (moving towards higher tracks rst):\n");
printf("Sequence: %d", head);
// Move right
for (int j = i + 1; j < n; j++) {
seek += abs(head - sorted[j]);
head = sorted[j];
printf(" -> %d", head);
}
// Then move left
for (int j = i - 1; j >= 0; j--) {
seek += abs(head - sorted[j]);
head = sorted[j];
printf(" -> %d", head);
}
printf("\nTotal Seek Time: %d\n", seek);
}
int main() {
int requests[100], n, head, disk_size;
printf("Enter number of disk requests: ");
scanf("%d", &n);
printf("Enter the request sequence:\n");
for (int i = 0; i < n; i++)
scanf("%d", &requests[i]);
printf("Enter initial head position: ");
scanf("%d", &head);
printf("Enter disk size: ");
scanf("%d", &disk_size);
fcfs(requests, n, head);
scan(requests, n, head, disk_size);
return 0;
}
fi
Output :