0% found this document useful (0 votes)
3 views

OS record_merged (3)

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

OS record_merged (3)

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

B.

E FOURTHSEMESTER

RECORD FOR

CS3461 OPERATING SYSTEM LABORATORY

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

….......................................................................... of the Second year B.E., Department of Computer

Science and Engineering, CS3461 OPERATING SYSTEM LABORATORY LAB in the Fourth

Semester.

Record Work Completed on

Staff in-charge Head of the Department

Submitted for the University Practical Examination held on

Internal Examiner External Examiner


INDEX

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:

1.Click on the “Download Citrix Workspace” button as shown on the image

2.Click the “Download for Windows “ button.

3.Click the “Download Citrix Workspace App for Windows” button.

4.Double click the downloaded executable file located at the bottom left

corner of the screen.

5.Click “Start” when the installation wizard pops up.

6.Accept the License Agreement by selecting the checkbox and click

“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”.

8.After the installation is completed successfully, click “Finish”. Now you

can go to virtualapps.fau.edu to log in and start using the available apps.

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.

1.Swapping of Two numbers:


Algorithm:
1. Prompt the user to enter values for A and B.
2. Swap the values using a temporary variable.
3. Display the swapped values of A and B.

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

2.Convert Fahrenheit to celcius:

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

3.Largest of three numbers:


Algorithm:
1.Prompt the user to enter values for A, B, and C.
2. Compare if A is greater than both B and C (if true, print "A is the Biggest number").
3. Else, compare if B is greater than C (if true, print "B is the Biggest number").
4. Else, print "C is the Biggest number" if neither of the previous conditions is true.

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

$ ./a.out Child process:


Process id is 19499 Value of x is 6
Process id of parent is 19498

Parent process: Process id is 19498 Value of x is 6 Process id of shell is 3266

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>

main(int argc, char *argv[])


{
int fd, n, len;
char buf[100];
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
if(fd<0)
{
printf("File creation problem\n");
exit(-1);
}
printf("Press Ctrl+D at end in a new line:\n");
17
while((n = read(0, buf, sizeof(buf))) > 0)
{

len = strlen(buf); write(fd, buf, len);


}
close(fd);
}

Output:
$ gcc fcreate.c

$ ./a.out hello File I/O


Open system call is used to either open or create a file. creat system call is used to create a file.
It is seldom used.
^D

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;

printf("Enter no. of process : ");


scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Burst time for process P%d (in ms) : ", (i + 1));
scanf("%d", &p[i].btime);
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;
}
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");

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%d\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");
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
----------------------------------------

Average waiting time : 12.25ms


Average turn around time : 20.00ms

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;

printf("Enter no. of process : ");


scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Burst time for process P%d (in ms) : ", (i + 1));
scanf("%d", &p[i].btime);

22
p[i].pid = i + 1;
}

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


for (j = i + 1; j < n; j++) {
if ((p[i].btime > p[j].btime) ||
(p[i].btime == p[j].btime && 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("\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
----------------------------------------

Average waiting time: 11.80ms


.Average turn around time : 19.00ms

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;

printf("Enter no. of process : ");


scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Burst time for process P%d (in ms) : ", (i + 1));
scanf("%d", &p[i].btime);
printf("Priority for process P%d : ", (i + 1));
scanf("%d", &p[i].pri);
p[i].pid = i + 1;
}

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\t Priority Scheduling\n\n");


for (i = 0; i < 38; i++) printf("-");
printf("\nProcess B-Time Priority T-TimeW-Time\n");
for (i = 0; i < 38; i++)
printf("-");
for (i = 0; i < n; i++)
printf("\n P%-4d\t%4d\t%3d\t%4d\t%4d", p[i].pid, p[i].btime, p[i].pri,
p[i].ttime, p[i].wtime);
printf("\n");
for (i = 0; i < 38; 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(" ");

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:

Enter no. of process : 5


Burst time for process P1 (in ms) : 10
Priority for process P1 : 3
Burst time for process P2 (in ms) : 7
Priority for process P2 : 1
Burst time for process P3 (in ms) : 6
Priority for process P3 : 3
Burst time for process P4 (in ms) : 13
Priority for process P4 : 4
Burst time for process P5 (in ms) : 5
Priority for process P5 : 2

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

Average waiting time: 13.80ms


Average turn around time :22.00ms

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;

printf("Enter no. of process : ");


scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Burst time for process P%d : ", (i + 1));
scanf("%d", &bur[i]);
bur1[i] = bur[i];
}
printf("Enter the time slice (in ms) : ");
scanf("%d", &t);

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


b[i] = bur[i] / t;
if ((bur[i] % t) != 0) b[i] += 1;
29
m += b[i];
}
printf("\n\t\tRound Robin Scheduling\n");
printf("\nGANTT Chart\n");
for (i = 0; i < m; i++) printf("");
printf("\n");

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

for (j = 0; j <= m; j++) printf("%d\t", a[j]);

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


for (j = i + 1; j < n; j++) {
30
if (p[i] > p[j]) {
temp = p[i];
p[i] = p[j];
p[j] = temp;

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

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


printf("-");
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nAverage waiting time: %.2f ms", awat);
printf("\nAverage turn around time: %.2f ms\n", atur);
}

31
Output:

Enter no. of process : 5


Burst time for process P1 : 10
Burst time for process P2 : 29
Burst time for process P3 : 3
Burst time for process P4 : 7
Burst time for process P5 : 12
Enter the time slice (in ms) : 10

Round Robin Scheduling

GANTT Chart
------------------------------------------------------------------------------

P1 |P2 |P3 |P4 |P5 |P2 |P5 |P2 |


------------------------------------------------------------------------------

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

Average waiting time: 23.00 ms


Average turn around time: 35.20 ms

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;

void* thread(void* arg)


{
//wait sem_wait(&mutex);
printf("\nEntered..\n");
//critical section sleep(4);
//signal
printf("\nJust Exiting...\n");
35
sem_post(&mutex);
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL); sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
sem_destroy(&mutex);
return 0;
}

Output:

$ gcc sem.c -lpthread


$ ./a.out Entered..
Just Exiting... Entered.. Just Exiting...

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

for (i = 1; i <= np; i++)


for (j = 1; j <= nr; j++) all[i][j] = ne[i][j] = max[i][j] = w[i] = 0;
printf("Enter the allocation matrix");
for (i = 1; i <= np; i++) {
for (j = 1; j <= nr; j++) {
scanf("%d", &all[i][j]);
av[0][j] = av[0][j] - all[i][j];
}
}

printf("Enter the maximum matrix");


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

37
for (j = 1; j <= nr; j++)
{
scanf("%d", &max[i][j]);
}
}

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


for (j = 1; j <= nr; j++) {
ne[i][j] = max[i][j] - all[i][j];
}
}

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


printf("pocess P%d", i);
for (j = 1; j <= nr; j++) {
printf("\n allocated %d\t", all[i][j]);
printf("maximum %d\t", max[i][j]);
printf("need %d\t", ne[i][j]);
}
printf("\n \n");
}

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;

for (j = 1; j <= nr; j++) {


if (ne[i][j] <= av[0][j] && w[i] == 0) cnt++;
}
if (cnt == nr) {
k++;
safe[k] = i;
for (l = 1; l <= nr; l++) av[0][l] = av[0][l] + all[i][l];
printf("\n P%d ", safe[k]);
printf("\t Availability ");
for (l = 1; l <= nr; l++) printf("R%d %d\t", l, av[0][l]);
w[i] = 1;
}
}
}
getch();

38
Output:
enter the number of processes in a system 3
enter the number of resources in a system 3

enter no. of instances of resource R1 10


enter no. of instances of resource R2 7
enter no. of instances of resource R3 7

Enter the allocation matrix


321
112
412

Enter the maximum matrix


444
345
524

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

printf("Enter the Allocation Matrix:\n");


for(int i = 0; i < n; i++)
for(int j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);

printf("Enter the available Resources:\n");


for(int j = 0; j < r; j++)
scanf("%d", &avail[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;

// Find need matrix


for(int i = 0; i < n; i++)
for(int j = 0; j < r; j++)
need[i][j] = max[i][j] - alloc[i][j];
while(flag)
{
flag = 0;
41
for(int i = 0; i < n; i++)
{
int c = 0;
for(int j = 0; j < r; j++)
{
if((finish[i] == 0) && (need[i][j] <= avail[j]))
{
c++;
if(c == r)
{
for(int k = 0; k < r; k++)
{
avail[k] += alloc[i][k];
}
finish[i] = 1;
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

System is in Deadlock and the Deadlock processes are:


P1 P2 P3

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

volatile bool running = true; // Global variable to control the loops

/* Thread function definition */


void* threadFunction(void* args)
{
while(running)
{
printf("I am threadFunction.\n");
sleep(1); // Sleep to reduce CPU usage
}
return NULL; // Return NULL to indicate thread completion
}

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;

printf("Enter process size (in KB of max 12KB): ");


scanf("%d", &size);

m1 = size / 4.0; // Ensure floating-point division


n = ceil(m1);
printf("Total No. of pages: %d", n);

printf("\nEnter relative address (in hex): \n");


scanf("%x", &ra); // Use %x for hexadecimal input

pgno = ra / 1000;
ofs = ra % 1000;

// Check if pgno is within the bounds of the pagetable


if (pgno >= 0 && pgno < 3) {
printf("page no = %d\n", pgno);
printf("page table:");
for (i = 0; i < n; i++)
printf("\n %d [%d]", i, pagetable[i]);
frameno = pagetable[pgno];
printf("\nPhysical address: %d%d\n", frameno, ofs);
} else {
printf("Error: Page number out of bounds.\n");
}

return 0; // Return statement for main

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

int main() { // Changed to int main()


int i, np, nh, j;
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i = 0; i < nh; i++) {
printf("Enter size for hole H%d : ", i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
}
printf("\nEnter number of process : ");
scanf("%d", &np);
for(i = 0; i < np; i++) {
printf("enter the size of process P%d : ", i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}

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:

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

Enter number of process : 4


enter the size of process P0 : 212
enter the size of process P1 : 417
enter the size of process P2 : 112
enter the size of process P3 : 426

First fit

Process PSize Hole


P0 212 H1
P1 417 H4
P2 112 H1
P3 426 Not allocated

Hole Actual Available


H0 100 100
H1 500 176
H2 200 200
H3 300 300
H4 600 183

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

for(i = 0; i < nb; i++) { // Changed to zero-based indexing


printf("Block %d:", i + 1);
scanf("%d", &b[i]);
bf[i] = 0;
}

printf("Enter the size of the files :-\n");


for(i = 0; i < nf; i++) { // Changed to zero-based indexing
printf("File %d:", i + 1);
scanf("%d", &f[i]);
}
for(i = 0; i < nf; i++) { // Changed to zero-based indexing
highest = 0;
for(j = 0; j < nb; j++) { // Changed to zero-based indexing
if(bf[j] != 1) { // if bf[j] is not allocated

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

return 0; // Return statement for main


}
Output:
Memory Management Scheme - Worst Fit
Enter the number of blocks:4
Enter the number of files:5
Enter the size of the blocks:-
Block 1:200
Block 2:400
Block 3:600
Block 4:300
Enter the size of the files :-
File 1:200
File 2:400
File 3:500
File 4:300
File 5:150

File_no: File_size: Block_no: Block_size: Fragment


1 200 3 600 400
2 400 1 200 0
3 500 1 200 0
4 300 2 400 100
5 150 4 300 150

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

void bsort(struct hole[], int); // Function prototype

int main() {
int i, np, nh, j;

printf("Enter the number of Holes : ");


scanf("%d", &nh);
for(i = 0; i < nh; i++) {
printf("Enter size for hole H%d : ", i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : ");
scanf("%d", &np);
for(i = 0; i < np; i++) {
printf("Enter the size of process P%d : ", i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i = 0; i < np; i++) {
bsort(h, nh);

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

return 0; // Return statement added


}

void bsort(struct hole bh[], int n) {


struct hole temp;
int i, j;
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) {
if(bh[i].size > bh[j].size) {
temp = bh[i];
bh[i] = bh[j];
bh[j] = temp;
}
}
}

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

Enter number of process : 4


Enter the size of process P0 : 212
Enter the size of process P1 : 417
Enter the size of process P2 : 112
Enter the size of process P3 : 426

Best fit

Process PSize Hole


P0 212 H3
P1 417 H1
P2 112 H2
P3 426 H4

Hole Actual Available


H1 500 83
H3 300 88
H2 200 88
H0 100 100
H4 600 174

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;

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


fr[i] = -1;
}

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


flag1 = 0;
flag2 = 0;

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


if(fr[i] == page[j]) { // Corrected syntax error
flag1 = 1;
flag2 = 1;
break;
}
}

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

printf("Number of page faults: %d", pf); // Corrected page fault count

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;

// Initialize frame array


for (i = 0; i < frsize; i++)
{
fr[i] = -1;
}

// Process each page request


for (j = 0; j < 12; j++)
{
flag1 = 0;
flag2 = 0;

// Check if page is already in frame


for (i = 0; i < frsize; i++)
{
if (fr[i] == p[j])
{
flag1 = 1;
flag2 = 1;
break;
}
}
// If page is not found, check for empty frame
if (flag1 == 0)
58
{
for (i = 0; i < frsize; i++)
{
if (fr[i] == -1)
{
fr[i] = p[j];
flag2 = 1;
break;
}
}
}

// If no empty frame, replace a page


if (flag2 == 0)
{
for (i = 0; i < frsize; i++)
fs[i] = 0;

for (k = j - 1, l = 1; l <= frsize - 1; l++, k--)


{
for (i = 0; i < frsize; i++)
{
if (fr[i] == p[k])
fs[i] = 1;
}
}

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


{
if (fs[i] == 0)
index = i;
}
fr[index] = p[j];
pf++;
}
display();
}

printf("\nNumber of page faults: %d\n", pf + frsize); // Corrected output message


return 0; // Added return statement
}

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;

printf("Enter length of the reference string: ");


scanf("%d", &n);
printf("Enter the reference string: ");
for (i = 0; i < n; i++)
scanf("%d", &page[i]);
printf("Enter no of frames: ");
scanf("%d", &m);
for (i = 0; i < m; i++)
fr[i] = -1;

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

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--1

Enter the name of the file -- A


1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit

Enter your choice—1


Enter the name of the file -- B
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit
Enter your choice--1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--3

Enter the name of the file -- A


File A is found

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--3

Enter the name of the file -- D


File D not found

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--2

Enter the name of the file -- B


File B is deleted

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice--4

66
The Files are -- A C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice—5

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) {

printf("Enter the 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 found ", f);
break;
}
}
if (k == dir[i].fcnt) {
printf("File %s not found", f);
}
break;
}
}
if (i == dcnt) {
printf("Directory %s not found", d);
}
break;
case 5:
if (dcnt == 0) {
printf("\nNo Directory's ");
} else {
printf("\nDirectory\tFiles");
for (i = 0; i < dcnt; i++) {
printf("\n%s\t\t", dir[i].dname);
for (k = 0; k < dir[i].fcnt; k++) {
printf("\t%s", dir[i].fname[k]);
}
}
}
break;
default:
exit(0);
}
}
return 0;
}

70
Output:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice -- 1

Enter name of directory -- CSE


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 1

Enter name of directory -- ECE


Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit
Enter your choice -- 2

Enter name of the directory -- CSE


Enter name of the file -- Ram
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 2

Enter name of the directory -- ECE


Enter name of the file -- Raju
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 5

Directory Files
CSE Ram
ECE Raju

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 4

Enter name of the directory -- ECE


Enter the name of the file -- Raju
File Raju is found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 3

71
Enter name of the directory -- CSE
Enter name of the file -- Ram
File Ram is deleted File Ram not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice -- 5

Directory Files
CSE
ECE Raju

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit
Enter your choice – 6

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("Enter number of files: ");


scanf("%d", &n);

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


printf("Enter number of blocks occupied by file %d: ", i + 1);
scanf("%d", &b[i]);
printf("Enter the starting block of file %d: ", i + 1);
scanf("%d", &sb[i]);
t[i] = sb[i];

for(j = 0; j < b[i]; j++)


c[i][j] = sb[i]++;
}

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

printf("\nEnter file index (1 to %d): ", n);


scanf("%d", &x);

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

for(i = 0; i < b[x - 1]; i++)


printf("%d ", c[x - 1][i]); // Corrected to use x - 1

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

Filename Start block Length


1 5 3
2 1 2
3 8 5

Enter file index (1 to 3): 2

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;

// Removed clrscr() for compatibility


printf("Enter number of files: ");
scanf("%d", &n);

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


printf("Enter starting block and size of file %d: ", i + 1);
scanf("%d%d", &sb[i], &s[i]);
printf("Enter number of blocks occupied by file %d: ", i + 1);
scanf("%d", &m[i]);
printf("Enter blocks of file %d: ", i + 1);
for (j = 0; j < m[i]; j++) {
scanf("%d", &b[i][j]);
}
}

printf("\nFile\tIndex\tLength\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\n", i + 1, sb[i], m[i]);
}

printf("\nEnter file index (1 to %d): ", n);


scanf("%d", &x);
printf("File index is: %d\n", x);

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;

printf("\n Enter number of Files: ");


scanf("%d", &n);

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


printf("\n Enter file %d name: ", i + 1);
scanf("%s", F[i]);
printf("\n Enter number of blocks: ");
scanf("%d", &b[i]);
}

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


printf("\n Enter Starting block of file %d: ", i + 1);
scanf("%d", &sb[i]);
printf("\n Enter blocks for file %d:\n", i + 1);
for(j = 0; j < b[i]; j++) {
printf("\n Enter block %d: ", j + 1);
scanf("%d", &blocks[i][j]);
}
}

printf("\nEnter the Filename: ");


scanf("%s", S);
for(i = 0; i < n; i++) {
if(strcmp(F[i], S) == 0) {
printf("\nFname\tBsize\tStart\tBlocks\n");
printf("\n--------------------------------------\n");
printf("\n%s\t%d\t%d\t", F[i], b[i], sb[i]);
printf("%d->", sb[i]);
for(j = 0; j < b[i]; j++) {
printf("%d->", blocks[i][j]);
77
}
}
}
printf("\n-------------------------------------------\n");
return 0;
}

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

Fname Bsize Start Blocks


------------------------------------------------

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:

1. Input the number of disk requests.


2. Input the request queue (disk I/O requests).
3. Input the initial head position.
4. For each request in the queue:
5. Calculate the absolute difference between the current head position and the requested track.
6. Add it to the total head movement.
7. Update the current head position.
8. Display the total head movement.
9. End
Program:
#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, head, total_seek_time = 0;

// Step 1: Input number of requests


printf("Enter the number of disk requests: ");
scanf("%d", &n);

int request[n];

// Step 2: Input request sequence


printf("Enter the disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);
}

// Step 3: Input initial head position


printf("Enter initial head position: ");
scanf("%d", &head);

int current_position = head;

// Step 4: Print scheduling result


printf("\nFCFS Disk Scheduling:\n");
printf("Initial Head Position: %d\n", head);
printf("Head Movements:\n");

// Step 5: Process requests and show each move


for(i = 0; i < n; i++) {
79
int distance = abs(request[i] - current_position);
printf("Move from %d to %d (Seek: %d)\n", current_position, request[i], distance);
total_seek_time += distance;
current_position = request[i];
}

// Step 6: Display total seek time


printf("\nTotal Seek Time: %d\n", total_seek_time);

// Step 7: Display seek sequence with arrows


printf("Seek Sequence: %d", head);
for(i = 0; i < n; i++) {
printf(" -> %d", request[i]);
}
printf("\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

FCFS Disk Scheduling:


Initial Head Position: 50
Head Movements:
Move from 50 to 82 (Seek: 32)
Move from 82 to 170 (Seek: 88)
Move from 170 to 43 (Seek: 127)
Move from 43 to 140 (Seek: 97)
Move from 140 to 24 (Seek: 116)
Move from 24 to 16 (Seek: 8)
Move from 16 to 190 (Seek: 174)

Total Seek Time: 642


Seek Sequence: 50 -> 82 -> 170 -> 43 -> 140 -> 24 -> 16 -> 190

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.

2. Input the number of disk I/O requests.

3. Input the sequence of requests.

4. Input the initial head position.

5. Create a visited[] array initialized to false for all requests.

6. While there are unvisited requests:

7. Find the unvisited request with the minimum absolute distance from the current head position.

8. Mark this request as visited.

9. Move the head to this request.

10. Add the seek time to the total.

11. Print total seek time and the seek sequence.

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;

// Step 1: Input number of requests


printf("Enter the number of disk requests: ");
scanf("%d", &n);

int request[n], visited[n];

// Step 2: Input request sequence


printf("Enter the disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);

81
visited[i] = 0; // Initialize all requests as unvisited
}

// Step 3: Input initial head position


printf("Enter initial head position: ");
scanf("%d", &head);

int current_position = head;

printf("\nSSTF Disk Scheduling:\n");


printf("Initial Head Position: %d\n", head);
printf("Head Movements:\n");

int completed = 0;
int sequence[n + 1]; // For arrow output
sequence[0] = head;

while(completed < n) {
int min_distance = INT_MAX;
int index = -1;

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


if(!visited[i]) {
int distance = abs(request[i] - current_position);
if(distance < min_distance) {
min_distance = distance;
index = i;
}
}
}

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++;
}
}

printf("\nTotal Seek Time: %d\n", total_seek_time);

// Display seek sequence


printf("Seek Sequence: ");
for(i = 0; i <= n; i++) {
printf("%d", sequence[i]);
if(i < n) printf(" -> ");
}
printf("\n");

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

SSTF Disk Scheduling:


Initial Head Position: 50
Head Movements:
Move from 50 to 43 (Seek: 7)
Move from 43 to 24 (Seek: 19)
Move from 24 to 16 (Seek: 8)
Move from 16 to 82 (Seek: 66)
Move from 82 to 140 (Seek: 58)
Move from 140 to 170 (Seek: 30)
Move from 170 to 190 (Seek: 20)

Total Seek Time: 208


Seek Sequence: 50 -> 43 -> 24 -> 16 -> 82 -> 140 -> 170 -> 190

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.

2. Divide the requests into two groups:

3. Requests less than the head position.

4. Requests greater than or equal to the head position.

5. Service requests in the current direction (either left or right).

6. Reverse direction after reaching the disk's end (max or min) and service the remaining requests.

7. Calculate the total seek time (sum of distances moved).

8. Stop

Program:
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

int main() {
int n, i, head, disk_size, direction, total_seek_time = 0;

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

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter total disk size (maximum track number): ");


scanf("%d", &disk_size);

printf("Enter direction (0 = left, 1 = right): ");


scanf("%d", &direction);

84
// Sort the requests
qsort(request, n, sizeof(int), compare);

// Separate requests into two sides


int left[n], right[n];
int left_count = 0, right_count = 0;

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


if(request[i] < head)
left[left_count++] = request[i];
else
right[right_count++] = request[i];
}

int sequence[n + 2], seq_index = 0;


int current_position = head;

printf("\nSCAN Disk Scheduling:\n");


printf("Initial Head Position: %d\n", head);
printf("Head Movements:\n");

if(direction == 1) { // Moving right


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

// 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;
}

} else { // Moving left


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;
85
}

// 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;
}
}

// Print total seek time


printf("\nTotal Seek Time: %d\n", total_seek_time);

// Print seek sequence


printf("Seek Sequence: %d", head);
for(i = 0; i < seq_index; i++) {
printf(" -> %d", sequence[i]);
}
printf("\n");

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

SCAN Disk Scheduling:


Initial Head Position: 50
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)
Move from 199 to 43 (Seek: 156)
Move from 43 to 24 (Seek: 19)
Move from 24 to 16 (Seek: 8)

Total Seek Time: 332


Seek Sequence: 50 -> 82 -> 140 -> 170 -> 190 -> 43 -> 24 -> 16

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.

2. Input the number of disk requests.

3. Input the disk request sequence.

4. Input the initial head position.

5. Input the disk size (maximum track number).

6. Sort the request queue in ascending order.

7. Separate the requests into two parts:

8. Requests less than the current head position.

9. Requests greater than or equal to the current head position.

10. Service requests to the right (greater than or equal to the head position) first:

11. Move the head to the rightmost request.

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 compare(const void *a, const void *b) {


return (*(int*)a - *(int*)b);
}

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

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter total disk size (maximum track number): ");


scanf("%d", &disk_size);

qsort(request, n, sizeof(int), compare);

int left[n], right[n];


int left_count = 0, right_count = 0;
for (i = 0; i < n; i++) {
if (request[i] < head)
left[left_count++] = request[i];
else
right[right_count++] = request[i];
}

int current = head;


int sequence[n + 2], seq_index = 0;
sequence[seq_index++] = head;

printf("\nC-SCAN Disk Scheduling:\n");


printf("Head Movements:\n");

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


int seek = abs(current - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current, right[i], seek);
total_seek_time += seek;
current = right[i];
sequence[seq_index++] = current;
}

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

printf("Jump from %d to 0 (Seek: %d)\n", current, current);


total_seek_time += current;
current = 0;
sequence[seq_index++] = current;
89
for (i = 0; i < left_count; i++) {
int seek = abs(current - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current, left[i], seek);
total_seek_time += seek;
current = left[i];
sequence[seq_index++] = current;
}
printf("\nTotal Seek Time: %d\n", total_seek_time);
printf("Seek Sequence: ");
for (i = 0; i < seq_index; i++) {
printf("%d", sequence[i]);
if (i < seq_index - 1) printf(" -> ");
}
printf("\n");

return 0;
}

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

C-SCAN 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)
Move from 190 to 199 (Seek: 9)
Jump from 199 to 0 (Seek: 199)
Move from 0 to 16 (Seek: 16)
Move from 16 to 24 (Seek: 8)
Move from 24 to 43 (Seek: 19)

Total Seek Time: 391


Seek Sequence: 50 -> 82 -> 140 -> 170 -> 190 -> 0 -> 16 -> 24 -> 43

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.

Initial head position.

Direction (left or right).

2.Sort the request sequence in ascending order.

3.Divide the requests:

Requests less than head (left).

Requests greater than or equal to head (right).

4.Service requests in the current direction:

If right, service right-side requests, then reverse and service left.

If left, service left-side requests, then reverse and service right.

5.Calculate total seek time (sum of all movements).

6.Output total seek time and seek sequence.

7.End.

Program:
#include <stdio.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {


return (*(int *)a - *(int *)b);
}

int main() {
int n, i, head, direction, total_seek_time = 0;

printf("Enter number of disk requests: ");


scanf("%d", &n);

int request[n];
91
printf("Enter disk request sequence:\n");
for(i = 0; i < n; i++) {
scanf("%d", &request[i]);
}

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter direction (0 = left, 1 = right): ");


scanf("%d", &direction);

qsort(request, n, sizeof(int), compare);

int left[n], right[n];


int left_count = 0, right_count = 0;

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


if(request[i] < head)
left[left_count++] = request[i];
else
right[right_count++] = request[i];
}

int current = head;


int sequence[n + 1], seq_index = 0;
sequence[seq_index++] = head;

printf("\nLOOK Disk Scheduling:\n");


printf("Head Movements:\n");

if(direction == 1) { // Moving right


for(i = 0; i < right_count; i++) {
int seek = abs(current - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current, right[i], seek);
total_seek_time += seek;
current = right[i];
sequence[seq_index++] = current;
}

for(i = left_count - 1; i >= 0; i--) {


int seek = abs(current - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current, left[i], seek);
total_seek_time += seek;
current = left[i];
sequence[seq_index++] = current;
}

} else { // Moving left


for(i = left_count - 1; i >= 0; i--) {
int seek = abs(current - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current, left[i], seek);
total_seek_time += seek;
current = left[i];
92
sequence[seq_index++] = current;
}
for(i = 0; i < right_count; i++) {
int seek = abs(current - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current, right[i], seek);
total_seek_time += seek;
current = right[i];
sequence[seq_index++] = current;
}
}

printf("\nTotal Seek Time: %d\n", total_seek_time);


printf("Seek Sequence: ");
for(i = 0; i < seq_index; i++) {
printf("%d", sequence[i]);
if(i < seq_index - 1) printf(" -> ");
}
printf("\n");

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.

2. Sort the request sequence in ascending order.

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.

5. Calculate and display total seek time and seek sequence.

Program:
#include <stdio.h>
#include <stdlib.h>

int compare(const void* a, const void* b) {


return (*(int*)a - *(int*)b);
}

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

printf("Enter initial head position: ");


scanf("%d", &head);

printf("Enter direction (0 = left, 1 = right): ");


scanf("%d", &direction);

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

int current = head;


int sequence[n + 1], seq_index = 0;
sequence[seq_index++] = head;

printf("\nC-LOOK Disk Scheduling:\n");


printf("Head Movements:\n");

if(direction == 1) { // Move right first


// Step 4: Service right-side
for(i = 0; i < right_count; i++) {
int seek = abs(current - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current, right[i], seek);
total_seek += seek;
current = right[i];
sequence[seq_index++] = current;
}

// Jump to the first left request


if(left_count > 0) {
int seek = abs(current - left[0]);
printf("Jump from %d to %d (Seek: %d)\n", current, left[0], seek);
total_seek += seek;
current = left[0];
sequence[seq_index++] = current;

// Step 4 (contd): Service left-side


for(i = 1; i < left_count; i++) {
seek = abs(current - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current, left[i], seek);
total_seek += seek;
current = left[i];
sequence[seq_index++] = current;
}
}
} else { // Move left first
// Step 5: Service left-side in reverse
for(i = left_count - 1; i >= 0; i--) {
int seek = abs(current - left[i]);
printf("Move from %d to %d (Seek: %d)\n", current, left[i], seek);
total_seek += seek;
current = left[i];
sequence[seq_index++] = current;
}

// Jump to the last right request


if(right_count > 0) {
int seek = abs(current - right[right_count - 1]);
95
printf("Jump from %d to %d (Seek: %d)\n", current, right[right_count - 1], seek);
total_seek += seek;
current = right[right_count - 1];
sequence[seq_index++] = current;

// Step 5 (contd): Service right-side in reverse


for(i = right_count - 2; i >= 0; i--) {
seek = abs(current - right[i]);
printf("Move from %d to %d (Seek: %d)\n", current, right[i], seek);
total_seek += seek;
current = right[i];
sequence[seq_index++] = current;
}
}
}

// Step 6 & 7: Output results


printf("\nTotal Seek Time: %d\n", total_seek);
printf("Seek Sequence: ");
for(i = 0; i < seq_index; i++) {
printf("%d", sequence[i]);
if(i < seq_index - 1) printf(" -> ");
}
printf("\n");

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

You might also like