Os Final PDF - Merged
Os Final PDF - Merged
An Autonomous Institution
PRACTICAL RECORD
Name : ----------------------------------
Register Number : ----------------------------------
Class : ----------------------------------
Semester : ----------------------------------
SNS COLLEGE OF ENGINEERING
(An Autonomous Institution
Register Number
Certified that this is the bonafide record of work done by the above student
for the 23ITB203 – PRINCIPLES OPERATING SYSTEM during the Academic year
2024-2025.
2 System Calls
5(a) Semaphore
Average Performance 4
Average Record 4
Average Viva 2
Total 10
Ex. No : 1
UNIX COMMANDS
Date:
AIM
To study and understand the UNIX general purpose, directory, file handling and
input output redirection commands.
Syntax: $ date
1
2. The Echo And Banner Commands
The echo command is used to print the message on the screen, whatever you happen to
type on the line.
Syntax: $ clear
This command clear the screen and places a ‘$’ prompt at the top left corner of the screen.
6. tput command
It is used for manipulation the screen of the terminal. Syntax
$ tput [option]
2
3 Smso Displays the text typed in bold face (including the system prompt)
and reverse the background color of the screen in while and
foreground with black.
7 cup RC Used to position the cursor at row number ‘R’ and column
number ‘C’.
Syntax: $ who
The –h option in who command displays the column header.
2. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.
Eg: $ who am i
3. The tty command
The tty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
4. Change the password : passwd
If you account still does not have a password, or has that already known to
others. Thepassword can be changed by using the passwd command.
Syntax: $passwd
3
5. read command
It is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a
Syntax: $ who
The –h option in whocommand displays the column header.
7. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.
Eg: $ who am i
8. The tty command
The tty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
9. Change the password : passwd
If you account still does not have a password, or has that already known to others.
Thepassword can be changed by using the passwd command.
Syntax: $passwd
10. read command
It is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a
4
11. The Who Command
The who command is more powerful and used to displays data about all the
users, who are currently logged into the system.
Syntax: $ who
The –h option in whocommand displays the column header.
12. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.
Eg: $ who am i
13. The tty command
Thetty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
14. Change the password : passwd
If you account still does not have a password, or has that already known to others. The
password can be changed by using the passwd command.
Syntax: $passwd
15. read command
it is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a
5
DIRECTORY HANDLING COMMANDS
1. Current working directory :pwd
Each user, those who has logged in to the system is always provided with a working directory
of his own, called Current Working Directory.
The pwd command is provided to know the current working directory, pwd is an abbreviation
for “print working directory”.
Syntax: $ pwd
$ cd program $ cd ..
5. Listing files and directories : ls
To view the contents of a directory the ls command is used
Syntax: $ ls
List files in the long format. The files are displayed along with
1. -l their mode, number of links, owner of the file, file size,
modification date and time and file name.
2. -t List in order of last modification time (most recent first).
3. -a List all entries, including the hidden files.
4. -d List directory file instead of its contents.
5. -p Puts a slash after each directory.
6. -u Lists in order of the last access time.
7. -x It produces a multi-columnar output of filenames.
6
It identifies the directories with/ and executable files with
8. -F
symbols.
7
FILE HANDLING COMMANDS
1. The cat command
The cat command is used to create a file
Syntax: $cat>filename
Eg: $cat>test
After finished typing press “ctrl-d” to save and end the file. In Unix the “ctrl-d” keys
indicate the “End of File” (EOF) character. Now a file is created with the name ‘test’ in the
disk.
Option Use
-v Used to display non-printing characters
-n Used for numbering the lines
3. Copying a file : cp
The cp command is used to copy the content of one file to another and copies the file from
one place to another.
Syntax: $ cp old_filenew_file
Eg: $ cp application.a application
When copying files from or to the different directories, you can specify their name along
with their path as shown below.
$ cp/usr/user1/one.c /usr/user1/program/one.c
.Option Use
-i Used to warm the user before overwriting the
destination file
-R Used to copy an entire directory structure.
8
4. Removing a file : rm
The rm command is used to remove or erase an exiting file.
Syntax: $ rm filename
Option Use
-i Used to ask the user for confirmation before
removing each file
5. Moving a file : mv
The mv command is used to move a file from one place to another, it removes a
specifiedfile from its original location and places it specified location.
Syntax: $ mv old_filenamenew_filename
6. Counting number of words in a file :wc
The wc command is used to count the number of words, lines and characters in a file.
Syntax: $wc filename
7. File permissions
File permission is the way of controlling the accessibility of the file for each of three namely
Users, Groups and Others.
There are three types of files permissions are available, they are
r – read
w – write
x – execute
The permission for each file can be divided into three parts of three bits each.
9
Let us take the myfile and analyze it bit by bit.
$ ls myfile
-rwxr-xr- 1 lak std 1525 may 30 12:15 myfile
-rwx Contain all three permissions and a hyphen (-) indicates ordinary file. This File is
readable , writable and executable by the owner of the file.
lak specifies owner of the file
r-x Indicates the absence of the write permission by the groupowner of the
r-- Indicates read permission for others.i.e, Those who are neither the owner of muni, nor
belongs to the std group.
Read permission 4
Write permission 42
Execute permission 1
10
Eg: $ chmod 761 exa.c
Assign all permissions to the owner, read and write permissions to the group, and only
executable permission to the others for exa.cfile.
9. ln – link command
This command is to establish an additional filename for the same ordinary
file.
Syntax:
$ ln firstname second name.
10. find command
This command is used to locate files in a directory and in a subdirectory.
Example:
To find the path of all files.
$ find . –print
11
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the Basic UNIX commands was executed and verified successfully.
12
Ex.No:2
Date: UNIX commands and system calls
AIM:
To understand and execute basic UNIX commands and system calls including fork,
exec, getpid, exit, wait, close, stat, opendir, readdir, open, read, and write to learn how they
work in managing processes, files, and directories in the UNIX operating system.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
13
int main() {
pid_t
pid; int
status;
char
buffer[1024];
ssize_t n;
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILUR
E);
} else if (pid == 0) {
// Child process
error exit(EXIT_FAILURE);
} else {
// Parent process
}
14
// Demonstrate open, read, write, close
perror("Open failed");
exit(EXIT_FAILURE);
perror("Write
failed"); close(fd);
exit(EXIT_FAILUR
E);
if (n == -1) {
perror("Read
failed"); close(fd);
exit(EXIT_FAILUR
E);
15
buffer[n] = '\0'; // Null-terminate the read
// Demonstrate
fileStat;
0) {perror("Stat failed");
exit(EXIT_FAILURE);
if ((dir = opendir(".")) ==
NULL) {perror("Opendir
failed"); exit(EXIT_FAILURE);
printf("Directory contents:\n");
16
NULL) {printf("%s\n", entry-
>d_name);
closedir(dir);
printf("Program
completed.\n"); return 0;
17
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the UNIX commands and system calls including fork, exec, getpid, exit, wait,
close, stat, opendir, readdir, open, read, and write successfully executed.
18
Ex. No : 3 a SHELL PROGRAMMING
Date: COMPARISON OF TWO STRINGS
AIM:
To write a shell program to compare the two strings.
ALGORITHM:
Step1: Enter into the vi editor and goto the insert mode for entering the code
Step2: Read the first string.
Step3: Read the second string.
Step4: Compare the two strings using the if loop
Step5: If the condition satisfied then print that two strings are equal else print two strings are
not equal.
Step6: Enter into the escape mode for the execution of the result and verify the output.
PROGRAM:
19
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT
Thus, the shell program are executed and output is verified successfully.
20
Ex. No : 3 b
MAXIMUM OF THREE NUMBERS
Date:
Aim:
To write a shell program to find greatest of three numbers.
ALGORITHM:
Step1: Declare the three variables.
PROGRAM:
21
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT
Thus, the shell program are executed and output is verified successfully.
22
.
Ex. No : 3 c
ARITHMETIC OPERATIONS USING
Date:
CASE
AIM:
To write a shell program to perform the arithmetic operations using case
ALGORITHM:
Step 1 : Read the input variables and assign the value
Step 2 : Print the various arithmetic operations which we are going to perform
Step 3 : Using the case operator assign the various functions for the arithmetic operators.
Step 4 : Check the values for all the corresponding operations.
Step 5 :Print the result and stop the execution
PROGRAM:
case $a in
1) d=expr $b + $c
echo "The sum is $d"
;;
2) d=expr $b - $c
echo "The difference is $d"
;;
3) d=expr $b \* $c
echo "The product is $d"
;;
23
4) if [ "$c" -ne 0 ]; then
d=expr $b / $c
echo "The quotient is $d"
else
echo "Division by zero is not allowed"
fi
;;
*) echo "Invalid choice"
;;
esac
24
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT
Thus, the shell program are executed and output is verified successfully.
25
Ex. No : 4 a
FCFS SCHEDULING
Date:
AIM:
Write a C program to implement the various process scheduling mechanisms suchas FCFS
scheduling.
ALGORITHM:
Step3: For each process in the ready Q, assign the process id and accept theCPU burst time.
Step4: Set the waiting of the first process as „0‟andits burst time as its turnaround time.
Step5: For each process in the Ready Q calculate
26
PROGRAM:
#include <stdio.h>int
main()
{
int i, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10];
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
p[i] = i + 1; // Assign process ID (p[i] = i+1)
}
printf("\n\nGantt Chart:\n");
printf(" |");
for (i = 0; i < n; i++) {
printf(" P%d |", p[i]);
}
printf("\n");
printf("0");
for (i = 0; i < n; i++)
{
sum += t[i]; printf("
%d", sum);
}
27
sum = 0;
printf("\n\nProcess ID\tWaiting Time\tTurn Around Time\n");
return 0;
}
28
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the program to perform various CPU Scheduling algorithms areexecuted and
output is verified successfully.
29
Ex. No : 4 b
SJF SCHEDULING
Date:
AIM :
To Write a C program to implement the various process scheduling mechanismssuch as SJF
Scheduling.
ALGORITHM :
Step5: Set the waiting time of the first process as „0‟ and its turnaround time as itsburst time.
Step6: Calculate
30
PROGRAM:
#include <stdio.h>int
main()
{
int i, j, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10], temp;
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
p[i] = i + 1;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (t[i] > t[j])
{
temp = t[i];
t[i] = t[j]; t[j]
= temp; temp
= p[i];p[i] =
p[j]; p[j] =
temp;
}
}
}
printf("\n\nGantt Chart:\n");
printf(" |");
31
for (i = 0; i < n; i++)
{
32
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the program to perform various CPU Scheduling algorithms areexecuted and
output is verified successfully.
33
Ex. No : 4 c PRIORITY SCHEDULING
Date:
AIM
Write a C program to implement the various process scheduling mechanisms suchas Priority
Scheduling.
ALGORITHM
Step3: For each process in the ready Q, assign the process id and accept theCPU burst time.
Step6: For each process in the Ready Q calculate Waiting time for process(n) =waiting time
ofprocess(n-1) +Burst time of process(n-1)
TurnaroundtimeforProcess(n)=waitingtimeofProcess(n)+Bursttimefor process(n)
Calculate Average waiting time=Total waiting Time/Number of process.
AverageTurn around time = Total Turn around Time/Number of process.
Step7: Stop the process.
34
PROGRAM:
#include <stdio.h>
int main()
{
int i, j, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10], pr[10], temp;
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
printf("Enter the Priority of process %d (lower number = higher priority): ", i + 1);scanf("%d",
&pr[i]);
p[i] = i + 1;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (pr[i] > pr[j])
{
temp = pr[i];
pr[i] = pr[j];
pr[j] = temp;
temp = t[i]; t[i]
= t[j];
35
t[j] = temp; temp = p[i];
p[i] = p[j]; p[j] = temp;
}
}
}
printf("\n\nGantt Chart:\n");
printf(" |");
sum = 0;
for (i = 0; i < n; i++) {
printf(" P%d |", p[i]);
}
printf("\n");
printf("0");
sum = 0;
for (i = 0; i < n; i++) {sum
+= t[i]; printf(" %d",
sum);
}
sum = 0;
printf("\n\nProcess ID\tPriority\tWaiting Time\tTurn Around Time\n");
36
ttat += tat;
printf("%d\t\t%d\t\t%d\t\t%d\n", p[i], pr[i], wt, tat);
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\nAverage Waiting Time: %.2f", awt); printf("\nAverage
Turnaround Time: %.2f\n", atat);
return 0;
}
37
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the program to perform various CPU Scheduling algorithms are executed andoutput is verified
successfully.
38
Ex. No : 4d ROUND ROBIN SCHEDULING
Date:
AIM:
Write a C program to implement the various process scheduling mechanisms suchas Round
Robin Scheduling.
ALGORITHM:
Step2: Accept the number of processes in the ready Queue and time
quantum(or)time slice
Step3: For each process in the ready Q, assign the process id and accept theCPU burst time
Step4: Calculate the no. of time slices for each process where
No. of time slice for process(n)=burst time process(n)/time slice
Step5: If the burst time is less than the time slice then the no. of time slices =1.
39
(b) AverageTurn around time=Total Turn around Time/Numberof process
Step8: Stop the process.
PROGRAM:
#include <stdio.h>int
main()
{
int time = 0;
int complete = 0;
int i, n, tq, sum = 0, count = 0, t[10], remaining_time[10], p[10], wt[10], tat[10];int total_wt =
0, total_tat = 0;
float avg_wt, avg_tat;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter Time Quantum: ");
scanf("%d", &tq);
for (i = 0; i < n; i++)
{
printf("Enter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
remaining_time[i] = t[i];p[i]
= i + 1;
}
printf("\n\nGantt Chart:\n");
while (complete < n)
{
for (i = 0; i < n; i++)
40
{
if (remaining_time[i] > 0)
{
if (remaining_time[i] <= tq)
{
time += remaining_time[i];
remaining_time[i] = 0; wt[i] =
time - t[i];
tat[i] = time;
total_wt += wt[i];
total_tat += tat[i];
printf(" | P%d |", p[i]);complete++;
}
else
{
time += tq;
remaining_time[i] -= tq;
printf(" | P%d |", p[i]);
}
}
}
}
printf("\n0");
for (i = 0; i < complete; i++)
{
printf(" %d", tat[i]);
}
printf("\n\nProcess ID\tBurst Time\tWaiting Time\tTurn Around Time\n");for (i = 0; i
< n; i++)
41
{
printf("%d\t\t%d\t\t%d\t\t%d\n", p[i], t[i], wt[i], tat[i]);
}
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;
printf("\nAverage Waiting Time: %.2f", avg_wt);
printf("\nAverage Turnaround Time: %.2f\n", avg_tat);
return 0;
}
42
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the program to perform various CPU Scheduling algorithms are executed and output is verified
successfully.
43
Ex. No : 5
SEMAPHORES
Date:
AIM:
To implement producer/consumer problem using semaphore.
ALGORITHM:
1 Declare variable for producer &consumer asp thread-t-tid produced idconsume.
2 Declare a structure to add items, semaphore variable set as struct.
3 Read number of items to be produced and consumed.
4 Declare and define semaphore function for creation and destroy.
5 Define producer function.
6 Define consumer function.
7 Call producer and consumer.
8 Stop the execution.
PROGRAM:
#include<stdio.h>void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;in = 0;
out = 0;
bufsize = 10;
while (choice !=3)
{
printf("\n1. Produce \t 2. Consume \t3. Exit");printf("\nEnter your
choice: ");
scanf("%d", &choice);switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");else
{
44
printf("\nEnter the value: ");scanf("%d",
&produce); buffer[in] = produce;
in = (in+1)%bufsize;
}
break; case 2:
if(in == out) printf("\nBuffer is Empty");else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);out = (out+1)%bufsize;
}
break;
}
}
}
45
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT
Thus the producer consumer program was executed and verified successfully.
46
Ex. No : 6
DEADLOCK DETECTION
Date:
AIM:
ALGORITHM:
Step 1: Start the program.
Step 2. Get the values of resources and
processes.
Step 8. If the new request comes then check that the system is in safety.
47
PROGRAM:
# include <stdio.h>void
main()
{
int found, flag, l, p[4][5], tp, c[4][5], i, j, k = 1, m[5], r[5], a[5], temp[5],sum = 0;
printf("Enter total number of processes: ");scanf("%d",
&tp);
printf("Enter claim matrix:\n");for (i = 1;
i <= 4; i++)
{
for (j = 1; j <= 5; j++)
{
scanf("%d", &c[i][j]);
}
}
printf("Enter allocation matrix:\n");for (i = 1; i
<= 4; i++)
{
for (j = 1; j <= 5; j++)
{
scanf("%d", &p[i][j]);
48
}
}
printf("Enter resource vector:\n");for (i = 1; i
<= 5; i++)
{
scanf("%d", &r[i]);
}
printf("Enter availability vector:\n");for (i = 1; i
<= 5; i++)
{
scanf("%d", &a[i]);
temp[i] = a[i];
}
for (i = 1; i <= 4; i++)
{
sum = 0;
for (j = 1; j <= 5; j++)
{
sum += p[i][j];
}
if (sum == 0)
{
49
m[k] = i;
k++;
}
}
for (i = 1; i <= 4; i++)
{
for (l = 1; l < k; l++)
{
if (i != m[l])
{
flag = 1;
for (j = 1; j <= 5; j++)
{
if (c[i][j] > temp[j])
{
flag = 0;
break;
}
}
}
if (flag == 1)
{
50
m[k] = i;
k++;
for (j = 1; j <= 5; j++)
{
temp[j] += p[i][j];
}
}
}
}
printf("Deadlock causing processes are: ");for (j = 1; j <=
tp; j++)
{
found = 0;
for (i = 1; i < k; i++)
{
if (j == m[i])
found = 1;
}
if (found == 0)
printf("%d\t", j);
}
}
51
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above deadlock detection program was executed and verifiedsuccessfully.
52
Ex. No : 7 A
FIRST FIT
Date:
AIM:
To write a C program for implementation memory allocation methods for fixed partition using first fit.
ALGORITHM:
Step 3: Get the number of blocks,files,size of the blocks using for loop.
PROGRAM:
#include <stdio.h>
#define max 25 void
main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp; static int bf[max], ff[max];printf("\n\tMemory
Management Scheme - First Fit\n");
printf("Enter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);
53
printf("\nEnter the size of the blocks:\n");for (i = 1; i
<= nb; i++)
{
printf("Block %d: ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
1; i <= nf; i++)
{
printf("File %d: ", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];if
(temp >= 0)
{
ff[i] = j; break;
}
}
}
frag[i] = temp; bf[ff[i]] = 1;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment\n"); for (i = 1; i <= nf;
i++)
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i, f[i], ff[i], b[ff[i]], frag[i]);
}
54
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully.
55
Ex. No : 7 b
WORST FIT
Date:
AIM:
To write a C program for implementation memory allocationmethods forfixed
partition using worst fit.
ALGORITHM:
Step 1:Define the max as 25.
Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,
bf[max],ff[max].
PROGRAM:
#include <stdio.h>
#define MAX 25 int
main()
{
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, highest; static int
bf[MAX] = {0}, ff[MAX] = {0}; printf("\n\tMemory Management
Scheme - Worst Fit\n");printf("Enter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");scanf("%d",
&nf);
56
printf("\nEnter the size of the blocks:\n");for (i = 0; i
< nb; i++)
{
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
0; i < nf; i++)
{
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}
if (bf[j] == 0)
{
temp = b[j] - f[i];
if (temp >= 0 && temp > highest)
{
ff[i] = j;
highest = temp;
}
}
}
if (highest != -1)
{
frag[i] = highest;
57
bf[ff[i]] = 1;
}
else
{
frag[i] = -1;
}
}
}
}
return 0;
}
58
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully.
59
Ex. No : 7 c
BEST FIT
Date:
AIM:
To write a C program for implementation memory
allocation methods for fixed partition using best fit.
ALGORITHM:
loop.
60
PROGRAM:
#include <stdio.h>
#define MAX 25 int
main()
{
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, lowest;static
int bf[MAX] = {0}, ff[MAX] = {0}; printf("\nMemory
Management Scheme - Best Fit\n");printf("Enter the number of
blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");scanf("%d",
&nf);
printf("\nEnter the size of the blocks:\n");for (i = 0; i
< nb; i++)
{
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
0; i < nf; i++)
{
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}
61
for (i = 0; i < nf; i++)
{
lowest = 10000;
ff[i] = -1;
if (ff[i] != -1)
{
frag[i] = lowest;
bf[ff[i]] = 1;
}
else
{
frag[i] = -1;
62
}
}
return 0;
}
63
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully.
64
Ex. No : 8 a
Date: FIFO PAGE REPLACEMENT
ALGORITHM
AIM:
To implement page replacement algorithms FIFO (First In First Out)
ALGORITHM:
Step1: Create a queue to hold all pages in memory. Step2: When the
page is required replace the page at thehead of the queue.
Step3: Now the new page is inserted at the tail of the queue.
PROGRAM:
#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int main()
{
printf("\n\t\t\tFIFO PAGE REPLACEMENT ALGORITHM");
printf("\nEnter no. of frames: ");
scanf("%d", &nof);
printf("Enter number of reference strings: ");
scanf("%d", &nor);
printf("\nEnter the reference string: ");for (i = 0;
i < nor; i++)
{
scanf("%d", &ref[i]);
}
printf("\nThe given reference string:");for (i = 0;
i < nor; i++)
{
printf("%4d", ref[i]);
}
for (i = 0; i < nof; i++)
65
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully.
66
Ex. No : 8 b
LRU PAGE REPLACEMENT ALGORITHM
Date:
AIM:
To implement page replacement algorithm LRU(Least Recently Used)
ALGORITHM:
Step1: Create a queue to hold all pages in memory. Step2: When the
page is required replace the page at thehead of the queue.
Step3: Now the new page is inserted at the tail of the queueStep4: Create
a stack.
Step5: When the page fault occurs replace page present at the bottom of the stack.
PROGRAM:
#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int recent[50],
lrucal[50], count = 0;
int lru_victim();int
main()
{
printf("\n\t\t\tLRU PAGE REPLACEMENT ALGORITHM");printf("\nEnter no.
of Frames: ");
scanf("%d", &nof);
printf("Enter no. of reference strings: ");scanf("%d",
&nor);
printf("\nEnter reference string: ");for (i = 0; i
< nor; i++)
{
scanf("%d", &ref[i]);
}
67
printf("\n\n\t\tLRU PAGE REPLACEMENT ALGORITHM ");printf("\n\tThe given
reference string:\n\t");
frm[i] = -1;
lrucal[i] = 0;
}
for (i = 0; i < 50; i++)
{
recent[i] = -1;
}
printf("\n");
for (i = 0; i < nor; i++)
{
flag = 0;
printf("\n\tReference NO %d ->\t", ref[i]);for (j = 0; j <
nof; j++)
if (frm[j] == ref[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
68
count++;
if (count <= nof)
{
victim++;
}
else
{
victim = lru_victim();
}
pf++;
frm[victim] = ref[i]; for (j =
0; j < nof; j++)
{
printf("%4d", frm[j]);
}
}
recent[ref[i]] = i;
}
printf("\n\n\tNo. of page faults: %d\n", pf);return 0;
int lru_victim()
{
int i, lru_index = -1, lru_time = 1000;for (i = 0; i
< nof; i++) {
69
}
}
return lru_index;
}
70
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully.
71
Ex. No : 8 c
Date: OPTIMAL(LFU)PAGE REPLACEMENT
ALGORITHM
AIM:
To implement Optimal (LFU) page replacement algorithms.
ALGORITHM:
Step1: Create a array
Step2: When the page fault occurs replace page that will not be used forthe longest periodof
time.
PROGRAM:
#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int
optcal[50], count = 0;
int optvictim(int index);int
main()
{
printf("\nOPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\nEnter the number of frames: ");
scanf("%d", &nof);
printf("Enter the number of reference strings: ");scanf("%d",
&nor);
printf("Enter the reference string: ");for (i =
0; i < nor; i++)
{
scanf("%d", &ref[i]);
}
printf("\nThe given reference string:\n");for (i = 0; i
< nor; i++)
72
{
printf("%4d", ref[i]);
}
for (i = 0; i < nof; i++)
{
frm[i] = -1;
optcal[i] = 0;
}
printf("\n");
for (i = 0; i < nor; i++)
{
flag = 0;
printf("\n\tReference %d ->\t", ref[i]);for (j = 0;
j < nof; j++)
{
if (frm[j] == ref[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
count++;
if (count <= nof)
{
victim++;
}
else
{
victim = optvictim(i);
}
73
pf++;
frm[victim] = ref[i]; for (j
= 0; j < nof; j++)
{
printf("%4d", frm[j]);
}
}
}
printf("\nNumber of page faults: %d\n", pf);return 0; //
Return success
}
int optvictim(int index)
{
int i, j, farthest = -1; int
victim_page = -1; for (i =
0; i < nof; i++)
{
for (j = index; j < nor; j++)
{
if (frm[i] == ref[j])
{
if (j > farthest)
{
farthest = j;
victim_page = i;
}
break;
}
}
if (j == nor)
{
return i;
74
}
}
return (victim_page != -1) ? victim_page : 0;
}
75
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above program was executed and verified successfully
76
EX. NO : 9A
FILE ALLOCATION STRATEGIES
Date: SEQUENTIAL FILE
ALLOCATION
AIM:
ALGORITHM:
Step 4: Allocate the required locations to each in sequential order Randomly select a location
from available location s1= random (100);
Check whether the required locations are free from the selected location.
PROGRAM:
#include <stdio.h>
int main() {
f[i] = 0;
77
while (1) { // Loop for multiple file allocations
if (f[j] == 0) {
f[j] = 1;
} else {
break;
if (j == (st + len)) {
scanf("%d", &c);
if (c != 1) {
break;
return 0;
78
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
79
EX. NO : 9B
FILE ALLOCATION TECHNIQUE-
Date: INDEXEDALLOCATION
AIM:
To write a C program to implement File Allocation concept using the technique indexed
allocation Technique.
ALGORITHM:
Step 2:Obtain the required data through char and int datatypes.
PROGRAM:
#include<stdio.h>
void main()
char a[10];
int i,ib,cib[10];
scanf("%s",a);
80
printf("\n index block:");
scanf("%d",&ib);
for(i=1;i<=5;i++)
printf("%s\t\t%d",a,ib);
printf("\n the above file utiltization index block of child blocks followin\t");
printf("\n");
for(i=1;i<=5;i++) {
printf("%d\t\t",cib[i]);
printf("\n");
81
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above C program was executed and verified successfully.
82
EX. NO : 9C
FILE ALLOCATION TECHNIQUE-
Date: LINKED ALLOCATION
AIM:
To write a C program to implement File Allocation concept using the Linked List
Technique.Algorithm
ALGORITHM:
block.
Step 5:"for" loop is created to print the file utilization of linked type of entered
PROGRAM:
#include <stdio.h>
int main() {
char a[10];
int i, sb, eb, fb1[10];
printf("\nEnter the file name: ");
scanf("%s", a);
83
printf("Enter the ending block: ");
scanf("%d", &eb);
84
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above C program was executed and verified successfully.
85
ΕΧ.ΝΟ: 10A DISK SCHEDULING ALGORITHM-
FCFS
Date:
AIM:
ALGORITHM:
Step 2: Let Request array represents an array storing indexes of tracks that have been
requested inascending order of their time of arrival. 'head' is the position of disk head.
Step 3: Let us one by one take the tracks in default order and calculate the absolute distance of
the trackfrom the head.
Step 5: Currently serviced track position now becomes the new head
position. Step 6: Go to step 2 until all tracks in request array have not
been serviced.
86
PROGRAM:
#include
<stdio.h>
#include
<stdlib.h> int
main() {
");scanf("%d", &max);
scanf("%d", &queue[i]);
}
printf("Enter the initial head
queuequeue[n] = head;
diff = abs(queue[i + 1] -
printf("Disk head moves from %d to %d with seek %d\n", queue[i], queue[i + 1], diff);
}
87
printf("Total seek time is %d\n",
return 0;
}
88
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the program for implementation of FCFS Disk Scheduling Algorithm was
executed and verified successfully.
89
EX. NO : 10B
DISK SCHEDULING ALGORITHMS –
Date:
C-SCAN ALGORITHM
AIM:
ALGORITHM:
Step 1 : Take the number of disk requests (`n`), request sequence (`requests[]`), initial head
position (`head`), and disk size (`size`).
Step 2 - Ask the user to input the direction: `0` for left (towards 0) or `1` for right (towards max
track).
Step 3 - Sort the disk requests array in ascending order to facilitate sequential scanning.
Step 4 - Move the disk head towards the highest or lowest track based on the specified direction.
Step 5 - After reaching the end, simulate a circular scan by jumping to the opposite end (start or
end of the disk).
Step 6 - Continue servicing the remaining requests after the head jump, processing them in
sequence.
Step 7 - Calculate the total distance traveled by the head and display the result.
90
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &n);
int requests[n];
scanf("%d", &requests[i]);
scanf("%d", &head);
scanf("%d", &size);
scanf("%d", &direction);
if (direction == 1) {
91
totalMovement += abs(size - 1 - head);
head = size - 1;
head = 0;
head = requests[i];
} else {
head = 0;
head = size - 1;
head = requests[i];
return 0;
}
92
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above C program was executed and verified successfully.
93
EX. NO : 10C
DISK SCHEDULING ALGORITHMS –
Date:
SSTF ALGORITHM
AIM:
ALGORITHM:
Step 2: Let Request array represents an array storing indexes of tracks that have been
requested in ascending order of their time of arrival. 'head' is the position of disk head.
Step 3: Let us one by one take the tracks in default order and calculate the absolute
distance of thetrack from the head.
Step 5: Currently serviced track position now becomes the new head
position. Step 6: Go to step 2 until all tracks in request array have not
been serviced.
94
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
float avg;
// clrscr();
scanf("%d", &n);
scanf("%d", &queue[i]);
scanf("%d", &head);
95
int distance = abs(head - queue[j]);
min_distance = distance;
min_index = j;
if (min_index != -1) {
seek += min_distance;
head = queue[min_index];
return 0;
96
SNS College of Engineering
Department of Information Technology
Performance 4
Record 4
Viva 2
Total 10
RESULT:
Thus the above C program was executed and verified successfully.
97