Osprojectfilesem 3
Osprojectfilesem 3
i. Information Maintenance
wc(word count)
Date(shows Date)
Rm(removes files)
rmdir,
ls.
Q2.Execute various LINUX commands for:
i. Process Control: fork, getpid, ps, kill,
sleep
ii. Communication: Input-output
redirection, Pipe
iii. Protection Management: chmod,
chown, chgrp
Output:
i. Process Control:
1. fork(): Used to create a new process
by duplicating the calling process. It’s
a system call, so it won't be executed
directly in the shell, but through C
programming.
Example in C:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
printf("This is the child process\n");
} else {
printf("This is the parent process\n");
}
return 0;
}
int main() {
printf("Process ID: %d\n", getpid());
return 0;
}
ii. Communication:
1. Input-output redirection: Allows
you to redirect the input and output of
commands to/from files or other
streams.
int main() {
pid_t pid = fork(); // Create child process
Output:
Code(ii):
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork(); // Create child process
return 0;
}
Output:
Code(iii):
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork(); // Create child process
if (pid == 0) { // Child process
std::cout << "Child process with PID " << getpid() << "
started." << std::endl;
sleep(2); // Simulating work in child
std::cout << "Child process finished." << std::endl;
} else { // Parent process
std::cout << "Parent process with PID " << getpid() << "
started." << std::endl;
wait(NULL); // Wait for child to finish
std::cout << "Parent process resumed after child
finished." << std::endl;
}
return 0;
}
Output:
4. Write a program to report behaviour of
Linux kernel including kernel version, CPU
type and CPU information.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
cout << "Kernel Version and Information:" << endl;
system("uname -r");
return 0;
}Output:
int main() {
std::cout << "Memory Information:" << std::endl;
system("free -h");
return 0;
}
Output:
int main() {
const char* source = "source.txt";
const char* destination = "destination.txt";
char buffer[1024];
ssize_t bytesRead;
while ((bytesRead = read(src, buffer, sizeof(buffer))) > 0) {
write(dest, buffer, bytesRead);
}
close(src);
close(dest);
Output:
(because there is no source file present)
struct Process {
int id, arrival_time, burst_time;
};
int main() {
int n;
std::cout << "Enter number of processes: ";
std::cin >> n;
std::vector<Process> processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter arrival time and burst time for
process " << i + 1 << ": ";
std::cin >> processes[i].arrival_time >>
processes[i].burst_time;
processes[i].id = i + 1;
}
int time = 0;
std::cout << "\nProcess Execution Order (FCFS):\n";
for (const auto& p : processes) {
if (time < p.arrival_time)
time = p.arrival_time; // Wait for the process to arrive
time += p.burst_time; // Execute the process
std::cout << "P" << p.id << " (Arrival: " << p.arrival_time
<< ", Burst: " << p.burst_time << ")\n";
}
return 0;
}
Output:
struct Process {
int id, burst_time, waiting_time, turnaround_time;
};
int main() {
int n;
std::cout << "Enter number of processes: ";
std::cin >> n;
std::vector<Process> processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter burst time for process " << i + 1 <<
": ";
std::cin >> processes[i].burst_time;
processes[i].id = i + 1;
}
int time = 0;
double total_wt = 0, total_tat = 0;
std::cout << "Process Execution Order (SJF):\n";
for (auto& p : processes) {
p.waiting_time = time;
p.turnaround_time = p.waiting_time + p.burst_time;
time += p.burst_time;
total_wt += p.waiting_time;
total_tat += p.turnaround_time;
std::cout << "P" << p.id << " (Burst: " << p.burst_time
<< ", Waiting Time: " << p.waiting_time
<< ", Turnaround Time: " << p.turnaround_time <<
")\n";
}
std::cout << "Average Waiting Time: " << total_wt / n <<
std::endl;
std::cout << "Average Turnaround Time: " << total_tat / n
<< std::endl;
return 0;
}
Output:
struct Process {
int id, burst_time, priority, waiting_time,
turnaround_time;
};
int main() {
int n;
std::cout << "Enter number of processes: ";
std::cin >> n;
std::vector<Process> processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter burst time and priority for process "
<< i + 1 << ": ";
std::cin >> processes[i].burst_time >>
processes[i].priority;
processes[i].id = i + 1;
}
int time = 0;
double total_wt = 0, total_tat = 0;
std::cout << "Process Execution Order (Priority
Scheduling):\n";
for (auto& p : processes) {
p.waiting_time = time;
p.turnaround_time = p.waiting_time + p.burst_time;
time += p.burst_time;
total_wt += p.waiting_time;
total_tat += p.turnaround_time;
std::cout << "P" << p.id << " (Burst: " << p.burst_time
<< ", Priority: " << p.priority
<< ", Waiting Time: " << p.waiting_time
<< ", Turnaround Time: " << p.turnaround_time <<
")\n";
}
return 0;
}
Output:
10. Write a program to implement SRTF
scheduling algorithm.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
struct Process {
int id, arrival_time, burst_time, remaining_time,
completion_time, waiting_time, turnaround_time;
};
int main() {
int n;
std::cout << "Enter number of processes: ";
std::cin >> n;
std::vector<Process> processes(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter arrival time and burst time for
process " << i + 1 << ": ";
std::cin >> processes[i].arrival_time >>
processes[i].burst_time;
processes[i].id = i + 1;
processes[i].remaining_time =
processes[i].burst_time; // Initialize remaining time
}
if (shortestProcess != -1) {
// Process the shortest job
processes[shortestProcess].remaining_time--;
currentTime++;
totalWaitingTime +=
processes[shortestProcess].waiting_time;
totalTurnaroundTime +=
processes[shortestProcess].turnaround_time;
isCompleted[shortestProcess] = true;
completed++;
}
} else {
// If no process is ready to execute, increment time
currentTime++;
}
}
// Output the results
std::cout << "\nProcess execution details (SRTF
Scheduling):\n";
for (const auto& p : processes) {
std::cout << "P" << p.id << " (Arrival Time: " <<
p.arrival_time
<< ", Burst Time: " << p.burst_time
<< ", Completion Time: " << p.completion_time
<< ", Turnaround Time: " << p.turnaround_time
<< ", Waiting Time: " << p.waiting_time << ")\n";
}
Output:
11. Write a program to calculate sum of n
numbers using Pthreads. A list of n
numbers is divided into two smaller list of
equal size, two separate threads are used
to sum the sub lists.
Code:
#include <iostream>
#include <pthread.h>
#include <vector>
struct ThreadData {
std::vector<int>* data;
int start, end;
int sum;
};
int main() {
int n;
std::cout << "Enter number of elements: ";
std::cin >> n;
std::vector<int> data(n);
std::cout << "Enter the numbers:\n";
for (int i = 0; i < n; ++i) {
std::cin >> data[i];
}
pthread_t threads[2];
ThreadData td1 = {&data, 0, n / 2, 0};
ThreadData td2 = {&data, n / 2, n, 0};
return 0;
}
Output:
int main() {
// Sample input for memory blocks and processes
vector<int> blockSize = {100, 500, 200, 300, 600}; //
Block sizes
vector<int> processSize = {212, 417, 112, 426}; //
Process sizes
// First-Fit Allocation
vector<int> firstFitAlloc = firstFit(blockSize,
processSize);
cout << "First-Fit Allocation:\n";
displayAllocation(blockSize, processSize, firstFitAlloc);
// Best-Fit Allocation
vector<int> bestFitAlloc = bestFit(blockSize,
processSize);
cout << "\nBest-Fit Allocation:\n";
displayAllocation(blockSize, processSize, bestFitAlloc);
// Worst-Fit Allocation
vector<int> worstFitAlloc = worstFit(blockSize,
processSize);
cout << "\nWorst-Fit Allocation:\n";
displayAllocation(blockSize, processSize, worstFitAlloc);
return 0;
}
Output: