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

AL3452 Os Lab

The document outlines a laboratory manual for operating systems, detailing various experiments including the installation of Windows, UNIX commands, shell programming, CPU scheduling algorithms, inter-process communication, and memory management techniques. It provides step-by-step procedures for each experiment, along with explanations of UNIX architecture, processes, and basic commands. The manual serves as a comprehensive guide for students to understand and implement key concepts in operating systems.

Uploaded by

rajanayaki
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)
49 views101 pages

AL3452 Os Lab

The document outlines a laboratory manual for operating systems, detailing various experiments including the installation of Windows, UNIX commands, shell programming, CPU scheduling algorithms, inter-process communication, and memory management techniques. It provides step-by-step procedures for each experiment, along with explanations of UNIX architecture, processes, and basic commands. The manual serves as a comprehensive guide for students to understand and implement key concepts in operating systems.

Uploaded by

rajanayaki
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

AL3452 OPERATING SYSTEMS LABORATORY

Index
Exp.no Name of the Experiment Page No. Date Signature
1 Installation of windows operating system

2A Illustrate UNIX commands


Shell Programming
2B1. Finding Odd Or Even Number
2B2. Arithmetic Operation using Switch Case
2B3. Executing Shell Commands Using Switch Case
2B 2B4. Generating Multiplication Table
2B5. Palindrome
2B6. Fibonacci Series
2B7. Check for Prime Number
Process management using system calls fork, exit, getpid,
3 wait, close
Implementation of various CPU scheduling algorithms using C programs
4A.First Come First Serve Scheduling (FCFS)
4 4B.Shortest Job First Scheduling (SJF)
4C.Priority Scheduling
4D.Round Robin
5 Illustrate the inter process communication strategy
6 Implementation of mutual exclusion by semaphore
7 Programs to avoid dead lock using banker’salgorithm
8 Deadlock detection algorithm implementation using C
9 C programs to implement threading
10 Implementation of pagingtechnique using C program
C programs to implement the memory allocation methods
11A. First fit
11
11B. Best fit
11C. Worst fit
C programs to implement the various page replacement algorithms
12A. First In First Out (FIFO)
12
12B. Least Recently Used (LRU)
C programs to implement the various file organization techniques
13 13A. Single Level Directory
13B. Two Level Directory
Implementation of file allocation strategies using c programs
14A. Sequentialfileallocation
14
14B. Indexedfileallocation
14C. Linked file allocation
C programs for the implementation of various disk scheduling algorithms
15 15A. First Come First Serve Scheduling (FCFS)
15B. Shortest Seek Time First Scheduling (SSTF)
Ex:No:1
INSTALLATION OF WINDOWS OPERATING SYSTEM
DATE:

AIM:
Installation of Windows Operating Systems.

PROCEDURE:
In order to install our copy of the Windows7 operating system please make sure to have the
following items:
-Windows7 Installation disk
-Drivers CD: *Use after installation process if needed.
*NOTE:Please make sure to backup all of your important data before proceeding with this
installation.

Step1:Insert the Windows7 operating system disk into your DVD drive, and then restart your
computer
Step2:You will see aprompt that says ‘Press any key to continue’ after the ZT logo
disappears.When you see this press any key immediately.
*NOTE: The following screenshot may differ from what you may see during your
installation.(i.e.Win7version,HDD disk space etc..)

Step3: ‘Starting Windows ’with the Windows7 logo will appear.

CS3461-OPERATING SYSTEMS LAB 1


Step4: Language options,by default English will be set along with“time and currency
format”and“keyboard or input method

Step5: Click “Install Now”

CS3461-OPERATING SYSTEMS LAB 2


Step6:End User LicenseAgreement(E.U.L.A.),check the box to accept,andclick“Next”

Step7:“Which type of installation?”window will appear.Upgrade will be greyed out;the


only option you should be able to choose is Custom(advanced).

CS3461-OPERATING SYSTEMS LAB 3


Step8: “Where do you want to install windows?”Make sure the partition is highlighted.
Step9:Delete the partition by clicking on Drive options(advanced)on the bottom
right corner of the field. Make sure the partition is highlighted and click on
Delete.If drive advanced options is greyed out,then the partition will not that to be
deleted.

*NOTE:Deletingthepartitionswillerasealldataonthesystem

CS3461-OPERATING SYSTEMS LAB 4


Step10:Disk0 Unallocated Space should be the only listing at this point.If it is press
next,If not please proceed to delete any additional partitions that may be listed.

Step11:The next screen will show“Installing Windows”.This process should take


approximately 10minutes after which the system will reboot

CS3461-OPERATING SYSTEMS LAB 5


*NOTE:Do not press any keys during this boot up process so as to not disturb the rest of
the installation.
Step12:The next screen will show“InstallingWindows”again to complete the installation
process.

Step13:“SetupisstartingWindows”willappearonthescreen,thenyouwillbeaskedtocreatea
usernameandacomputername.

CS3461-OPERATING SYSTEMS LAB 6


Step14:Thenextstepwillpromptyoutocreateapasswordforyouraccount(optional).

Step15:Thenextscreenwillcomeupandaskyoutochooseoneofthreeoptions:“Userecommend
edsettings”, “Installimportantupdatesonly”and“Askmelater”.

CS3461-OPERATING SYSTEMS LAB 7


Step16:Thisscreenwillallowyoutochooseyourlocaltimezoneandalsoadjustthedateandtime.

CS3461-OPERATING SYSTEMS LAB 8


Step17:Click on the appropriate location of your Computer to enable Windows7 to
apply the correct network settings.

RESULT:
The Windows installation is done on the computer.

CS3461-OPERATING SYSTEMS LAB 9


EX.NO:2A
ILLUSTRATE UNIX COMMANDS
DATE:

WHAT IS UNIX?

UNIX is an operating system which was first developed in the 1960s, and has been under
constant development ever since. By operating system, we mean the suite of programs
which make the computer work. It is a stable, multi-user, multi-tasking system for servers,
desktops and laptops.

UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows
which provides an easy to use environment. However, knowledge of UNIX is required for
operations which aren't covered by a graphical program, or for when there is no windows
interface available, for example, in a telnet session.

TYPES OF UNIX

There are many different versions of UNIX, although they share common similarities. The
most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X.

Here in the School, we use Solaris on our servers and workstations, and Fedora Linux on the
servers and desktop PCs.

THE UNIX OPERATING SYSTEM

The UNIX operating system is made up of three parts; the kernel, the shell and the
programs.

THE KERNEL

The kernel of UNIX is the hub of the operating system: it allocates time and memory to
programs and handles the filestore and communications in response to system calls.

As an illustration of the way that the shell and the kernel work together, suppose a user
types rm myfile (which has the effect of removing the filemyfile). The shell searches the
filestore for the file containing the program rm, and then requests the kernel, through
system calls, to execute the program rm on myfile. When the process rm myfile has
finished running, the shell then returns the UNIX prompt % to the user, indicating that it is
waiting for further commands.

THE SHELL

The shell acts as an interface between the user and the kernel. When a user logs in, the login
program checks the username and password, and then starts another program called the
shell. The shell is a command line interpreter (CLI). It interprets the commands the user
types in and arranges for them to be carried out. The commands are themselves programs:
when they terminate, the shell gives the user another prompt (% on our systems).

The adept user can customize his/her own shell, and users can use different shells on the
same machine. Staff and students in the school have the tcsh shell by default.
CS3461-OPERATING SYSTEMS LAB 10
The tcsh shell has certain features to help the user inputting commands.

Filename Completion - By typing part of the name of a command, filename or directory and
pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the
shell finds more than one name beginning with those letters you have typed, it will beep,
prompting you to type a few more letters before pressing the tab key again.

History - The shell keeps a list of the commands you have typed in. If you need to repeat a
command, use the cursor keys to scroll up and down the list or type history for a list of
previous commands.

FILES AND PROCESSES

Everything in UNIX is either a file or a process.

A process is an executing program identified by a unique PID (process identifier).

A file is a collection of data. They are created by users using text editors, running compilers
etc.

Examples of files:

 a document (report, essay etc.)

 the text of a program written in some high-level programming language

 instructions comprehensible directly to the machine and incomprehensible to a casual


user, for example, a collection of binary digits (an executable or binary file);

 a directory, containing information about its contents, which may be a mixture of other
directories (subdirectories) and ordinary files.

CS3461-OPERATING SYSTEMS LAB 11


UNIX ARCHITECTURE

The main concept that unites all versions of UNIX is the following four basics −
 Kernel: The kernel is the heart of the operating system. It interacts with hardware and
most of the tasks like memory management, tash scheduling and file management.
 Shell: The shell is the utility that processes your requests. When you type in a command
at your terminal, the shell interprets the command and calls the program that you want.
The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell
are most famous shells which are available with most of the Unix variants.
 Commands and Utilities: There are various command and utilities which you would
use in your day to day activities. cp, mv, cat and grepetc. are few examples of
commands and utilities. There are over 250 standard commands plus numerous others
provided through 3rd party software. All the commands come along with various
optional options.
 Files and Directories: All data in UNIX is organized into files. All files are organized
into directories. These directories are organized into a tree-like structure called the file
system.

THE DIRECTORY STRUCTURE

All the files are grouped together in the directory structure. The file-system is arranged in a
hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally
called root (written as a slash / )

In the diagram above, we see that the home directory of the undergraduate
student "ee51vn" contains two sub-directories (docs and pics) and a file called report.doc.

The full path to the file report.doc is "/home/turing/z123456/cs241/p1.cpp"

CS3461-OPERATING SYSTEMS LAB 12


UNIX PROCESS

When a program is started on UNIX, it creates what is known as a “process” on the system.
Every process is assigned a unique serial number called its process id or PID for short. Processes
can be created by any user, but can only be destroyed by someone with the permissions to do so
– usually the user that created the process or the system administrator. This ensures that the
compute jobs you start on the system will not be disturbed by any other user of the system until
they complete or you decide to stop them yourself.

Processes and process management becomes important on UNIX systems that are shared
between a number of users. The concept of users and PIDs is the main tool by which the
available system resources are shared fairly among everybody who needs access to them.
Processes can be suspended or given lower priority in cases where one or more users shoot step
out of the way for someone else, but wish to do so without losing their work of up to that point.

One further consideration on this topic is the fact that a running UNIX process can spawn
“child” processes. For example, any program you run from inside a UNIX Shell will be a child
process of that shell. Conversely, the shell is the parent process of this child. Child processes
have associated with them both their own process id (PID) as well as their parent’s process id
(PPID).

Normally this concept of parent and child processes is not something you need to be bothered
with as a user. However, it can be useful to understand how UNIX organizes processes if you are
trying to keep track of certain system resources (e.g. memory and CPU), if you are working with
environment variables, or if you need to track down a rogue program or script.

CS3461-OPERATING SYSTEMS LAB 13


BASICS UNIX COMMANDS

Date andTime

[sce@localhost ~]$ date


Thu Mar 31 09:38:06 CXT 2016
[sce@localhost ~]$ date +%d
31
[sce @localhost ~]$ date +%m
03
[sce@localhost ~]$ date +%y
16
[sce@localhost ~]$ date +%a
Thu
[sce@localhost ~]$ date +%T
09:38:38
[sce@localhost ~]$ date +%M
38
[sce@localhost ~]$ date +%S
48

calendar

[sce@localhost ~]$ cal


March 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[sce@localhost ~]$ cal 7 2000


July 2000
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8 9
10 11 12 13 14 1516
17 18 19 20 21 2223
24 25 26 27 28 2930
31

Echo

[sce@localhost ~]$ echo Operating System Lab

who am i

[sce@localhost ~]$ who am i


sce01 pts/1 2016-03-31 09:37 (192.168.1.124)
CS3461-OPERATING SYSTEMS LAB 14
sce02 pts/2 2016-03-31 09:40 (192.168.1.125)
sce03 pts/3 2016-03-31 09:42 (192.168.1.126)
sce04 pts/4 2016-03-31 09:45 (192.168.1.127)

who

[sce@localhost ~]$ who


sce01 pts/1 2016-03-31 09:37 (192.168.1.124)

[sce@localhost ~]$ who -H

NAME LINE TIME COMMENT


sce pts/1 2016-03-31 09:37 (192.168.1.124)
sce pts/2 2016-03-31 09:40 (192.168.1.125)
sce3 pts/3 2016-03-31 09:42 (192.168.1.126)
sce4 pts/4 2016-03-31 09:45 (192.168.1.127)

[sce@localhost ~]$ who -q


sce1
man

[sce@localhost ~]$ manecho


ECHO(1) User Commands ECHO(1)

NAME
echo - display a line of text
SYNOPSIS
echo [OPTION]... [STRING]...
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version
output version information and exit
If -e is in effect, the following sequences are recognized:
\0NNN the character whose ASCII code is NNN (octal)
\\ backslash
\a alert (BEL)
\b backspace
\c suppress trailing newline
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
NOTE: your shell may have its own version of echo, which usually super-
sedes the version described here. Please refer to your shellΓÇÖs docu-
mentation for details about the options it supports.
AUTHOR
CS3461-OPERATING SYSTEMS LAB 15
Written by FIXME unknown.
:
bc

[sce@localhost ~]$ echo "82+112" | bc


194

finger

[sce@localhost ~]$ fingersce01


Login: sce Name: sce
Directory: /home/sce Shell: /bin/bash
On since Thu Mar 31 09:37 (CXT) on pts/1 from 192.168.1.124
No mail.
No Plan.

id

[sce@localhost ~]$ id
uid=500(sce01) gid=501(sce) groups=501(sce)

uname

[sce@localhost ~]$ uname -n


localhost.localdomain
[sce@localhost ~]$ uname -o
GNU/Linux
[sce@localhost ~]$ uname -s
Linux
[sce@localhost ~]$ uname -v
#1 SMP Tue Aug 18 15:51:54 EDT 2009

tr

[sce@localhost ~]$ tr 0-9 "*"


0
*
24
**
190
***
23456
*****
[3]+ Stopped tr 0-9 "*"

ps

[sce@localhost ~]$ ps -e
PID TTY TIME CMD
1? 00:00:00 init
2? 00:00:00 migration/0
CS3461-OPERATING SYSTEMS LAB 16
3? 00:00:00 ksoftirqd/0
4? 00:00:00 watchdog/0
5? 00:00:00 migration/1
6? 00:00:00 ksoftirqd/1
7? 00:00:00 watchdog/1
8? 00:00:00 events/0
9? 00:00:00 events/1
10 ? 00:00:00 khelper
11 ? 00:00:00 kthread
15 ? 00:00:00 kblockd/0
16 ? 00:00:00 kblockd/1
2883 ? 00:00:00 login
2885 pts/1 00:00:00 bash
2937 pts/1 00:00:00 man
2969 pts/1 00:00:00 bc
2982 pts/1 00:00:00 tr
2984 pts/1 00:00:00 ps
[sce@localhost ~]$

tty

[sce@localhost a]$ tty


/dev/pts/1

CS3461-OPERATING SYSTEMS LAB 17


DIRECTORY MANIPULATION COMMANDS

mkdir
[sce@localhost a]$ mkdir dir1
[sce01@localhost a]$ mkdir dir2
[scee01@localhost a]$ mkdir dir3
[scee01@localhost a]$ ls
dir1 dir2 dir3

rmdir
[sce@localhost a]$ rmdir dir3
[sce01@localhost a]$ ls
dir1 dir2

pwd
[sce@localhost a]$ pwd
/home/sce01/dir1/a

FILE MANIPULATION COMMANDS

cat-To create a file


[sce@localhost a]$ cat>file1
hi
welcome
to
scecollege
[4]+ Stopped cat > file1

To display the contents of a file

[sce@localhost a]$ cat file1


hi
welcome
to
scecollege
[sce@localhost a]$

ls

[sce@localhost a]$ ls
dir1 dir2 file1

[sce@localhost a]$ cat>file2


adc
ca
dbms
os
evs
tpde
CS3461-OPERATING SYSTEMS LAB 18
daa
[5]+ Stopped cat > file2

[sce@localhost a]$ cat>file2


adc
ca
dbms
os
evs
tpde
da
[6]+ Stopped cat > file2

To append the contents of file2 with file1

[sce@localhost a]$ cat file1>>file2


[sce@localhost a]$ cat file2
adc
ca
dbms
os
evs
tpde
da

hi
welcome
to
scecollege

[sce@localhost a]$ ls
dir1 dir2 file1 file2

To copy the contents from file2 to file3

[sce@localhost a]$ cp file2 file3


[sce@localhost a]$ cat file3
adc
ca
dbms
os
evs
tpde
da

hi
welcome
to
scecollege
[sce@localhost a]$ mv file1 file2
[sce@localhost a]$ cat file1
CS3461-OPERATING SYSTEMS LAB 19
cat: file1: No such file or directory

[sce@localhost a]$ cat file2


hi
welcome
to
scecollege

To remove file1

[sce@localhost a]$ rm file1


rm: cannot remove `file1': No such file or directory
[sce@localhost a]$ rm file2
[sce@localhost a]$ ls
dir1 dir2 file3

To number the content s of file

[sce@localhost a]$ cat-n file3


1 adc
2 ca
3 dbms
4 os
5 evs
6 tpde
7 da
8
9 hi
10 welcome
11 to
12 sce
13 college

[sce@localhost a]$

Cp-copy the content from file2 to file3


[sce@localhost a]$ cp file2 file3
[sce@localhost a]$ cat file3
adc
ca
dbms
os
evs
tpde
da

hi
welcome
to
scecollege

CS3461-OPERATING SYSTEMS LAB 20


[sce@localhost a]$ ls
dir1 dir2 file1 file2
[sce@localhost a]$ cp file2 file3
[sce@localhost a]$ cat file3
adc
ca
dbms
os
evs
tpde
da

hi
welcome
to
scecollege
[sce@localhost a]$ ls
dir1 dir2 file1 file2

mv-Move the content from file1 to file2 and delete file1

[sce@localhost a]$ mv file1 file2


[sce@localhost a]$ cat file1
cat: file1: No such file or directory
[sce@localhost a]$ cat file2
hi
welcome
to
scecollege
rm- To remove the file

[sce@localhost a]$ rm file2


[sce@localhost a]$ ls
dir1 dir2 file3
[sce@localhost a]$ cat -n b
cat: b: No such file or directory

To number the file contents

[sce@localhost a]$ cat -n file3


1 adc
2 ca
3 dbms
4 os
5 evs
6 tpde
7 da
8
9 hi
10 welcome
11 to
CS3461-OPERATING SYSTEMS LAB 21
12 sce
13 college

[sce@localhost a]$ cat>file1


computer graphics
computer architecture
programming and datastructure
computer networks
database management systems

[sce@localhost a]$ cat>>file1


ooad
software engineering

[sce@localhost a]$ catfile1


computer graphics
computer architecture
programming and datastructure
computer networks
database management systems
ooad
software engineering

sort

[sce@localhost a]$ sort -u file1


computer architecture
computer graphics
computer networks
ooad
programming and datastructure
software engineering

cmp-to compare

[scele1 file3 differ: byte 1, line 1

wc- word count

[sce@localhost a]$ wc file1


6 12 114 file1
[sce@localhost a]$ wc -l file1
6 file1
[sce@localhost a]$ wc -c file1
114 file1

Head- To display first 2 lines from file1

[sce@localhost a]$ cat file1


computer graphics
CS3461-OPERATING SYSTEMS LAB 22
computer architecture
programming and datastructure
computer networks
ooad
software engineering

[sce@localhost a]$ head -2 file1


computer graphics
computer architecture

tail-to display last 2 lines from file1

[sce@localhost a]$ tail -2 file1


ooad
software engineering

grep-to display the lines with the letter “s”

[sce@localhost a]$ grep s file1


computer graphics
programming and datastructure
computer networks
software engineering
[sce@localhost a]$

Cut

[sce@localhost a]$ cat file1


computer graphics
computer architecture
programming and datastructure
computer networks
ooad
software engineering

To cut all the characters except 2nd character

[sce@localhost a]$ cut -c2 file1


o
o
r
o
o
o

And (&&) - To perform more than one command

[sce@localhost a]$ cal&& date


March 2016
CS3461-OPERATING SYSTEMS LAB 23
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Thu Mar 31 10:44:26 CXT 2016

Or (||) - To perform either command

[sce@localhost a]$ cal|| date


March 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

RESULT:
The basic Unix commands are executed successfully.

CS3461-OPERATING SYSTEMS LAB 24


EX.NO:2B1
SHELL PROGRAMMING-FINDING ODD OR EVEN NUMBER
DATE:

AIM:
To write simple shell programs by using conditional statements.

ALGORITHM:
Step1:Start the program
Step2:Read the value of n
Step3:Calculate ‘r=expr $n%2’
Step 4:If the value of r equals 0 then print the number is even
Step5:If the value of r not equal to 0 then print the number is odd
Step 6:Stop the program

PROGRAM:
echo "enter the value of n:"
read n
r=`expr $n % 2`
if test $r -eq 0
then
echo "even number"
else
echo "odd number"
fi

OUTPUT:

RESULT:

Thus shell programs by using if conditional statements.

CS3461-OPERATING SYSTEMS LAB 25


EX.NO:2B2 SHELL PROGRAMMING- ARITHEMATIC OPERATION USING
CONDITIONAL STATEMENTS
DATE:

AIM:
To write simple shell programs by using conditional statements.

ALGORITHM:
Step 1:Start the program
Step 2:Declare variables
Step 3:Start loop
Step 4:Get num1,operator,num2 value from user
Step 5:Start switch case and pass operator in switch case
Step 6:Write the case for arithmetic operators and their associated statements
Step 7:Define default in switch case
Step 8:Take input from the user to check loop continuity
Step 9:If the user enters yes then repeat steps 2-7
else end the loop.
Step 10:Stop the program

PROGRAM:

echo "enter the value of a"


read a
echo "enter the value of b"
read b
c=`expr $a + $b`
echo "sum:" $c
c=`expr $a - $b`
echo "sub:" $c
c=`expr $a \* $b`
echo "mul:" $c
c=`expr $a / $b`
echo "div:" $c

CS3461-OPERATING SYSTEMS LAB 26


OUTPUT:

RESULT:

Thus simple shell programs by using conditional statements.

CS3461-OPERATING SYSTEMS LAB 27


EX.NO:2B3
SHELL PROGRAMMING
EXECUTING SHELL COMMANDS USING WHILE LOOP
DATE:

AIM:
To write simple shell programs by using WHILE loop.

ALGORITHM:
Step 1:Start the program
Step 2:Parse the command line arguments and store them in variables.
Stepn 3:Use a switch case statement to determine which command to execute based on the user input.
Step 4:For each case in the switch statement, execute the corresponding command using the appropriate syntax and
options.
Step 5:Use error handling to catch any errors that may occur during the execution of the command.
Step 6:Stop the program

PROGRAM:

while test $ch='y'


do
echo enter the choice:
echo 1.number of user logged in:
echo 2.print calendar:
echo 3.print date:
echo 4.break:
read d
case $d in
1) who i am;;
2) cal 20;;
3) date;;
4) break;;
esac
echo " continue (y)"
read ch
done

CS3461-OPERATING SYSTEMS LAB 28


OUTPUT

RESULT: Thus simple shell programs by using while loop was executed.

CS3461-OPERATING SYSTEMS LAB 29


EX.NO:2B4
SHELL PROGRAMMING
GENERATING MULTIPLICATION TABLE
DATE:

AIM:
To write simple shell programs by using for loop.

ALGORITHM:
Step 1:Start the program
Step 2:Take an input from the user for the number of rows and columns they want in the multiplication table.
Step 3:Create a nested loop with outer loop iterating through the rows and inner loop iterating through the columns.
Step 4:Multiply the row number with the column number and store the result in a variable.
Step 5:Print the result to the console in a formatted way, such that the columns align with each other.
Step 6:Repeat step 3 and 4 until all rows and columns are filled.
Step 7:Stop the program.

PROGRAM:

echo "which table you want"


read n
for ((i=1;i<10;i++))
do
x=`expr $i \* $n`
echo $i "*" $n "=" $x

OUTPUT:

RESULT:
Thus simple shell programs by using for loop was executed.

CS3461-OPERATING SYSTEMS LAB 30


EX.NO:2B5
SHELL PROGRAMMING- PALINDROME
DATE:

AIM:
To write simple shell programs for palindrome

ALGORITHM:
Step 1:Start the program
Step 2:Take an input from the user for the word or phrase to check.
Step 3:Convert the input to lowercase and remove all spaces and punctuation marks.
Step 4:Initialize two pointers, one at the beginning of the string and one at the end of the string.
Step 5:Compare the characters at the two pointers. If they are equal, move the pointers one position towards
each other.
Step 6:Repeat step 4 until the pointers meet in the middle of the string, or until a pair of characters are found
that are not equal.
Step 7:If the pointers met in the middle of the string, the input is a palindrome. If they did not meet in the
middle, the input is not a palindrome.
Step 8:Stop the program

PROGRAM:

echo "enter the string"


read s
n=`expr $s | wc -c`
a=''
while test $n -gt 0
do
x=`expr $s | cut -c $n`
a=`echo $a$x`
n=`expr $n-1 | bc`
done
echo "the reversed string is" $a
if test "$s" = "$a"
then
echo "the given string is palindrome"
else
echo "the given string is not palindrome"
fi

CS3461-OPERATING SYSTEMS LAB 31


OUTPUT:

RESULT:

Thus simple shell programs for palindrome was executed.

CS3461-OPERATING SYSTEMS LAB 32


EX.NO:2B6
SHELL PROGRAMMING- FIBONACCI SERIES
DATE:

AIM:
To write simple shell programs for Fibonacci series.

ALGORITHM:
Step 1:Start the program
Step 2:Take an input from the user for the number of terms to generate in the Fibonacci series.
Step 3:Initialize two variables, a and b, to the values 0 and 1 respectively.
Step 4:Print the value of a to the console.
Step 5:Repeat the following steps n-1 times, where n is the number of terms specified by the user:
a. Compute the next term in the Fibonacci series by adding a and b together.
b. Set a to the value of b.
c. Set b to the value of the next term computed in step 4a.
d. Print the value of a to the console.
Step 6:Stop the program.

PROGRAM:
echo "enter the limit"
read i
n=2
x=0
y=1
echo "fibonacci series"
echo $x
echo $y
while test $n -lt $i
do
z=`expr $x+$y|bc`
echo $z
x=$y
y=$z
n=`expr $n+1|bc`
done

CS3461-OPERATING SYSTEMS LAB 33


OUTPUT:

RESULT:
Thus simple shell programs for Fibonacci series was completed successfully.

CS3461-OPERATING SYSTEMS LAB 34


EX.NO:2B7
SHELL PROGRAMMING- CHECK FOR PRIME NUMBER
DATE:

AIM:
To write simple shell programs for checking prime number .

ALGORITHM:
Step 1:Start the program
Step 2:Take an input from the user for the number to check.
Step 3:If the input is less than 2, print “Not a prime number” and exit the program.
Step 4:Initialize a variable is_prime to True.
Step 5:Loop through all numbers from 2 to the square root of the input number:
If the input number is divisible by the current number in the loop, set is_prime to False and exit the loop.
If is_prime is still True after the loop, print “Prime number” to the console.
Otherwise, print “Not a prime number” to the console.
Step 6:Stop the program.

PROGRAM:

echo "enter the value of n"


read n
flag=0
for ((i=2;i<n/2;i++))
do
r=`expr $n%$i|bc`
if test $r -eq 0
then flag=1
break
fi
done
if test $flag -eq 0
then
echo "$n is a prime number"
else
echo "$n is not a prime number"
fi

CS3461-OPERATING SYSTEMS LAB 35


OUTPUT:

RESULT:
Thus simple shell programs for checking prime number was completed successfully.

CS3461-OPERATING SYSTEMS LAB 36


EX:NO: 3
PROCESS MANAGEMENT USING SYSTEM CALLS
DATE: FORK, EXIT, GETPID, WAIT, CLOSE

AIM:
To write C Programs using the following system calls of UNIX operating system fork,
exec, getpid, exit, wait, close.

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:
#include<stdio.h>
#include<unistd.h>
main() { int pid,pid1,pid2; pid=fork();
if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n the parent process ID is %d\n”, pid1);
}
else { pid2=getpid();
printf(“\n the child process ID is %d\n”, pid2);
}

CS3461-OPERATING SYSTEMS LAB 37


OUTPUT:

RESULT:
Thus the C programs using the following system calls of UNIX operating system fork,
exec, getpid, exit, wait, close has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 38


EX:No:4A
CPU SCHEDULING ALGORITHM- FIRST COME FIRST SERVE (FCFS)
DATE:

AIM:
To write a C program to implement FCFS CPU Scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait time and
turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
struct fcfs
{
int pid,btime,wtime,ttime;
}
p[10];
int main()
{
int i,n;
int towtwtime=0,totttime=0;
printf("\n fcfs scheduling");
printf("enter the number of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i].pid=1;
printf("burst time of the process");
scanf("%d",&p[i].btime);
}
p[0].wtime=0;
p[0].ttime=p[0].btime;
totttime+=p[i].ttime;
for(i=0;i<n;i++)
{
p[i].wtime=p[i-1].wtime+p[i-1].btime;
p[i].ttime=p[i].wtime+p[i].btime;
totttime+=p[i].ttime;
towtwtime+=p[i].wtime;
}
for(i=0;i<n;i++)
{
printf("waiting tie of the process");
printf("turn around time for process");
printf("\n");
}
printf("\n total waiting time:%d",towtwtime);
printf("\n average waiting time:%f",(float)towtwtime);
printf("\n total turn around time:%d",totttime);
printf("\n average turn around time:%f",(float)totttime);
}

OUTPUT:

RESULT:

Thus the FCFS CPU scheduling algorithms has been executed successfully.
EX:NO:4B
CPU SCHEDULING ALGORITHMS- SJF SCHEDULING
DATE:

AIM:
To write a C program to implement SJF CPU Scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime
Step 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.

PROGRAM:

#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];

CS3461-OPERATING SYSTEMS LAB 41


b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
} }
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf("\n\t\t process \t\t waiting time \t\tturn around time \n");
for(i=0;i<n;i++)
printf("\t\t p[%d] \t\t %d \t\t %d \n",p[i],w[i],t[i]);
printf("the average waitingtimeis %f\n",awt);
printf("the average turn around time is %f\n",att);
return 1;
}

CS3461-OPERATING SYSTEMS LAB 42


OUTPUT:

RESULT:

Thus the SJF CPU scheduling algorithms has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 43


EX:NO:4C
CPU SCHEDULING ALGORITHMS- PRIORITY SCHEDULING
DATE:

AIM:
To write a C program to implement Priority Scheduling algorithms.

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and priority.
Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as turnaround time.
Step 8: Stop the program.

PROGRAM:

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];

CS3461-OPERATING SYSTEMS LAB 44


pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n;
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}

CS3461-OPERATING SYSTEMS LAB 45


OUTPUT:

RESULT:
Thus the priority CPU scheduling algorithms has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 46


EX:NO:4D
CPU SCHEDULING ALGORITHMS- ROUND ROBIN
DATE:

AIM:
To write a C program to implement Round Robin algorithms.

ALGORITHM:
Step 1: Declare and initialize variables: count, j, n, time, remain, flag, time_quantum, wait_time,
turnaround_time.
Step 2: Declare arrays to store arrival time (at), burst time (bt), and remaining time (rt) for each
process.
Step 3: Read the time quantum (time_quantum).
Step 4: Start a loop until all processes are executed (remain is not 0).
Step 5: Check if the remaining time of the current process (rt[count]) is less than or equal to the
time quantum (time_quantum) and greater than 0
Step 7: Calculate the total of burst time and waiting time and assign as turnaround time.
Step 8: Stop the execution of program.

PROGRAM:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

CS3461-OPERATING SYSTEMS LAB 47


for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;}

CS3461-OPERATING SYSTEMS LAB 48


OUTPUT:

RESULT:
Thus the round robin CPU scheduling algorithms has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 49


EX:NO:5
IPC USING SHARED MEMORY
DATE:

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<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc,char*argv[])
{
int shmid,cntr;
key_t key;
char*segptr;
char buff[]="poooda....";
key=ftok(".",'s');
if((shmid=shmget(key,SEGSIZE,IPC_CREAT|IPC_EXCL|0666))==-1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("creating a new shared memory seg \n");

CS3461-OPERATING SYSTEMS LAB 50


printf("SHMID:%d",shmid);
}
system("icps -m");
if((segptr=(char*)shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("writing data to shared memory...\n");
strcpy(segptr,buff);
printf("DONE \n ");
printf("reading data from shared memory....\n");
printf("DATA:-%s \n",segptr);
printf("DONE \n");
printf("removing shared memory segment...\n");
if(shmctl(shmid,IPC_RMID,0)==-1)
printf("can't remove shared memory segment....\n");
else
printf("removed successfully");
}

CS3461-OPERATING SYSTEMS LAB 51


OUTPUT:

RESULT:
Thus the c program for IPC has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 52


EX:NO:6
MUTUAL EXCLUSION BY SEMAPHORE
DATE:

AIM:
To write a C-program to implement the producer – consumer problem for mutual exclusion using
semaphores.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the required variables.
Step 3: Initialize the buffer size and get maximum item you want to produce.
Step 4: Get the option, which you want to do either producer, consumer or exit from the
operation.
Step 5: If you select the producer, check the buffer size if it is full the producer should not
produce the item or otherwise produce the item and increase the value buffer size.
Step 6: If you select the consumer, check the buffer size if it is empty the consumer should not
consume the item or otherwise consume the item and decrease the value of buffer size.
Step 7: If you select exit come out of the program. Step 8: Stop the program.

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();

CS3461-OPERATING SYSTEMS LAB 53


else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}

CS3461-OPERATING SYSTEMS LAB 54


OUTPUT:

RESULT:
Thus the implementation of mutual exclusion by Semaphore using C program has been
executed successfully.

CS3461-OPERATING SYSTEMS LAB 55


EX:No:7
C PROGRAMS TO AVOID DEADLOCK USING BANKER'S
ALGORITHM
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>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);

CS3461-OPERATING SYSTEMS LAB 56


}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;

CS3461-OPERATING SYSTEMS LAB 57


Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}

CS3461-OPERATING SYSTEMS LAB 58


OUTPUT

CS3461-OPERATING SYSTEMS LAB 59


RESULT:
Thus the program for Deadlock Avoidance using Banker’s Algorithm has been executed
successfully.

CS3461-OPERATING SYSTEMS LAB 60


EX:NO:8
C PROGRAM TO IMPLEMENT DEADLOCK DETECTION
DATE: ALGORITHM

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 .produce the result of state of process
Step-6 : Stop the program

PROGRAM:

#include<stdio.h>
int max[100][100],alloc[100][100],need[100][100],avail[100],n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("\n dead lock detection algm");
input();
show();
cal();
}
void input()
{
int i,j;
printf("\n eneter the no. of process");
scanf("%d",&n);
printf("\n enter the no. of instants");
scanf("%d",&r);
printf("\n enter the max matrix");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("enter the allocation matrix:");
for(i=0;i<n;i++)

CS3461-OPERATING SYSTEMS LAB 61


for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("\n enter the available resource:");
for(j=0;j<r;j++)
scanf("%d",&avail[j]);
}
void show()
{
int i,j;
printf("\n process \tallocation\t\tmax\t\t\tavailable\t");
for(i=0;i<n;i++)
{
printf("\np%d\t",i+1);
for(j=0;j<r;j++)
printf("\t%d",alloc[i][j]);
printf("\t");
for(j=0;j<r;j++)
printf("\t%d",max[i][j]);
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d",avail[j]);
}}}
void cal()
{
int fin[100],tmp[100][100],flag=1,k,cl=0,safe[100],i,j,dead[100];
for(i=0;i<n;i++)
fin[i]=0;
for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((fin[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)

CS3461-OPERATING SYSTEMS LAB 62


{
avail[k]+=alloc[i][j];
fin[i]=1;
flag=1;
}
printf("\n p%d->",i);
if(fin[i]=1)
i=n;
}
}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
dead[j]=i;
j++;
flag=1;
}}
if(flag==1)
{
printf("\n system is in deadlock and the deadlock process are");
for(i=0;i<n;i++)
printf("\n p%d\t" ,dead[i]);
}
else
printf("\n no dead lock");
}

CS3461-OPERATING SYSTEMS LAB 63


OUTPUT:

RESULT:
Thus the Deadlock Detection Algorithm has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 64


Ex:No:9
C PROGRAM TO IMPLEMENT THREADING
DATE:

AIM:
To write a c program to implement Threading and Synchronization Applications.

ALGORITHM:
Step 1: Start the process
Step 2: Declare process thread, thread-id.
Step 3: Read the process thread and thread state.
Step 4: Check the process thread equals to thread-id by using if condition.
Step 5: Check the error state of the thread.
Step 6: Display the completed thread process. Step 7: Stop the process
Step 7:Stop the Process

PROGRAM

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
void *even(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 0) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
void *odd(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr);
while(count % 2 != 1) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);

CS3461-OPERATING SYSTEMS LAB 65


}
pthread_exit(0);
}
int main(){
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&thr, 0);
pthread_cond_init(&cond, 0);
pthread_create(&thread1, 0, &even, NULL);
pthread_create(&thread2, 0, &odd, NULL);
pthread_join(thread1, 0);
pthread_join(thread2, 0);
pthread_mutex_destroy(&thr);
pthread_cond_destroy(&cond);
return 0;
}

OUTPUT:

[cse4a@localhost ~]$ cc 92.c -lpthread


[cse4a@localhost ~]$ ./a.out
1 2 3 4 5 6 7 8 9 10

RESULT:
Thus the C program for threading has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 66


Ex:NO:10
IMPLEMENT THE PAGING TECHNIQUE USING C PROGRAM
DATE:

AIM:
To write a C program for Paging Techniques.

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>
int main()
{
int lmem[10][10],pmem[10][10],ptabel[10],psize,i,j,n,phyadd;
printf("\nPAGING");
printf("\nenter the number of pages");
scanf("%d",&n);
printf("\nenter the page size");
scanf("%d",&psize);
printf("\nenter the data values to be stored");
for(i=0;i<n;i++)
{
for(j=0;j<psize;j++)
{
printf("\nenter the values for %d%d=",i,j);
scanf("%d",&lmem[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("\nenter the basic addr for %d page:",i);
scanf("%d",&ptabel[i]);
}
printf("\n*****LOGICAL MEMORY*****");
printf("\npage number\toffset\tvalue");
for(i=0;i<n;i++)

CS3461-OPERATING SYSTEMS LAB 67


{
for(j=0;j<psize;j++)
{
printf("\n%d\t\t%d\t%d\t",i,j,lmem[i][j]);
}
}
printf("\n****PAGE TABE****");
printf("\nindex\tbasaddr");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d",i,ptabel[i]);
}
printf("\n****PHYSICAL ADDRESS****");
printf("\nlocation\tvalue\tpage number");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
phyadd=(ptabel[i]*psize)+j;
printf("\n%d\t\t%d\tpage%d",phyadd,lmem[i][j],i);
}
}
}

CS3461-OPERATING SYSTEMS LAB 68


OUTPUT:

RESULT:
Thus the C program for Paging has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 69


Ex:No:11A
MEMORY ALLOCATION METHODS-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,size and fragment.
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;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First 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=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++)

CS3461-OPERATING SYSTEMS LAB 70


{
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:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
return 0;
}

CS3461-OPERATING SYSTEMS LAB 71


OUTPUT:

RESULT:
Thus the C program for first fit memory allocation has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 72


Ex:No:11B
MEMORY ALLOCATION METHOD-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 blocks using 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.

PROGRAM:

#include<stdio.h>
#define max 25
int main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
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=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++)
{

CS3461-OPERATING SYSTEMS LAB 73


for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j; lowest=temp;
}}}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
return 0;
}

OUTPUT:

RESULT:
Thus the C program for best fit memory allocation has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 74


Ex:No:11C
MEMORY ALLOCATION METHOD -WORST FIT
DATE:

AIM:
To write a C program for implementation memory allocation methods for fixed 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 blocks using 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]=j break the for loop.
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 and fragment.
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=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=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]);
}

CS3461-OPERATING SYSTEMS LAB 75


for(i=1;i<=nf;i++)
{ for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j; highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
return 0;
}

OUTPUT:

RESULT:
Thus the C program for worst fit memory allocation has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 76


EX:No:12A
PAGE REPLACEMENT ALGORITHM
DATE: FIRST IN FIRST OUT (FIFO)

AIM:
To write a C program for implementation of FIFO page replacement algorithm.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the necessary variables.
Step 3: Enter the number of frames.
Step 4: Enter the reference string end with zero.
Step 5: FIFO page replacement selects the page that has been in memory the longest time and
when the page must be replaced the oldest page is chosen.
Step 6: When a page is brought into memory, it is inserted at the tail of the queue.
Step 7: Initially all the three frames are empty.
Step 8: The page fault range increases as the no of allocated frames also increases.
Step 9: Print the total number of page faults.
Step 10: Stop the program.

PROGRAM:

#include<stdio.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;
int 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++)
{

CS3461-OPERATING SYSTEMS LAB 77


if (fr[i] == page[j]) {
flag1 = 1;
flag2 = 1;
break;
}
}
}
if (flag1 == 0) {
for (i = 0; i < frsize; i++) {
if (fr[i] == -1) {
fr[i] = page[j];
flag2 = 1;
pf++;
break;
}
}
}
if (flag2 == 0) {
fr[top] = page[j];
top++;
}

OUTPUT:

RESULT:
Thus the various FIFO Page Replacement Algorithms has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 78


EX:No:12B
PAGE REPLACEMENT ALGORITHM
DATE: LEAST RECENTLY USED (LRU)

AIM:
To write a c program to implement LRU page replacement algorithm.

ALGORITHM:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least recently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process

PROGRAM:

#include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}

CS3461-OPERATING SYSTEMS LAB 79


if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++) fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k]) fs[i]=1;
}}
for(i=0;i<3;i++)
{
if(fs[i]==0) index=i;
}
fr[index]=p[j]; pf++;
}
display();
}
printf("\n no of page faults :%d",pf+frsize); }
void display()
{
int i; printf("\n"); for(i=0;i<3;i++) printf("\t%d",fr[i]);
}

CS3461-OPERATING SYSTEMS LAB 80


OUTPUT:

RESULT:
Thus the various LRU Page Replacement Algorithms has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 81


Ex:NO:13A
FILE ORGANIZATION TECHNIQUES
DATE: SINGLE LEVEL DIRECTORY

AIM:
To write C program to organize the file using single level directory

ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level
Step-7: Display the files
Step-8: Stop the program

PROGRAM:

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;

CS3461-OPERATING SYSTEMS LAB 82


case 2: printf("\nEnter 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) printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter 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)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
}

CS3461-OPERATING SYSTEMS LAB 83


OUTPUT:

RESULT:
Thus the program to organize the file using single level directory has been executed successfully

CS3461-OPERATING SYSTEMS LAB 84


Ex:NO:13B
DATE: FILE ORGANIZATION TECHNIQUES
TWO LEVEL DIRECTORY

AIM:

To write C program to organize the file using two level directory.

ALGORITHM:
Step-1: Start the program.
Step-2: Declare the count, file name, graphical interface.
Step-3: Read the number of files
Step-4: Read the file name
Step-5: Declare the root directory
Step-6: Using the file eclipse function define the files in a single level
Step-7: Display the files
Step-8: Stop the program

PROGRAM:

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;

CS3461-OPERATING SYSTEMS LAB 85


case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("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]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
} Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
printf("File created");
break;
}

}
printf("Directory %s not found",d);
jmp : break;
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++)

CS3461-OPERATING SYSTEMS LAB 86


{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: 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);
}
}
}

CS3461-OPERATING SYSTEMS LAB 87


OUTPUT:

RESULT:
Thus the program to organize the file using two level directory has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 88


Ex:No:14A FILE ALLOCATION STRATEGIES
SEQUENTIAL FILE ALLOCATION
DATE:

AIM:
To write a C program for sequential file allocation

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:

#include<stdio.h>
struct file
{
char fname[10];
int start;
int size,block[10];
}f[10];
main()
{
int i,j,n;
printf("\n enter the number of files");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the file name:");
scanf("%s",f[i].fname);
printf("\n enter the starting block");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("\n enter the number of size");
scanf("%d",&f[i].size);
}
printf("\n file\tstart\tsize\tblock\n");
for(i=0;i<n;i++)

CS3461-OPERATING SYSTEMS LAB 89


{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size;j++)
printf("%6d",f[i].start+(j-1));
printf("\n");
}
}

OUTPUT:

RESULT:
Thus the various Sequential File Allocation methods has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 90


Ex:No:14B FILE ALLOCATION STRATEGIES
LINKED FILE ALLOCATION
DATE:

AIM:
To write a C program for linked file allocation

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM
#include <stdio.h>
struct file {
char fname[10];
int start;
int size;
int block[10];
} f[10];
void display(int n);
int main() {
int i, j, n;
printf("Enter the number of files: ");
scanf("%d", &n);

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


printf("\nEnter the file name: ");
scanf("%s", f[i].fname);
printf("Enter the starting block: ");
scanf("%d", &f[i].start);
f[i].block[0] = f[i].start;
printf("Enter the number of blocks: ");
scanf("%d", &f[i].size);
printf("Enter the block numbers:\n");
for (j = 1; j <= f[i].size; j++) {
scanf("%d", &f[i].block[j]);
}
} display(n);

CS3461-OPERATING SYSTEMS LAB 91


return 0;
}
void display(int n) {
int i, j;
printf("\nFile\tStart\tSize\tBlocks\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t%d\t", f[i].fname, f[i].start, f[i].size);
for (j = 1; j <= f[i].size; j++) {
printf("%d->", f[i].block[j]);
}
printf("End\n");
}

OUTPUT:

RESULT:
Thus the various Linked File Allocation methods has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 92


Ex:No:14 C FILE ALLOCATION STRATEGIES
INDEXED FILE ALLOCATION
DATE:

AIM:
To write a C program for index file allocation

ALGORITHM:
Step-1: Start the program.
Step-2: Get the number of records user want to store in the system.
Step-3: Using Standard Library function open the file to write the data into the file.
Step-4: Store the entered information in the system.
Step-5: Using do..While statement and switch case to create the options such as
1-DISPLAY, 2.SEARCH, 3.EXIT.
Step-6: Close the file using fclose() function.
Step-7: Process it and display the result.
Step-8: Stop the program.

PROGRAM:

#include <stdio.h>
int main()
{
int f[50], i, j, indexBlock, n, c;
int index[50];
for (i = 0; i < 50; i++)
{
f[i] = 0;
}
while (1)
{
printf("\nEnter the index block: ");
scanf("%d", &indexBlock);
if (f[indexBlock] == 0)
{
f[indexBlock] = 1;
printf("Enter the number of blocks for the file: ");
scanf("%d", &n);
} else {
printf("Block already allocated, try another block.\n");
continue;
}
printf("Enter the block numbers:\n");
for (i = 0; i < n; i++) {
scanf("%d", &index[i]);

CS3461-OPERATING SYSTEMS LAB 93


}
int isAllocated = 1;
for (i = 0; i < n; i++) {
if (f[index[i]] == 1) {
printf("Block %d is already allocated.\n", index[i]);
isAllocated = 0;
break;
}
}
if (!isAllocated) {
printf("Please try again with different blocks.\n");
continue;
}
for (i = 0; i < n; i++) {
f[index[i]] = 1
}
printf("Blocks allocated successfully.\n");
printf("File indexed at block %d with blocks: ", indexBlock);
for (i = 0; i < n; i++) {
printf("%d ", index[i]);
}
printf("\n");

printf("Enter 1 to add more files, 0 to exit: ");


scanf("%d", &c);
if (c == 0) {
break;
}
}
return 0;
}

CS3461-OPERATING SYSTEMS LAB 94


OUTPUT:

RESULT:
Thus the various Indexed File Allocation methods has been executed successfully.

CS3461-OPERATING SYSTEMS LAB 95


Ex:No:15A
DISK SCHEDULING ALGORITHM
DATE: FIRST COME FIRST SERVE SCHEDULING (FCFS)

AIM:
To write a C program for implementing 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 the
track 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 program

PROGRAM:
#include <stdio.h>
#include <stdlib.h> // For abs()
int main() {
int queue[20], n, head, i, j, 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 = 1; i <= n; i++) {
scanf("%d", &queue[i]);
}

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


scanf("%d", &head);
queue[0] = head;

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


{
diff = abs(queue[j + 1] - queue[j]);
seek += diff;

CS3461-OPERATING SYSTEMS LAB 96


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

printf("Total seek time is %d\n", seek);


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

return 0;
}

OUTPUT:

RESULT:
Thus the program for the implementation of FCFS disk scheduling algorithms has been
executed successfully.

CS3461-OPERATING SYSTEMS LAB 97


Ex:No:15B
DISK SCHEDULING ALGORITHM
DATE: SHORTEST SEEK TIME FIRST SCHEDULING(SSTF)

AIM:
To write a C program for implementing 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.
‘head’ is the position of disk head.
Step 3:Find the positive distance of all tracks in the request array from head.
Step 4:Find a track from requested array which has not been accessed/serviced yet and has
minimum distance from head.
Step 5:Increment the total seek count with this distance.
Step 6:Currently serviced track position now becomes the new head position.
Step 7:Go to step 2 until all tracks in request array have not been serviced.
Step 8:Stop the program

PROGRAM:

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,k,req[50],mov=0,cp,index[50],min,a[50],j=0,mini,cp1;
printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
cp1=cp;
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
index[i]=abs(cp-req[i]);
}
min=index[0];
mini=0;
for(i=1;i<n;i++)

CS3461-OPERATING SYSTEMS LAB 98


{
if(min>index[i])
{
min=index[i];
mini=i;
}
}
a[j]=req[mini];
j++;
cp=req[mini]; req[mini]=999;
} printf("Sequence is : ");
printf("%d",cp1);
mov=mov+abs(cp1-a[0]); printf(" -> %d",a[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(a[i]-a[i-1]); printf(" -> %d",a[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}

OUTPUT:

RESULT:
Thus the program for the implementation of SSTF disk scheduling algorithms has been executed
successfully.

CS3461-OPERATING SYSTEMS LAB 99

You might also like