0% found this document useful (0 votes)
3 views40 pages

Os Lab

The document outlines the objectives and theoretical foundations of various operating systems, focusing on Windows requirements and DOS commands. It also discusses CPU scheduling algorithms, specifically First-Come-First-Served (FCFS) and Shortest-Job-First (SJF), detailing their algorithms and providing source code examples. Additionally, it covers key features of Windows 10 and important DOS commands for system management.

Uploaded by

dawon14123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views40 pages

Os Lab

The document outlines the objectives and theoretical foundations of various operating systems, focusing on Windows requirements and DOS commands. It also discusses CPU scheduling algorithms, specifically First-Come-First-Served (FCFS) and Shortest-Job-First (SJF), detailing their algorithms and providing source code examples. Additionally, it covers key features of Windows 10 and important DOS commands for system management.

Uploaded by

dawon14123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

PROGRAM-1

1.1 OBJECTIVE:

Study of software and hardware requirements of various Operating Systems


(Windows/Linux/Unix) and Implementation of DOS commands on command prompt.

1.2 THEORY:
A. Windows

Here's what it takes to upgrade to Windows 10 on your PC or tablet:

▪ Processor: 1 gigahertz (GHz) or faster processor or SoC


▪ RAM: 1 gigabyte (GB) for 32-bit or 2 GB for 64-bit
▪ Hard disk space: 16 GB for 32-bit OS or 20 GB for 64-bit OS
▪ Graphics card: DirectX 9 or later with WDDM 1.0 driver
▪ Display: 800 x 600

Below are some additional details regarding requirements for key features:

1. BitLocker Drive Encryption


• BitLocker Drive Encryption is a data protection feature that integrates with the
operating system and addresses the threats of data theft or exposure from lost,
stolen, or inappropriately decommissioned computers.

• (available with Windows 10 Pro or Windows 10 Enterprise only) requires a Trusted


Platform Module (TPM) 1.2 or higher and Trusted Computing Group (TCG)-
compliant BIOS or UEFI.
• BitLocker can be used on devices without TPM, but you will need to save a startup
key on a removable device such as a USB flash drive.
• TPM 2.0 and InstantGo support is required when you want to automatically encrypt
the local drive when joining a device to Azure Active Directory (AAD). Check with
your PC manufacturer to confirm if your device supports the correct TPM version
and InstantGo for the scenario you want to enable.

1
2. BitLocker To Go is BitLocker Drive Encryption on removable data drives. This feature
includes the encryption of:

• USB flash drives


• SD cards
• External hard disk drives
• Other drives that are formatted by using the NTFS, FAT16, FAT32, or exFAT file
system.

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:

i. High fidelity microphone array


ii. Hardware driver with microphone array geometry exposed

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

Part I: Important commands

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.

7. Create a new directory / Folder (mkdir)

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

Part II. In this part basic DOS commands will be introduced.

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.

3. Type ver to display the Windows OS’s version number.

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)

Part III. Create the following structure using MS DOS commands

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:

First-Come-First-Served algorithm is the simplest scheduling algorithm is the simplest scheduling


algorithm. Processes are dispatched according to their arrival time on the ready queue. Being a non
preemptive discipline, once a process has a CPU, it runs to completion. The FCFS scheduling is
fair in the formal sense or human sense of fairness but it is unfair in the sense that long jobs make
short jobs wait and unimportant jobs make important jobs wait.
FCFS is more predictable than most of other schemes since it offers time. FCFS scheme is not
useful in scheduling interactive users because it cannot guarantee good response time. The code for
FCFS scheduling is simple to write and understand. One of the major drawback of this scheme is
that the average time is often quite long. The First-Come-First-Served algorithm is rarely used as a
master scheme in modern operating systems but it is often embedded within other schemes. It is a
non preemption type scheduling algorithm.

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.

2.4 SOURCE CODE:


#include<stdio.h>
int main(){

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:

Program to simulate SJF CPU scheduling Algorithm.

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:

1. Enter number of processes.


2. Enter the burst time of all the processes.
3. Sort all the processes according to their burst time.
4. Find waiting time, WT of all the processes.
5. For the smallest process, WT = 0.
6. For all the next processes i, find waiting time by adding burst time of all the previously
completed process.
7. Calculate Turnaround time = WT + BT for all the processes.
8. Calculate average waiting time = total waiting time / no. of processes.
9. Calculate average turnaround time= total turnaround time / no. of processes.

3.3 SOURCE CODE :


1 #include <iostream>
2 using namespace std;
3
4 int main() {
5
6
7 int A[100][4];
8 int i, j, n, total = 0, index, temp;
9 float avg_wt, avg_tat;
10
11 cout << "Enter number of process: ";
12 cin >> n;
13
14 cout << "Enter Burst Time:" << endl;
15
16 for (i = 0; i < n; i++) {
17 cout << "P" << i + 1 << ": ";
18 cin >> A[i][1];
19 A[i][0] = i + 1;
10
20 }
21
22 for (i = 0; i < n; i++) {
23 index = i;
24 for (j = i + 1; j < n; j++)
25 if (A[j][1] < A[index][1])
26 index = j;
27 temp = A[i][1];
28 A[i][1] = A[index][1];
29 A[index][1] = temp;
30
31 temp = A[i][0];
32 A[i][0] = A[index][0];
33 A[index][0] = temp;
34 }
35
36 A[0][2] = 0;
37 for (i = 1; i < n; i++) {
38 A[i][2] = 0;
39 for (j = 0; j < i; j++)
40 A[i][2] += A[j][1];
41 total += A[i][2];
42 }
43
44 avg_wt = (float)total / n;
45 total = 0;
46 cout << "P BT WT TAT" << endl;
47
48
49 for (i = 0; i < n; i++) {
50 A[i][3] = A[i][1] + A[i][2];
51 total += A[i][3];
52 cout << "P" << A[i][0] << " " << A[i][1] << " " << A[i][2] <<
" " << A[i][3] << endl;
53 }
54
55 avg_tat = (float)total / n;
56 cout << "Average Waiting Time= " << avg_wt << endl;
57 cout << "Average Turnaround Time= " << avg_tat << endl;
58 }

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:

Enter number of processes.


• Enter the burst time of all the processes.
• Sort all the processes according to their burst time.
• Find waiting time, WT of all the processes.
• For the smallest process, WT = 0.
• For all the next processes i, find waiting time by adding burst time of all the
previously completed process.
• Calculate Turnaround time = WT + BT for all the processes.
• Calculate average waiting time = total waiting time / no. of processes.
• Calculate average turnaround time= total turnaround time / no. of process

4.4 SOURCE CODE :

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:

Initialize ready queue.


2) Read process details.
3) Sort processes based on priority.
4) Set current time and completed count.
5) While processes in queue:
a. Check arrival time.
b. Execute process.
c. Update current time.
d. Calculate turnaround and waiting time.
e. Increment completed count.
6) Calculate average turnaround and waiting time.
7) Print process details and averages.

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.

5.4 SOURCE CODE:

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:

1. Set a time quantum for each process (e.g., 2 units of time).


2. Initialize a queue to hold the processes.
3. Iterate over the processes and enqueue them in the queue.
4. Repeat until all processes are completed:
• Dequeue the process from the front of the queue.
• Execute the process for the time quantum.
• If the process completes within the time quantum:
• Update its waiting time and turnaround time.
• If the process does not complete within the time quantum:
• Update its remaining time.
• Enqueue the process back into the queue.
5. Calculate the average waiting time and average turnaround time for all processes.

6.4 SOURCE CODE:

#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;

//Input details of processes


for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}

20
//Input time slot
int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);

//Total indicates total time


//counter indicates which process is executed
int total = 0, counter = 0,i;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%f", average_wait_time);
printf("\nAvg Turnaround Time:%f", average_turnaround_time);
return 0;
}

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]

Def: Request[i,j] = The number of resource j requested by Process i

1. If Request[i] <= Need[i],


go to Step 2. Otherwise, report error.

2. If Request[i] <= Available,


goto Step 3. Otherwise, let process I wait: not enough resources available.

3. Pretending to have allocated the request resources to process i:


Available := Available - Request[i];
Allocation[i] := Allocation[i] + Request[i];
Need[i] := Need[i] - Request[i];

4. If Safe(Available, Allocation, Need) = false


then
Available := Available + Request[i];
Allocation[i] := Allocation[i] - Request[i];
Need[i] := Need[i] + Request[i];

Let Process i wait: allocation may cause deadlock


else
let process proceed with allocated resource

Function Safe(Available, Allocation, Need) : boolean;


1. Work := Available;
For i := 1 to n do
Finish[i] := f
Else
2. While there is an i such that Finish[i] = false and Need[i] <= Work
do begin
Work := Work + Allocation[i];
Finish[i] := true
23
end;
3. If for all i Finish[i] = true,
then
return(true)
else
return(false);

7.4 SOURCE CODE:

#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]);

for(i = 0; i < p; i++)


for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
24
process = -1;

for(i = 0; i < p; i++)


{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

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.

2 Step: At first get the no of processes and blocks.

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.

8.4 Source Code:


#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int i, j;
int allocation[n],flag[n];
for(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 (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i] && flag[j]==0)
{
allocation[i] = j;
blockSize[j] -= processSize[i];
flag[j]=1;
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\tBlock Size\tInt.Freg\n");
for (int i = 0; i < n; i++)
{
27
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\t",copyblk[allocation[i]]);
printf("%d\t\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]);
}
firstFit(blockSize, m, processSize, n);
return 0 ;
}
8.5 Output:

Objective: b.

Write a program to simulate Internal fragmentation for best fit


28
Theory:

Best-Fit Allocation is a memory allocation technique used in operating systems to allocate


memory to a process. In Best-Fit, the operating system searches through the list of free blocks of
memory to find the block that is closest in size to the memory request from the process. Once a
suitable block is found, the operating system splits the block into two parts: the portion that will
be allocated to the process, and the remaining free block.

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.

Write a program to simulate Internal fragmentation for worst fit

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:

1- Input memory blocks and processes with sizes.


2- Initialize all memory blocks as free.
3- Start by picking each process and find the
maximum block size that can be assigned to
current process i.e., find max(block_Size[1],
block_Size[2], ..... blockSize[n]) >
process_Size[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes

Source Code:

#include<stdio.h>

void worstFit(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 wstIdx = -1;
for (int j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}
if (wstIdx != -1)
{
allocation[i] = wstIdx;
31
blockSize[wstIdx] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\tBlock Size\tInt.Freg\n");
for (int i = 0; i < n; i++)
{
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\t",copyblk[allocation[i]]);
printf("%d\t\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]);
}
worstFit(blockSize, m, processSize, n);
return 0 ;
}
Output:

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:

1. Begin turning the pages.


i) If the set can hold no more pages.
a) Add pages one at a time into the collection until it is full or all requests for pages have been fulfil
led.
b) Maintain the pages in the queue simultaneously to carry out FIFO.
b) Increased page error
ii) Other
Do nothing if the current page is included in the collection.
If not, either a) the current page in the string should be substituted for the first page in the queue sin
ce it was the first to be placed into memory, or b) the first page in the queue should be removed.
b) Add the currently viewing page to the queue.
d) Page faults that increase.
2. Return page errors.

9.4 SOURCE CODE:

#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.

10.4 SOURCE CODE :

#include<stdio.h>

int findLRU(int time[], int n){


int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i){


if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}
return pos;
}

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]);
}

for(i = 0; i < no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){


flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){


if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

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");

for(j = 0; j < no_of_frames; ++j){


printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);

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.

11.3 SOURCE CODE:


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

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

You might also like