Operating System Assignment - DHANANJAY SINGHAL
Operating System Assignment - DHANANJAY SINGHAL
Operating System Assignment - DHANANJAY SINGHAL
HANSRAJ COLLEGE
BSC (HONS) COMPUTER SCIENCE
SEMESTER - 3
OPERATING SYSTEM
DHANANJAY SINGHAL
21CS/13
Question 1 :
Write a program (using fork() and/or exec() commands) where parent and child execute:
a) same program, same code.
b) same program, different code.
c) before terminating, the parent waits for the child to finish its task.
Solution 1(a) :
"""
os.fork() is used to create child process Returns 0 in child process and child's
process id in parent process
"""
# This code won't work on Windows, use online compiler to execute
import os
pid = os.fork()
if pid < 0:
print("Fork failed")
quit()
Output 1(a) :
Solution 1(b) :
import os
pid = os.fork()
# p > 0 ---> Parent process
if pid > 0:
print(f"I am parent process. Actual process id : {os.getpid()} ")
print("Exiting the parent process")
else:
print("Forking Error")
quit()
Output 1(b) :
Solution 1(c) :
# c) before terminating, the parent waits for the child to finish its task.
import os
pid = os.fork()
# p > 0 ---> Parent process
if pid > 0:
print(f"I am parent process. Actual process id : {os.getpid()} ")
os.waitpid(-1, 0)
print("Exiting the parent process")
else:
print("Forking Error")
quit()
Output 1(c) :
Question 2 :
Write a program to report behaviour of Linux kernel including kernel version, CPU type and
model. (CPU information)
Solution 2 :
import platform
Output 2 :
Question 3 :
Solution 3 :
import psutil
print(f"Total memory : {psutil.virtual_memory()}")
print(f"Total memory (in GB) : {psutil.virtual_memory().total / (1024.0 **
3):.3f}")
print(f"Used memory (in GB) : {psutil.virtual_memory().used / (1024.0 ** 3):.3f}")
print(f"Available memory (in GB) : {psutil.virtual_memory().available / (1024.0 **
3):.3f}")
print(f"Percentage : {psutil.virtual_memory().percent}")
Output 3 :
Question 4 :
Write a program to print file details including owner access permissions, file access time,
where file name is given as argument
Solution 4 :
import os
from stat import *
statinfo = os.stat('Downloads')
mode = statinfo.st_mode
if S_ISDIR(mode):
print("Directory")
elif S_ISREG(mode):
print("Regular File")
filePerm = filemode(mode)
Question 5 :
Solution 5 :
file1 = "file1.txt"
file2 = "file2.txt"
lines=" "
with open(file1,'r',encoding='utf8') as src:
lines = src.readlines()
Output 5 :
file1.txt (before) executing the code : file2.txt (before) executing the code :
file2.txt (after) executing the code :
Question 6 :
Solution 6 :
#include <iostream>
using namespace std;
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
}
}
int main()
{
int n;
cout << "Enter number of processes : ";
cin >> n;
int processes[n];
for (int i = 0; i < n; i++)
{
processes[i] = i + 1;
}
int burst_time[n];
cout << "Enter burst time of processes :- " << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << " : ";
cin >> burst_time[i];
}
findavgTime(processes, n, burst_time);
return 0;
}
Output 6 :
Question 7 :
Solution 7 :
#include <iostream>
using namespace std;
while (1)
{
bool done = true;
// Driver code
int main()
{
int n;
cout << "Enter number of processes : ";
cin >> n;
int processes[n];
for (int i = 0; i < n; i++)
{
processes[i] = i + 1;
}
int burst_time[n];
cout << "Enter burst time of processes :- " << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << " : ";
cin >> burst_time[i];
}
// Time quantum
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Output 7 :
Question 8 :
Solution 8 :
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
/*
at = Arrival time,
bt = Burst time,
ct = Completion time,
ta = Turn around time,
wt = Waiting time
*/
} Schedule;
int main()
{
Schedule pro[10];
// An array of Processes
int n, i, j, pcom;
// n = number of processes, i= iteration variable
cout << "Enter the Process id, arrival time and burst time of " << n << "
processes :::" << endl;
i = 0;
pcom = 0;
while (pcom < n)
{
for (j = 0; j < n; j++)
{
if (pro[j].at > i)
break;
}
if (j > 0)
{
{
i = pro[j].at;
}
pro[j].ct = i + 1;
pro[j].bt--;
}
i++;
pcom = 0;
for (j = 0; j < n; j++)
{
if (pro[j].bt == 0)
pcom++;
}
}
Output 8 :
Question 9 :
Solution 9 :
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Process
{
int pID;
int priority;
float arrivalTime;
float burstTime;
float completionTime;
float waitingTime;
float turnAroundTime;
};
return;
}
int main()
{
int n;
printAvgTime(p, n);
cout << endl;
return 0;
}
Output 9 :
Question 10 :
Solution 10 :
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Process
{
int pID;
int priority;
int arrivalTime;
int burstTime;
int completionTime;
int waitingTime;
int turnAroundTime;
};
void swapProcess(struct Process *a, struct Process *b)
{
struct Process temp = *a;
*a = *b;
*b = temp;
}
if (remainingTime[minIndex] <= 0)
continue;
remainingTime[minIndex]--;
if (remainingTime[minIndex] == 0)
{
count++;
p[minIndex].completionTime = time + 1;
}
}
return;
}
return;
}
int main()
{
int n;
printAvgTime(p, n);
cout << endl;
return 0;
}
Output 10 :
Question 11 :
Solution 11 :
#include <bits/stdc++.h>
using namespace std;
struct Process
{
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
if (check == false)
{
t++;
continue;
}
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// Increment complete
complete++;
check = false;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Driver code
int main()
{
Process proc[] = {{1, 6, 2}, {2, 2, 5}, {3, 8, 1}, {4, 3, 0}, {5, 4, 4}};
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}
Output 11 :
Question 12 :
Solution 12 :
if __name__ == "__main__":
thread = Thread(target=callThread, args=(10, ))
thread.start()
thread.join()
print("Parent thread")
print("Thread finished... Exiting")
Output 12 :
Question 13 :
Solution 13 :
#include <iostream>
using namespace std;
class MemoryManagementAlgo
{
public:
int *block_size;
int total_blocks;
int *process_size;
int total_process;
MemoryManagementAlgo(int blkSize[], int tBlocks, int prSize[], int tProcess)
{
block_size = blkSize;
total_blocks = tBlocks;
process_size = prSize;
total_process = tProcess;
}
void First_Fit()
{
int allocation[total_process];
for (int i = 0; i < total_process; i++)
{
allocation[i] = -1;
}
void Best_Fit()
{
int allocation[total_process];
for (int i = 0; i < total_process; i++)
{
allocation[i] = -1;
}
void Worst_Fit()
{
int allocation[total_process];
for (int i = 0; i < total_process; i++)
{
allocation[i] = -1;
}
int main()
{
/*
blkSize - Array to store Block Sizes
prcSize - Array to store Process Size
tblocks - Total number of blocks
tprc - Total number of process
*/
int blkSize[tblocks];
cout << "Enter block sizes :::" << endl;
for (int i = 0; i < tblocks; i++)
{
cout << i + 1 << " - ";
cin >> blkSize[i];
}
int prcSize[tprc];
cout << "Enter process sizes :::" << endl;
for (int i = 0; i < tprc; i++)
{
cout << i + 1 << " - ";
cin >> prcSize[i];
}
cout << "\nEnter choice : \n1 - First Fit \n2 - Best Fit \n3 - Worst Fit\n";
int choice;
cin >> choice;
MemoryManagementAlgo ob(blkSize, tblocks, prcSize, tprc);
switch (choice)
{
case 1:
{
cout << "Your choice : First Fit" << endl;
ob.First_Fit();
break;
}
case 2:
{
cout << "Your choice : Best Fit" << endl;
ob.Best_Fit();
break;
}
case 3:
{
cout << "Your choice : Worst Fit" << endl;
ob.Worst_Fit();
break;
}
default:
{
cout << "Invalid choice" << endl;
break;
}
}
return 0;
}
Output 13 :
END OF ASSIGNMENT