OS record_merged (3)
OS record_merged (3)
E FOURTHSEMESTER
RECORD FOR
NAME:……...............................................……………………
REGNO:….………………………………….…………………
DEPARTMENTOFCOMPUTERSCIENCEANDENGINEERINGUNIVERSITY
COLLEGE OF ENGINEERING TINDIVANAM MELPAKKAM
TINDIVANAM-604001
ANNAUNIVERSITYPRACTICALEXAMINATIONS
APRIL/MAY-2025
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LABORATORY RECORD NOTE BOOK
2025-2026
This is to certify that this is a bonafide record of the work done by
Mr./Ms./Mrs...............................................................................................................,Register Number
Science and Engineering, CS3461 OPERATING SYSTEM LABORATORY LAB in the Fourth
Semester.
PAGE
S.NO DATE TOPIC NO. SIGNATURE
INDEX
PAGE
S.NO DATE TOPIC NO. SIGNATURE
Ex. No: 1
INSTALLATION OF WINDOWS OPERATING SYSTEM
Date:
INSTALLATION STEPS:
4.Double click the downloaded executable file located at the bottom left
“Next”.
7.Read the information carefully and select the checkbox if you are
installing the app on your personal computer, or you have administrator rights. Then click
“Install”.
1
Ex. No: 2a
BASIC UNIX COMMANDS
Date:
AIM:
To Study about basic Unix Commands.
COMMANDS:
Command : Cat
Purpose : It is used to display the contents of the file as well as used to create a new file.
Syntax : cat <file name >
Example : $ cat sam
Command : More
Purpose : It is used to display the contents of the file on the screen at a time.
Syntax : more <file name >
Example : $ more sam
Command : wc
Purpose : It is used to count the number of lines ,words and characters in a file or group of files.
Syntax : wc [options] <file name >
Example : $ wc –l sudha
Command : file
Purpose : It is used to determine the type of the file.
Syntax : file <file name > Example : $ file sudha
Command : spell
Purpose : It is used to find the spelling errors in the file.
Syntax : spell [options] <file name >
Example : $ spell -b sudha
Command : split
Purpose : It is used to split the given file into smaller pieces of given size.
Syntax : split –size <file name > < splitted file name >
Example : $ split –2 sudha su
Command : cp
Purpose : It is used to copy one or more files.
Syntax : cat <source file name > <destination file name>
Example : $ cp sudha subha
Command : mv
Purpose : It is used to move a file within a directory with different names and also used to move
a file to different directory with its original name.
Syntax : mv <source file name > <destination file name>
Example : $ mv sudha subha
2
Command : rm
Purpose : It is used to remove a file from the disk.
Syntax : rm <file name >
Example : $ rm sudha
Command : Who
Purpose : It is used to get the information about all the users currently working in the system.
Syntax : who
Example : $ who
Command : Who am i
Purpose : It is used to know in which terminal the user is currently logged on.
Syntax : who am i
Example : $ who am I
Command : Date
Purpose : It is used to display the system date and time. Syntax : date
Example : $ date
Command : cal
Purpose : It prints the calender for the specified year and month.
Syntax : cal <month> <year>
Example : $ cal 05 2003
Command : Id
Purpose : It is used to display the login name.
Syntax : id
Example : $ id
Command : clear
Purpose : It is used to clear the screen.
Syntax : clear
Example : $ clear
Command : uname
Purpose : It is used to display the details about the OS in which we are working.
Syntax : uname [options]
Example : $ uname –n
Command : tty
Purpose : It is used to know the terminal name on which we work.
Syntax : tty
Example : $ tty
3
Command : pwd
Purpose : It is used to display the absolute pathname of current working directory.
Syntax : pwd
Example : $ pwd
Command : bc
Purpose : It is used to perform simple mathematical calculations.Syntax : bc <operation>
Example : $ bc 3+5 8 ^d
Command : ls
Purpose : It is used to display the files in the current working directory.
Syntax : ls [options] <arguments>
Example : $ ls –p
Command : echo
Purpose : It echoes the argument on the standard output device.
Syntax : echo [options] <string>
Example : $ echo ‘sathya’
Command : man
Purpose : It gives details about the unix commands.
Syntax : man < command name >
Example : $ man echo
Command : head
Purpose : It is used to display the top portion of the file.
Syntax : head [options] <file name>
Example : $ head -5 sudha
Command : Tail
Purpose : It is used to display the bottom portion of the file.
Syntax : tail [options] <file name >
Example : $ tail –5 sudha
Command : pr
Purpose : It is used to display the contents of the file by separating them into pages
and each page begins with the header information.
Syntax : pr [options] <file name >
Example : $ pr sudha
Command : cut
Purpose : It is used to extract selected fields or columns from each line of one or more files and
display them on the standard output device.
Syntax : cut [options] <file name >
Example : $ cut –c5 sudha
4
Command : paste
Purpose : It concatenates the line from each input file column by column with tab characters in
between them.
Syntax : paste [options] <file name >
Example : $ paste f1 f2
Command : uniq
Purpose : It compares adjacent lines in the file and displays the output by eliminating duplicate
adjacent lines in it.
Syntax : uniq [options] <file name >
Example : $ uniq -c sudha
Command : sort
Purpose : It sorts one or more files based on ASCII sequence and also to merge the file.
Syntax : sort [options] <file name >
Example : $ sort -r sudha
Command : tee
Purpose : It is used to read the contents from standard input or from output of another command
and reproduces the output to in standard output
and direct into output to one or more files.
Syntax : tee [options] <file name >
Example : $ tee date dat.txt
Command : grep
Purpose : It is used to search the specified pattern from one or more files.
Syntax : grep [options] <pattern> <file name >
Example : $ grep “name” sudha
Command : mkdir
Purpose : It is used to create new directory or more than one directory.
Syntax : mkdir <directory name >
Example : $ mkdir psa
Command : rmdir
Purpose : It is used to remove the directory if it is empty.
Syntax : rmdir <directory name >
Example : $ rmdir psa
Command : cd
Purpose : It is used to change the control from one working directory to another specified
directory.
Syntax : cd <directory name >
Example : $ cd psa
5
Command : cd ..
Purpose : It is used to quit from current directory and move to the previous directory.
Syntax : cd ..
Example : $ cd ..
Command : ps
Purpose : It is used to display the attributes of a process.
Syntax : ps
Example : $ ps
$ ps –f ( Display the ancestry of a process )
$ ps –u ( Display the activities of a user )
$ ps –a ( Lists processes of all users but not the system processes)
Command : kill
Purpose : It is used to terminate the process.
Syntax : kill <PID>
Example : $ kill 105
Command : kill $!
Purpose : $! is the system variable which stores the process id of the last background job. The
command kill $! is used to kill the last process.
Syntax : kill $!
Example : $ kill $!
Command : at
Purpose : It is used to execute the process at the time specified.
Syntax : echo <time>
Example : $ at 14:08 (or) $ at 3 PM (or) $ at 4 :50 AM
RESULT:
Thus, the basic unix commands was executed and verified.
6
Ex. No: 2b
SHELL PROGRAMMING
Date:
Aim:
To write simple shell programs by using conditional, branching and looping statements.
Program:
# Swapping values – swap.sh
echo -n "Enter value for A : "
read a
echo -n "Enter value for B : "
read b
t=$a
a=$b
b=$t
echo "Values after Swapping"
echo "A Value is $a and B Value is $b"
Output:
$ sh swap.sh
Enter value for A : 12
Enter value for B : 23
Values after Swapping A Value is 23 and B Value is 12
Algorithm:
1. Prompt the user to enter the Fahrenheit value.
2. Read the input value and store it in a variable f.
3. Apply the conversion formula: Centigrade = (f - 32) * 5 / 9.
4. Display the result showing the converted Centigrade value.
5.End the script after displaying the result.
Program:
# Degree conversion – degconv.sh
echo -n "Enter Fahrenheit : "
read f
c=`expr \( $f - 32 \) \* 5 / 9`
echo "Centigrade is : $c"
7
Output
$ sh degconv.sh
Enter Fahrenheit : 213
Centigrade is : 100
Program:
# Biggest – big3.sh
echo -n "Give value for A B and C: "
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is the Biggest number"
elif [ $b -gt $c ]
then
echo "B is the Biggest number"
else
echo "C is the Biggest number"
fi
Output
$ sh big3.sh
Give value for A B and C: 4 3 4
C is the Biggest number
4. Grade Determination
Algorithm:
1. Prompt the user to enter their mark.
2. Check the mark and assign a grade based on the given conditions (S, A, B, C, D, E, or U).
3. Display the grade based on the condition that matches the mark.
4. End the script after displaying the grade.
Program:
# Grade – grade.sh
echo -n "Enter the mark : "
read mark
if [ $mark -gt 90 ]
then
echo "S Grade"
elif [ $mark -gt 80 ]
then
8
echo "A Grade"
elif [ $mark -gt 70 ]
then
echo "B Grade"
elif [ $mark -gt 60 ]
then
echo "C Grade"
elif [ $mark -gt 55 ]
then
echo "D Grade"
elif [ $mark -ge 50 ]
then
echo "E Grade"
else
echo "U Grade"
fi
Output :
$ sh grade.sh
Enter the mark : 65
C Grade
5. Vowel or Consonant
Algorithm:
1. Prompt the user to enter a lowercase character.
2. Check if the character is a vowel (a, e, i, o, or u) using a case statement.
3. Display "It's a Vowel" if the character matches one of the vowels.
4. Display "It's a Consonant" if the character is not a vowel.
Program:
# Vowel - vowel.sh
echo -n "Key in a lower case character : "
read choice
case $choice in
a|e|i|o|u) echo "It's a Vowel";;
*) echo "It's a Consonant"
esac
Output
$ sh vowel.
Key in a lower case character : e
It's a Vowel
9
6.Simple calculator:
Algorithm:
1. Prompt the user to enter two numbers.
2. Display the operation options (Addition, Subtraction, Multiplication, and Division).
3. Read the user’s choice for the operation and perform the selected arithmetic operation.
4. Display the result of the operation or show an "Invalid Option" message if the choice
is invalid.
Program:
# Arithmetic operations — calc.sh
echo -n "Enter the two numbers : "
read a b
echo " 1. Addition"
echo " 2. Subtraction"
echo " 3. Multiplication"
echo " 4. Division"
echo -n "Enter the option : "
read option
case $option in
1) c=`expr $a + $b`
echo "$a + $b = $c";;
2) c=`expr $a - $b`
echo "$a - $b = $c";;
3) c=`expr $a \* $b`
echo "$a * $b = $c";;
4) c=`expr $a / $b`
echo "$a / $b = $c";;
*) echo "Invalid Option"
esac
Output
$ sh calc.sh
Enter the two numbers : 2 4
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter the option : 1
2+4=6
7.Multipilication Table
Algorithm:
1. Prompt the user for a number.
2. Loop from 1 to 10.
3. Multiply the number by x.
4. Print the multiplication result.
10
Program:
# Multiplication table – multable.sh
clear
echo -n "Which multiplication table? : "
read n
for x in 1 2 3 4 5 6 7 8 9 10
do
p=`expr $x \* $n`
echo -n "$n X $x = $p"
sleep 1
done
Output
$ sh multable.sh
Which multiplication table? : 6
6X1=6
6 X 2 = 12
....
Number Reverse
Algorithm:
1. Ask for Input: Prompt the user to enter a number (n) to be reversed.
2. Initialize Variables: Set a variable rd to 0 (to store the reversed number).
3. Reverse the Number: Use a while loop to repeatedly extract the last digit of n, add it
to rd, and remove that last digit from n.
4. Display the Result: After the loop ends, print the reversed number.
Program:
# To reverse a number – reverse.sh
echo -n "Enter a number : "
read n
rd=0
while [ $n -gt 0 ]
do
rem=`expr $n % 10`
rd=`expr $rd \* 10 + $rem`
n=`expr $n / 10`
done
echo "Reversed number is $rd"
Output
$ sh reverse.sh
Enter a number : 234
Reversed number is 432
11
Prime Number
Algorithm:
1. Ask for Input: Prompt the user to enter a number (n) to check if it’s prime.
2. Initialize Variables: Set i = 2 (the first possible divisor) and m = n / 2 (the maximum value
to check for divisibility).
3. Check Divisibility: Use a loop to check if any number from 2 to m divides n evenly. If any
number divides it, it is not a prime number.
4. Display the Result: If no divisors are found, then the number is prime; otherwise, it’s not
prime.
Program:
# Prime number – prime.sh
echo -n "Enter the number : "
read n
i=2
m=`expr $n / 2`
until [ $i -gt $m ]
do
q=`expr $n % $i`
if [ $q -eq 0 ]
then
echo "Not a Prime number"
exit
fi
i=`expr $i + 1`
done
echo "Prime number"
Output
$ sh prime.sh
Enter the number : 17
Prime number
Result:
Thus the shell programming was developed and output was gaine
12
Ex. No:3a
PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEM
Date: (fork, getpid, exit)
Aim:
To write a c program for process management system calls(Fork,exit,Getpid)
Algorithm:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2.
STEP 3: Call fork()
system call to create process.
STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid().
STEP 6: Print the process id.
STEP 7:Stop the program
Program:
/* Process creation - fork.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
pid_t pid; int x=5;
pid = fork(); x++;
if (pid < 0)
{
printf("Process creation error");
exit(-1);
}
else if (pid == 0)
{
printf("Child process:");
printf("\nProcess id is %d",
getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:");
printf("\nProcess id is %d",
getpid());
13
printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
}
Output:
$ gcc fork.c
Result:
Thus ,Program for system calls of unix operating system was developed and output was
obtained.
14
Ex. No:3b
PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEM
Date: (wait)
Aim:
To block a parent process until child completes using wait system call.
Algorithm:
1. Create a Child Process:
• Use fork() to create a child process. The parent process receives the child's process ID
(pid), and the child receives pid == 0.
2. Parent Process (pid > 0):
• The parent process waits for the child to finish using wait(NULL).
• Once the child finishes, the parent prints even numbers from 2 to 10.
3. Child Process (pid == 0):
• The child process prints odd numbers from 1 to 9.
4. Parent Prints After Child:
• After the child finishes, the parent process prints a message indicating its own output.
5. End of Execution:
• Both the parent and the child processes end their execution.
Program:
/* Wait for child termination - wait.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
int i, status;
pid_t pid;
pid = fork();
if (pid < 0)
{
printf("\nProcess creation failure\n");
exit(-1);
}
else if(pid > 0)
{
15
wait(NULL);
printf ("\nParent starts\nEven Nos: ");
for (i=2;i<=10;i+=2)
printf ("%3d",i);
printf ("\nParent ends\n");
}
else if (pid == 0)
{
printf ("Child starts\nOdd Nos: ");
for (i=1;i<10;i+=2)
printf ("%3d",i);
printf ("\nChild
ends\n");
}
}
Output:
$ gcc wait.c
$./a.out
Child starts
Odd Nos: 13 57
Child ends
Parent starts
Even Nos: 2 46
Parent ends
Result:
Thus using wait system call zombie child processes were avoided.
16
Ex. No:3c
PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEM
Date:
(Close)
Aim:
To write a c program for process management system calls(Close).
Algorithm:
1. Check Command-Line Argument:
• Ensure the user provides a filename as a command-line argument. If not, print the usage
message and exit.
2. Open/Create the File:
• Use open() to create or truncate the file (in write-only mode) with the specified
filename. If the file cannot be opened, print an error and exit.
3. Prompt User for Input:
• Display a message instructing the user to enter input and press Ctrl+D when done.
4. Read User Input and Write to File:
• Continuously read user input (using read()), and for each chunk of data read, write it to
the file using write().
5. Close the File:
• After Ctrl+D is pressed (EOF), close the file using close().
Program:
/* File creation - fcreate.c */
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include <fcntl.h>
Output:
$ gcc fcreate.c
Result:
Thus ,Program for system calls of unix operating system was developed and output was
obtained.
18
Ex. No:4a
CPU SCHEDULING -FIRST COME FIRST SERVE(FCFS)
Date:
Aim:
To write a c program to simulate the CPU scheduling algorithm First Come First
Serve (FCFS).
Algorithm:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process name and the burst time
Step 4: Set the waiting of the first process as ‗0‘and its burst time as its turnaround time
Step 5: for each process in the Ready Q calculate
a). Waiting time (n) = waiting time (n-1) + Burst time (n-1)
b). Turnaround time (n)= waiting time(n)+Burst time(n)
Step 6: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process
Program:
/* FCFS Scheduling - fcfs.c */ #include<stdio.h>
struct process {
int pid;
int btime;
int wtime;
int ttime;
} p[10];
int main() {
int i, j, k, n, ttur, twat;
float awat, atur;
p[0].wtime = 0;
for (i = 0; i < n; i++) {
p[i + 1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
19
ttur = twat = 0;
for (i = 0; i < n; i++) {
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\nFCFS Scheduling\n\n");
printf("\n\nGANTT Chart\n");
printf("-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++)
printf("-");
printf("\n");
printf("|");
for (i = 0; i < n; i++) {
k = p[i].btime / 2;
for (j = 0; j < k; j++)
printf(" ");
printf("P%d", p[i].pid);
for (j = k + 1; j < p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++)
printf("-");
printf("\n");
printf("0");
for (i = 0; i < n; i++) {
20
for (j = 0; j < p[i].btime; j++)
printf(" ");
printf("%2d", p[i].ttime);
}
Output:
Enter no. of process : 4
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 4
Burst time for process P3 (in ms) : 11
Burst time for process P4 (in ms) : 6
FCFS Scheduling
----------------------------------------
Process B-Time T-Time W-Time
----------------------------------------
P1 10 10 0
P2 4 14 10
P3 11 25 14
P4 6 31 25
----------------------------------------
GANTT Chart
----------------------------------------
| P1 | P2 | P3 | P4 |
----------------------------------------
0 10 14 25 31
Result:
Thus waiting time & turnaround time for processes based on FCFS scheduling was
computed and the average waiting time was determined.
21
Ex. No:4b
CPU SCHEDULING-SHORTEST JOB FIRST (SJF)
Date:
Aim:
To write a program to stimulate the CPU scheduling algorithm Shortest job
first (Non- Preemption)
Algorithm:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to
lowest to highest burst time.
Step 5: Set the waiting time of the first process as ‗0‘ and its turnaround time as its burst
time.
Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue, calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process the process.
Step 9: Stop
Program:
/* SJF Scheduling – sjf.c */
#include<stdio.h>
struct process {
int pid;
int btime;
int wtime;
int ttime;
} p[10], temp;
int main() {
int i, j, k, n, ttur, twat;
float awat, atur;
22
p[i].pid = i + 1;
}
p[0].wtime = 0;
for (i = 0; i < n; i++) {
p[i + 1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for (i = 0; i < n; i++) {
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\nSJF Scheduling\n\n");
for (i = 0; i < 28; i++) printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for (i = 0; i < 28; i++) printf("-");
for (i = 0; i < n; i++)
printf("\n P%-4d\t%4d\t%3d\t%2d", p[i].pid, p[i].btime, p[i].ttime,
p[i].wtime);
printf("\n");
for (i = 0; i < 28; i++) printf("-");
printf("\n\nAverage waiting time: %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);
printf("\n\nGANTT Chart\n");
printf("-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++) printf("-");
printf("\n|");
for (i = 0; i < n; i++) {
k = p[i].btime / 2;
for (j = 0; j < k; j++) printf(" ");
23
printf("P%d", p[i].pid);
for (j = k + 1; j < p[i].btime; j++) printf(" ");
printf("|");
}
printf("\n-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++) printf("-");
printf("\n0");
for (i = 0; i < n; i++) {
for (j = 0; j < p[i].btime; j++) printf(" ");
printf("%2d", p[i].ttime);
}
Output:
Enter no. of process : 5
Burst time for process P1 (in ms) : 10
Burst time for process P2 (in ms) : 6
Burst time for process P3 (in ms) : 5
Burst time for process P4 (in ms) : 6
Burst time for process P5 (in ms) : 9
SJF Scheduling
----------------------------------------
Process B-Time T-Time W-Time
----------------------------------------
P3 5 5 0
P2 6 11 5
P4 6 17 11
P5 9 26 17
P1 10 36 26
----------------------------------------
GANTT Chart
-----------------------------------------------
| P3 | P2 | P4 | P5 | P1 |
-----------------------------------------------
0 5 11 17 26 36
Result:
Thus waiting time & turnaround time for processes based on SJF scheduling was
computed and the average waiting time was determined.
24
Ex. No:4c
CPU SCHEDULING - PRIORITY
Date:
Aim:
To write a c program to simulate the CPU scheduling priority algorithm.
Algorithm:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‗0‘ and its burst time as its turnaround time
Step 6: Arrange the processes based on process priority
Step 7: For each process in the Ready Q calculate Step 8: for each process
in the Ready Q calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate in an order.
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process
Step 9: Print the results.
Step 10:stop
Program:
/* Priority Scheduling- pri.c */ #include<stdio.h>
struct process {
int pid;
int btime;
int pri;
int wtime;
int ttime;
} p[10], temp;
int main() {
int i, j, k, n, ttur, twat;
float awat, atur;
25
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if ((p[i].pri > p[j].pri) ||
(p[i].pri == p[j].pri && p[i].pid > p[j].pid)) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
p[0].wtime = 0;
for (i = 0; i < n; i++) {
p[i + 1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for (i = 0; i < n; i++) {
ttur += p[i].ttime;
twat += p[i].wtime;
}
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nGANTT Chart\n");
printf("-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++)
printf("-");
printf("\n|");
for (i = 0; i < n; i++) {
k = p[i].btime / 2;
for (j = 0; j < k; j++)
printf(" ");
26
printf("P%d", p[i].pid);
for (j = k + 1; j < p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for (i = 0; i < (p[n - 1].ttime + 2 * n); i++)
printf("-");
printf("\n0");
for (i = 0; i < n; i++) {
for (j = 0; j < p[i].btime; j++)
printf(" ");
printf("%2d", p[i].ttime);
}
27
Output:
Priority Scheduling
--------------------------------------
Process B-Time Priority T-TimeW-Time
--------------------------------------
P2 7 1 7 0
P5 5 2 12 7
P1 10 3 22 12
P3 6 3 28 22
P4 13 4 41 28
--------------------------------------
GANTT Chart
----------------------------------------------------
| P2 | P5 | P1 | P3 | P4 |
----------------------------------------------------
0 7 12 22 28 41
Result:
Thus waiting time & turnaround time for processes based on Priority scheduling was
computed and the average waiting time was determined.
28
Ex. No:4d
CPU SCHEDULING – ROUND ROBIN
Date:
Aim:
To simulate the CPU scheduling algorithm round-robin
Algorithm:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where No. of time slice for
process (n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
a)Waiting time for process (n) = waiting time of process(n-1)+ burst time of
process(n-1 ) + the time difference in getting the CPU fromprocess(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number ofprocess
Step 8: Stop the process
Program:
/* Round robin scheduling- rr.c */
#include<stdio.h>
int main() {
int i, x = -1, k[10], m = 0, n, t, s = 0;
int a[50], temp, b[50], p[10], bur[10], bur1[10];
int wat[10], tur[10], ttur = 0, twat = 0, j = 0;
float awat, atur;
a[0] = 0;
while (j < m)
{
if (x == n - 1)
x = 0;
else
x++;
if (bur[x] >= t) {
bur[x] -= t;
a[j + 1] = a[j] + t;
if (b[x] == 1) {
p[s] = x;
k[s] = a[j + 1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d |", x + 1);
} else if (bur[x] != 0) {
a[j + 1] = a[j] + bur[x];
bur[x] = 0;
if (b[x] == 1) {
p[s] = x;
k[s] = a[j + 1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d |", x + 1);
}
}
printf("\n");
for (i = 0; i < m; i++) printf(" ");
printf("\n");
temp = k[i];
k[i] = k[j];
k[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
wat[i] = k[i] - bur1[i];
tur[i] = k[i];
}
for (i = 0; i < n; i++) {
ttur += tur[i];
twat += wat[i];
}
printf("\n\n");
for (i = 0; i < 30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for (i = 0; i < 30; i++)
printf("-");
for (i = 0; i < n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i] + 1, bur1[i], tur[i], wat[i]);
printf("\n");
31
Output:
GANTT Chart
------------------------------------------------------------------------------
0 10 20 23 30 40 50 52 61
------------------------------------------
Process Burst Trnd Wait
------------------------------------------
P1 10 10 0
P2 29 61 32
P3 3 23 20
P4 7 30 23
P5 12 52 40
------------------------------------------
Result:
Thus waiting time and turnaround time for processes based on Round robin scheduling
was computed and the average waiting time was determined.
32
Ex. No: 5
INTER PROCESS COMMUNICATION
Date: USING SHARED MEMORY
Aim:
To write a c program to implement IPC using shared memory.
Algorithm:
Step 1: Start the process
Step 2: Declare the segment size
Step 3: Create the shared memory
Step 4: Read the data from the shared memory
Step 5: Write the data to the shared memory
Step 6: Edit the data
Step 7: Stop the process.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void*shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345,1024,0666|IPC_CREAT);
printf(“key of shared memory is %d\n”,shmid);
shared_memory=shmat(shmid,NULL,0);
printf(“process attached at %p\n”, shared _memory);
printf(“enter some data to write to shared memory\n”);
read(0,buff,100);
strcpy(shared_memory,buff);
printf(“you wrote :%s\n”,(char*)shared_memory);
}
33
Output:
Key of share memory is 3145741
Process attached at 0x7f26fe1c7000
Enter some data to write to shared memory: Hello vengat
You wrote : hello vengat
Result:
Thus ,The program for Inter Process Communication using shared memory is executed
and verified successfully.
34
Ex. No: 6
SEMAPHORE IMPLEMENTATION
Date:
Aim :
To implement mutual exclusion by semaphore in c program
Algorithm:
1. Include necessary header files:
stdio.h for standard input/output operations.
pthread.h for thread-related operations.
semaphore.h for semaphore functions.
unistd.h for the sleep function.
2. Declare a semaphore variable mutex.
3. Define the thread function thread:
The function takes a void* argument and returns a void*.
It waits for the semaphore using sem_wait(&mutex) to enter the critical section.
Prints a message indicating that it has entered the thread.
Sleeps for 4 seconds to simulate some work being done in the critical section.
Prints a message indicating that it is exiting the thread.
Signals the semaphore using sem_post(&mutex) to release the lock.
4. In the main function:
Initialize the semaphore using sem_init(&mutex, 0, 1) with an initial value of 1.
Create two threads using pthread_create:
The first thread (t1) calls the thread function.
Sleep for 2 seconds using sleep(2).
The second thread (t2) also calls the thread function.
Wait for both threads to finish using pthread_join.
Destroy the semaphore using sem_destroy(&mutex).
Program:
/* C program to demonstrate working of Semaphores */
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
sem_t mutex;
Output:
Result:
Thus ,The C Programs To Implement Mutual Exclusion By SEMAPHORE was
successfully Executed and Verified.
36
Ex. No: 7
BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
Date:
Aim:
To write a C program to implement banker‟s algorithm for deadlock avoidance.
Algorithm:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker‟s algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it
is a deadlock process
Step-6: produce the RESULT of state of process
Step-7: Stop the program
Program:
#include <stdio.h>
#include <stdlib.h>
main() {
int r[1][10], av[1][10];
int all[10][10], max[10][10], ne[10][10], w[10], safe[10];
int i = 0, j = 0, k = 0, l = 0, np = 0, nr = 0, count = 0, cnt = 0;
clrscr();
printf("enter the number of processes in a system");
scanf("%d", &np);
printf("enter the number of resources in a system");
scanf("%d", &nr);
for (i = 1; i <= nr; i++) {
printf("Enter no. of instances of resource R%d ", i);
scanf("%d", &r[0][i]);
av[0][i] = r[0][i];
}
37
for (j = 1; j <= nr; j++)
{
scanf("%d", &max[i][j]);
}
}
printf("\nAvailability ");
for (i = 1; i <= nr; i++) printf("R%d %d\t", i, av[0][i]);
printf("\n ");
printf("\n safe sequence");
for (count = 1; count <= np; count++) {
for (i = 1; i <= np; i++) {
Cnt = 0;
38
Output:
enter the number of processes in a system 3
enter the number of resources in a system 3
pocess P1
allocated 3 maximum 4 need 1
allocated 2 maximum 4 need 2
allocated 1 maximum 4 need 3
-------------------------------------------------------------
pocess P2
allocated 1 maximum 3 need 2
allocated 1 maximum need 3
allocated 2 maximum 5 need 3
-------------------------------------------------------------
pocess P3
allocated 4 maximum 5 need 1
allocated 1 maximum 2 need 1
allocated 2 maximum 4 need 2
-----------------------------------------------------------
Availability R1 2 R2 3 R3 2
---------------------------------------------------------
safe sequence
P3 Availability R1 6 R2 4 R3 4
P1 Availability R1 9 R2 6 R3 5
P2 Availability R1 10 R2 7 R3 7
Result:
Thus C Programs To Avoid Deadlock Using Banker's Algorithm Was Developed And
Output Was Verified.
39
Ex. No: 8
DEADLOCK DETECTION ALGORITHM
Date:
Aim:
To write a C program to implement algorithm for deadlock detection.
Algorithm:
Step-1: Start the program.
Step-2: Declare the memory for the process.
Step-3: Read the number of process, resources, allocation matrix and available matrix.
Step-4: Compare each and every process using the banker‟s algorithm.
Step-5: If the process is in safe state then it is a not a deadlock process otherwise it
is a deadlock process
Step-6: produce the RESULT of state of process
Step-7: Stop the program.
Program:
#include <stdio.h>
#include <stdlib.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main()
{
printf("Deadlock Detection Algo\n");
input();
show();
cal();
return 0;
}
void input()
{
printf("Enter the number of Processes: ");
scanf("%d", &n);
printf("Enter the number of resource instances: ");
scanf("%d", &r);
printf("Enter the Max Matrix:\n");
for(int i = 0; i < n; i++)
40
for(int j = 0; j < r; j++)
scanf("%d", &max[i][j]);
void show()
{
printf("Process\t Allocation\t Max\t Available\n");
for(int i = 0; i < n; i++)
{
printf("P%d\t", i + 1);
for(int j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\t");
for(int j = 0; j < r; j++)
printf("%d ", max[i][j]);
printf("\t");
if (i == 0)
{
for(int j = 0; j < r; j++)
printf("%d ", avail[j]);
}
printf("\n");
}
}
void cal()
{
int finish[100] = {0};
int dead[100];
int j = 0;
int flag = 1;
flag = 0;
for(int i = 0; i < n; i++)
{
if(finish[i] == 0)
{
dead[j] = i;
j++;
flag = 1;
}
}
if(flag == 1)
{
printf("\n\nSystem is in Deadlock and the Deadlock processes are:\n");
for(int i = 0; i < j; i++)
{
printf("P%d\t", dead[i] + 1);
}
}
else
{
printf("\nNo Deadlock Occurred\n");
}
}
42
Output:
Deadlock Detection Algo
Enter the number of Processes: 3
Enter the number of resource instances: 3
Enter the Max Matrix:
360
433
344
Enter the Allocation Matrix:
333
203
124
Enter the available Resources:
120
Process Allocation Max Available
P1 333 360 120
P2 203 433
P3 124 344
Result:
Thus ,The C Program to implement Deadlock Detection Algorithm was executed and verified
successfully.
43
Ex. No: 9
THREADING
Date:
Aim :
To Write C program to implement Threading.
Algorithm :
step 1 : start.
step 2 : import the thread header file and intiate i=0;
step3: create a function and print value received by using pointer in the parameter.
step 4 : declare id and j value in main function and call the function in main function.
step 5 : stop.
PROGRAM:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> // For sleep function
#include <stdbool.h> // For boolean type
int main()
{
/* Creating thread id */
pthread_t id;
int ret;
/* Creating thread */
ret = pthread_create(&id, NULL, &threadFunction, NULL);
if(ret == 0) {
printf("Thread created successfully.\n");
}
else {
printf("Thread not created.\n");
return 0; /* Return from main */
}
while(running)
{
44
printf("I am main function.\n");
sleep(1); // Sleep to reduce CPU usage
}
running = false; // Signal the thread to stop
pthread_join(id, NULL); // Wait for the thread to finish
return 0;
Output:
Thread created successfully.
I am threadFunction.
I am threadFunction.
I am threadFunction.
I am threadFunction.
…
…
I am threadFunction.
I am main function.
I am main function.
I am main function.
I am main function.
…
…
I am main function.
I am threadFunction.
… and so on.
Result:
Thus,the C program to implement Threading was executed and verified successfully.
45
Ex. No: 10
PAGING TECHNIQUE
Date:
Aim:
To write a c program to implement Paging technique for memory management.
Algorithm:
Step 1: Start the process
Step 2: Declare page number, page table, frame number and process size. Step 3:
Read the process size, total number of pages
Step 4: Read the relative address
Step 5: Calculate the physical address Step
6: Display the address
Step 7: Stop the process
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int size, m, n, pgno, pagetable[3] = {5, 6, 7}, i, j, frameno;
double m1;
int ra = 0, ofs;
pgno = ra / 1000;
ofs = ra % 1000;
46
}
Output:
Enter process size (in KB of max 12KB): 12
Total No. of pages: 3
Enter relative address (in hex):
2643
page no = 2
page table:
0 [5]
1 [6]
2 [7]
Physical address: 7643
Result:
Thus,the C Program for implementing Paging Techniques was executed and verified successfully.
47
Ex. No: 11a
FIRST FIT ALLOCATION
Date:
Aim:
To allocate memory requirements for processes using first fit allocation.
Algorithm:
1. Define the maximum number of blocks and files that the program can handle using #define.
2. Declare and initialize the necessary variables: b[max] for block sizes, f[max] for file sizes,
frag[max] for fragmentation, bf[max] for block flags, and ff[max] for file flags.
3. Get the number of blocks and files from the user.
4. Ask the user to enter the size of each block.
5. Ask the user to enter the size of each file.
6. For each file, find the first block that is large enough to hold it.
7. If a block is found, set the flag for that block to 1 to indicate it is in use.
8. Calculate the fragmentation for that file and store it in frag[].
9. Repeat steps 6 to 8 for all files.
10.Display the results (file number, file size, block number, block size, and
fragmentation) using printf().
11.End the program with getch().
Program:
/* First fit allocation - ffit.c */
#include <stdio.h>
struct process {
int size;
int flag;
int holeid;
} p[10];
struct hole {
int size;
int actual;
} h[10];
48
for(i = 0; i < np; i++) {
for(j = 0; j < nh; j++) {
if(p[i].flag != 1) {
if(p[i].size <= h[j].size) {
p[i].flag = 1;
p[i].holeid = j;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole");
for(i = 0; i < np; i++) {
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i = 0; i < nh; i++)
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size);
printf("\n");
return 0; // Added return statement
49
Output:
First fit
Result:
Thus processes were allocated memory using first fit method.
50
Ex. No: 11b
WORST FIT ALLOCATION
Date:
Aim:
To allocate memory requirements for processes using worst fit allocation.
Algorithm:
1. Start the program.
2. Declare variables and constants, including the maximum number of blocks, files, and
fragmentation.
3. Clear the screen and print the memory management scheme's name - Worst Fit.
4. Prompt the user to input the number of blocks and files.
5. Prompt the user to enter the size of each block and file.
6. Loop through each file to allocate a block for it.
7. For each file, loop through each block to find the largest block that can
accommodate the file.
8. If a block is not allocated, calculate the remaining space after allocating the file.
9. If the remaining space is greater than or equal to zero, compare the remaining space with the
highest space found so far.
10.If the remaining space is greater than the current highest space, set the block index to the
current file index, and update the highest space to the remaining space.
11.Store the highest space found as the file's fragmentation value.
12.Mark the block as allocated and update the highest space found to zero.
13.Print the file number, size, block number, block size, and fragmentation value.
14.End the program.
Program:
#include <stdio.h>
#define max 25
int main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp, highest = 0;
static int bf[max], ff[max];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d", &nb);
printf("Enter the number of files:");
scanf("%d", &nf);
printf("\nEnter the size of the blocks:-\n");
51
temp = b[j] - f[i];
if(temp >= 0) {
if(highest < temp) {
ff[i] = j;
highest = temp;
}
}
}
}
frag[i] = highest;
if (highest > 0) { // Only mark as allocated if a block was found
bf[ff[i]] = 1;
}
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment");
for(i = 0; i < nf; i++) { // Changed to zero-based indexing
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, f[i], ff[i] + 1, b[ff[i]], frag[i]);
}
Result:
Thus processes were allocated memory using first fit method.
52
Ex. No: 11c
BEST FIT ALLOCATION
Date:
Aim:
To allocate memory requirements for processes using best fit allocation.
Algorithm:
1. Read the number of blocks and files.
2. Read the sizes of the blocks and files.
3. Initialize an array bf to keep track of whether each block is used or not. Set all values to 0.
4. For each file, loop through all the blocks to find the block with the smallest amount of
fragmentation that can accommodate the file.
5. Keep track of the index of the block with the smallest amount of fragmentation in an array ff.
6. Mark the selected block as used in the bf array.
7. Calculate and store the amount of fragmentation in an array frag.
8. Print the results.
Program:
#include <stdio.h>
struct process {
int size;
int flag;
int holeid;
} p[10];
struct hole {
int hid;
int size;
int actual;
} h[10];
int main() {
int i, np, nh, j;
53
for(j = 0; j < nh; j++) {
if(p[i].flag != 1) {
if(p[i].size <= h[j].size) {
p[i].flag = 1;
p[i].holeid = h[j].hid;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");
for(i = 0; i < np; i++) {
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i = 0; i < nh; i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual, h[i].size);
printf("\n");
54
Output:
Enter the number of Holes : 5
Enter size for hole H0 : 100
Enter size for hole H1 : 500
Enter size for hole H2 : 200
Enter size for hole H3 : 300
Enter size for hole H4 : 600
Best fit
Result:
Thus processes were allocated memory using best fit method.
55
Ex. No:12a
FIFO Page Replacement
Date:
Aim :
To Write C programs to implement the various FIFO Page Replacement Algorithms
Algorithm:
1. Start the process
2. Read number of pages n
3. Read number of pages no
4. Read page numbers into an array a[i]
5. Initialize avail[i]=0 .to check page hit
6. Replace the page with circular queue, while re-placing check page
availability in the frame
Place avail[i]=1 if page is placed in theframe Count page faults
7. Print the RESULTs.
8. Stop the process.
Program:
#include <stdio.h>
#include<conio.h>
int fr[3];
void display() {
int i;
printf("\n");
for(i = 0; i < 3; i++) {
printf("%d\t", fr[i]);
}
}
void main() {
int i, j, page[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int flag1 = 0, flag2 = 0, pf = 0, frsize = 3, top = 0;
if(flag1 == 0) {
56
for(i = 0; i < frsize; i++) {
if(fr[i] == -1) {
fr[i] = page[j];
flag2 = 1;
break;
}
}
}
if(flag2 == 0) {
fr[top] = page[j];
top++;
pf++; // Increment page fault count
if(top >= frsize) {
top = 0;
}
}
display();
}
Output:
2 -1 -1
2 3 -1
2 3 -1
2 3 1
5 3 1
5 2 1
5 2 4
5 2 4
3 2 4
3 2 4
3 5 4
3 5 2
Number of page faults: 6
Result:
Thus page replacement was implemented using FIFO algorithm.
57
Ex. No:12b
LRU PAGE REPLACEMENT
Date:
Aim:
To implement LRU page replacement technique.
Algorithm:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
Program:
#include <stdio.h>
#include <stdlib.h> // Added for standard library functions
int fr[3];
void display();
int main() // Changed void main() to int main()
{
int p[12] = {2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2};
int i, j, fs[3];
int index, k, l, flag1, flag2, pf = 0;
int frsize = 3;
void display()
{
int i;
printf("\nFrames: "); // Added a message for clarity
for (i = 0; i < 3; i++)
{
printf("%d ", fr[i]); // Corrected printf to display frame contents
}
printf("\n"); // Added newline for better formatting
}
59
Output:
Frames: 2 -1 -1
Frames: 2 3 -1
Frames: 2 3 -1
Frames: 2 3 1
Frames: 2 5 1
Frames: 2 5 1
Frames: 2 5 4
Frames: 2 5 4
Frames: 3 5 4
Frames: 3 5 2
Frames: 3 5 2
Frames: 3 5 2
Number of page faults: 7
Result:
Thus page replacement was implemented using LRU page replacement algorithm.
60
Ex. No:12c
OPTIMAL PAGE REPLACEMENT
Date:
Aim:
To implement optimal page replacement technique.
Algorthim:
1. Start Program
2. Read Number Of Pages And Frames
3.Read Each Page Value
4. Search For Page In The Frames
5.If Not Available Allocate Free Frame
6. If No Frames Is Free Repalce The Page With The Page That Is Leastly Used
7.Print Page
Number Of Page Faults
8.Stop process.
Program:
#include <stdio.h>
int fr[10], n, m;
void display(int m);
int main()
{
int i, j, page[20], fs[10];
int max, found = 0, lg[10], index, k, l, flag1 = 0, flag2 = 0, pf = 0;
float pr;
pf = m;
for (j = 0; j < n; j++)
{
flag1 = 0;
flag2 = 0;
for (i = 0; i < m; i++)
{
if (fr[i] == page[j])
{
flag1 = 1;
flag2 = 1;
break;
}
}
if (flag1 == 0)
61
{
for (i = 0; i < m; i++)
{
if (fr[i] == -1)
{
fr[i] = page[j];
flag2 = 1;
break;
}
}
}
if (flag2 == 0)
{
for (i = 0; i < m; i++)
lg[i] = 0;
for (i = 0; i < m; i++)
{
for (k = j + 1; k < n; k++)
{
if (fr[i] == page[k])
{
lg[i] = k - j;
break;
}
}
}
found = 0;
for (i = 0; i < m; i++)
{
if (lg[i] == 0)
{
index = i;
found = 1;
break;
}
}
if (found == 0)
{
max = lg[0];
index = 0;
for (i = 0; i < m; i++)
{
if (max < lg[i])
{
max = lg[i];
index = i;
}
}
}
fr[index] = page[j];
pf++;
}
display(m);
62
}
printf("Number of page faults : %d\n", pf);
pr = (float)pf / n * 100;
printf("Page fault rate = %f \n", pr);
return 0;
}
void display(int m)
{
int i;
for (i = 0; i < m; i++)
printf("%d\t", fr[i]);
printf("\n");
}
Output:
Enter length of the reference string: 12
Enter the reference string: 1 2 3 4 1 2 5 1 2 3 4 5
Enter no of frames: 3
1 -1 -1
1 2 -1
1 2 3
1 2 4
1 2 4
1 2 4
1 2 5
1 2 5
1 2 5
3 2 5
4 2 5
4 2 5
Number of page faults : 7
Page fault rate = 58.333332
Result:
Thus page replacement was implemented using Optimal page replacement algorithm.
63
Ex. No:13a
FILE ORGANIZATION TECHNIQUES
Date: (SINGLE LEVEL DIRECTORY)
Aim:
To organize files in a single level directory structure, I,e., without sub-directories.
Algorithm:
1. Get name of directory for the user to store all the files
2. Display menu
3. Accept choice
4. If choice =1 then
Accept filename without any collission
Store it in the di1rectory
5. If choice =2 then
Accept filename
Remove filename from the directory array
6. If choice =3 then
Accept filename
Check for existence of file in the directory array
7. If choice =4 then
List all files in the directory array
8. If choice =5 then
9. Stop
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct {
char dname[10];
char fname[25][10];
int fcnt;
} dir;
int main() {
int i, ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while (1) {
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5. Exit\nEnter your choice--
");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\n Enter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]);
dir.fcnt++;
break;
64
case 2:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++) {
if (strcmp(f, dir.fname[i]) == 0) {
printf("File %s is deleted ", f);
strcpy(dir.fname[i], dir.fname[dir.fcnt - 1]);
break;
}
}
if (i == dir.fcnt) // Corrected from I to i
printf("File %s not found", f);
else
dir.fcnt--;
break;
case 3:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for (i = 0; i < dir.fcnt; i++) {
if (strcmp(f, dir.fname[i]) == 0) {
printf("File %s is found ", f);
break;
}
}
if (i == dir.fcnt) // Corrected from I to i
printf("File %s not found", f);
break;
case 4:
if (dir.fcnt == 0)
printf("\n Directory Empty");
else {
printf("\n The Files are -- ");
for (i = 0; i < dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
}
break;
default:
exit(0);
}
}
return 0;
}
65
Output:
Enter name of directory -- CSE
66
The Files are -- A C
Result:
Thus, Implementation of various File Organization Techniques(Single Level directory) was
Successfully executed and Verified.
67
Ex. No:13b
FILE ORGANIZATION TECHNIQUES
Date:
(TWO LEVEL DIRECTORY)
Aim:
To organize files as two-level directory with each user having his own user file directory
(UFD).
Algorithm:
1. Display menu
2. Accept choice
3. If choice =1 then
Accept directory name
Create an entry for that directory
4. If choice =2 then
Get directory name
If directory exist then accept filename without collision else report error
5. If choice =3 then
Get directory name
If directory exist then Get filename
If file exist in that directory then delete entry else report error
6. If choice =4 then
Get directory name
If directory exist then Get filename
If file exist in that directory then Display filename else report error
7. If choice =5 then Display files directory-wise
8. If choice =6 then
9. Stop
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct {
char dname[10], fname[10][10];
int fcnt;
} dir[10];
int main() {
int i, ch, dcnt, k;
char f[30], d[30];
dcnt = 0;
while (1) {
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \nEnter your choice -- ");
scanf("%d", &ch);
switch (ch) {
case 1:
68
if (dcnt < 10) {
printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created");
} else {
printf("Maximum directory limit reached.");
}
break;
case 2:
printf("\n Enter name of the directory -- ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
printf("Enter name of the file -- ");
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
}
if (i == dcnt) {
printf("Directory %s not found", d);
}
break;
case 3:
printf("\nEnter name of the directory -- ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
printf("Enter name of the file -- ");
scanf("%s", f);
for (k = 0; k < dir[i].fcnt; k++) {
if (strcmp(f, dir[i].fname[k]) == 0) {
printf("File %s is deleted ", f);
dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
break;
}
}
if (k == dir[i].fcnt) {
printf("File %s not found", f);
}
break;
}
}
if (i == dcnt) {
printf("Directory %s not found", d);
}
break;
69
case 4:
printf("\nEnter name of the directory -- ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
70
Output:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice -- 1
Directory Files
CSE Ram
ECE Raju
71
Enter name of the directory -- CSE
Enter name of the file -- Ram
File Ram is deleted File Ram not found
Directory Files
CSE
ECE Raju
Result:
Thus, Implementation Of various File Organization Techniques(Two Level directory) was Successfully
executed and Verified
72
Ex. No:14a
SEQUENTIAL FILE ALLOCATION METHOD
Date:
Aim:
Write a C Program to implement Sequential File Allocation method.
Algorithm:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches the entire entire memory block until a
holewhich is big enough is encountered. It allocates that memory block for
therequesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest
holewhichcanbe allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques and display the
bestalgorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
Program:
#include <stdio.h>
int main() {
int n, i, j, b[20], sb[20], t[20], x,
c[20][20];
printf("\nFilename\tStart block\tLength\n");
for(i = 0; i < n; i++)
printf("%d\t\t%d\t\t%d\n", i + 1, t[i], b[i]);
if (x < 1 || x > n) {
printf("Invalid file index.\n");
return 1; // Exit if the index is out of bounds
}
73
printf("\nFile name: %d", x);
printf("\nLength: %d", b[x - 1]); // Corrected to use x - 1
printf("\nBlocks occupied: ");
return 0;
}
Output:
Enter number of files: 3
Enter number of blocks occupied by file 1: 3
Enter the starting block of file 1: 5
Enter number of blocks occupied by file 2: 2
Enter the starting block of file 2: 1
Enter number of blocks occupied by file 3: 5
Enter the starting block of file 3: 8
File name: 2
Length: 2
Blocks occupied: 1 2
Result:
Thus, the following Sequential File Allocation Strategies using C programs was developed successfully
and output was gained.
74
Ex. No:14b
INDEXED FILE ALLOCATION METHOD
Date:
Aim:
Write a C Program to implement Indexed File Allocation method.
Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3:check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6:If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till requiredStep 11:
Terminate the process.
Program:
#include <stdio.h>
int main() {
int n, m[20], i, j, sb[20], s[20], b[20][20], x;
printf("\nFile\tIndex\tLength\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\n", i + 1, sb[i], m[i]);
}
if (x < 1 || x > n) {
printf("Invalid file index.\n");
75
return 1; // Exit if the index is out of bounds
}
i = x - 1;
printf("Starting block is: %d\n", sb[i]);
printf("Blocks occupied are: ");
for (j = 0; j < m[i]; j++) {
printf("%3d", b[i][j]);
}
printf("\n");
return 0;
}
Output:
Enter no. of files:2
Enter starting block and size of file1:3
5
Enter blocks occupied by file1:5
Enter blocks of file1:3 4 5 6 7
Enter starting block and size of file2:9
6
Enter blocks occupied by file2:6
Enter blocks of file2:9 10 11 12 13 14
File Index Length
1 3 5
2 9 6
Enter file name:2
File name is:2
Index is:9
Block occupied are: 9 10 11 12 13 14
Result:
Thus, The Indexed File Allocation Strategies using C programs was developed successfully and
output was gained.
76
Ex. No:14c
LINKED FILE ALLOCATION METHOD
Date:
Aim:
Write a C Program to implement Linked File Allocation method.
Algorithm:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of thestack
Step 6: Stop the allocation.
Program:
#include <stdio.h>
#include <string.h>
int main() {
static int b[20], i, j, blocks[20][20];
char F[20][20], S[20];
int sb[20], x, n;
Output:
Enter number of Files: 2
Enter file 1 name: fcfs
Enter number of blocks: 3
Enter file 2 name: sjf
Enter number of blocks: 2
Enter Starting block of file 1: 8
Enter blocks for file 1:
Enter block 1: 3
Enter block 2: 5
Enter block 3: 7
Enter Starting block of file 2: 2
Enter blocks for file 2:
Enter block 1: 6
Enter block 2: 8
Enter the Filename: fcfs
fcfs 3 8 8->3->5->7->
------------------------------------------------
Result:
Thus,The Linked File Allocation Strategies using C programs was developed successfully and output
was gained.
78
Ex. No:15a
Disk Scheduling-FCFS
Date:
Aim:
To implement the FCFS Disk Scheduling Algorithm and calculate the total head movement required
to service disk I/O requests.
Algorithm:
int main() {
int n, i, head, total_seek_time = 0;
int request[n];
return 0;
}
Output:
Enter the number of disk requests: 7
Enter the disk request sequence:
82 170 43 140 24 16 190
Enter initial head position: 50
Result:
Thus,The C program for FCFS disk scheduling algorithm is executed and verified successfully.
80
Ex. No:15b
Disk Scheduling-SSTF
Date:
Aim:
To implement the SSTF Disk Scheduling Algorithm and calculate the total head movement required
to service disk I/O requests.
Algorithm:
1. Start.
7. Find the unvisited request with the minimum absolute distance from the current head position.
12. End.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n, i, head, total_seek_time = 0;
81
visited[i] = 0; // Initialize all requests as unvisited
}
int completed = 0;
int sequence[n + 1]; // For arrow output
sequence[0] = head;
while(completed < n) {
int min_distance = INT_MAX;
int index = -1;
if(index != -1) {
visited[index] = 1;
printf("Move from %d to %d (Seek: %d)\n", current_position, request[index], min_distance);
total_seek_time += min_distance;
current_position = request[index];
sequence[completed + 1] = current_position; // Store move in sequence
completed++;
}
}
return 0;
}
82
Output:
Enter the number of disk requests: 7
Enter the disk request sequence:
82 170 43 140 24 16 190
Enter initial head position: 50
Result:
Thus,The C program for SSTF disk scheduling algorithm is executed and verified successfully.
83
Ex. No:15c
Disk Scheduling-SCAN
Date:
Aim:
To implement the SCAN Disk Scheduling Algorithm and calculate the total head movement required
to service disk I/O requests.
Algorithm:
1. Sort the disk requests in ascending order.
6. Reverse direction after reaching the disk's end (max or min) and service the remaining requests.
8. Stop
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, head, disk_size, direction, total_seek_time = 0;
int request[n];
printf("Enter the disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);
}
84
// Sort the requests
qsort(request, n, sizeof(int), compare);
// Go to end of disk
if(current_position != disk_size - 1) {
total_seek_time += abs(disk_size - 1 - current_position);
current_position = disk_size - 1;
}
// Reverse direction
for(i = left_count - 1; i >= 0; i--) {
int seek = abs(current_position - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current_position, left[i], seek);
total_seek_time += seek;
current_position = left[i];
sequence[seq_index++] = current_position;
}
// Go to start of disk
if(current_position != 0) {
total_seek_time += current_position;
current_position = 0;
}
// Reverse direction
for(i = 0; i < right_count; i++) {
int seek = abs(current_position - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current_position, right[i], seek);
total_seek_time += seek;
current_position = right[i];
sequence[seq_index++] = current_position;
}
}
return 0;
}
86
Output:
Enter number of disk requests: 7
Enter the disk request sequence:
82 170 43 140 24 16 190
Enter initial head position: 50
Enter total disk size (maximum track number): 200
Enter direction (0 = left, 1 = right): 1
Result:
Thus,The C program for SCAN disk scheduling algorithm is executed and verified successfully.
87
Ex. No:15d
Disk Scheduling C-SCAN
Date:
Aim:
To implement the C-SCAN Disk Scheduling Algorithm and calculate the total head movement
required to service disk I/O requests.
Algorithm:
1. Start.
10. Service requests to the right (greater than or equal to the head position) first:
12. After reaching the end of the disk, jump to the start (position 0).
13. Service requests to the left (less than the head position) after the jump.
14. Calculate total seek time by summing all the head movements.
15. Display the total seek time and the order of serviced requests.
16. End.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, head, disk_size, total_seek_time = 0;
88
printf("Enter number of disk requests: ");
scanf("%d", &n);
int request[n];
printf("Enter the disk request sequence:\n");
for (i = 0; i < n; i++) {
scanf("%d", &request[i]);
}
if (current != disk_size - 1) {
int seek = abs(current - (disk_size - 1));
printf("Move from %d to %d (Seek: %d)\n", current, disk_size - 1, seek);
total_seek_time += seek;
current = disk_size - 1;
}
return 0;
}
Output:
Result:
Thus,The C program for C-SCAN disk scheduling algorithm is executed and verified successfully.
90
Ex. No:15e
Disk scheduling-LOOK
Date:
Aim:
To implement the LOOK Disk Scheduling Algorithm and calculate the total head movement
required to service disk I/O requests.
Algorithm:
1.Input:
Number of requests.
Request sequence.
7.End.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, head, direction, total_seek_time = 0;
int request[n];
91
printf("Enter disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);
}
return 0;
}
Result:
Thus,The C program for LOOK disk scheduling algorithm is executed and verified successfully.
93
Ex. No:15f
Disk Scheduling C-LOOK
Date:
Aim:
To implement the C-LOOK Disk Scheduling Algorithm and calculate the total head movement
required to service disk I/O requests.
Algorithm:
1. Input number of requests, request sequence, initial head position, and direction.
3. Divide requests into two lists: less than and greater than or equal to the head.
4. Service requests in the given direction, then jump to the farthest request in the opposite list and
continue.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, head, direction, total_seek = 0;
// Step 1: Input
printf("Enter number of disk requests: ");
scanf("%d", &n);
int request[n];
printf("Enter disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);
}
// Step 2: Sort
qsort(request, n, sizeof(int), compare);
// Step 3: Divide
int left[n], right[n], left_count = 0, right_count = 0;
for(i = 0; i < n; i++) {
94
if(request[i] < head)
left[left_count++] = request[i];
else
right[right_count++] = request[i];
}
return 0;
}
Output:
Enter number of disk requests: 7
Enter disk request sequence:
82 170 43 140 24 16 190
Enter initial head position: 50
Enter direction (0 = left, 1 = right): 1
C-LOOK Disk Scheduling:
Head Movements:
Move from 50 to 82 (Seek: 32)
Move from 82 to 140 (Seek: 58)
Move from 140 to 170 (Seek: 30)
Move from 170 to 190 (Seek: 20)
Jump from 190 to 16 (Seek: 174)
Move from 16 to 24 (Seek: 8)
Move from 24 to 43 (Seek: 19)
Total Seek Time: 341
Seek Sequence: 50 -> 82 -> 140 -> 170 -> 190 -> 16 -> 24 -> 43
Result:
Thus,The C program for C-LOOK disk scheduling algorithm is executed and verified successfully.
96
Ex. No:16 INSTALL ANY GUEST OPERATING SYSTEM USING
VMWARE
Date:
AIM:
To install Kali Linux on Windows using a virtual machine software called Vware.
PROCEDURE :
1.Download and install a virtual machine software such as VMware on your Windows machine.
2.Download the Kali Linux ISO files from the official Kali Linux website.
69
97
3.Open the virtual machine software and click on "New" to create a new virtual machine.
4.Follow the instructions to set up the virtual machine, such as specifying the amount of RAM and
hard disk space to allocate.
5.When prompted, select the Kali Linux ISO file as the installation media.
98
6.Complete the Kali Linux installation process within the virtual machine, following the prompts.
71
99
100
73
101
7.Once the installation is complete, start the Kali Linux virtual machine from the virtualization
software.
102
8.You should now have Kali Linux running as a virtual machine on your Windows
computer.
RESULT :
Thus Installation Of Any Guest Operating System Using Vmware Was
Successfully Verified.
103
104