Os Lab
Os Lab
1.1 OBJECTIVE:
1.2 THEORY:
A. Windows
Below are some additional details regarding requirements for key features:
1
2. BitLocker To Go is BitLocker Drive Encryption on removable data drives. This feature
includes the encryption of:
As with BitLocker, you can open drives that are encrypted by BitLocker To Go by using a
password or smart card on another computer.
In Control Panel, use BitLocker Drive Encryption. It requires a USB flash drive (available in
Windows 10 Pro and Windows 10 Enterprise only).
3. Client Hyper-V
• Many of you need to run multiple operating systems.
• Hyper-V lets you run multiple operating systems as virtual machines on
Windows.
• Hyper-V specifically provides hardware virtualization.
• That means each virtual machine runs on virtual hardware.
• Hyper-V lets you create virtual hard drives, virtual switches, and a number of
other virtual devices all of which can be added to virtual machines.
• It requires a 64-bit system with second level address translation (SLAT) capabilities
and additional 2 GB of RAM (available in Windows 10 Pro and Windows 10
Enterprise only).
4. Cortana
• It is a virtual assistant developed by Microsoft which uses the Bing search engine to
perform tasks such as setting reminders and answering questions for the user.
• It is only currently available on Windows 10 for the United States, United Kingdom,
China, France, Italy, Germany, Brazil, Mexico, Japan, Canada, Spain, Australia and
India.
5. Microsoft account is required for some features.
6. Miracast
• Miracast is a wireless display standard designed for mirroring a smartphone, tablet, or
PC’s screen to a television without requiring any physical HDMI cables. It’s becoming
more widespread with each passing day.
• Itrequires a display adapter which supports Windows Display Driver Model
(WDDM) 1.3, and a Wi-Fi adapter that supports Wi-Fi Direct.
7. Movies & TV application is not available in all regions. For the most up-to-date list of
regions, please go to the Movies & TV information page.
8. Secure boot requires firmware that supports UEFI v2.3.1 Errata B and has the Microsoft
Windows Certification Authority in the UEFI signature database.
9. Skype is available only in select countries and regions. Calling to select countries and
regions only. Excludes special, premium and non-geographic numbers. For details, visit
the Office FAQ page.
10. Snap: The number of applications that can be snapped will depend upon the minimum
resolution for the application with a limit of two applications in Tablet mode and four
applications in Desktop mode.
11. Speech recognition
2
It will vary by device microphone. For a better speech experience, you will need a:
12. Tablet mode is available on tablets and 2-in-1s with GPIO indicators or those that have a
laptop and slate indicator will be able to be configured to enter "tablet mode"
automatically.
13. Touch: To use touch, you need a tablet or a monitor that supports multi-touch.
14. Two-factor authentication requires the use of a PIN, Biometric (finger print reader or
illuminated infrared camera), or a phone with Wi-Fi or Bluetooth capabilities.
15. Windows Hello requires a camera configured for near infrared (IR) imaging or fingerprint
reader for biometric authentication. Devices without biometric sensors can use Windows
Hello with a PIN or a portable Microsoft compatible security key.
16. Xbox
• It is a video gaming brand created and owned by Microsoft. The brand consists of five video game
consoles, as well as applications (games), streaming services, an online service by the name
of Xbox network, and the development arm by the name of Xbox Game Studios. The brand
was first introduced in the United States in November 2001, with the launch of the original Xbox
console.
• Itsapplication requires an Xbox Live account, which is not available in all regions.
17. Wi-Fi Direct Printing requires a Wi-Fi adapter that supports Wi-Fi Direct and a device
that supports Wi-Fi Direct Printing.
Windows Command
1. driverquery
Drivers are very important in your PC. Missing a important driver can hamper your work.
Use driverquery command to get a full list of installed drivers in your pc. It’ll help you to
find the missing driver.
2. ipconfig
ipconfig will provide you your ip address along with your local network.
ipconfig:
Displays the current IP address of your computer and the DNS server address. Try and
observe the outputs of “ipconfig” and “ipconfig /all”.
What are the IP and MAC (Media Access Control) addresses of your computer?
3. systeminfo
Use systeminfo to know very basic information about your pc’s hardware, like –
motherboard, processor & ram.
4. ping
The ping command sends packets of data to a specific IP address (or domain) on a network
and then lets you know how long it took to transmit that data and get a response.
If you get the response properly then the connection of the device is working properly if not
a particular server or your online connection is blocking communication between
yourcomputer and another.
ping <ip or domain>
5. tasklist
Use tasklist to get current list of all tasks running on your pc.
6. Change the drive
3
Use the cd.. command to go one folder up.
You can make a new folder using the mkdir (Make Directory) command. The syntax of these
commands is mkdir Folder.
8. Dir
To test if it worked, use the dir command. The newly created folder appears in the list.
If you are working on the “C:” drive and you want to create a new folder in “D:,” called
Google, type mkdir d:\Google and then press Enter.
Type “dir” to see the list of the files and “dir /a” to see the list of the hidden files as well. Make
hello.txt visible again.
9. Cls
To clear the existing commands in prompts type cls and press Enter.cls
1. Execute the two MS-DOS commands which will set the default to the root directory (\) on the d:
drive.
d:
cd \
4
2. Type date and time and then press ENTER. DOS will let you check and change the date and
time. If the correct date/time is displayed, simply press ENTER. If the date/time is incorrect, type
the correct date/time and press ENTER.
4. Use the dir command to list the contents of the root directory on the d: drive (i.e. d:\> dir).
5. Type “help [command]” to display help information on that command. (i.e. help dir)
C:\>
5
Part IV: Basis commands and their functions
cd : Change directory or display current directory path.
cls : Clear the window.
dir : Display list of contents of current directory.
help : Display list of commands or help about a command.
notepad : Run the Windows Notepad text editor.
type : Displays the contents of a text file.
attrib : Displays or changes file attributes.
assoc : Displays or modifies file extension associations.
chkdsk : Checks a disk and displays a status report.
color : Sets the text and background colors.
comp : Compares the contents of two files or sets of files.
copy : Copies one or more files to another location.
date : Displays or sets the computer's date
del (or erase): Deletes one or more files.
echo : Displays messages, or turns command echoing on/off.
exit : Closes the DOS window.
find : Searches for a text string in a file or files.
md (or mkdir) : Creates a directory.
more : Displays the contents of a file one screen at a time.
move : Moves one or more files from one directory to another directory.
rd (or rmdir) : Removes a directory.
ren (or rename) : Renames a file or files.
sort : Sorts input.
time : Displays or sets the computer's time.
tree : Graphically displays the directory structure of a drive or directory.
xcopy : Copies files and directory trees.
6
PROGRAM-2
2.1 OBJECTIVE:
Program to simulate FCFS CPU scheduling Algorithm.
2.2 THEORY:
2.3 ALGORITHM:
Step 1 : Input the number of processes required to be scheduled using FCFS, burst time for each
process and its arrival time.
Step 2 : Using enhanced bubble sort technique, sort the all given processes in ascending order
according to arrival time in a ready queue.
Step 3 : Calculate the Finish Time, Turn Around Time and Waiting Time for each process which in
turn help to calculate Average Waiting Time and Average Turn Around Time required by CPU to
schedule given set of process using FCFS.
Step 3.1 : for i = 0, Finish Time T 0 = Arrival Time T 0 + Burst Time T 0
Step 3.2 : for i >= 1, Finish Time T i = Burst Time T i + Finish Time T i - 1
Step 3.3 : for i = 0, Turn Around Time T 0 = Finish Time T 0 - Arrival Time T 0
Step 3.4 : for i >= 1, Turn Around Time T i = Finish Time T i - Arrival Time T i
Step 3.5 : for i = 0, Waiting Time T 0 = Turn Around Time T 0 - Burst Time T 0
Step 3.6 : for i >= 1, Waiting Time T i = Turn Around Time T i - Burst Time T i - 1
Step 4 : Process with less arrival time comes first and gets scheduled first by the CPU.
Step 5 : Calculate the Average Waiting Time and Average Turn Around Time.
Step 6 : Stop.
7
int n;
printf("Enter number of processes\n");
scanf("%d",&n);
int pid[n];
printf("Enter process id\n");
for(int i=0;i<n;i++){
scanf("%d",&pid[i]);
}
int a[n];
int b[n];
for(int i=0;i<n;i++){
printf("Enter arrival time and burst time for process %d\n",i+1);
scanf("%d%d",&a[i],&b[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<n-1;j++){
if(a[j]>a[j+1]){
int temp1=a[j];
a[j]=a[j+1];
a[j+1]=temp1;
int temp2=b[j];
b[j]=b[j+1];
b[j+1]=temp2;
int temp3=pid[j];
pid[j]=pid[j+1];
pid[j+1]=temp3;
}
}
}
int ct[n];
int wt[n];
int tat[n];
float avgct=0.0;
float avgwt=0.0;
float avgtat=0.0;
for(int i=0;i<n;i++){
ct[i]=a[i]+b[i];
tat[i]=ct[i]-a[i];
wt[i]=tat[i]-b[i];
avgct=avgct+ct[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("Process ID Arrival Time Burst Time Completion Time Waiting
Time TurnAround Time\n");
for(int i=0; i<n; i++){
printf("%d\t\t", pid[i]);
printf("%d\t\t", a[i]);
printf("%d\t\t", b[i]);
printf("%d\t\t", ct[i]);
printf("%d\t\t", wt[i]);
printf("%d\t\t", tat[i]);
printf("\n");
}
printf("Average completion time : %d\n",avgct/n);
printf("Average waiting time : %d\n",avgwt/n);
8
printf("Average turn around time : %d\n",avgtat/n);
return 0;
}
2.5 OUTPUT:
9
PROGRAM-3
3.1 OBJECTIVE:
3.2 THEORY:
Shortest-Job-First (SJF) is a non-preemptive discipline in which waiting job (or process) with the
smallest estimated run-time-to-completion is run next. In other words, when CPU is available, it is
assigned to the process that has smallest next CPU burst.
The SJF scheduling is especially appropriate for batch jobs for which the run times are known in
advance. Since the SJF scheduling algorithm gives the minimum average time for a given set of
processes, it is probably optimal.The SJF algorithm favors short jobs (or processors) at the expense
of longer ones.The obvious problem with SJF scheme is that it requires precise knowledge of how
long a job or process will run, and this information is not usually available.The best SJF algorithm
can do is to rely on user estimates of run times.
3.3 ALGORITHM:
3.5 OUTPUT:
11
PROGRAM-4
4.1 OBJECTIVE:
Program to simulate SRTF CPU scheduling Algorithm.
4.2 THEORY:
The process which is currently in execution , runs until it complete or a new process is added in the
CPU Scheduler that requires smaller amount of time for execution. It is also known as shortest
remaining time first(SRTF). It is a preemptive type CPU scheduling algorithm.
4.3 ALGORITHM:
1 #include <stdio.h>
2 int main()
3 {
4 int n, i, j, time, min, index;
5 int bt[10], at[10], ct[10], tat[10], wt[10], rem_bt[10];
6 float avg_tat, avg_wt;
7
8 printf("Enter the number of processes: ");
9 scanf("%d", &n);
10
11 // Input the arrival time and burst time for each process
12 for(i = 0; i < n; i++)
13 {
14 printf("Enter arrival time and burst time for process %d: ", i+1);
15 scanf("%d %d", &at[i], &bt[i]);
16 rem_bt[i] = bt[i]; // Initialize remaining burst time
17 }
18
19 // Simulate the SRTF scheduling algorithm
20 int completed = 0;
21 time = 0;
22 while(completed != n)
23 {
24 min = 9999;
25 index = -1;
26 for(i = 0; i < n; i++)
12
27 {
28 // Check if process i is not completed and has shorter burst time
29 if(at[i] <= time && rem_bt[i] < min && rem_bt[i] > 0)
30 {
31 min = rem_bt[i];
32 index = i;
33 }
34 }
35
36 if(index == -1) // No process is available at this time
37 {
38 time++;
39 continue;
40 }
41
42 // Execute process with shortest remaining burst time
43 rem_bt[index]--;
44 time++;
45
46 // Check if the process is completed
47 if(rem_bt[index] == 0)
48 {
49 completed++;
50 ct[index] = time;
51 tat[index] = ct[index] - at[index];
52 wt[index] = tat[index] - bt[index];
53 }
54 }
55
56 // Calculate average turnaround time and waiting time
57 avg_tat = 0;
58 avg_wt = 0;
59 for(i = 0; i < n; i++)
60 {
61 avg_tat += tat[i];
62 avg_wt += wt[i];
63 }
64 avg_tat /= n;
65 avg_wt /= n;
66
67 // Output the results
68 printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
69 for(i = 0; i < n; i++)
70 {
71 printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i], bt[i], ct[i], tat[i],
wt[i]);
72 }
73 printf("\nAverage Turnaround Time = %.2f\n", avg_tat);
74 printf("Average Waiting Time = %.2f\n", avg_wt);
75
76 return 0;
77 }
13
4.5 OUTPUT:
14
PROGRAM- 5
5.1 OBJECTIVE:
Program to simulate Priority (Preemptive and Non- Preemptive) CPU scheduling Algorithm.
5.2 THEORY:
Preemptive priority scheduling is an operating system scheduling algorithm where each process
is assigned a priority, and the process with the highest priority is executed first. If a higher priority
process arrives during the execution of a lower priority process, the lower priority process is
preempted. This ensures that processes with higher priority get immediate attention, leading to
efficient utilization of resources and responsiveness.
A non- Preemptive priority algorithm is a scheduling algorithm that assigns priority levels to
processes and determines the order in which they should be executed based on their priorities.
Unlike primitive algorithms like First-Come, First-Served (FCFS) or Round Robin (RR), which do
not consider priority, non-primitive priority algorithms prioritize processes based on certain criteria.
5.3 ALGORITHM:
Preemptive:
Non-Preemptive:
Step 1 : Input the number of processes required to be scheduled using Non-Preemptive Priority
Scheduling Algorithm, burst time for each process, arrival time and there respective scheduling
priority.
Step 2 : Using enhanced bubble sort technique, sort the all given processes in ascending order
according to arrival time and if two or more processes having same arrival time then processes are
sorted according to their priorities (low value represents high priority) in a ready queue.
Step 3 : Calculate the Finish Time, Turn Around Time and Waiting Time for each process which in
turn help to calculate Average Waiting Time and Average Turn Around Time required by CPU to
schedule given set of processes.
Step 3.1 : for i = 0, Finish Time T 0 = Arrival Time T 0 + Burst Time T 0
Step 3.2 : for i >= 1, Finish Time T i = Burst Time T i + Finish Time T i - 1
Step 3.3 : for i = 0, Turn Around Time T 0 = Finish Time T 0 - Arrival Time T 0
Step 3.4 : for i >= 1, Turn Around Time T i = Finish Time T i - Arrival Time T i
Step 3.5 : for i = 0, Waiting Time T 0 = Turn Around Time T 0 - Burst Time T 0
15
Step 3.6 : for i >= 1, Waiting Time T i = Turn Around Time T i - Burst Time T i - 1
Step 4 : Process with less arrival time (not necessary this process will have highest priority) comes
first and gets scheduled first by the CPU.
Step 5 : Calculate the Average Waiting Time and Average Turn Around Time.
Step 6 : Stop.
Preemptive:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Process {
int id;
int arrivalTime;
int burstTime;
int priority;
int remainingTime;
int turnaroundTime;
int waitingTime;
int completionTime;
};
bool compareArrivalTime(const Process& p1, const Process& p2) {
return p1.arrivalTime < p2.arrivalTime;
}
bool comparePriority(const Process& p1, const Process& p2) {
return p1.priority < p2.priority;
}
void preemptivePriorityScheduling(vector<Process>& processes) {
int n = processes.size();
int currentTime = 0;
int completedProcesses = 0;
while (completedProcesses < n) {
sort(processes.begin(), processes.end(), comparePriority);
int shortestProcess = -1;
int shortestPriority = INT_MAX;
for (int i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 &&
processes[i].priority < shortestPriority) {
shortestProcess = i;
shortestPriority = processes[i].priority;
}
}
if (shortestProcess == -1) {
currentTime++;
continue;
}
processes[shortestProcess].remainingTime--;
currentTime++;
if (processes[shortestProcess].remainingTime == 0) {
completedProcesses++;
processes[shortestProcess].completionTime = currentTime;
processes[shortestProcess].turnaroundTime = processes[shortestProcess].completionTime -
processes[shortestProcess].arrivalTime;
16
processes[shortestProcess].waitingTime = processes[shortestProcess].turnaroundTime -
processes[shortestProcess].burstTime;
}
}
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
vector<Process> processes(n);
cout << "Enter the arrival time, burst time, and priority for each process:\n";
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
cout << "Process " << processes[i].id << ": ";
cin >> processes[i].arrivalTime >> processes[i].burstTime >> processes[i].priority;
processes[i].remainingTime = processes[i].burstTime;
}
preemptivePriorityScheduling(processes);
cout << "\nPreemptive Priority Scheduling:\n";
cout << "Process\tPriority\tArrival Time\tBurst Time\tCompletion Time\tTurnaround
Time\tWaiting
Time\n";
double totalTurnaroundTime = 0.0;
double totalWaitingTime = 0.0;
for (const Process& process : processes) {
cout << "P" << process.id << "\t" << process.priority << "\t\t" << process.arrivalTime << "\t\t"
<< process.burstTime << "\t\t" << process.completionTime << "\t\t" << process.turnaroundTime
<<
"\t\t" << process.waitingTime << endl;
totalTurnaroundTime += process.turnaroundTime;
totalWaitingTime += process.waitingTime;
}
double averageTurnaroundTime = totalTurnaroundTime / n;
double averageWaitingTime = totalWaitingTime / n;
cout << "\nAverage Turnaround Time = " << averageTurnaroundTime << endl;
cout << "Average Waiting Time = " << averageWaitingTime << endl;
return 0;
}
Non-Preemptive:
78 #include<stdio.h>
79
80 int main()
81 {
82 int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
83 printf("Enter Total Number of Process:");
84 scanf("%d",&n);
85
86 printf("\nEnter Burst Time and Priority\n");
87 for(i=0;i<n;i++)
88 {
89 printf("\nP[%d]\n",i+1);
90 printf("Burst Time:");
91 scanf("%d",&bt[i]);
17
92 printf("Priority:");
93 scanf("%d",&pr[i]);
94 p[i]=i+1; //contains process number
95 }
96
97 //sorting burst time, priority and process number in ascending order using
selection sort
98 for(i=0;i<n;i++)
99 {
100 pos=i;
101 for(j=i+1;j<n;j++)
102 {
103 if(pr[j]<pr[pos])
104 pos=j;
105 }
106
107 temp=pr[i];
108 pr[i]=pr[pos];
109 pr[pos]=temp;
110
111 temp=bt[i];
112 bt[i]=bt[pos];
113 bt[pos]=temp;
114
115 temp=p[i];
116 p[i]=p[pos];
117 p[pos]=temp;
118 }
119
120 wt[0]=0; //waiting time for first process is zero
121
122 //calculate waiting time
123 for(i=1;i<n;i++)
124 {
125 wt[i]=0;
126 for(j=0;j<i;j++)
127 wt[i]+=bt[j];
128
129 total+=wt[i];
130 }
131
132 avg_wt=total/n; //average waiting time
133 total=0;
134
135 printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
136 for(i=0;i<n;i++)
137 {
138 tat[i]=bt[i]+wt[i]; //calculate turnaround time
139 total+=tat[i];
140 printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
141 }
142
143 avg_tat=total/n; //average turnaround time
144 printf("\n\nAverage Waiting Time=%d",avg_wt);
145 printf("\nAverage Turnaround Time=%d\n",avg_tat);
18
146
147 return 0;
148}
5.5 OUTPUT:
Preemptive:
Non-Preemptive:
19
PROGRAM: 6
6.1 OBJECTIVE:
Program to simulate Round Robin CPU scheduling Algorithm.
6.2 THEORY:
Round Robin is a CPU scheduling algorithm that allocates a fixed time quantum to each process in
a cyclic manner. It is designed to provide fair sharing of CPU time among multiple processes. The
algorithm works by dividing the available CPU time into small time slices, called time quantum or
time slice, and each process is allowed to execute for a predefined time quantum. If the process
does not complete within the allocated time quantum, it is preempted, and the next process in the
queue is given a chance to execute. The preempted process is then placed back at the end of the
queue.
6.3 ALGORITHM:
#include<stdio.h>
int main()
{
//Input no of processed
int n;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
20
//Input time slot
int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);
21
6.5 OUTPUT:
22
PROGRAM: 7
7.1 OBJECTIVE:
Write a program to simulate Banker’s (Safety and Request) Algorithm
7.2 THEORY:
The Banker's algorithm, sometimes referred to as the avoidance algorithm, is a resource allocation
and deadlock avoidance algorithm developed by Dijkstra that tests for safety by simulating the
allocation of predetermined maximum possible amounts of all resources, and then makes an "s-
state" check to test for possible deadlock conditions for all other pending activities, before deciding
whether allocation should be allowed to continue.
7.3 ALGORITHM:
Given
n processes and m types of resources, define the following data structures:
– Available: the number of available resources of each type
– Max: maximum demand of each process on each type resource
− Allocation: number of resources of each type currently allocated to each process.
− Remaining Need: Need[i,j] = Max[i,j] – Allocation[i,j]
#include <stdio.h>
#include <conio.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}
25
7.5 OUTPUT:
26
PROGRAM: 8
8.1 Objective:
a. Write a program to simulate Internal fragmentation for first fit
8.2 Theory:
In this scheme we check the blocks in a sequential manner which means we pick the first process
then compare it’s size with first block size if it is less than size of block it is allocated otherwise we
move to second block and so on.
8.3 Algorithm:
1 Step: START.
3 Step: Allocate the process by if(size of block>=size of the process) then allocate the process else
move to the next block.
4 Step: Now Display the processes with blocks and allocate to respective process.
5 Step: STOP.
Objective: b.
Algorithm:
1. Get no. of Processes and no. of blocks.
2. After that get the size of each block and process requests.
3. Then select the best memory block that can be allocated using the above definition.
4. Display the processes with the blocks that are allocated to a respective process.
5. Value of Fragmentation is optional to display to keep track of wasted memory
6. Stop.
Source Code:
#include<iostream>
using namespace std;
void bestFit(int blockSize[], int m, int processSize[], int n)
{
int allocation[n],flag[n];
for (int i = 0; i < n; i++)
{ allocation[i] = -1;
flag[i]=0;
}
int copyblk[m];
for(int i=0;i<m;i++){
copyblk[i]=blockSize[i];
}
for (int i = 0; i < n; i++)
{
int bestIdx = -1;
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (bestIdx == -1)
bestIdx = j;
else if (blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
if (bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\tBlock Size\tInt.Freg\n");
for (int i = 0; i < n; i++)
29
{
printf(" %i\t\t", i+1);
printf("%i\t\t", processSize[i]);
if (allocation[i] != -1)
printf("%i\t\t", allocation[i] + 1);
else
printf("NA\t\t");
printf("%d\t\t",copyblk[allocation[i]]);
printf("%d\t\t",blockSize[allocation[i]]);
printf("\n");
}
}
int main()
{int m;
printf("Enter the number of blocks in the memory: ");
scanf("%d",&m);
int n;
printf("Enter the number of processes in the memory: ");
scanf("%d",&n);
int blockSize[m] ,processSize[n];
for(int i = 0; i < m; i++)
{ printf("Enter the size of block %d : ",i+1);
scanf("%d",&blockSize[i]);
}
for(int i = 0; i < n; i++)
{
printf("Enter the size of process %d : ",i+1);
scanf("%d",&processSize[i]);
}
bestFit(blockSize, m, processSize, n);
return 0 ;
}
Output:
Objective: c.
30
Theory:
Worst Fit allocates a process to the partition which is largest sufficient among the freely available
partitions available in the main memory. If a large process comes at a later stage, then memory
will not have space to accommodate it.
Algorithm:
Source Code:
#include<stdio.h>
32
PROGRAM: 9
9.1 OBJECTIVE:
Program to simulate FIFO Page Replacement Algorithm.
9.2 THEORY:
Treats page frames allocated to a process as a circular buffer: When the buffer is full, the oldest
page is replaced. Hence first-in, first-out: A frequently used page is often the oldest, so it will be
repeatedly paged out by FIFO. Simple to implement: requires only a pointer that circles through the
page frames of the process.
9.3 ALGORITHM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
33
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
9.5 OUTPUT:
34
PROGRAM: 10
10.1 OBJECTIVE:
Program to simulate LRU Page Replacement Algorithm.
10.2 THEORY:
Replaces the page that has not been referenced for the longest time: By the principle of locality, this
should be the page least likely to be referenced in the near future. performs nearly as well as the
optimal policy.
10.3 ALGORITHM:
Step 1: Define the size of the page table (number of pages that can be held in memory at once).
Step 2: Initialize two variables: page faults and page hits, both initially set to 0.
Step 3: Initialize an empty list to hold the pages currently in memory.
Step 4: Iterate through the reference string.
Step 5: For each page request, check if the page is already in the list of pages currently in memory:
•
If the page is in memory, remove it from its current position in the list and append it to the end of
the list (since it was the most recently used page).
• If the page is not in memory, add it to the end of the list if there is space (i.e., the list is not yet
full), or
else remove the least recently used page (i.e., the first page in the list) and replace it with the new
page at the end of the list.
Step 6: If a page was replaced in step 5b, increment the page fault counter.
Step 7: After all page requests have been processed, calculate the number of page hits.
Step 8: Finally, print the number of page faults and page hits to the console.
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
35
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
return 0;
}
36
10.5 OUTPUT:
37
PROGRAM: 11
11.1 OBJECTIVE:
Program to simulate Paging Technique of Memory Management.
11.2 THEORY:
Paging is a memory management technique in which the memory is divided into fixed size pages.
Paging is used for faster access to data. When a program needs a page, it is available in the main
memory as the OS copies a certain number of pages from your storage device to main memory.
Paging allows the physical address space of a process to be non contiguous.
OS performs an operation for storing and retrieving data from secondary storage devices for use in
main memory. Paging is one of such memory management scheme. Data is retrieved from storage
media by OS, in the same sized blocks called as pages. Paging allows the physical address space of
the process to be non contiguous. The whole program had to fit into storage contiguously.
Paging is to deal with external fragmentation problem. This is to allow the logical address space of
a process to be non contiguous, which makes the process to be allocated physical memory.
struct MainMem
{
int start_add;
int pn;
};
int main()
{
int s,i,totPages,pno,offset,n,r,arr[200]={0},phy_add,fno[100];
struct MainMem mm[100];
srand((unsigned)time(NULL));
printf("Logical Address To Physical Address\n");
printf("Enter the Size of File : ");
scanf("%d",&n);
printf("Enter the Page Size : ");
scanf("%d",&s);
totPages=n/s;
for(i=0;i<totPages;i++)
{
r=rand()%totPages;
if(arr[r] == 1)
{
i--;
continue;
}
arr[r]=1;
mm[i].pn=r;
mm[i].start_add=i*s;
38
fno[r]=i;
}
printf("\n*********************************\n");
printf("The Structure of Main Memory\n");
printf("*********************************\n");
printf("Frame\tPage\nNumber\tNumber\n------\t------- \n");
for(i=0; i < totPages; i++)
{
printf("%d\t%d\n",i,mm[i].pn);
}
printf("*********************************\n");
printf("Enter The Logical Address\nPage Number : ");
scanf("%d",&pno);
printf("Offset : ");
scanf("%d",&offset);
if(pno >= totPages || offset > s)
{
printf("Invalid Input\n");
return 0;
}
phy_add = mm[fno[pno]].start_add + offset;
printf("*********************************\n");
printf("Physical Address : %d\n",phy_add);
printf("*********************************\n");
return 0;
}
11.4 OUTPUT:
39