0% found this document useful (0 votes)
17 views101 pages

Os Final PDF - Merged

This document is a practical record for the course 'Principles of Operating System' at SNS College of Engineering for the academic year 2024-2025. It includes a list of experiments related to UNIX commands, system calls, CPU scheduling, memory allocation, and file handling, along with a certification section for student records. The document serves as a guide for students to document their practical work and performance in the course.

Uploaded by

suryacuteboy006
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)
17 views101 pages

Os Final PDF - Merged

This document is a practical record for the course 'Principles of Operating System' at SNS College of Engineering for the academic year 2024-2025. It includes a list of experiments related to UNIX commands, system calls, CPU scheduling, memory allocation, and file handling, along with a certification section for student records. The document serves as a guide for students to document their practical work and performance in the course.

Uploaded by

suryacuteboy006
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/ 101

SNS COLLEGE OF ENGINEERING

An Autonomous Institution

(Accredited by NAAC-UGC with ’A’ Grade, Approved by AICTE


&
Affiliated to Anna University)

DEPARTMENT OF INFORMATION TECHNOLOGY


Academic Year 2024-2025

23ITB203 – PRINCIPLES OF OPERATING


SYSTEM

PRACTICAL RECORD

Name : ----------------------------------
Register Number : ----------------------------------
Class : ----------------------------------
Semester : ----------------------------------
SNS COLLEGE OF ENGINEERING
(An Autonomous Institution

(Accredited by NAAC-UGC with ’A’ Grade, Approved by AICTE


&
Affiliated to Anna University)

DEPARTMENT OF INFORMATION TECHNOLOGY

23ITB203 – PRINCIPLES OF OPERATING


SYSTEM

Name : ………………………… Branch : ……………


Year : ………………………… Semester : ……………

Register Number

Certified that this is the bonafide record of work done by the above student
for the 23ITB203 – PRINCIPLES OPERATING SYSTEM during the Academic year
2024-2025.

Signature of Lab In-charge Head of the Department

Submitted for the practical examination held on……………………..

Internal Examiner External Examiner


INDEX

Ex.No Date Name of Experiments P.No Marks Signature

1 Implementation of Unix Command

2 System Calls

3(a) Shell Programming Comparison Of Two Strings

3(b) Shell Programming Maximum Of Three Numbers

3(c) Shell Programming Arithmetic Operation Using


case

4(a) CPU Scheduling - FCFC Scheduling

4(b) CPU Scheduling- SJF Scheduling

4(c) CPU Scheduling – Priority Scheduling

4(d) CPU Scheduling- Round Robin Scheduling

5(a) Semaphore

6(a) Deadlock Detection

7(a) Memory Allocation- First Fit

7(b) Memory Allocation- Worst Fit

7(c) Memory Allocation- Best Fit

8(a) Page Replacement Algorithm- FIFO

8(b) Page Replacement Algorithm- LRU

8(c) Page Replacement Algorithm- OPTIMAL(LFU)

9(a) File Allocation- Sequential


9(a) File Allocation- Indexed

9(b) File Allocation- Linked

10(a) Disk Scheduling Algorithm- FCFS

10(b) Disk Scheduling Algorithm- C SCAN

10(c) Disk Scheduling Algorithm- SSTF

SNS College of Engineering


Department of Information Technology

Average Performance 4

Average Record 4

Average Viva 2

Total 10
Ex. No : 1
UNIX COMMANDS
Date:

AIM
To study and understand the UNIX general purpose, directory, file handling and
input output redirection commands.

UNIX COMMAND CATEGORIES

1. General purpose commands


2. Directory handling commands
3. File handling commands
4. Input and Output Redirection

GENERAL PURPOSE COMMANDS


1. The Date Command
The data command is used to display the current date with day of
week, month, day, time (24Hours clock) and the year.

Syntax: $ date

Format Purpose Example


+ %m To display only month $ date +%m
+%h To display month name $ date +%h
+%d To display day of month $ date +%d
+%y To display last two digits of the year $ date +%y
+%D To display date as MM/DD/YY $ date +%D
+%a To display abbreviated weekday $ date +%a
+%j To display Day of the year $ date +%j
+%w To display the day of the year $ date +%w
+%T To display time as HH:MM:SS $ date +%T
+%r To display the time as HH:MM:SS (A.M. / P.M) $ date +%r
+%H To display Hours $ date +%H
+%M To display Minutes $ date +%M
+%S To display Seconds $ date +%S

1
2. The Echo And Banner Commands
The echo command is used to print the message on the screen, whatever you happen to
type on the line.

Syntax: $ echo text


Eg: $ echo learning UNIX is fun
Learning UNIX is fun.
The banner command prints your message in large letters to give the impression of the banner.
Syntax: $ banner text
3. The Unix Calendar : Cal
The calendar (cal) command helps us to keep track of our day’s .i.e., it displays the specified
month or year calendar.

Syntax: $ cal month or year


$ Cal 2004 prints the calendar for the entire year
$ Cal 9 2004 prints the calendar for the month of September 2004.
4. man command.
It is a help command. It prints the manual page of a particular command on our terminal.
Syntax
$ man [command-Name]
Example $ man pwd.
5. Clearing the screen
As a beginner, most of the time, you may get error message. You screen may appear clumsy
and you want to start a fresh. To clear your screen, do the following.

Syntax: $ clear
This command clear the screen and places a ‘$’ prompt at the top left corner of the screen.
6. tput command
It is used for manipulation the screen of the terminal. Syntax
$ tput [option]

S.No Options Purpose


1 Clear Cleans the screen
2 longname Displays the complete name of the terminal

2
3 Smso Displays the text typed in bold face (including the system prompt)
and reverse the background color of the screen in while and
foreground with black.

4 Rmso Displays the text in normal format

5 Init Initialize the terminal

6 Reset Resets the terminal

7 cup RC Used to position the cursor at row number ‘R’ and column
number ‘C’.

8 Cols Displays the number of columns for the current terminal.

9 Blink Make the cursor to blink.

1. The Who Command


The who command is more powerful and used to displays data about all the
users, who are currently logged into the system.

Syntax: $ who
The –h option in who command displays the column header.
2. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.

Eg: $ who am i
3. The tty command
The tty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
4. Change the password : passwd
If you account still does not have a password, or has that already known to
others. Thepassword can be changed by using the passwd command.

Syntax: $passwd
3
5. read command
It is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a

6. The Who Command


The who command is more powerful and used to displays data about all the
users, who are currently logged into the system.

Syntax: $ who
The –h option in whocommand displays the column header.
7. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.

Eg: $ who am i
8. The tty command
The tty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
9. Change the password : passwd
If you account still does not have a password, or has that already known to others.
Thepassword can be changed by using the passwd command.

Syntax: $passwd
10. read command
It is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a

4
11. The Who Command
The who command is more powerful and used to displays data about all the
users, who are currently logged into the system.
Syntax: $ who
The –h option in whocommand displays the column header.
12. The who am i command
The who command with ‘am’ and ‘i’ displays the single line of the output pertaining the
login details of the user.
Eg: $ who am i
13. The tty command
Thetty (teletype) command is used to know the terminal name that we are using
Syntax: $ tty
14. Change the password : passwd
If you account still does not have a password, or has that already known to others. The
password can be changed by using the passwd command.
Syntax: $passwd
15. read command
it is used to get an input from standard input device.
Syntax:
read variable_name.
Example:
read a

5
DIRECTORY HANDLING COMMANDS
1. Current working directory :pwd
Each user, those who has logged in to the system is always provided with a working directory
of his own, called Current Working Directory.

The pwd command is provided to know the current working directory, pwd is an abbreviation
for “print working directory”.

Syntax: $ pwd

2. Create a directory :mkdir


The mkdir is used to create an empty directory in a disk.
Sysntax: $ mkdirdirname
3. Removing a directory :rmdir
The rmdir is used to remove a directory from the disk. Before removing a directory, the
directory must be empty . i.e., The directory which has going to remove must be empty i.e; it
has no files and other directories.

Syntax: $ rmdir dirname


4. Changing the working directory : cd
We have seen how to create a directory. The cd command is used to move from one directory
to another.

$ cd program $ cd ..
5. Listing files and directories : ls
To view the contents of a directory the ls command is used
Syntax: $ ls

S.no Option Purpose

List files in the long format. The files are displayed along with
1. -l their mode, number of links, owner of the file, file size,
modification date and time and file name.
2. -t List in order of last modification time (most recent first).
3. -a List all entries, including the hidden files.
4. -d List directory file instead of its contents.
5. -p Puts a slash after each directory.
6. -u Lists in order of the last access time.
7. -x It produces a multi-columnar output of filenames.

6
It identifies the directories with/ and executable files with
8. -F
symbols.

9. -r Sorts file in reverse order of alphabets.


10. -R Recursively lists all files in Sub-directories
11. -i Shows node numbers of a file
12. -c Sorts by time of change of the i-node
13. -s Displays number of the blocks used by a file.

7
FILE HANDLING COMMANDS
1. The cat command
The cat command is used to create a file
Syntax: $cat>filename
Eg: $cat>test
After finished typing press “ctrl-d” to save and end the file. In Unix the “ctrl-d” keys
indicate the “End of File” (EOF) character. Now a file is created with the name ‘test’ in the
disk.

2. Display contents of a file


The cat command is also used to view the contents of a specified file.
Syntax: $ cat filename

Option Use
-v Used to display non-printing characters
-n Used for numbering the lines

3. Copying a file : cp

The cp command is used to copy the content of one file to another and copies the file from
one place to another.

Syntax: $ cp old_filenew_file
Eg: $ cp application.a application
When copying files from or to the different directories, you can specify their name along
with their path as shown below.

$ cp/usr/user1/one.c /usr/user1/program/one.c

.Option Use
-i Used to warm the user before overwriting the
destination file
-R Used to copy an entire directory structure.

8
4. Removing a file : rm
The rm command is used to remove or erase an exiting file.
Syntax: $ rm filename

Option Use
-i Used to ask the user for confirmation before
removing each file

-r (or) -R Used to search and delete all the files and


suibdirectories.

-f Used to prompt for removal if a file is write


protected.

5. Moving a file : mv

The mv command is used to move a file from one place to another, it removes a
specifiedfile from its original location and places it specified location.

Syntax: $ mv old_filenamenew_filename
6. Counting number of words in a file :wc

The wc command is used to count the number of words, lines and characters in a file.
Syntax: $wc filename
7. File permissions

File permission is the way of controlling the accessibility of the file for each of three namely
Users, Groups and Others.

There are three types of files permissions are available, they are
r – read
w – write
x – execute
The permission for each file can be divided into three parts of three bits each.

First three bits Owner of the file


Next three bits Group to which owner of the file belongs
Last three bits Others

9
Let us take the myfile and analyze it bit by bit.
$ ls myfile
-rwxr-xr- 1 lak std 1525 may 30 12:15 myfile
-rwx Contain all three permissions and a hyphen (-) indicates ordinary file. This File is
readable , writable and executable by the owner of the file.
lak specifies owner of the file
r-x Indicates the absence of the write permission by the groupowner of the

file.std Is the group owner of the file

r-- Indicates read permission for others.i.e, Those who are neither the owner of muni, nor
belongs to the std group.

8. Changing the file permissions :chmod


The chmod command is used to set three permissions for categories of users for a file(s).
Syntax: $ chmod category operation permission file(s)
Where
Category Is the user type
Operation Is used to aasignare remove permission
Permission Is the type of permission
File(s) Are the files to assign are remove permission.
The below table illustrating these

Category Operation Permission

u - Users + Assign r - read


g – group - Remove W - write
o - others = Assign absolutely x - execute
a - all

Eg:$ chmod +x exa.c


Octal notations: The file permissions can be changed using octal notation also. The
octal notations for file permission are

Read permission 4
Write permission 42
Execute permission 1

10
Eg: $ chmod 761 exa.c
Assign all permissions to the owner, read and write permissions to the group, and only
executable permission to the others for exa.cfile.

9. ln – link command
This command is to establish an additional filename for the same ordinary
file.
Syntax:
$ ln firstname second name.
10. find command
This command is used to locate files in a directory and in a subdirectory.
Example:
To find the path of all files.
$ find . –print

11
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the Basic UNIX commands was executed and verified successfully.

12
Ex.No:2
Date: UNIX commands and system calls

AIM:

To understand and execute basic UNIX commands and system calls including fork,
exec, getpid, exit, wait, close, stat, opendir, readdir, open, read, and write to learn how they
work in managing processes, files, and directories in the UNIX operating system.

ALGORITHM:

1. Fork a new process: Use fork() to create a child process.


2. Execute command in child: Use execlp() in the child process to run ls.
3. Get Process ID: Print the process IDs using getpid() for both parent and child.
4. Wait in parent: Use wait() to let the parent wait for the child to complete
5. Open and write to a file: Use open(), write(), read(), and close() to manage file I/O.
6. Get file information: Use stat() to print file size and permissions.
7. Read directory: Use opendir() and readdir() to list directory contents.
8. Exit: Terminate the program using exit().

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include

<sys/types.h>

#include

<sys/wait.h>

#include <sys/stat.h>

#include <dirent.h>

#include <fcntl.h>

#include <string.h>
13
int main() {
pid_t

pid; int

status;

char

buffer[1024];

ssize_t n;

// Demonstrate fork, exec, getpid, exit, and

wait pid = fork();

if (pid < 0) {

perror("Fork failed");

exit(EXIT_FAILUR

E);

} else if (pid == 0) {

// Child process

printf("Child Process ID: %d\n", getpid());

char *args[] = {"/bin/ls", "-l", NULL}; // ls command

execvp(args[0], args); // Replace child process with ls

command perror("Exec failed"); // exec only returns on

error exit(EXIT_FAILURE);

} else {

// Parent process

wait(&status); // Wait for child to finish

printf("Child process finished with status %d\n", WEXITSTATUS(status));

}
14
// Demonstrate open, read, write, close

int fd = open("sample.txt", O_CREAT | O_RDWR,

0666); if (fd == -1) {

perror("Open failed");

exit(EXIT_FAILURE);

// Write to the file

char *text = "Hello, this is a sample text written to the

file.\n"; if (write(fd, text, strlen(text)) == -1) {

perror("Write

failed"); close(fd);

exit(EXIT_FAILUR

E);

// Read the file

lseek(fd, 0, SEEK_SET); // Move the file pointer to the

beginningn = read(fd, buffer, sizeof(buffer) - 1);

if (n == -1) {

perror("Read

failed"); close(fd);

exit(EXIT_FAILUR

E);

15
buffer[n] = '\0'; // Null-terminate the read

dataprintf("File contents:\n%s\n", buffer);

close(fd); // Close the file

// Demonstrate

stat struct stat

fileStat;

if (stat("sample.txt", &fileStat) <

0) {perror("Stat failed");

exit(EXIT_FAILURE);

printf("File size: %ld bytes\n", fileStat.st_size);

// Demonstrate opendir and

readdir DIR *dir;

struct dirent *entry;

if ((dir = opendir(".")) ==

NULL) {perror("Opendir

failed"); exit(EXIT_FAILURE);

printf("Directory contents:\n");

while ((entry = readdir(dir)) !=

16
NULL) {printf("%s\n", entry-

>d_name);

closedir(dir);

printf("Program

completed.\n"); return 0;

17
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:

Thus the UNIX commands and system calls including fork, exec, getpid, exit, wait,
close, stat, opendir, readdir, open, read, and write successfully executed.

18
Ex. No : 3 a SHELL PROGRAMMING
Date: COMPARISON OF TWO STRINGS

AIM:
To write a shell program to compare the two strings.

ALGORITHM:
Step1: Enter into the vi editor and goto the insert mode for entering the code
Step2: Read the first string.
Step3: Read the second string.
Step4: Compare the two strings using the if loop
Step5: If the condition satisfied then print that two strings are equal else print two strings are
not equal.
Step6: Enter into the escape mode for the execution of the result and verify the output.

PROGRAM:

echo "Enter the first string"


read str1

echo "Enter the second string"


read str2

if [ "$str1" = "$str2" ]; then


echo "Strings are equal"
else
echo "Strings are unequal"
fi

19
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT
Thus, the shell program are executed and output is verified successfully.

20
Ex. No : 3 b
MAXIMUM OF THREE NUMBERS
Date:

Aim:
To write a shell program to find greatest of three numbers.

ALGORITHM:
Step1: Declare the three variables.

Step2: Check if A is greater than B and C.

Step3:If so print A is greater.

Step4: Else check if B is greater than C.


Step5:If so print B is greater.

Step6: Else print C is greater.

PROGRAM:

echo "Enter A"


read a

echo "Enter B"


read b

echo "Enter C"


read c

if [ "$a" -gt "$b" ] &&[ "$a" -gt "$c" ]; then


echo "A is greater"
elif [ "$b" -gt "$a" ] && [ "$b" -gt "$c" ]; then
echo "B is greater"
else
echo "C is greater"
fi

21
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT
Thus, the shell program are executed and output is verified successfully.

22
.
Ex. No : 3 c
ARITHMETIC OPERATIONS USING
Date:
CASE

AIM:
To write a shell program to perform the arithmetic operations using case

ALGORITHM:
Step 1 : Read the input variables and assign the value
Step 2 : Print the various arithmetic operations which we are going to perform
Step 3 : Using the case operator assign the various functions for the arithmetic operators.
Step 4 : Check the values for all the corresponding operations.
Step 5 :Print the result and stop the execution

PROGRAM:

echo "1. Addition"


echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "Enter your choice"
read a

echo "Enter the value of b"


read b

echo "Enter the value of c"


read c

echo "b is $b, c is $c"

case $a in
1) d=expr $b + $c
echo "The sum is $d"
;;
2) d=expr $b - $c
echo "The difference is $d"
;;
3) d=expr $b \* $c
echo "The product is $d"
;;
23
4) if [ "$c" -ne 0 ]; then
d=expr $b / $c
echo "The quotient is $d"
else
echo "Division by zero is not allowed"
fi
;;
*) echo "Invalid choice"
;;
esac

24
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT
Thus, the shell program are executed and output is verified successfully.

25
Ex. No : 4 a
FCFS SCHEDULING
Date:

AIM:

Write a C program to implement the various process scheduling mechanisms suchas FCFS

scheduling.

ALGORITHM:

Step1: Start the process.

Step2: Accept the number of processes in the ready Queue.

Step3: For each process in the ready Q, assign the process id and accept theCPU burst time.

Step4: Set the waiting of the first process as „0‟andits burst time as its turnaround time.
Step5: For each process in the Ready Q calculate

(a) Waiting time for process(n)=waiting time of process(n-1)+ Burst timeof


process(n-1)
(b)TurnaroundtimeforProcess(n)=waitingtimeofProcess(n)+Bursttimefor process(n)
Step6: Calculate

(c) Average waiting time=Total waiting Time/Number of process

(d) Average Turn around time=Total Turn around Time/Number

ofprocess Time/Number ofprocess

Step7: Stop the process

26
PROGRAM:

#include <stdio.h>int
main()
{
int i, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10];
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
p[i] = i + 1; // Assign process ID (p[i] = i+1)
}
printf("\n\nGantt Chart:\n");
printf(" |");
for (i = 0; i < n; i++) {
printf(" P%d |", p[i]);
}
printf("\n");
printf("0");
for (i = 0; i < n; i++)
{
sum += t[i]; printf("
%d", sum);
}

27
sum = 0;
printf("\n\nProcess ID\tWaiting Time\tTurn Around Time\n");

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


{
wt = sum;
tat = sum + t[i];
sum += t[i]; twt
+= wt;
ttat += tat;
printf("%d\t\t%d\t\t%d\n", p[i], wt, tat);
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\nAverage Waiting Time: %.2f", awt); printf("\nAverage
Turnaround Time: %.2f\n", atat);

return 0;
}

28
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:

Thus the program to perform various CPU Scheduling algorithms areexecuted and
output is verified successfully.

29
Ex. No : 4 b
SJF SCHEDULING
Date:

AIM :
To Write a C program to implement the various process scheduling mechanismssuch as SJF
Scheduling.

ALGORITHM :

Step1: 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 theCPU burst time.
Step 4: Start the Ready according the shortest Burst time by sorting according to lowestto highest burst
time.

Step5: Set the waiting time of the first process as „0‟ and its turnaround time as itsburst time.

Step6: For each process in the ready queue, calculate

(a) Waiting time for process(n)=waiting time of process(n-1)+Burst


timeof process(n-1)

(b) Turn around time f or Process(n)=waiting time of


Process(n)+Burst time forprocess(n)

Step6: Calculate

(c) Average waiting time = Total waiting Time/Number ofprocess

(d) (d)AverageTurn around time=Total Turn around


Time/Number of process
Step7: Stop the process

30
PROGRAM:

#include <stdio.h>int
main()
{
int i, j, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10], temp;
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
p[i] = i + 1;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (t[i] > t[j])
{
temp = t[i];
t[i] = t[j]; t[j]
= temp; temp
= p[i];p[i] =
p[j]; p[j] =
temp;
}
}
}
printf("\n\nGantt Chart:\n");
printf(" |");

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

printf(" P%d |", p[i]);


}
printf("\n");
printf("0");
sum = 0;
for (i = 0; i < n; i++)
{

sum += t[i]; printf("


%d", sum);
}
sum = 0;
printf("\n\nProcess ID\tWaiting Time\tTurn Around Time\n");for (i = 0;
i < n; i++)
{
wt = sum;
tat = sum + t[i];
sum += t[i]; twt
+= wt;
ttat += tat;
printf("%d\t\t%d\t\t%d\n", p[i], wt, tat);
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\nAverage Waiting Time: %.2f", awt);
printf("\nAverage Turnaround Time: %.2f\n", atat);
return 0;
}

32
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:

Thus the program to perform various CPU Scheduling algorithms areexecuted and
output is verified successfully.

33
Ex. No : 4 c PRIORITY SCHEDULING

Date:

AIM

Write a C program to implement the various process scheduling mechanisms suchas Priority
Scheduling.

ALGORITHM

Step1: Start the process.

Step2: Accept the number of processes in the ready Queue.

Step3: For each process in the ready Q, assign the process id and accept theCPU burst time.

Step4: Sort the ready queue according to the priority number.


Step5 : Set the waiting of the first process as „0‟and its burst time as its turn aroundtime.

Step6: For each process in the Ready Q calculate Waiting time for process(n) =waiting time
ofprocess(n-1) +Burst time of process(n-1)

TurnaroundtimeforProcess(n)=waitingtimeofProcess(n)+Bursttimefor process(n)
Calculate Average waiting time=Total waiting Time/Number of process.
AverageTurn around time = Total Turn around Time/Number of process.
Step7: Stop the process.

34
PROGRAM:

#include <stdio.h>
int main()
{
int i, j, n, sum = 0, wt, tat, twt = 0, ttat = 0;int
t[10], p[10], pr[10], temp;
float awt, atat;
printf("Enter number of processes:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
printf("Enter the Priority of process %d (lower number = higher priority): ", i + 1);scanf("%d",
&pr[i]);
p[i] = i + 1;
}
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (pr[i] > pr[j])
{
temp = pr[i];
pr[i] = pr[j];
pr[j] = temp;
temp = t[i]; t[i]
= t[j];

35
t[j] = temp; temp = p[i];
p[i] = p[j]; p[j] = temp;
}
}
}
printf("\n\nGantt Chart:\n");
printf(" |");
sum = 0;
for (i = 0; i < n; i++) {
printf(" P%d |", p[i]);
}
printf("\n");

printf("0");
sum = 0;
for (i = 0; i < n; i++) {sum
+= t[i]; printf(" %d",
sum);
}
sum = 0;
printf("\n\nProcess ID\tPriority\tWaiting Time\tTurn Around Time\n");

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


{
wt = sum;
tat = sum + t[i];
sum += t[i]; twt
+= wt;

36
ttat += tat;
printf("%d\t\t%d\t\t%d\t\t%d\n", p[i], pr[i], wt, tat);
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\nAverage Waiting Time: %.2f", awt); printf("\nAverage
Turnaround Time: %.2f\n", atat);

return 0;
}

37
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the program to perform various CPU Scheduling algorithms are executed andoutput is verified
successfully.

38
Ex. No : 4d ROUND ROBIN SCHEDULING

Date:

AIM:

Write a C program to implement the various process scheduling mechanisms suchas Round
Robin Scheduling.

ALGORITHM:

Step1: Start the process

Step2: Accept the number of processes in the ready Queue and time
quantum(or)time slice

Step3: For each process in the ready Q, assign the process id and accept theCPU burst time

Step4: Calculate the no. of time slices for each process where
No. of time slice for process(n)=burst time process(n)/time slice

Step5: If the burst time is less than the time slice then the no. of time slices =1.

Step6: Consider the ready queue is a circular Q,calculate

(a) Waiting time for process(n) = waiting time of process(n-1)+burst time


ofprocess(n-1)+the time difference in getting the CPU from
process(n-1)
(b) Turnaroundtimeforprocess(n)=waitingtimeofprocess(n)+b
ursttimeof process(n)+the time difference in getting CPU from
process(n).
Step7: Calculate

(a) Average waiting time=Total waiting Time/Number of process

39
(b) AverageTurn around time=Total Turn around Time/Numberof process
Step8: Stop the process.

PROGRAM:

#include <stdio.h>int

main()
{
int time = 0;
int complete = 0;
int i, n, tq, sum = 0, count = 0, t[10], remaining_time[10], p[10], wt[10], tat[10];int total_wt =
0, total_tat = 0;
float avg_wt, avg_tat;
printf("Enter number of processes: ");
scanf("%d", &n);
printf("Enter Time Quantum: ");
scanf("%d", &tq);
for (i = 0; i < n; i++)
{
printf("Enter the Burst Time of process %d: ", i + 1);
scanf("%d", &t[i]);
remaining_time[i] = t[i];p[i]
= i + 1;
}
printf("\n\nGantt Chart:\n");
while (complete < n)
{
for (i = 0; i < n; i++)

40
{
if (remaining_time[i] > 0)
{
if (remaining_time[i] <= tq)
{
time += remaining_time[i];
remaining_time[i] = 0; wt[i] =
time - t[i];
tat[i] = time;
total_wt += wt[i];
total_tat += tat[i];
printf(" | P%d |", p[i]);complete++;
}
else
{
time += tq;
remaining_time[i] -= tq;
printf(" | P%d |", p[i]);
}
}
}
}
printf("\n0");
for (i = 0; i < complete; i++)
{
printf(" %d", tat[i]);
}
printf("\n\nProcess ID\tBurst Time\tWaiting Time\tTurn Around Time\n");for (i = 0; i
< n; i++)

41
{
printf("%d\t\t%d\t\t%d\t\t%d\n", p[i], t[i], wt[i], tat[i]);
}
avg_wt = (float)total_wt / n;
avg_tat = (float)total_tat / n;
printf("\nAverage Waiting Time: %.2f", avg_wt);
printf("\nAverage Turnaround Time: %.2f\n", avg_tat);

return 0;
}

42
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the program to perform various CPU Scheduling algorithms are executed and output is verified
successfully.

43
Ex. No : 5
SEMAPHORES
Date:

AIM:
To implement producer/consumer problem using semaphore.

ALGORITHM:
1 Declare variable for producer &consumer asp thread-t-tid produced idconsume.
2 Declare a structure to add items, semaphore variable set as struct.
3 Read number of items to be produced and consumed.
4 Declare and define semaphore function for creation and destroy.
5 Define producer function.
6 Define consumer function.
7 Call producer and consumer.
8 Stop the execution.

PROGRAM:

#include<stdio.h>void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;in = 0;
out = 0;
bufsize = 10;
while (choice !=3)
{
printf("\n1. Produce \t 2. Consume \t3. Exit");printf("\nEnter your
choice: ");
scanf("%d", &choice);switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\nBuffer is Full");else
{

44
printf("\nEnter the value: ");scanf("%d",
&produce); buffer[in] = produce;

in = (in+1)%bufsize;
}
break; case 2:
if(in == out) printf("\nBuffer is Empty");else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);out = (out+1)%bufsize;
}
break;
}
}
}

45
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT
Thus the producer consumer program was executed and verified successfully.

46
Ex. No : 6
DEADLOCK DETECTION
Date:

AIM:

To Implement Dead Lock Detection Algorithm using C.

ALGORITHM:
Step 1: Start the program.
Step 2. Get the values of resources and
processes.

Step 3. Get the avail value.

Step 4. After allocation find the needvalue.


Step 5. Check whether its possible toallocate.
Step 6. If it is possible then the system is insafe state.
Step 7. Else system is not in safety state.

Step 8. If the new request comes then check that the system is in safety.

Step 9. Or not if we allow the


request.

Step 10. Stop the program.

47
PROGRAM:

# include <stdio.h>void
main()
{
int found, flag, l, p[4][5], tp, c[4][5], i, j, k = 1, m[5], r[5], a[5], temp[5],sum = 0;
printf("Enter total number of processes: ");scanf("%d",
&tp);
printf("Enter claim matrix:\n");for (i = 1;
i <= 4; i++)
{
for (j = 1; j <= 5; j++)
{
scanf("%d", &c[i][j]);
}
}
printf("Enter allocation matrix:\n");for (i = 1; i
<= 4; i++)
{
for (j = 1; j <= 5; j++)
{
scanf("%d", &p[i][j]);

48
}
}
printf("Enter resource vector:\n");for (i = 1; i
<= 5; i++)
{
scanf("%d", &r[i]);
}
printf("Enter availability vector:\n");for (i = 1; i
<= 5; i++)
{
scanf("%d", &a[i]);
temp[i] = a[i];
}
for (i = 1; i <= 4; i++)
{
sum = 0;
for (j = 1; j <= 5; j++)
{
sum += p[i][j];
}
if (sum == 0)
{

49
m[k] = i;
k++;
}
}
for (i = 1; i <= 4; i++)
{
for (l = 1; l < k; l++)
{
if (i != m[l])
{
flag = 1;
for (j = 1; j <= 5; j++)
{
if (c[i][j] > temp[j])
{
flag = 0;
break;
}
}
}
if (flag == 1)
{

50
m[k] = i;
k++;
for (j = 1; j <= 5; j++)
{
temp[j] += p[i][j];
}
}
}
}
printf("Deadlock causing processes are: ");for (j = 1; j <=
tp; j++)
{
found = 0;
for (i = 1; i < k; i++)
{
if (j == m[i])
found = 1;
}
if (found == 0)
printf("%d\t", j);
}
}

51
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above deadlock detection program was executed and verifiedsuccessfully.

52
Ex. No : 7 A
FIRST FIT
Date:

AIM:
To write a C program for implementation memory allocation methods for fixed partition using first fit.

ALGORITHM:

Step 1:Define the max as 25.

Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,bf[max],ff[max].

Step 3: Get the number of blocks,files,size of the blocks using for loop.

Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]

Step 5: Check highest<temp,if so assign ff[i]=j,highest=temp

Step 6: Assign frag[i]=highest, bf[ff[i]]=1,highest=0 Step 7: Repeat step 4 to


step 6.

Step 8: Print file no,size,block no,sizeandfragment.

Step 9: Stop the program.

PROGRAM:

#include <stdio.h>
#define max 25 void
main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp; static int bf[max], ff[max];printf("\n\tMemory
Management Scheme - First Fit\n");
printf("Enter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");
scanf("%d", &nf);

53
printf("\nEnter the size of the blocks:\n");for (i = 1; i
<= nb; i++)
{
printf("Block %d: ", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
1; i <= nf; i++)
{
printf("File %d: ", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)

{
if (bf[j] != 1)
{
temp = b[j] - f[i];if
(temp >= 0)
{
ff[i] = j; break;
}
}
}
frag[i] = temp; bf[ff[i]] = 1;
}
printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment\n"); for (i = 1; i <= nf;
i++)
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i, f[i], ff[i], b[ff[i]], frag[i]);
}
54
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully.

55
Ex. No : 7 b
WORST FIT
Date:

AIM:
To write a C program for implementation memory allocationmethods forfixed
partition using worst fit.

ALGORITHM:
Step 1:Define the max as 25.
Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp, highest=0,
bf[max],ff[max].

Step 3: Get the number of blocks,files,size of the blocksusing for loop.

Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]


Step 5: Check temp>=0,if so assign ff[i]=jbreak the
forloop.

Step 6: Assign frag[i]=temp,bf[ff[i]]=1;

Step 7: Repeat step 4 to step 6.


Step 8: Print file no,size,block
no,size andfragment.
Step 9: Stop the program.

PROGRAM:

#include <stdio.h>
#define MAX 25 int
main()
{
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, highest; static int
bf[MAX] = {0}, ff[MAX] = {0}; printf("\n\tMemory Management
Scheme - Worst Fit\n");printf("Enter the number of blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");scanf("%d",
&nf);
56
printf("\nEnter the size of the blocks:\n");for (i = 0; i
< nb; i++)
{
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
0; i < nf; i++)
{
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}

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


{
highest = -1;
for (j = 0; j < nb; j++)
{

if (bf[j] == 0)
{
temp = b[j] - f[i];
if (temp >= 0 && temp > highest)
{
ff[i] = j;
highest = temp;
}
}
}
if (highest != -1)
{
frag[i] = highest;

57
bf[ff[i]] = 1;
}
else
{
frag[i] = -1;
}
}

printf("\nFile No:\tFile Size:\tBlock No:\tBlock Size:\tFragment\n");for (i = 0; i < nf;


i++)
{
if (frag[i] != -1)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, f[i], ff[i] + 1, b[ff[i]], frag[i]);
}
else
{
printf("%d\t\t%d\t\tNot Allocated\n", i + 1, f[i]);

}
}
return 0;
}

58
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully.

59
Ex. No : 7 c
BEST FIT
Date:

AIM:
To write a C program for implementation memory
allocation methods for fixed partition using best fit.

ALGORITHM:

Step 1:Define the max as 25.


Step 2: Declare the variable frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0,
bf[max],ff[max].

Step 3: Get the number of blocks,files,size of the blocksusing for

loop.

Step 4: In for loop check bf[j]!=1, if so temp=b[j]-f[i]


Step 5: Check lowest>temp,if so assign ff[i]=j,highest=temp

Step 6: Assign frag[i]=lowest,


bf[ff[i]]=1,lowest=10000

Step 7: Repeat step 4 to step 6.


Step 8: Print file no,size,block no,size and
fragment.
Step 9: Stop the program.

60
PROGRAM:

#include <stdio.h>
#define MAX 25 int
main()
{
int frag[MAX], b[MAX], f[MAX], i, j, nb, nf, temp, lowest;static
int bf[MAX] = {0}, ff[MAX] = {0}; printf("\nMemory
Management Scheme - Best Fit\n");printf("Enter the number of
blocks: ");
scanf("%d", &nb);
printf("Enter the number of files: ");scanf("%d",
&nf);
printf("\nEnter the size of the blocks:\n");for (i = 0; i
< nb; i++)
{
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");for (i =
0; i < nf; i++)
{
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}

61
for (i = 0; i < nf; i++)
{
lowest = 10000;
ff[i] = -1;

for (j = 0; j < nb; j++)


{
if (bf[j] == 0)
{
temp = b[j] - f[i];
if (temp >= 0 && temp < lowest)
{
ff[i] = j;
lowest = temp;
}
}
}

if (ff[i] != -1)
{
frag[i] = lowest;
bf[ff[i]] = 1;
}
else
{
frag[i] = -1;

62
}
}

printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragment\n");for (i = 0; i < nf;


i++)
{
if (frag[i] != -1)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, f[i], ff[i] + 1, b[ff[i]], frag[i]);
} else {
printf("%d\t\t%d\t\tNot Allocated\n", i + 1, f[i]);
}
}

return 0;
}

63
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully.

64
Ex. No : 8 a
Date: FIFO PAGE REPLACEMENT
ALGORITHM

AIM:
To implement page replacement algorithms FIFO (First In First Out)

ALGORITHM:
Step1: Create a queue to hold all pages in memory. Step2: When the
page is required replace the page at thehead of the queue.

Step3: Now the new page is inserted at the tail of the queue.

PROGRAM:

#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int main()
{
printf("\n\t\t\tFIFO PAGE REPLACEMENT ALGORITHM");
printf("\nEnter no. of frames: ");
scanf("%d", &nof);
printf("Enter number of reference strings: ");
scanf("%d", &nor);
printf("\nEnter the reference string: ");for (i = 0;
i < nor; i++)
{
scanf("%d", &ref[i]);
}
printf("\nThe given reference string:");for (i = 0;
i < nor; i++)
{
printf("%4d", ref[i]);
}
for (i = 0; i < nof; i++)

65
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully.

66
Ex. No : 8 b
LRU PAGE REPLACEMENT ALGORITHM
Date:

AIM:
To implement page replacement algorithm LRU(Least Recently Used)

ALGORITHM:
Step1: Create a queue to hold all pages in memory. Step2: When the
page is required replace the page at thehead of the queue.

Step3: Now the new page is inserted at the tail of the queueStep4: Create
a stack.

Step5: When the page fault occurs replace page present at the bottom of the stack.

PROGRAM:

#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int recent[50],
lrucal[50], count = 0;
int lru_victim();int
main()

{
printf("\n\t\t\tLRU PAGE REPLACEMENT ALGORITHM");printf("\nEnter no.
of Frames: ");
scanf("%d", &nof);
printf("Enter no. of reference strings: ");scanf("%d",
&nor);
printf("\nEnter reference string: ");for (i = 0; i
< nor; i++)
{
scanf("%d", &ref[i]);
}
67
printf("\n\n\t\tLRU PAGE REPLACEMENT ALGORITHM ");printf("\n\tThe given
reference string:\n\t");

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


{
printf("%4d", ref[i]);
}
for (i = 0; i < nof; i++)
{

frm[i] = -1;
lrucal[i] = 0;
}
for (i = 0; i < 50; i++)
{
recent[i] = -1;
}
printf("\n");
for (i = 0; i < nor; i++)
{
flag = 0;
printf("\n\tReference NO %d ->\t", ref[i]);for (j = 0; j <
nof; j++)

if (frm[j] == ref[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{

68
count++;
if (count <= nof)
{
victim++;

}
else
{
victim = lru_victim();
}
pf++;
frm[victim] = ref[i]; for (j =
0; j < nof; j++)

{
printf("%4d", frm[j]);
}
}
recent[ref[i]] = i;
}
printf("\n\n\tNo. of page faults: %d\n", pf);return 0;

int lru_victim()
{
int i, lru_index = -1, lru_time = 1000;for (i = 0; i
< nof; i++) {

if (frm[i] != -1 && lrucal[i] < lru_time)


{
lru_time = lrucal[i];
lru_index = i;

69
}
}

return lru_index;
}

70
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully.

71
Ex. No : 8 c
Date: OPTIMAL(LFU)PAGE REPLACEMENT
ALGORITHM

AIM:
To implement Optimal (LFU) page replacement algorithms.

ALGORITHM:
Step1: Create a array
Step2: When the page fault occurs replace page that will not be used forthe longest periodof
time.

PROGRAM:

#include <stdio.h>
int i, j, nof, nor, flag = 0, ref[50], frm[50], pf = 0, victim = -1;int
optcal[50], count = 0;
int optvictim(int index);int
main()
{
printf("\nOPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\nEnter the number of frames: ");
scanf("%d", &nof);
printf("Enter the number of reference strings: ");scanf("%d",
&nor);
printf("Enter the reference string: ");for (i =
0; i < nor; i++)
{
scanf("%d", &ref[i]);
}
printf("\nThe given reference string:\n");for (i = 0; i
< nor; i++)

72
{
printf("%4d", ref[i]);
}
for (i = 0; i < nof; i++)
{
frm[i] = -1;
optcal[i] = 0;
}
printf("\n");
for (i = 0; i < nor; i++)

{
flag = 0;
printf("\n\tReference %d ->\t", ref[i]);for (j = 0;
j < nof; j++)
{
if (frm[j] == ref[i])
{
flag = 1;
break;
}
}
if (flag == 0)
{
count++;
if (count <= nof)
{
victim++;
}
else
{
victim = optvictim(i);
}
73
pf++;
frm[victim] = ref[i]; for (j
= 0; j < nof; j++)
{
printf("%4d", frm[j]);
}
}

}
printf("\nNumber of page faults: %d\n", pf);return 0; //
Return success
}
int optvictim(int index)
{
int i, j, farthest = -1; int
victim_page = -1; for (i =
0; i < nof; i++)
{
for (j = index; j < nor; j++)
{
if (frm[i] == ref[j])
{
if (j > farthest)
{
farthest = j;
victim_page = i;
}
break;
}
}
if (j == nor)
{
return i;
74
}
}
return (victim_page != -1) ? victim_page : 0;
}

75
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above program was executed and verified successfully

76
EX. NO : 9A
FILE ALLOCATION STRATEGIES
Date: SEQUENTIAL FILE
ALLOCATION

AIM:

To write a C program for implementing sequential file allocation method.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the number of files.

Step 3: Get the memory requirement of each file.

Step 4: Allocate the required locations to each in sequential order Randomly select a location
from available location s1= random (100);

Check whether the required locations are free from the selected location.

Step 5: Print the results file no, length, Blocks allocated.

Step 6: Stop the program

PROGRAM:

#include <stdio.h>

int main() {

int f[50], i, st, j, len, c;

// Initialize the file allocation array

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

f[i] = 0;

77
while (1) { // Loop for multiple file allocations

printf("\nEnter the starting block & length of the file: ");

scanf("%d%d", &st, &len);

// Check and allocate blocks

for (j = st; j < (st + len); j++) {

if (f[j] == 0) {

f[j] = 1;

printf("%d -> %d ", j, f[j]);

} else {

printf("\nBlock %d already allocated\n", j);

break;

if (j == (st + len)) {

printf("\nThe file is allocated to disk\n");

printf("\nDo you want to enter more files? (yes - 1 / no - 0): ");

scanf("%d", &c);

if (c != 1) {

break;

return 0;

78
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:

Thus the above program was executed and verified successfully.

79
EX. NO : 9B
FILE ALLOCATION TECHNIQUE-
Date: INDEXEDALLOCATION

AIM:

To write a C program to implement File Allocation concept using the technique indexed
allocation Technique.

ALGORITHM:

Step 1: Start the Program

Step 2:Obtain the required data through char and int datatypes.

Step 3: Enter the filename, index block.

Step 4: Print the file name index loop.

Step 5:Fill is allocated to the unused index blocks

Step 6: This is allocated to the unused linked allocation.

Step 7: Stop the execution.

PROGRAM:

#include<stdio.h>

void main()

char a[10];

int i,ib,cib[10];

printf("\n enter the file name:");

scanf("%s",a);
80
printf("\n index block:");

scanf("%d",&ib);

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

printf("\n enter the child of index block %d:",1); scanf("%d",&cib[i]);

printf("\n the list of files\t index block\n");

printf("%s\t\t%d",a,ib);

printf("\n the above file utiltization index block of child blocks followin\t");

printf("\n");

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

printf("%d\t\t",cib[i]);

printf("\n");

81
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above C program was executed and verified successfully.

82
EX. NO : 9C
FILE ALLOCATION TECHNIQUE-
Date: LINKED ALLOCATION

AIM:

To write a C program to implement File Allocation concept using the Linked List
Technique.Algorithm

ALGORITHM:

Step 1: Start the Program

Step 2:Obtain the required data through char and int

datatypeStep 3:Enter the filename, starting block ending

block.

Step 4: Print the free block using loop.

Step 5:"for" loop is created to print the file utilization of linked type of entered

type Step 6: This is allocated to the unused linked allocation

Step 7: Stop the execution.

PROGRAM:

#include <stdio.h>
int main() {
char a[10];
int i, sb, eb, fb1[10];
printf("\nEnter the file name: ");
scanf("%s", a);

printf("\nEnter the starting block: ");


scanf("%d", &sb);

83
printf("Enter the ending block: ");
scanf("%d", &eb);

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


printf("Enter the free block %d: ", i + 1);
scanf("%d", &fb1[i]);
}

printf("\nFile name \t Starting block \t Ending block\n");


printf("%s\t\t %d\t\t %d\n", a, sb, eb);
printf("\n%s File Utilization of Linked type of following blocks:\n", a);
printf("%d -> ", sb);

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


printf("%d -> ", fb1[i]);
}
printf("%d\n", eb);
return 0;
}

84
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above C program was executed and verified successfully.

85
ΕΧ.ΝΟ: 10A DISK SCHEDULING ALGORITHM-
FCFS
Date:

AIM:

To Write C programs to simulate implementation FCFS Disk Scheduling Algorithm

ALGORITHM:

Step 1: Start the Program

Step 2: Let Request array represents an array storing indexes of tracks that have been
requested inascending order of their time of arrival. 'head' is the position of disk head.

Step 3: Let us one by one take the tracks in default order and calculate the absolute distance of
the trackfrom the head.

Step 4: Increment the total seek count with this distance.

Step 5: Currently serviced track position now becomes the new head

position. Step 6: Go to step 2 until all tracks in request array have not

been serviced.

Step 7: Stop the executed

86
PROGRAM:

#include

<stdio.h>

#include

<stdlib.h> int

main() {

int queue[20], n, head, i, seek = 0, max,

diff; float avg;

printf("Enter the max range of disk:

");scanf("%d", &max);

printf("Enter the size of queue

request: ");scanf("%d", &n);

printf("Enter the queue of disk positions to be

read:\n"); for (i = 0; i < n; i++) {

scanf("%d", &queue[i]);
}
printf("Enter the initial head

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

// Add initial head position to

queuequeue[n] = head;

// Calculate the total seek

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

diff = abs(queue[i + 1] -

queue[i]); seek += diff;

printf("Disk head moves from %d to %d with seek %d\n", queue[i], queue[i + 1], diff);
}
87
printf("Total seek time is %d\n",

seek); avg = seek / (float)n;

printf("Average seek time is %.2f\n", avg);

return 0;
}

88
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:

Thus the program for implementation of FCFS Disk Scheduling Algorithm was
executed and verified successfully.

89
EX. NO : 10B
DISK SCHEDULING ALGORITHMS –
Date:
C-SCAN ALGORITHM

AIM:

To write a C program for implementing first-come, first-served (FCFS) algorithm.

ALGORITHM:

Step 1 : Take the number of disk requests (`n`), request sequence (`requests[]`), initial head
position (`head`), and disk size (`size`).

Step 2 - Ask the user to input the direction: `0` for left (towards 0) or `1` for right (towards max
track).

Step 3 - Sort the disk requests array in ascending order to facilitate sequential scanning.

Step 4 - Move the disk head towards the highest or lowest track based on the specified direction.

Step 5 - After reaching the end, simulate a circular scan by jumping to the opposite end (start or
end of the disk).

Step 6 - Continue servicing the remaining requests after the head jump, processing them in
sequence.

Step 7 - Calculate the total distance traveled by the head and display the result.

90
PROGRAM :

#include <stdio.h>

#include <stdlib.h>

int main() {

int n, head, size, totalMovement = 0, direction;

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

scanf("%d", &n);

int requests[n];

printf("Enter the disk request sequence: \n");

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

scanf("%d", &requests[i]);

printf("Enter the initial head position: ");

scanf("%d", &head);

printf("Enter the total disk size: ");

scanf("%d", &size);

printf("Enter head movement direction (0 for left, 1 for right): ");

scanf("%d", &direction);

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

if (direction == 1) {

printf("Head moves from %d to %d with movement of %d\n", head, size - 1, abs(size - 1 -


head));

91
totalMovement += abs(size - 1 - head);

head = size - 1;

printf("Head jumps from %d to 0\n", size - 1);

head = 0;

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

printf("Head moves from %d to %d with movement of %d\n", head, requests[i], abs(requests[i] -


head));

totalMovement += abs(requests[i] - head);

head = requests[i];

} else {

printf("Head moves from %d to 0 with movement of %d\n", head, abs(head - 0));

totalMovement += abs(head - 0);

head = 0;

printf("Head jumps from 0 to %d\n", size - 1);

head = size - 1;

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

printf("Head moves from %d to %d with movement of %d\n", head, requests[i], abs(requests[i] -


head));

totalMovement += abs(requests[i] - head);

head = requests[i];

printf("\nTotal head movement: %d\n", totalMovement);

return 0;

}
92
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above C program was executed and verified successfully.

93
EX. NO : 10C
DISK SCHEDULING ALGORITHMS –
Date:
SSTF ALGORITHM

AIM:

To Write C programs to simulate implementation SSTF Disk Scheduling Algorithm.

ALGORITHM:

Step 1: Start the Program

Step 2: Let Request array represents an array storing indexes of tracks that have been
requested in ascending order of their time of arrival. 'head' is the position of disk head.

Step 3: Let us one by one take the tracks in default order and calculate the absolute
distance of thetrack from the head.

Step 4: Increment the total seek count with this distance.

Step 5: Currently serviced track position now becomes the new head

position. Step 6: Go to step 2 until all tracks in request array have not

been serviced.

Step 7: Stop the execution.

94
PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main() {

int queue[100], head, seek = 0, n, i, j, temp;

float avg;

// Clear screen (optional, uncomment if needed)

// clrscr();

printf("* SSTF Disk Scheduling Algorithm *\n");

printf("Enter the size of Queue: ");

scanf("%d", &n);

printf("Enter the Queue:\n");

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

scanf("%d", &queue[i]);

printf("Enter the initial head position: ");

scanf("%d", &head);

// Implementing SSTF Scheduling

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

// Finding the closest request to the current head position

int min_index = -1;

int min_distance = 99999; // A large number to start with

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

if (queue[j] != -1) { // Check if the request has not been serviced

95
int distance = abs(head - queue[j]);

if (distance < min_distance) {

min_distance = distance;

min_index = j;

// If a valid request was found, update seek and head

if (min_index != -1) {

seek += min_distance;

printf("Disk head moves from %d to %d with seek %d\n", head, queue[min_index],


min_distance);

head = queue[min_index];

queue[min_index] = -1; // Mark this request as serviced

printf("\nTotal Seek Time is %d\n", seek);

avg = seek / (float)n;

printf("Average Seek Time is %.2f\n", avg);

return 0;

96
SNS College of Engineering
Department of Information Technology

Performance 4

Record 4

Viva 2

Total 10

RESULT:
Thus the above C program was executed and verified successfully.

97

You might also like