OS LAB FILE Prayank
OS LAB FILE Prayank
OPERATING SYSTEMS
Practical File
Submitted To:
Ms. Trasha Gupta
INDEX
3. 23/8/2024
Write a menu-driven program to implement Round Robin
Algorithm. Input - Sequence of Processes with their arrival
time and burst time and Maximum Time Quantum. Output -
Sequence of processes executing, Average Waiting time
and Average Turnaround time.
5. 6/9/2024
Write a program to create a child process using fork(). Print
the process id of the child and the parent process. Also
Explain the behavior of fork in parent and child process.
6. 13/9/2024
Write a C program that creates a new child process using
fork(). The child process should be assigned to find the
length of your name using the execlp() system call.
7. 20/9/2024
Write a program that creates another process. The child
process should print the contents of any text on the screen
using the cat command. The parent process should exit
only after the child process has completed its execution.
8. 4/10/2024
Write a program to implement Banker's Algorithm. The
program should either print the safe sequence of execution
of given processes (if any exists) or print "There is a
deadlock in the system."
9. 11/10/2024
Write a program to implement the FCFS and Elevator
Algorithm for Disk Scheduling Print the Seek time for each
algorithm. You may consider your own input queue for the
requests (cylinder no.).
10. Write a program to implement the SSTF and LOOK Disk 18/10/2024
Scheduling Algorithms Print the Seek time for each
algorithm. You may consider your own input queue for the
requests (cylinder no.).
Aim: Write a C program to copy the contents of one file to another file using system calls like read(),
write(), open() and close().
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define BUFFER_SIZE 10000
int main() {
string source_;
string destination_;
ifstream source(source_);
if (!source.is_open()) {
cout << "Error in opening the source file: " << source_ << endl;
return 1;
}
ofstream destination(destination_);
if (!destination.is_open()) {
cout << "Error in opening the destination file: " << destination_ << endl;
source.close();
return 1;
}
string line;
while (getline(source, line)) {
destination << line << endl;
}
source.close();
destination.close();
cout << "File copied successfully" << endl;
return 0;
Output:
Experiment II
Aim: Write a menu-driven program to implement First Come First Serve and Shortest Time First
Algorithm (Non Preemptive) scheduling algorithm. Input - Sequence of Processes with their arrival time
and burst time. Output - Sequence of processes executing, Average Waiting time and Average
Turnaround time.
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
int main() {
int n, choice;
printf("Enter the number of processes: ");
scanf("%d", &n);
do {
printf("\nScheduling Algorithms:\n");
printf("1. First Come First Serve (FCFS)\n");
printf("2. Shortest Job First (SJF) Non-Preemptive\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
fcfs(processes, n);
break;
case 2:
sjf(processes, n);
break;
case 3: printf("Exiting...
\n"); break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
printf("\nFCFS Scheduling:\n");
print_gantt_chart(processes, n);
calculate_avg_time(processes, n);
}
void sjf(struct Process processes[], int n) {
int current_time = 0;
int completed = 0;
int is_completed[n];
for (int i = 0; i < n; i++) {
is_completed[i] = 0;
}
if (idx == -1) {
current_time++; }
else {
processes[idx].waiting_time = current_time - processes[idx].arrival_time;
processes[idx].turnaround_time = processes[idx].waiting_time +
processes[idx].burst_time;
current_time += processes[idx].burst_time;
is_completed[idx] = 1;
completed++;
}
}
printf("\nSJF Non-Preemptive Scheduling:\n");
print_gantt_chart(processes, n);
calculate_avg_time(processes, n);
}
Output:
Experiment III
Aim: Write a menu-driven program to implement Round Robin Algorithm. Input - Sequence of Processes
with their arrival time and burst time and Maximum Time Quantum. Output - Sequence of processes
executing, Average Waiting time and Average Turnaround time.
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
struct Process {
int pid; // Process ID
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int waitingTime;
int turnaroundTime;
};
if (processes[idx].remainingTime == 0) { processes[idx].completionTime
= currentTime; processes[idx].turnaroundTime =
processes[idx].completionTime -
processes[idx].arrivalTime;
processes[idx].waitingTime = processes[idx].turnaroundTime -
processes[idx].burstTime;
completed+
+; } else {
readyQueue.push(idx);
inQueue.insert(idx); // Mark it as being in the queue again
}
// Add any new processes that arrive during this time slice for
(int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 &&
inQueue.find(i) == inQueue.end())
{ readyQueue.push(i);
inQueue.insert(i);
}
}
}
int main() {
int n, timeQuantum;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
cout << "Enter arrival time and burst time for process " << i + 1 << ": "; cin
>> processes[i].arrivalTime >> processes[i].burstTime;
}
roundRobin(processes, timeQuantum);
return 0;
}
Output:
Experiment IV
Aim: Write a menu-driven program to implement the Shortest remaining time First and Preemptive
Priority-based Scheduling Algorithms. Input - Sequence of Processes with their arrival time and burst
time. Output - Sequence of processes executing, Average Waiting time and Average Turnaround time.
#include <stdio.h>
#include <limits.h>
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time;
int waiting_time;
int turnaround_time;
int priority;
int completed;
};
// Function prototypes
void srtf(struct Process[], int);
void preemptive_priority(struct Process[], int);
void calculate_avg_time(struct Process[], int);
void print_gantt_chart(struct Process[], int);
int main() {
int n, choice;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
processes[i].remaining_time = processes[i].burst_time;
processes[i].completed = 0;
}
do {
printf("\nScheduling Algorithms:\n");
printf("1. Shortest Remaining Time First (SRTF)\n");
printf("2. Preemptive Priority-Based Scheduling\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
srtf(processes, n);
break;
case 2:
preemptive_priority(processes, n);
break;
case 3: printf("Exiting...
\n"); break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
min_remaining_time = processes[i].remaining_time;
shortest_process = i;
}
}
if (shortest_process == -1) {
current_time++;
continue;
}
processes[shortest_process].remaining_time--;
min_remaining_time = processes[shortest_process].remaining_time;
if (processes[shortest_process].remaining_time == 0)
{ processes[shortest_process].completed = 1;
completed++;
processes[shortest_process].turnaround_time = current_time + 1 -
processes[shortest_process].arrival_time;
processes[shortest_process].waiting_time = processes[shortest_process].turnaround_time -
processes[shortest_process].burst_time;
current_time++;
}
Output:
Experiment V
Aim: Write a program to create a child process using fork(). Print the process id of the child and the
parent process. Also Explain the behavior of fork in parent and child process.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
std::cerr << "Fork failed!" << std::endl;
return 1;
} else if (pid == 0) {
std::cout << "This is the child process." << std::endl;
std::cout << "Child Process ID: " << getpid() << std::endl;
std::cout << "Parent Process ID from child: " << getppid() <<
std::endl; } else {
std::cout << "This is the parent process." << std::endl;
std::cout << "Parent Process ID: " << getpid() << std::endl;
std::cout << "Child Process ID from parent: " << pid << std::endl;
}
return 0;
}
Output:
Experiment VI
Aim: Write a C program that creates a new child process using fork(). The child process should be
assigned to find the length of your name using the execlp() system call.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t child_pid = fork();
if (child_pid < 0)
{ perror("fork
failed"); return 1;
} else if (child_pid == 0) {
printf("Child process calculating length of name...\n");
execlp("wc", "wc", "-m", NULL);
perror("execlp failed");
return 1;
} else {
printf("Parent process waiting for the child...\n");
wait(NULL);
printf("Child process has finished.\n");
}
return 0;
}
Output:
Experiment VII
Aim: Write a program that creates another process. The child process should print the contents of any
text on the screen using the cat command. The parent process should exit only after the child process
has completed its execution.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t child_pid;
char filename[100];
child_pid = fork();
if (child_pid < 0)
{ perror("fork
failed"); return 1;
} else if (child_pid == 0) {
printf("Child process is displaying the contents of %s:\n", filename);
wait(NULL);
printf("Child process has completed. Parent exiting.\n");
}
return 0;
}
Output:
Experiment VIII
Aim: Write a program to implement Banker's Algorithm. The program should either print the safe
sequence of execution of given processes (if any exists) or print "There is a deadlock in the system."
#include <stdio.h>
#include <stdbool.h>
bool is_safe(int processes[], int avail[], int max[][10], int allocation[][10], int n, int m)
{
int need[n][m];
calculate_need(need, max, allocation, n, m);
bool finish[n];
for (int i = 0; i < n; i++)
finish[i] = false;
int safe_seq[n];
int work[m];
for (int i = 0; i < m; i++)
work[i] = avail[i];
int count = 0;
if (can_allocate) {
for (int k = 0; k < m; k++)
work[k] += allocation[p][k];
safe_seq[count++] = p;
finish[p] = true;
found = true;
}
}
}
if (!found) {
printf("There is a deadlock in the system.\n");
return false;
}
}
printf("System is in a safe state.\nSafe sequence: ");
for (int i = 0; i < n; i++)
printf("P%d ", safe_seq[i]);
printf("\n");
return true;
}
int main() {
int n, m;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the number of resource types: ");
scanf("%d", &m);
int processes[n];
for (int i = 0; i < n; i++)
processes[i] = i;
int allocation[n][10];
printf("Enter the allocation matrix:\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &allocation[i][j]);
int max[n][10];
printf("Enter the maximum demand matrix:\n");
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &max[i][j]);
int avail[m];
printf("Enter the available resources:\n");
for (int i = 0; i < m; i++)
scanf("%d", &avail[i]);
return 0;
}
Output:
Experiment IX
Aim: Write a program to implement the FCFS and Elevator Algorithm for Disk Scheduling Print the Seek
time for each algorithm. You may consider your own input queue for the requests (cylinder no.).
#include <stdio.h>
#include <stdlib.h>
int i = 0;
while (i < n && requests[i] < current_position)
i++;
if (current_position != max_cylinder) {
seek_time += abs(max_cylinder - current_position);
current_position = max_cylinder;
printf(" -> %d", current_position);
}
if (current_position != 0) {
seek_time += abs(current_position - 0);
current_position = 0;
printf(" -> %d", current_position);
}
int main() {
int n, start, max_cylinder, direction;
int requests[n];
printf("Enter the disk request queue (cylinder numbers):\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
fcfs(requests, n, start);
elevator(requests, n, start, direction, max_cylinder);
return 0;
}
Output:
Experiment X
Aim: Write a program to implement the FCFS and Elevator Algorithm for Disk Scheduling Print the Seek
time for each algorithm. You may consider your own input queue for the requests (cylinder no.).
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void look(int requests[], int n, int start, int direction, int max_cylinder) {
int seek_time = 0;
int current_position = start;
int i = 0;
while (i < n && requests[i] < current_position) i++;
int main() {
int n, start, direction, max_cylinder;
int requests[n];
printf("Enter the disk request queue (cylinder numbers):\n");
for (int i = 0; i < n; i++) {
scanf("%d", &requests[i]);
}
sstf(requests, n, start);
look(requests, n, start, direction, max_cylinder);
return 0;
}
Output:
Experiment XI
Aim: Write a program to implement FIFO and Least Recently Used algorithms for page replacement.
Input - Reference String and Number of frames. Output - Number of page faults for each LRU and FIFO.
#include <stdio.h>
int main() {
int num_pages, num_frames;
printf("Enter the number of pages in the reference string: ");
scanf("%d", &num_pages);
int pages[num_pages];
return 0;
}
Output: