Os CN
Os CN
Applications,
National Institute of Technology, Kurukshetra
Haryana, India
Lab manual
1
Index of Experiments
Teache
Sno. Experiment Page
r Sign
Implement File Directories: Single Level, Two Level, Tree Level, Acyclic 27
3 Graph Directory
2
3
1. Implement CPU scheduling schemes: FCFS, SJF, SRTF, Priority, Round
Robin, Multi-Level Queue and find out avg. turn-around time, avg. waiting
time, response time, throughput.
Solution:
FCFS scheduling :
import java.util.Scanner;
int totalTat = 0;
int totalWt = 0;
arrivalTimes[i] = scanner.nextInt();
4
System.out.println("Enter burst times:");
burstTimes[i] = scanner.nextInt();
int currentTime = 0;
currentTime = arrivalTimes[i];
currentTime = completionTimes[i];
responseTimes[i] = waitingTimes[i];
totalTat += turnAroundTimes[i];
totalWt += waitingTimes[i];
scanner.close();
}
5
}
Output :
SJF scheduling:
import java.util.*;
class sjf {
6
static class util1 {
int p_id;
int bt1;
};
static void update(int node, int st, int end, int ind, int id1, int b_t) {
if (st == end) {
tr[node].p_id = id1;
tr[node].bt1 = b_t;
return;
}
int mid = (st + end) / 2;
if (ind <= mid)
update(2 * node, st, mid, ind, id1, b_t);
else
update(2 * node + 1, mid + 1, end, ind, id1, b_t);
if (tr[2 * node].bt1 < tr[2 * node + 1].bt1) {
tr[node].bt1 = tr[2 * node].bt1;
tr[node].p_id = tr[2 * node].p_id;
} else {
tr[node].bt1 = tr[2 * node + 1].bt1;
tr[node].p_id = tr[2 * node + 1].p_id;
}
}
static util1 query(int node, int st, int end, int lt, int rt) {
if (end < lt || st > rt)
return range;
if (st >= lt && end <= rt)
return tr[node];
int mid = (st + end) / 2;
util1 lm = query(2 * node, st, mid, lt, rt);
util1 rm = query(2 * node + 1, mid + 1, end, lt, rt);
if (lm.bt1 < rm.bt1)
return lm;
return rm;
}
while (counter != 0) {
for (; upper_range <= n;) {
upper_range++;
if (ar[upper_range].at > tm || upper_range > n) {
upper_range--;
break;
}
if (res.bt1 != Integer.MAX_VALUE) {
counter--;
int index = mp[res.p_id];
tm += (res.bt1);
ar[index].ct = tm;
ar[index].tat = ar[index].ct - ar[index].at;
ar[index].wt = ar[index].tat - ar[index].bt;
non_preemptive_sjf(n);
}
static void print(int n) {
int totalTAT = 0;
int totalWT = 0;
System.out.println("ProcessId Arrival Time Burst Time Completion Time Turn Around
Time Waiting Time");
for (int i = 1; i <= n; i++) {
System.out.printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
ar[i].id, ar[i].at, ar[i].bt, ar[i].ct, ar[i].tat, ar[i].wt);
8
totalTAT += ar[i].tat;
totalWT += ar[i].wt;
}
double avgTAT = (double) totalTAT / n;
double avgWT = (double) totalWT / n;
System.out.printf("\nAverage Turnaround Time: %.2f\n", avgTAT);
System.out.printf("Average Waiting Time: %.2f\n", avgWT);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of processes: ");
int n = sc.nextInt();
range.p_id = Integer.MAX_VALUE;
range.bt1 = Integer.MAX_VALUE;
for (int i = 1; i <= 4 * sh + 1; i++) {
tr[i].p_id = Integer.MAX_VALUE;
tr[i].bt1 = Integer.MAX_VALUE;
}
for (int i = 1; i <= n; i++) {
System.out.print("Enter arrival time for process " + i + ": ");
ar[i].at = sc.nextInt();
System.out.print("Enter burst time for process " + i + ": ");
ar[i].bt = sc.nextInt();
ar[i].id = i;
}
execute(n);
print(n);
}
}
Output:
9
SRTF scheduling:
import java.util.*;
class srtf{
int completed = 0;
int currentTime = 0;
int prevTime = 0;
Process currentProcess = null;
while (completed != n) {
int minRemainingTime = Integer.MAX_VALUE;
Process nextProcess = null;
if (nextProcess == null) {
currentTime++;
continue;
}
if (currentProcess != nextProcess) {
prevTime = currentTime;
currentProcess = nextProcess;
}
currentProcess.remainingTime--;
currentTime++;
if (currentProcess.remainingTime == 0) {
completed++;
currentProcess.completionTime = currentTime;
currentProcess.turnaroundTime = currentProcess.completionTime -
currentProcess.arrivalTime;
currentProcess.waitingTime = currentProcess.turnaroundTime -
currentProcess.burstTime;
currentProcess = null;
}
}
int totalTAT = 0;
int totalWT = 0;
Output :
Priority scheduling :
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Process {
int id, arrivalTime, burstTime, priority, completionTime, waitingTime, turnaroundTime;
this.burstTime = burstTime;
this.priority = priority;
}
}
int currentTime = 0;
for (Process process : processes) {
if (currentTime < process.arrivalTime) {
currentTime = process.arrivalTime;
}
process.completionTime = currentTime + process.burstTime;
process.turnaroundTime = process.completionTime - process.arrivalTime;
process.waitingTime = process.turnaroundTime - process.burstTime;
currentTime += process.burstTime;
}
sc.close();
}
}
Output :
Round Robin :
import java.util.Scanner;
class RoundRobin {
// Driver Method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Get Burst time and Arrival time for each process from the user
for (int i = 0; i < n; i++) {
processes[i] = i + 1; // Process IDs are 1-based
16
Output :
Solution:
In contiguous allocation, each file is stored in a contiguous block of memory. This technique
can be implemented using a simple array where each element represents a block of memory.
#include <iostream>
using namespace
std;
class ContiguousAllocation {
public:
int* memoryBlock;
int blockSize;
ContiguousAllocation(int
size) { memoryBlock = new
int[size]; blockSize = size;
for (int i = 0; i < blockSize; i++) {
17
memoryBlock[i] = -1; // -1 means empty block
}
}
18
memoryBlock[i] = 1; // 1 means allocated
}
cout << "File allocated successfully!" << endl;
}
void display() {
cout << "Memory Blocks: ";
for (int i = 0; i < blockSize; i++) {
cout << memoryBlock[i] << " ";
}
cout << endl;
}
};
int main() {
int size = 10;
ContiguousAllocation contigAlloc(size);
contigAlloc.display();
contigAlloc.allocate(3, 4); // Allocate 4 blocks starting from
block 3 contigAlloc.display();
return 0;
}
Output:
19
Linked-list Allocation
In linked-list allocation, each file is divided into blocks, and each block contains a pointer to the
next block. It can be implemented using a linked list.
#include <iostream>
using namespace
std;
class Block {
public:
int blockNum;
Block* next;
Block(int num) {
blockNum =
num; next =
nullptr;
}
};
class LinkedListAllocation {
public:
Block* head;
LinkedListAllocation(
) { head = nullptr;
}
void display() {
Block* temp = head;
20
cout << "Allocated blocks: ";
while (temp != nullptr) {
cout << temp->blockNum << " ";
21
temp = temp->next;
}
cout << endl;
}
};
int main() {
int blocks[] = {2, 4, 6, 8}; // Blocks allocated for a file
LinkedListAllocation linkedAlloc;
linkedAlloc.allocate(blocks,
4); linkedAlloc.display();
return 0;
}
Output:
22
Indirect Allocation (Indexing)
In indirect allocation, a file is divided into blocks, and the block addresses are stored in an index
block. The index block points to the file’s data blocks.
#include <iostream>
#include <vector>
using namespace
std;
class IndirectAllocation
{ public:
vector<int> indexBlock;
vector<int> dataBlocks;
void display() {
cout << "Index Block: ";
for (int i = 0; i < indexBlock.size();
i++) { cout << indexBlock[i] << " ";
}
cout << endl;
int main() {
IndirectAllocation indirectAlloc;
vector<int> fileBlocks = {5, 8, 9, 2}; // Blocks allocated for a file
indirectAlloc.allocate(fileBlocks);
indirectAlloc.display();
23
return 0;
}
24
Output:
25
3. Implement File Directories: Single Level, Two Level, Tree Level, Acyclic Graph
Directory.
Solution:
Single-Level Directory
#include <iostream>
#include <vector>
#include <string>
using namespace
std;
class SingleLevelDirectory {
vector<string> files;
public:
void createFile(const string& filename) {
files.push_back(filename);
cout << "File '" << filename << "' created." << endl;
}
void displayFiles() {
cout << "Files in Single-Level Directory:" <<
endl; for (const auto& file : files) {
cout << file << endl;
}
}
};
int main() {
SingleLevelDirectory dir;
dir.createFile("file1.txt");
dir.createFile("file2.txt");
dir.displayFiles();
return 0;
}
Output:
26
Two-Level Directory
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace
std;
class TwoLevelDirectory {
map<string, vector<string>> userDirectories;
public:
void createFile(const string& user, const string& filename) {
userDirectories[user].push_back(filename);
cout << "File '" << filename << "' created for user '" << user << "'." << endl;
}
int main() {
TwoLevelDirectory dir;
dir.createFile("user1", "file1.txt");
dir.createFile("user1", "file2.txt");
dir.createFile("user2", "file3.txt");
dir.displayFiles("user1");
dir.displayFiles("user2");
return 0;
}
Output:
27
Tree-Level Directory
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace
std;
class TreeLevelDirectory {
map<string, vector<string>> directoryStructure;
public:
void createFile(const string& directory, const string& filename) {
directoryStructure[directory].push_back(filename);
cout << "File '" << filename << "' created in directory '" << directory << "'." << endl;
}
int main() {
TreeLevelDirectory dir;
dir.createFile("root", "file1.txt");
dir.createFile("root", "file2.txt");
dir.createFile("subdir", "file3.txt");
dir.displayFiles("root");
dir.displayFiles("subdir");
return 0;
}
Output:
28
Acyclic Graph Directory
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace
std;
class AcyclicGraphDirectory {
map<string, vector<string>> directoryStructure;
public:
void linkDirectory(const string& parent, const string& child) {
directoryStructure[parent].push_back(child);
cout << "Directory '" << child << "' linked to '" << parent << "'." << endl;
}
int main() {
AcyclicGraphDirectory dir;
dir.createFile("root", "file1.txt");
dir.linkDirectory("root", "subdir1");
dir.createFile("subdir1", "file2.txt");
dir.linkDirectory("subdir1", "sharedDir");
dir.createFile("sharedDir", "file3.txt");
dir.displayStructure("root");
dir.displayStructure("subdir1");
return 0;
}
29
Output:
30
4. Implement Disk Scheduling: FCFS, SSTF, SCAN, Circular SCAN, Look and
Circular Look.
Solution:
FCFS:
#include <iostream>
#include <vector>
#include <cmath>
cout << "\nTotal Head Movement: " << totalHeadMovement << " tracks\n";
}
int main()
{ int
head;
int n;
vector<int> requests(n);
cout << "Enter the disk track requests:
"; for (int i = 0; i < n; ++i) {
cin >> requests[i];
}
31
cout << "Enter the initial head
position: "; cin >> head;
fcfsDiskScheduling(requests, head);
return 0;
}
OUTPUT:
SSTF:
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
return closestRequest;
32
}
while (!requests.empty()) {
// Find the closest request
int closestRequest = findClosestRequest(requests, currentHead);
cout << "\nTotal Head Movement: " << totalHeadMovement << " tracks\n";
}
int main()
{ int
head;
int n;
vector<int> requests(n);
cout << "Enter the disk track requests (valid range: 0 to
199): "; for (int i = 0; i < n; ++i) {
cin >> requests[i];
if (requests[i] < 0 || requests[i] > 199) {
cout << "Error: Request " << requests[i] << " is out of range. Please
33
restart.\n"; return 1;
}
}
sstfDiskScheduling(requests, head);
return 0;
}
OUTPUT:
SCAN:
#include <iostream>
#include <vector>
#include <algorithm> // For sort
#include <cmath> // For abs()
void cscanDiskScheduling(int start, int end, int head, vector<int>& requests, char
direction) { int totalHeadMovement = 0;
vector<int> left, right;
// Separate requests into two groups: those to the left and right of the
head for (int request : requests) {
if (request < head)
34
left.push_back(request);
else
right.push_back(request);
}
cout << "\nTotal Head Movement: " << totalHeadMovement << " tracks\n";
}
int main() {
int start, end, head;
char direction;
vector<int> requests(num);
for (int i = 0; i < num; i++) {
cout << "Enter block " << i + 1 << ":
"; cin >> requests[i];
}
return 0;
}
OUTPUT:
36
CSCAN:
#include
<iostream>
#include <vector>
#include
<algorithm>
#include <cmath> // For abs()
function using namespace std;
void CSCAN(int arr[], int head, int size, int disk_size)
{
int seek_count = 0;
int distance, cur_track;
vector<int> left, right;
vector<int>
seek_sequence;
// Append the end values (0 and disk_size - 1) to represent the start and end of
the disk left.push_back(0); // Start of the disk (cylinder 0)
right.push_back(disk_size - 1); // End of the disk (maximum cylinder)
// Once the head reaches the end (disk_size - 1), jump back to the beginning
(cylinder 0) seek_count += (disk_size - 1 - head); // Add the jump distance from
head to 0
37
head = 0; // Now, head is at cylinder 0
// Service the requests on the left side of the head (after returning to
cylinder 0) for (int i = 0; i < left.size(); i++) {
cur_track = left[i];
seek_sequence.push_back(cur_trac
k); distance = abs(cur_track -
head); seek_count += distance;
head = cur_track;
}
int main()
{
int size, head, disk_size;
// Taking input from the
user
cout << "Enter the number of
requests: "; cin >> size;
int arr[size];
cout << "Enter the disk size (maximum cylinder
number): "; cin >> disk_size;
return 0;
38
}
39
OUTPUT:
LOOK SCHEDULING-
#include <iostream>
#include <algorithm>
#include <vector>
using namespace
std;
sort(left.begin(), left.end());
sort(right.begin(), right.end());
if (direction == "left") {
40
total_movement += abs(head -
left[i]); head = left[i];
seek_sequence.push_back(head);
}
cout << "Total Head Movement in LOOK: " << total_movement << endl; cout
<< "Seek Sequence: ";
for (int i = 0; i < seek_sequence.size(); i++) { cout
<< seek_sequence[i];
if (i < seek_sequence.size() - 1) cout << " -> ";
}
cout << endl;
}
int main() {
int n, head;
string direction;
C-LOOK SCHEDULING-
#include <bits/stdc++.h>
using namespace std;
int size = 8;
int disk_size = 200;
int size = 8;
for (int i = 0; i < size; i++)
{ if (arr[i] < head)
left.push_back(arr[i]);
if (arr[i] > head)
right.push_back(arr[i]);
}
42
std::sort(left.begin(), left.end());
std::sort(right.begin(), right.end());
for (int i = 0; i < right.size(); i++) {
cur_track = right[i];
seek_sequence.push_back(cur_trac
k); distance = abs(cur_track -
head); seek_count += distance;
head = cur_track;
}
seek_count += abs(head - left[0]);
head = left[0];
seek_sequence.push_back(cur_trac
k); distance = abs(cur_track -
head); seek_count += distance;
head = cur_track;
}
43
CLOOK(arr, head);
return 0;
}
OUTPUT:
44
5. Implement of different Network Topologies. Locating different interfaces,
routers and switches. Studying different pools of IP addresses.
Solution:
BUS TOPOLOGY_-
STAR TOPOLOGY_-
45
RING TOPOLOGY_-
MESH TOPOLOGY_-
46
6. Learn and observe the usage of different networking commands e.g.
PING, TRACEROUTE. Learning remote login using telnet session.
Measuring typical average delays between different locations of the
network.
Solution:
1. PING Command
The PING command is used to check the network connectivity between your system
and a target system (usually a remote server). It sends Internet Control Message
Protocol (ICMP) Echo Request messages and measures the round-trip time for the
messages to reach the destination.
The output shows the round-trip time and the average time it took to send and receive
the request.
2. TRACEROUTE Command
The TRACEROUTE command is used to trace the path that packets take from your
machine to a destination system, showing each intermediate router’s IP address and
the time taken to reach each hop.
Example :
47
The TRACEROUTE output displays each hop along the network path and the round-trip
times for each. It helps diagnose where the network delays are occurring.
48