0% found this document useful (0 votes)
6 views113 pages

1-15_OS_record

The document outlines the curriculum and practical laboratory exercises for the Operating Systems course at Arunai Engineering College for the 2023-2024 academic year. It includes a series of experiments covering topics such as installation of Windows OS, study of UNIX OS, UNIX commands, and shell programming. Each experiment is designed to provide hands-on experience with operating system concepts and commands.

Uploaded by

vmohan74748
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)
6 views113 pages

1-15_OS_record

The document outlines the curriculum and practical laboratory exercises for the Operating Systems course at Arunai Engineering College for the 2023-2024 academic year. It includes a series of experiments covering topics such as installation of Windows OS, study of UNIX OS, UNIX commands, and shell programming. Each experiment is designed to provide hands-on experience with operating system concepts and commands.

Uploaded by

vmohan74748
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/ 113

ARUNAI ENGINEERING COLLEGE

(Affiliated to Anna University)


Velu Nagar, Thiruvannamalai-606603
www.arunai.org

DEPARTMENT OF ARTIFICIAL INTELLIGENCE &


DATA SCIENCE

BACHELOR OF TECHNOLOGY

2023 – 2024 (EVEN SEMESTER)

SECOND YEAR

AL3452 OPERATING SYSTEMS LABORATORY


ARUNAI ENGINEERING COLLEGE
TIRUVANNAMALAI – 606 603

DEPARTMENT OF ARTIFICIAL INTELLIGENCE &


DATA SCIENCE
CERTIFICATE

Certified that this is a bonafide record of work done by

Name :

University Reg.No :

Semester :

Branch :

Year :

Staff-in-Charge Head of the Department

Submitted for the


Practical Examination held on

Internal Examiner External Examiner


PAGE
EX.NO EXPERIMENTS SIGN
NO
1 Installation of Windows Operating System

2A Study of UNIX Operating System

2B UNIX COMMANDS

2C Shell Programming

Process Management Using System Cells: Fork, Exit,


3
Getpid, Wait, Close

4 Various CPU Scheduling Algorithm

Illustrate the Inter Process Communication (IPC)


5
Strategy

6 Implementation of Semaphores

7 Implementation Deadlock Detection Algorithm

8 Bankers Algorithm for DeadLock Avoidance

9 Implementation of Thread in C

Implementation of Paging Technique of


10
Memory Management
Implementation of Memory Allocation Methods for
11
Fixed Partition
Implementation of Page Replacement
12
Algorithm
Implementation of Various File
13
Organization Techniques

14 Implementation of File Allocation Strategies

Implementation of Various Disk


15
Scheduling Algorithm
Ex.no:1
Date: Installation of windows operating system

Aim

To Install windows operating system

Procedure

1. Boot your PC using Windows 7 DVD/USB drive and press any key if you see Press any key to
continue message.

2. Next select your language, keyboard type (Generally the US) and time format.

3. Click the Install button.


4. Click the box labeled I agree with the license terms and click Next to proceed further.

5. In the next screen, click on the Custom (advanced) option. I hope you are not upgrading here. If you
are doing an upgrade then just click on the Upgrade option.

6. This is an important step as you need to select the drive where you would like to install Windows 7.
Note that selecting a wrong partition will wipe out the data. Also, note that Windows 7 creates another
small partition of about 200 MB if you are installing Windows 7 on an empty hard drive. The hidden
200MB partition will not be shown in the Windows Explorer!
7. You can also format the selected partition by opening drive option and then choosing
the Format option.

8. Click on the Next button to start the Windows 7 installation. Windows may restart many times during
the installation and you need not worry about that.

9. After completing the installation, Windows will give you the below screen to enter your username
and password.
10. In the next step you will be asked to enter the Product Key. Enter the key that you have got and click
the Next button.

11. Here you need to select the Windows 7 update option. Click Use recommended settings option.

9. Select Time Zone, date and time and click Next.

10. In the next screen, you need to select the type of network. That is, choose between Home network,
Work network and Public network.
11. Finally, the setup will ask you to create a group depending on the type of Network you have chosen.
If you are not sure, just skip as you can do it later as well.

12. You will see the Windows 7 desktop. That is, you have successfully installed Windows 7 on your PC.
Don’t forget to read 10 things to do soon after installing Windows 7 to make your Windows 7 secure.

Result :

Thus the installation of Windows Operating System has been completed successfully.
Ex.no:2(a)
Date: Study of UNIX Operating System

Aim

To introduce the concepts of UNIX Operating System

Operating System

An Operating System is a set of programs that:

o Functions as an virtual machine by presenting an interface that is easier to program than


the underlying hardware

o Acts as resource management through orderly and controlled allocation of the processors,
memories, and I/O devices among the programs competing for it.

UNIX Features

1. Multi-user system—Multi-user capability of UNIX allows several users to use the same
computer to perform their tasks. Several terminals [Keyboards and Monitors] are connected to a
single powerful server.

2. Multi-tasking system—Multitasking is the capability of the operating system to perform various


task simultaneously, i.e. a user can run multiple tasks concurrently.

3. Programming Facility—the UNIX shell has all the necessary ingredients like conditional and
control structures, etc.

4. Security—Every user must have a single login name and password. So, accessing another user’s
data is impossible without his permission.

Apart from these features, UNIX has an extensive Tool kit, exhaustive system calls and Libraries and
enhanced GUI (X Window).

Organization of UNIX

1. The kernel is the heart of the system, a collection of programs written in C that directly
communicate with the hardware. It manages the system resources, allocates time between user and
processes, decides process priorities, and performs all other tasks. The kernel, in traditional
parlance, is often called the Operating system.

2. The shell, on the other hand, is the "sleeping beauty" of UNIX. It is actually the interface
between the user and the kernel. The shell is the agency which takes care of the features of
redirection and has a programming capability of its own.

3. The Tools and Applications consist of Application Software, Compilers, Database Package,
Internet tools, UNIX commands, etc.

File System

All files in UNIX are related to one another. The file system of UNIX resembles a tree that grows
from top to bottom as shown in the figure. The file system begins with a directory called root (at the
top). The root directory is denoted by a slash (\). Branching from root there are several directories
such as bin, lib, etc, tmp, dev. Each of these directories contains several sub-directories and files.

Result :

Thus the study of UNIX Operating System has been completed successfully.
Ex.no:2(b)
Date: UNIX Commands

Aim

To study and execute Unix commands.

Login

Type telnet server_ipaddress in run window.

User has to authenticate himself by providing username and password. Once verified, a greeting and
$ prompt appears. The shell is now ready to receive commands from the user. Options suffixed with
a hyphen (–) and arguments are separated by space.

General commands

commands functions
Date Used to display the current system date and time.
Date+%D Displays date only
Date+%T Displays time only
Date +% Y Displays the year part of date
date +% H Displays the hour part of time
Cal Calendar of the current month
cal year Displays calendar for all months of the specified year
cal month year Displays calendar for the specified month of the year
Who Login details of all users such as their IP, Terminal No, User name,
who am i Used to display the login details of the user
Uname Displays the Operating System
uname –r Shows version number of the OS (kernel).
uname –n Displays domain name of the server
echo $HOME Displays the user's home directory
Bc Basic calculator. Press Ctrl+d to quit
lp file Allows the user to spool a job along with others in a print queue
man cmdname Manual for the given command. Press q to exit
history To display the commands used by the user since log on.
exit Exit from a process. If shell is the only process then logs out

Directory commands

Command Function
Pwd Path of the present working directory
mkdir dir A directory is created in the given name under the current directory
mkdir dir1 dir2 A number of sub-directories can be created under one stroke
Change Directory. If the subdir starts with / then path starts from root
cd subdir (absolute) otherwise from current working directory
cd To switch to the home directory.
cd / To switch to the root directory.
cd .. To move back to the parent directory
rmdir subdir Removes an empty sub-directory.

File commands

Commands Functions
To create a file with some contents. To end typing press Ctrl+d.
cat > filename The > symbol means redirecting output to a file. (< for input)
cat filename Displays the file contents
cat >> filename Used to append contents to a file
cp src des Copy files to given location. If already exists, it will be overwritten
cp –i src des Warns the user prior to overwriting the destination file
cp –r src des Copies the entire directory, all its sub-directories and files
mv old new To rename an existing file or directory. –i option can also be used
mv f1 f2 f3 dir To move a group of files to a directory
mv –v old new Display name of each file as it is moved
rm file Used to delete a file or group of files. –i option can also be used
rm * To delete all the files in the directory
rm –r * Deletes all files and sub-directories
rm –f * To forcibly remove even write-protected files
Ls Lists all files and subdirectories (blue colored) in sorted manner.
Is name To check whether a file or directory exists
ls name* Short-hand notation to list out filenames of a specific pattern
ls –a Lists all files including hidden files (files beginning with .)
ls –x dirname To have specific listing of a directory.
ls –R Recursive listing of all files in the subdirectories
ls –l Long listing showing file access rights (read/write/executerwx for
user/group/others-ugo)
cmp file1 file2 Used to compare two files. Displays nothing if files are identical
wc file it produces a statistics of lines (l), words(w), and characters(c).
chmod perm file Changes permission for the specified file. (r=4, w=2, x=1) chmod 740
file sets all rights for user, read only for groups and no rights for others

The commands can be combined using the pipeline (|) operator.

For example:

number of users logged in can be obtained as. who | wc -l Finally to terminate the unix session
execute the command exit or logout.
Output

$ date

Sat Apr 9 13:03:47 IST 2011

$ date +%D

04/09/11

$ date +%T

13:05:33

$ date +%Y

2011

$ date +%H

13

$ cal 08 1998
August 1998

Su Mo Tu We Th Fr Sa

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

$ who
root : 0 Apr 9 08:41

vijai pts/0 Apr 9 13:00 (scl-64)


cse4001 pts/3 Apr 9 13:18 (scl-41.smkfomra.com)

$ uname

Linux

$ uname -r

2.4.20-8smp
$ uname -n

localhost.localdomain

$ echo $HOME
/home/vijai

$ echo $USER

vijai

$ bc

3+5

$ pwd

/home/vijai/shellscripts/loops

$ mkdir filter

$ ls

filter list.sh regexpr shellscripts

$ cd shellscripts/loops/

$ cd

$ cd /

[vijai@localhost /]$

[vijai@localhost /]$ cd /home/vijai/shellscripts/loops/ $ cd ..

[vijai@localhost shellscripts]$

$ rmdir filter

$ ls

list.sh regexpr shellscripts

$ cat > greet

hi cse

wishing u the best

$ cat greet

hi ece-a

wishing u the best

$ cat >> greet

Bye

$ cat greet

hi cse

wishing u the best


bye

$ ls

greet list.sh regexpr shellscripts

$ ls -a

.. bash_logout .canna .gtkrc regexpr .viminfo.tmp

.. .bash_profile .emacs .kde shellscripts .xemacs

.bash_history .bashrc gree list.sh .viminfo

$ ls -l

-rw-rw-r-- 1 vijai vijai 32 Apr 11 14:52 greet

-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh

drwxrwxr-x 2 vijai vijai 4096 Apr 9 14:30 regexpr

$ cp greet ./regexpr/

$ ls

Greet list.sh regexpr shellscripts

$ ls ./regexpr

demo greet

$ cp -i greet ./regexpr/

cp: overwrite 'greet'? n

$ mv greet greet.txt

$ ls greet.txt list.sh regexpr shellscripts

$ mv greet.txt ./regexpr/

$ ls

list.sh regexpr shellscripts

$ rm -i *.sh

rm: remove regular file 'fact.sh'? y

rm: remove regular file 'prime.sh'? y

$ ls

list.sh regexpr shellscripts

$ wc list.sh

4 9 30 list.sh

$ wc -l list.sh
4 list.sh

$ cmp list.sh fact.sh

list.sh fact.sh differ: byte 1, line 1

$ ls -l list.sh

-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh

$ chmod ug+x list.sh

$ ls - l list.sh

-rwxrwxr-- 1 vijai vijai 30 Apr 4 13:58 list.sh

$ chmod 740 list.sh

$ ls - l list.sh

-rwxr----- 1 vijai vijai 30 Apr 4 13:58 list.s

Result:
Illustration of UNIX command has been executed successfully.
En.no:2(c)
Date: Shell Programming

Aim

To create shell scripts using shell programming constructs.

The activities of a shell are not restricted to command interpretation alone. The shell also has
rudimentary programming features. Shell programs are stored in a file (with extension .sh). Shell
programs run in interpretive mode. Bourne shell (sh), C shell (csh) and Korn shell (ksh) are also
widely used. Linux offers Bash shell (bash) .

Preliminaries

1. Comments in shell script start with #.

2. Shell variables are loosely typed i.e. not declared. Variables in an expression or output must be
prefixed by $.

3. The read statement is shell's internal tool for making scripts interactive.

4. Output is displayed using echo statement.

5. Expressions are computed using the expr command. Arithmetic operators are + - * / %. Meta
characters * ( ) should be escaped with a \.

6. The shell scripts are executed

$ sh filename

Decision-making

Shell supports decision-making using if statement. The else statement is optional.

if [ condition ]

then

statements

else

statements

fi

The else-if ladder has the following syntax.

if [condition ]

then

statements

elif [ condition ]
then

statements .. .

else

statements

fi

The set of relational operators are –eq –ne –gt –ge –lt –le and logical operators used in
conditional expression are –a –o !

Multi-way branching

The case statement is used to compare a variables value against a set of constants. If it matches
a constant, then the set of statements followed after ) is executed till a ;; is encountered. The
optional default block is indicated by *. Multiple constants can be specified in a single pattern
separated by |.

case variable in

constant1)

statements ;;

constant2)

statements ;;
. . . *)

Statements

Esac

Loops

Shell supports a set of loops such as for, while and until to execute a set of statements
repeatedly. The body of the loop is contained between do and done statement.

The for loop doesn't test a condition, but uses a list instead.

for variable in list

do
statements

done

The while loop executes the statements as long as the condition remains true.

while [ condition ]

do

statements

done
The until loop complements the while construct in the sense that the statements are executed as
long as the condition remains false.

until [ condition ]

do

statements

done

A) Swapping values of two variables


# Swapping values – swap.sh echo
n "Enter value for A : " read a
echo -n "Enter value for B : " read b
t=$a
a=$b
b=$t
echo "Values after Swapping"
echo "A Value is $a and B Value is $b"
Output
$ sh swap.sh
Enter value for A : 12
Enter value for B : 23
Values after Swapping
A Value is 23 and B Value is 12
B) Farenheit to Centigrade Conversion
# Degree conversion – degconv.sh
echo -n "Enter Fahrenheit : " read f
c=`expr \( $f - 32 \) \* 5 / 9` echo
"Centigrade is : $c"
Output
$ sh degconv.sh
Enter Fahrenheit : 213
Centigrade is : 100
C) Biggest of 3 numbers
# Biggest – big3.sh
echo -n "Give value for A B and C: "
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is the Biggest number"
elif [ $b -gt $c ]
then
echo "B is the Biggest number"
else
echo "C is the Biggest number"
fi
Output
$ sh big3.sh
Give value for A B and C: 4 3 4
C is the Biggest number
D) Grade Determination
# Grade – grade.sh
echo -n "Enter the mark : "
read mark
if [ $mark -gt 90 ]
then
echo "S Grade"
elif [ $mark -gt 80 ]
then
echo "A Grade"
elif [ $mark -gt 70 ]
then
echo "B Grade"
elif [ $mark -gt 60 ]
then
echo "C Grade"
elif [ $mark -gt 55 ]
then
echo "D Grade"
elif [ $mark -ge 50 ]
then
echo "E Grade"
else
echo "U Grade"
fi
Output
$ sh grade.sh
Enter the mark : 65
C Grade
E) Vowel or Consonant
# Vowel - vowel.sh
echo -n "Key in a lower case character : "
read choice
case $choice in
a|e|i|o|u) echo "It's a Vowel";;
*) echo "It's a Consonant"
esac
Output
$ sh vowel.sh
Key in a lower case character : e
It's a Vowel
F) Simple Calculator
# Arithmetic operations — calc.sh echo
-n "Enter the two numbers : " read a b
echo " 1. Addition" echo " 2. Subtraction" echo " 3. Multiplication" echo " 4. Division"
echo -n "Enter the option : " read option
case $option in
1) c=`expr $a + $b` echo
"$a + $b = $c";;
2) c=`expr $a - $b` echo
"$a - $b = $c";;
3) c=`expr $a \* $b` echo
"$a * $b = $c";;
4) c=`expr $a / $b` echo
"$a / $b = $c";;
*) echo "Invalid Option"
esac
Output
$ sh calc.sh
Enter the two numbers : 2 4
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter the option : 1
2+4=6
G) Multiplication Table
# Multiplication table – multable.sh clear
echo -n "Which multiplication table? : " read n
for x in 1 2 3 4 5 6 7 8 9 10 do
p=`expr $x \* $n` echo -n
"$n X $x = $p" sleep 1
done
Output
$ sh multable.sh
Which multiplication table? : 6
6X1=6
6 X 2 = 12 .....
H) Number Reverse
# To reverse a number – reverse.sh
echo -n "Enter a number : "
read n
rd=0
while [ $n -gt 0 ] do
rem=`expr $n % 10` rd=`expr
$rd \* 10 + $rem` n=`expr $n /
10`
done
echo "Reversed number is $rd"
Output
$ sh reverse.sh
Enter a number : 234
Reversed number is 432
I) Prime Number
# Prime number – prime.sh echo –
n "Enter the number : " read n
i=2
m=`expr $n / 2` until [
$i -gt $m ] do
q=`expr $n % $i` if
[ $q -eq 0 ] then
echo "Not a Prime number"
exit
fi
i=`expr $i + 1`
done
echo "Prime number"
Output
$ sh prime.sh
Enter the number : 17
Prime number

Result
Thus shell scripts were executed using different programming construct
Ex.no:3 Process Management using System Calls : Fork, Exit,
Date: Getpid, Wait, Close
(a) (Fork, Getpid, Exit)
Aim:
To create a new child process using fork system call.
Algorithm :
1. Declare a variable x to be shared by both child and parent.

2. Create a child process using fork system call.

3. If return value is -1 then


a. Print "Process creation unsuccessfull"
b. Terminate using exit system call.
4. If return value is 0 then
a. Print "Child process"
b. Print process id of the child using getpid system call
c. Print value of x
d. Print process id of the parent using getppid system call
5. Otherwise
a. Print "Parent process"
b. Print process id of the parent using getpid system call
c. Print value of x
d. Print process id of the shell using getppid system call.
6. Stop
Program
/* Process creation - fork.c */
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
pid_t pid; int x = 5;
pid = fork();
x++;
if (pid < 0)
{
printf("Process creation error"); exit(-1);
} else if (pid == 0)
{
printf("Child process:"); printf("\nProcess id is
%d", getpid()); printf("\nValue of x is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:"); printf("\nProcess id is
%d", getpid()); printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}

Output

$ gcc fork.c

$ ./a.out Child

process: Process id is 19499

Value of x is 6

Process id of parent is 19498

Parent process:

Process id is 19498

Value of x is 6

Process id of shell is 3266

Result Thus a child process is created with copy of its parent's address spac
b) (wait system call)

Aim

To block a parent process until child completes using wait system call.

Algorithm

1. Create a child process using fork system call.

2. If return value is -1 then a. Print "Process creation unsuccessfull"

3. Terminate using exit system call.

4. If return value is > 0 then

a. Suspend parent process until child completes using wait system call

b. Print "Parent starts"

c. Print even numbers from 0–10

d. Print "Parent ends"

5. If return value is 0 then

a. Print "Child starts"

b. Print odd numbers from 0–10

c. Print "Child ends"

6. Stop

Program

/* Wait for child termination - wait.c */

#include<stdio.h>

#include<stdlib.h>

#include <unistd.h>

#include<sys/wait.h>

#include <sys/types.h>

main()

int i, status; pid_t

pid;

pid = fork();

if (pid < 0)

{ printf("\nProcess creation failure\n");


exit(-1);

else if(pid > 0)

{ wait(NULL);

printf ("\nParent starts\nEven Nos: ");

for (i=2;i<=10;i+=2)

printf ("%3d",i);

printf ("\nParent ends\n");

else if (pid == 0)

{ printf ("Child starts\nOdd Nos: ");

for (i=1; i<10;i+=2)

printf(“%3d”,i);

printf(“/nchild ends\n”);

Output

$ gcc wait.c

$ ./a.out

Child Starts

Odd Nos: 1 3 5 7 9 Child ends

Parent starts

Even Nos: 2 4 6 8 10 Parent ends

Result

Thus using wait system call zombie child processes were avoided.
(c)open&close system call

Aim

To create a file and to write contents.

Algorithm

1. Declare a character buffer buf to store 100 bytes.

2. Get the new filename as command line argument.

3. Create a file with the given name using open system call with O_CREAT and O_TRUNC options.

4. Check the file descriptor.

a) If file creation is unsuccessful, then stop.

5. Get input from the console until user types Ctrl+D

a) Read 100 bytes (max.) from console and store onto buf using read system call

b) Write length of buf onto file using write system call.

6. Close the file using close system call.

7. Stop

Program

/* File creation - fcreate.c */

#include<stdio.h>

#include<stdlib.h>

#include <string.h>

#include <fcntl.h>

main(int argc, char *argv[])

int fd, n, len;

char buf[100];

if (argc != 2)

printf("Usage: ./a.out \n"); exit(-1);

fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);

if(fd < 0)

{ printf("File creation problem\n");


exit(-1);

printf("Press Ctrl+D at end in a new line:\n");

while((n = read(0, buf, sizeof(buf))) > 0)

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

close(fd);

Output

$ gcc fcreate.c

$ ./a.out hello File

I/O

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

Result

Thus a file has been created with input from the user. The process can be verified by using cat
command
Ex.no: 4 Write C programs to implement the various CPU
Date: Scheduleing Algorithm
a) FCFS Scheduling

Aim

To schedule snapshot of processes queued according to FCFS scheduling.

Process Scheduling

CPU scheduling is used in multiprogrammed operating systems.

By switching CPU among processes, efficiency of the system can be improved. Some scheduling
algorithms are FCFS, SJF, Priority, Round-Robin, etc. Gantt chart provides a way of visualizing CPU
scheduling and enables to understand better.

First Come First Serve (FCFS)

Process that comes first is processed first FCFS scheduling is non-preemptive Not efficient as it
results in long average waiting time. Can result in starvation, if processes at beginning of the queue
have long bursts.

Algorithm

1. Define an array of structure process with members pid, btime, wtime & ttime.

2. Get length of the ready queue, i.e., number of process (say n)

3. Obtain btime for each process.

4. The wtime for first process is 0.

5. Compute wtime and ttime for each process as: a. wtimei+1 = wtimei + btimei b. ttimei = wtimei +
btimei

6. Compute average waiting time awat and average turnaround time atur

7. Display the btime, ttime and wtime for each process.

8. Display awat time and atur

9. Display GANTT chart for the above scheduling

10. Stop

Program

/* FCFS Scheduling - fcfs.c */

#include <stdio.h>

struct process

int pid;
int btime;

int wtime;

int ttime;

p[10];

main()

int i,j,k,n,ttur,twat;

float awat,atur;

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

scanf("%d", &n);

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

printf("Burst time for process P%d (in ms) : ",(i+1));

scanf("%d", &p[i].btime);

p[i].pid = i+1;

p[0].wtime = 0;

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

p[i+1].wtime = p[i].wtime + p[i].btime;

p[i].ttime = p[i].wtime + p[i].btime;

ttur = twat = 0;

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

ttur += p[i].ttime;

twat += p[i].wtime;

awat = (float)twat / n;

atur = (float)ttur / n;
printf("\n FCFS Scheduling\n\n");

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

printf("-");

printf("\nProcess B-Time T-Time W-Time\n");

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

printf("-");

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

printf("\n P%d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime); printf("\n");

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

printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);

printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");

printf("-");

for(i=0; i<(p[n-1].ttime+2”n);i++)

printf("-");

printf("\n");

printf("|");

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

k = p[i].btime/2;

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

printf(" ");

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

for(j=k+1; j<p[i].btime;j++)

printf(“ ”);

printf("|");

printf("\n");

printf("-");

for(i=0; i<(p[n-1].ttime+2”n);i++)
printf("-");

printf("\n");

printf("0");

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

{for(j=0;j<p[i].btime;j++)

printf(" ");

printf("%2d",p[i].ttime);

}
}

Output

Enter no. of process : 4

Burst time for process P1 (in ms) : 10

Burst time for process P2 (in ms) : 4

Burst time for process P3 (in ms) : 11

Burst time for process P4 (in ms) : 6

FCFS Scheduling -

Process B-Time T-Time W-Time ----------------------------

P1 10 10 0
P2 4 14 10

P3 11 25 14
P4 6 31 25 -- -

Average waiting time : 12.25ms

Average turn around time : 20.00ms

GANTT Chart

- -

| P1 | P2 | P3 | P4 |

- -

0 10 14 25 31

Result

Thus waiting time & turnaround time for processes based on FCFS scheduling was computed and the
average waiting time was determined
b) SJF Scheduling

Aim:

To schedule snapshot of processes queued according to SJF scheduling.

Shortest Job First (SJF)

Process that requires smallest burst time is processed first. SJF can be preemptive or non–
preemptive When two processes require same amount of CPU utilization, FCFS is used to break the
tie. Generally efficient as it results in minimal average waiting time. Can result in starvation, since
long critical processes may not be processed.

Algorithm

1. Define an array of structure process with members pid, btime, wtime & ttime.

2. Get length of the ready queue, i.e., number of process (say n)

3. Obtain btime for each process.

4. Sort the processes according to their btime in ascending order. a. If two process have same
btime, then FCFS is used to resolve the tie.

5. The wtime for first process is 0.

6. Compute wtime and ttime for each process as: a. wtimei+1 = wtimei + btimei b. ttimei = wtimei +
btimei

7. Compute average waiting time awat and average turn around time atur.

8. Display btime, ttime and wtime for each process.

9. Display awat and atur

10. Display GANTT chart for the above scheduling

11. Stop

Program

/* SJF Scheduling – sjf.c */

#include<stdio.h>

struct process

int pid;

int btime;

int wtime;

int ttime;

p[10], temp;
main()

int i,j,k,n,ttur,twat;

float awat,atur;

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

scanf("%d", &n);

for(i=0; i p[j].btime) || (p[i].btime == p[j].btime && p[i].pid > p[j].pid))

temp = p[i]; p[i] = p[j]; p[j] = temp;

p[0].wtime = 0;

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

printf("Burst time for process P%d (in ms) : ",(i+1));

scanf("%d", &p[i].btime);

p[i].pid = i+1;

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

for(j=i+1;j<n;j++)

if((p[i].btime > p[j].btime) || (p[i].btime == p[j].btime && p[i].pid > p[j].pid))

temp = p[i]; p[i] = p[j]; p[j] = temp;

p[0].wtime = 0;

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

p[i+1].wtime = p[i].wtime + p[i].btime; p[i].ttime = p[i].wtime + p[i].btime;

ttur = twat = 0;

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

ttur += p[i].ttime; twat += p[i].wtime;

awat = (float)twat / n;

atur = (float)ttur / n;

printf("\n SJF Scheduling\n\n");

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

printf("-");

printf("\nProcess B-Time T-Time W-Time\n");

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

printf(“-“);

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

printf("\n P%-4d\t%4d\t%3d\t%2d", p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);

printf("\n");

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

printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);

printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");

printf("-");

for(i=0; i<(p[n-1].ttime+2*n);i++)

printf(“-“)4;

printf(“\n|”);

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

K=p[i].btime/2;
For(j=0;j<k;j++)

printf(" ");

printf("%2d",p[i].ttime);

Output

Enter no. of process : 5

Burst time for process P1 (in ms) : 10

Burst time for process P2 (in ms) : 6

Burst time for process P3 (in ms) : 5

Burst time for process P4 (in ms) : 6

Burst time for process P5 (in ms) : 9

SJF Scheduling -- -

Process B-Time T-Time W-Time


-

P3 5 5 0

P2 6 11 5
P4 6 17 11

P5 9 26 17
P1 10 36 26

Average waiting time : 11.80ms

Average turn around time : 19.00ms

GANTT Chart

| P3 | P2 | P4 | P5 | P1 |

0 5 11 17 26 36

Result

Thus waiting time & turnaround time for processes based on SJF scheduling was computed and the
average waiting time was determined
c) Priority Scheduling

Aim

To schedule snapshot of processes queued according to Priority scheduling.

Priority

Process that has higher priority is processed first. Prioirty can be preemptive or non–preemptive
When two processes have same priority, FCFS is used to break the tie. Can result in starvation, since
low priority processes may not be processed.

Algorithm

1. Define an array of structure process with members pid, btime, pri, wtime & ttime.

2. Get length of the ready queue, i.e., number of process (say n)

3. Obtain btime and pri for each process.

4. Sort the processes according to their pri in ascending order.

a. If two process have same pri, then FCFS is used to resolve the tie.

5. The wtime for first process is 0. 6. Compute wtime and ttime for each process as:

a. wtimei+1 = wtimei + btimei

b. ttimei = wtimei + btimei

7. Compute average waiting time awat and average turn around time atur

8. Display the btime, pri, ttime and wtime for each process.

9. Display awat and atur

10. Display GANTT chart for the above scheduling

11. Stop

Program

/* Priority Scheduling - pri.c */

#include <stdio.h>

struct process

int pid;

int btime;

int pri; int wtime;

int ttime;

p[10], temp;
main()

int i,j,k,n,ttur,twat;

float awat,atur;

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

scanf("%d", &n);

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

printf("Burst time for process P%d (in ms) : ", (i+1));

scanf("%d", &p[i].btime);

printf("Priority for process P%d : ", (i+1));

scanf("%d", &p[i].pri); p[i].pid = i+1;

for(i=0; i p[j].pri) || (p[i].pri == p[j].pri && p[i].pid > p[j].pid) )

temp = p[i]; p[i] = p[j]; p[j] = temp;

p[0].wtime = 0;

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

for(j=i+1;j<n;j++)

if((p[i].pri > p[j].pri) || (p[i].pri == p[j].pri && p[i].pid > p[j].pid) )

temp = p[i]; p[i] = p[j]; p[j] = temp;

p[0].wtime = 0;
for(i=0; i<n;i++)

p[i+1].wtime = p[i].wtime + p[i].btime;

p[i].ttime = p[i].wtime + p[i].btime;

ttur = twat = 0;

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

ttur += p[i].ttime;

twat += p[i].wtime;

awat = (float)twat / n;

atur = (float)ttur / n;

printf("\n\t Priority Scheduling\n\n");

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

printf("-");

printf("\nProcess B-Time Priority T-Time W-Time\n");

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

printf(“-“);

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

printf("\n P%-4d\t%4d\t%3d\t%4d\t%4d", p[i].pid,p[i].btime,p[i].pri,p[i].ttime,p[i].wtime);


printf("\n");

for(i=0; i<38;i==)

printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);

printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");

printf("-");

for(i=0; i<(p[n-1].ttime=2*n);i++)

printf("-");
printf("\n|");

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

k = p[i].btime/2;

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

printf(" ");

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

for(j=k+1; j<(p[n-1].ttime+2*n);i++)

printf("-");
printf("\n0");

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

for(j=0;j<p[i].btime;j++)

printf(“ “);

printf("%2d",p[i].ttime);

Output

Enter no. of process : 5

Burst time for process P1 (in ms) : 10

Priority for process P1 : 3

Burst time for process P2 (in ms) : 7

Priority for process P2 : 1

Burst time for process P3 (in ms) : 6

Priority for process P3 : 3

Burst time for process P4 (in ms) : 13

Priority for process P4 : 4

Burst time for process P5 (in ms) : 5

Priority for process P5 : 2

Priority Scheduling

Process B-Time Priority T-Time W-Time


P2 7 1 7 0

P5 5 2 12 7
P1 10 3 22 12

P3 6 3 28 22
P4 13 4 41 28

- -

Average waiting time : 13.80ms

Average turn around time : 22.00ms

GANTT Chart

| P2 | P5 | P1 | P3 | P4 |

0 7 12 22 28 41

Result:

Thus waiting time & turnaround time for processes based on Priority scheduling was computed and
the average waiting time was determined
d) Round Robin Scheduling

Aim:

To schedule snapshot of processes queued according to Round robin scheduling.

Round Robin

All processes are processed one by one as they have arrived, but in rounds. Each process cannot take
more than the time slice per round. Round robin is a fair preemptive scheduling algorithm. A process
that is yet to complete in a round is preempted after the time slice and put at the end of the queue.
When a process is completely processed, it is removed from the queue.

Algorithm

1. Get length of the ready queue, i.e., number of process (say n)

2. Obtain Burst time Bi for each processes Pi.

3. Get the time slice per round, say TS

4. Determine the number of rounds for each process.

5. The wait time for first process is 0.

6. If Bi > TS then process takes more than one round. Therefore turnaround and waiting time should
include the time spent for other remaining processes in the same round.

7. Calculate average waiting time and turn around time

8. Display the GANTT chart that includes

a. order in which the processes were processed in progression of rounds

b. Turnaround time Ti for each process in progression of rounds.

9. Display the burst time, turnaround time and wait time for each process (in order of rounds they
were processed).

10. Display average wait time and turnaround time

11. Stop

Program:

/* Round robin scheduling - rr.c */

#include<stdio.h>

main()

int i,x=-1,k[10],m=0,n,t,s=0;

int a[50],temp,b[50],p[10],bur[10],bur1[10];

int wat[10],tur[10],ttur=0,twat=0,j=0;

float awat,atur;
printf("Enter no. of process : ");

scanf("%d", &n);

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

printf("Burst time for process P%d : ", (i+1));

scanf("%d", &bur[i]); bur1[i] = bur[i];

printf("Enter the time slice (in ms) : ");

scanf("%d", &t);

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

b[i] = bur[i] / t;

if((bur[i]%t) != 0)

b[i] += 1;

m += b[i];

printf("\n\t\tRound Robin Scheduling\n");

printf("\nGANTT Chart\n");

for(i=0; i< m)

if(x == n-1)

x = 0;

else

x++;

if(bur[x] >= t)

bur[x] -= t;

a[j+1] = a[j] + t;

if(b[x] == 1)

p[s] = x;
k[s] = a[j+1];

s++;

j++;

b[x] -= 1;

printf(" P%d |", x+1);

else if(bur[x] != 0)

a[j+1] = a[j] + bur[x];

bur[x] = 0;

if(b[x] == 1)

p[s] = x;

k[s] = a[j+1];

s++;

j++;

b[x] -= 1;

printf(" P%d |",x+1);

printf("\n");

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

printf(“ ------- “);

printf(“\n”);

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

printf("%d\t", a[j]);

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

for(j=i+1;j<n;j++)
{

if(p[i] > p[j])

temp = p[i];

p[i] = p[j];

p[j] = temp;

temp = k[i];

k[i] = k[j];

k[j] = temp;

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

wat[i] = k[i] - bur1[i];

tur[i] = k[i];

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

ttur += tur[i];

twat += wat[i];

printf("\n\n");

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

printf("-");

printf("\nProcess\tBurst\tTrnd\tWait\n");

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

printf(“_”);

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

printf("-");

awat = (float)twat / n;

atur = (float)ttur / n;

printf("\n\nAverage waiting time : %.2f ms", awat);


printf("\nAverage turn around time : %.2f ms\n", atur);

}
Output

Enter no. of process : 5

Burst time for process P1 : 10

Burst time for process P2 : 29

Burst time for process P3 : 3

Burst time for process P4 : 7


Burst time for process P5 : 12

Enter the time slice (in ms) : 10

Round Robin Scheduling

GANTT Chart --- -

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

0 10 20 23 30 40 50 52 61

- -

- Process Burst Trnd Wait

P1 10 10 0
P2 29 61 32

P3 3 23 20

P4 7 30 23
P5 12 52 40

Average waiting time : 23.00 ms

Average turn around time : 35.20 ms

Result :

Thus waiting time and turnaround time for processes based on Round robin scheduling was
computed and the average waiting time was determined.
Ex.no:5 Illustrate the Inter Process Communication (IPC)
Date: strategy

AIM:
To Write C programs to simulate Intra & Inter – Process Communication (IPC) techniques: Pipes,
Messages Queues, and Shared Memory.

DESCRIPTION:
Inter Process Communication (IPC) is a mechanism that involves communication of one process with
another process. This usually occurs only in one system.

Communication can be of two types:


Between related processes initiating from only one process, such as parent and child processes.

Between unrelated processes, or two or more different processes.

Following are some important terms that we need to know before proceeding further on this topic.

Pipes − Communication between two related processes. The mechanism is half duplex meaning the
first process communicates with the second process. To achieve a full duplex i.e., for the second
process to communicate with the first process another pipe is required.

FIFO − Communication between two unrelated processes. FIFO is a full duplex, meaning the first
process can communicate with the second process and vice versa at the same time.

Message Queues − Communication between two or more processes with full duplex capacity. The
processes will communicate with each other by posting a message and retrieving it out of the queue.
Once retrieved, the message is no longer available in the queue.

Shared Memory − Communication between two or more processes is achieved through a shared
piece of memory among all processes. The shared memory needs to be protected from each other
by synchronizing access to all the processes.

Semaphores − Semaphores are meant for synchronizing access to multiple processes. When one
process wants to access the memory (for reading or writing), it needs to be locked (or protected)
and released when the access is removed. This needs to be repeated by all the processes to secure
data. Signals − Signal is a mechanism to communica on between mul ple processes by way of
signaling. This means a source process will send a signal (recognized by number) and the destination
process will handle it accordingly.

Note − Almost all the programs in this tutorial are based on system calls under Linux Operating
System (executed in Ubuntu).
A ) ECHOSERVER USING PIPE
SOURCE CODE:
/* Implementation of echoserver using Pipes */

#include <stdio.h>

#include<string.h>

#define msgsize 29

int main()

int ser[2],cli[2],pid;

char inbuff[msgsize];

char *msg="Thank you";

system("clear");

pipe(ser);

pipe(cli);

printf("\n server read id =%d,write id=%d",ser[0],ser[1]);

printf("\n client read id =%d,write id=%d",cli[0],cli[1]);

pid=fork();

if(pid==0)

{ printf("\n i am in child process !");

close(cli[0]);

close(ser[1]);

write(cli[1],msg,msgsize);

printf("\n message written to pipe..!");

sleep(2);

read(ser[0],inbuff,msgsize);

printf("\n echo message received from server");

printf("\n %s",inbuff); }

else

close(cli[1]);
close(ser[0]);

printf("\n parent process");

read(cli[0],inbuff,msgsize);

write(ser[1],inbuff,msgsize);

printf("\n parent ended!")

}}

OUTPUT:
$ vi echopipe.c

$ cc echopipe.c

$ ./a.out

server read id =3,write id=4

client read id =5,write id=6

i am in child process !

client read id =5,write id=6

parent process

parent ended!

$ message written to pipe..!

echo message received from server

RESULT:
Thus the program was executed and verified successfully.
B) ECHO SERVER USING MESSAGES SOURCE CODE:
/* Implementation of echo server using messages */

#include <sys/ipc.h>

#include<stdio.h>

#include<string.h>

#include <sys/msg.h>

#include<stdlib.h>

struct

{
long mtype;

char mtext[20];

}
send,recv;

main()

int qid,pid,len;

qid=msgget((key_t)0X2000,IPC_CREAT|0666);

if(qid==-1)

perror("\n message failed");

exit(1);

send.mtype=1; strcpy(send.mtext,"\n hello i am parent");

len=strlen(send.mtext);

pid=fork();

if(pid>0)

if(msgsnd(qid,&send,len,0)==-1)

perror("\n message sending failed");

exit(1); }
printf("\n message has been posted");

sleep(2);

if(msgrcv(qid,&recv,100,2,0)==-1)

perror("\n msgrcv error:");

exit(1);

printf("\n message received from child - %s\n",recv.mtext);

else

send.mtype=2;

strcpy(send.mtext,"\n hi i am child");

len=strlen(send.mtext);

if(msgrcv(qid,&recv,100,1,0)==-1)

{ perror("\n child message received failed");

exit(1); }

if(msgsnd(qid,&send,len,0)==-1)
{ perror("\n child message send failed"); }

printf("\n received from parent - %s",recv.mtext);

}}

OUTPUT:
$ vi echomsg.c

$ cc echomsg.c

$ ./a.out

received from parent - hello i am parent message has been posted

message received from child - hi i am child

RESULT:
Thus the program was executed and verified successfully.
Ex.no:6
Date: Implementation of semaphores
Aim :

To synchronize producer and consumer processes using semaphore.

Semaphore

A semaphore is a counter used to synchronize access to a shared data amongst multiple processes.
To obtain a shared resource, the process should: o Test the semaphore that controls the resource. o
If value is positive, it gains access and decrements value of semaphore. o If value is zero, the process
goes to sleep and awakes when value is > 0.

When a process relinquishes resource, it increments the value of semaphore by 1.

Producer-Consumer problem

A producer process produces information to be consumed by a consumer process A producer can


produce one item while the consumer is consuming another one. With bounded-buffer size,
consumer must wait if buffer is empty, whereas producer must wait if buffer is full. The buffer can
be implemented using any IPC facility.
Algorithm

1. Define semaphore variables full, empty and mutex

2. Define wait and signal operation

3. Display menu-driven and accept user choice.

4. If choice = 1 then

i. Call wait (empty)

ii. Call wait (mutex)

iii. If buffer is not full then append item to buffer

iv. Call signal (full)

v. Call signal (mutex)

5. If choice = 2 then

i. Call wait (full)

ii. Call wait (mutex)

iii. If buffer is not empty then remove first item from the buffer

iv. Call signal (mutex)

v. Call signal (empty)

6. If choice = 3 then display buffer contents

7. Stop
Program
/* Producer-Consumer problem using semaphore – pcsem.c */

#include<stdio.h>

#include<string.h>

#define size 5

struct process

{
char item[10];

p[10];

int flag=0, full=0, empty=size, mutex=1;

int wait(int s)

if(s==0) flag=1;

else s--;

return s;

int signal(int s)

s++;

return s;

main()

int c, i;

printf("\nProducer-Consumer Problem\n");

while(1)

printf("\n1.Produce 2.Consume 3.Display 4.Exit\n");

printf("Enter your choice : ");

scanf("%d", &c);
switch(c)

case 1: empty = wait(empty);

mutex = wait(mutex);

if(flag == 0)

printf("Enter the item to produce : ");

scanf("%s", p[full].item);

full = signal(full);

else

printf("\nBuffer is FULL\n");

flag = 0;

mutex = signal(mutex);

break;

case 2:

full = wait(full);

mutex = wait(mutex);

if(flag == 0)

printf("Item %s is consumed\n",p[0].item);

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

strcpy(p[i].item, p[i+1].item); flag=0;

else

printf("\nBuffer is EMPTY\n"); flag = 0;

mutex = signal(mutex);
empty = signal(empty);

break;

case 3:

if(full != 0)

printf("\nItems in the buffer : ");

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

printf("\n%s", p[i].item);

Else

printf("\nBuffer is EMPTY\n"); flag = 0;

break;

case 4:

exit(0);

break;

Output:

Producer-Consumer Problem

1.Produce 2.Consume 3.Display

4.Exit Enter your choice : 1

Enter the item to produce : bread

1.Produce 2.Consume 3.Display

4.Exit Enter your choice : 1

Enter the item to produce : butter

1.Produce 2.Consume 3.Display

4.Exit Enter your choice : 1

Enter the item to produce : bun


1.Produce 2.Consume 3.Display 4.Exit

Enter your choice : 1

Enter the item to produce : jam

1.Produce 2.Consume 3.Display 4.Exit

Enter your choice : 2

Item bread is consumed

1.Produce 2.Consume 3.Display 4.Exit

Enter your choice : 2

Item butter is consumed


1.Produce 2.Consume 3.Display 4.Exit

Enter your choice : 3

Items in the buffer : bun jam

1.Produce 2.Consume 3.Display 4.Exit

Enter your choice : 4

Result :
Thus synchronization between producer and consumer process for access to a shared memory
segment is implemented.
Exp. No:7 Implementation of deadlock detection algorithm
Date:
Aim :
To detect whether the given system is in a deadlocked state or not.

Deadlock Detection

Data structures used are Available, Allocation and Request Detection algorithm checks every
possible allocation sequence for all processes Resources allocated to deadlocked processes will be
idle until deadlock is broken Deadlocks occur only when process request cannot be granted
immediately. Deadlock eventually cripples system throughput and causes CPU utilization to drop
Algorithm:
1. See if any Processes Requests can be satisfied.

2. If so satisfy the needs and remove that Process and all the Resources it holds

3. Repeat step1 for all processes

4. If all Processes are finally removed then there is no Deadlock

5. List the deadlocked process

Program:
/* Deadlock detection - deaddeduct.c */

#include <stdio.h>

main()

int found, flag, l, i, j, k=1, sum=0, tp, tr;

int p[8][8], c[8][8], m[8], r[8], a[8], temp[8];

printf("Enter No. of Processes : ");

scanf("%d", &tp);

printf("Enter No. of Resources : ");

scanf("%d", &tr);

printf("\nEnter Claim / Request matrix :\n");

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

for(j=1; j<=tr; j++)

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

printf("\nEnter Allocation matrix : \n");


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

for(j=1; j<=tr; j++)

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

printf("\nEnter Total resources :\n");

for(i=1; i<=tr; i++) scanf("%d", &r[i]);

printf("\nEnter Availability vector :\n");

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

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

temp[i] = a[i];

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

sum = 0;

for(j=1; j<=tr; j++)

sum += p[i][j];

if(sum == 0)

m[k] = i;

k++;

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

for(l=1; l<=tr; j++)

if(i != m[l])

flag = 1;

for(j=1; j<=tr; j++)

if(c[i][j] < temp[j])


{

flag = 0;

break;

if(flag == 1)

m[k] = i;

k++;

for(j=1; j<=tr; 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++)

found = 0;

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

if(j == m[i])

found = 1;

if(found == 0)

printf("P%d ", j);

}
Output:
Enter No. of Processes :4

Enter No. of Processes : 4

Enter No. of Resources : 5

Enter Claim / Request matrix :

01001

00101

00001

10101

Enter Allocation matrix :

10110

11000

00010

00000

Enter Total resources :

21121

Enter Availability vector :

00001

Deadlock causing processes are : P2 P3

Result:
Thus given system is checked for deadlock and deadlocked processes are listed
out.
Exp. No: 8 Bankers Algorithm for deadlock avoidance
Date:
Aim :
To avoid deadlocks to a resource allocation system with multiple instances using bankers algorithm.

Banker’s Algorithm:
Data structures maintained are:

o Available—vector of available resources

o Max—matrix contains demand of each process

o Allocation—matrix contains resources allocated to each process

o Need—matrix contains remaining resource need of each process Safety algorithm is used to
determine whether system is in a safe state Resource request algorithm determines whether
requests can be safetly granted

Algorithm:
1. Read number of resources

2. Read max. instances of each resource type

3. Read number of process

4. Read allocation matrix for each process

5. Read max matrix for each process

6. Display available resources

7. Display need matrix using formula Need = Max – Allocation

8. Determine the order of process to be executed for a safe state

9. Stop

Program :
/* Banker algorithm for deadlock avoidance - bankersalgo.c */

#include<stdio.h>

#include<conio.h>

main()

int output[10], ins[5], avail[5], allocated[10][5];

int need[10][5], max[10][5], p[10];


int k=0, d=0, t=0, i, pno, j, nor, count=0;

printf("Enter number of resources : ");

scanf("%d", &nor);

printf("\nEnter max instances of each resources\n");

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

avail[i]=0;

printf("%c = ",(i+65));

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

printf("\nEnter the No. of processes : ");

scanf("%d", &pno);

printf("\nEnter Allocation matrix \n ");

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

printf("\t%c", (i+65));

printf("\n");

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

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

avail[j] += allocated[i][j];

printf("\nEnter Max matrix \n ");

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

printf("\t%c", (i+65));

avail[i] = ins[i] - avail[i];

printf("\n");

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

{
printf("P%d\t",i);

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

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

printf("\n");

printf("Available resources are : \n");

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

printf("%c = %d \n", (i+65), avail[i]);

printf("\nNeed matrix is :\n");

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

Printf(“P%d\t”,i);

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

printf("%d\t", max[i][j]-allocated[i][j]);

printf("\n"); }

A:

d = -1;

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

count = 0;

t = p[i];

for (j=0; j<= avail[j]) count++;

if(count == nor)

output[k++] = p[i];

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

avail[j]+=allocated[t][j];

else

p[++d]=p[i];
}

if(d != -1)

pno = d + 1;

goto A;

printf("\n Process Execution Order : ");

printf("<");

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

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

printf(">");

Output

Enter number of resources : 3

Enter max instances of each resources

A = 10

B=5

C=7

Enter the No. of processes : 5

Enter Allocation matrix

A B C
P0 0 1 0

P1 2 0 0

P2 3 0 2

P3 2 1 1
P4 0 0 2

Enter Max matrix

A B C

P0 7 5 3

P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3

Available resources are :

A=3

B=3

C=2

Need matrix is :

A B C
P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1
P4 4 3 1

Process Execution Order : < P1 P3 P4 P0 P2 >

Result:
Thus deadlock is avoided for multiple instances of resources using ban
Ex.no:9 C Program to implement threading
Date:
AIM :
Write C programs to demonstrate various thread related concepts

PROGRAM :
Implementing a program using thread */

#include <pthread.h>

#include<stdio.h> #define NUM_THREADS 3

int je,jo,evensum=0,sumn=0,oddsum=0,evenarr[50],oddarr[50];

void *Even(void *threadid)

int i,n;

je=0;

n=(int)threadid;

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

if(i%2==0)

evenarr[je]=i;

evensum=evensum+i;

je++; } } }

void *Odd(void *threadid)

int i,n;

jo=0;

n=(int)threadid;

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

if(i%2!=0)

{
oddarr[jo]=i;

oddsum=oddsum+i;

jo++;

}}

void *SumN(void *threadid)

int i,n;

n=(int)threadid;

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

sumn=sumn+i;

}}

int main()

pthread_t threads[NUM_THREADS];

int i,t;

printf("Enter a number\n");

scanf("%d",&t);

pthread_create(&threads[0],NULL,Even,(void *)t);

pthread_create(&threads[1],NULL,Odd,(void *)t);

pthread_create(&threads[2],NULL,SumN,(void *)t);

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

pthread_join(threads[i],NULL);

printf("The sum of first N natural nos is %d\n",sumn);

printf("The sum of first N even natural nos is %d\n",evensum);

printf("The sum of first N odd natural nos is %d\n",oddsum);

printf("The first N even natural nos is --- \n");

for(i=0;i<je;i++)
printf("%d\n",evenarr[i]);

printf("The first N odd natural nos is --- \n");

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

printf("%d\n",oddarr[i]);

pthread_exit(NULL);

OUTPUT:
$ vi threadf.c

$ cc threadf.c -pthread

$ ./a.out

Enter a number 12

The sum of first N natural nos is 78

The sum of first N even natural nos is 42

The sum of first N odd natural nos is 36

The first N even natural nos is ---- 2 4 6 8 10 12

The first N odd natural nos is ---- 1 3 5 7 9 11

RESULT:
Thus the program was executed and verified successfully
Exp. No. 10 Implementation of paging technique of memory
Date: management
Aim:
To implement paging technique for memory management.

Paging
Paging permits physical address space of a process to be noncontiguous. It avoids external
fragmentation and the need for compaction.

Physical memory is broken into frames.

Logical memory is broken into pages , where page size = frame size Address consist of two parts:
page number and page offset

Page number is used as an index into page table to obtain base address Base address is added with
offset to obtain physical memory address

Algorithm
1. Read physical memory size

2. Read page size

3. Read number of processes

4. Read page table entry for each process

5. Read page number and offset for a procese

6. Compute base address from page table

7. Add offset to base address

8. Display the physical memory address

9. Stop

Program
#include<stdio.h>

main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

printf("Enter Physical memory size : ");

scanf("%d", &ms);

printf("\nEnter Page size : ");


scanf("%d", &ps);

nop = ms / ps;

printf("\nNo. of Frames available are : %d \n",nop);

printf("\nEnter no. of processes : ");

scanf("%d",&np);

rempages = nop;

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

printf("\nEnter no. of pages for process P%d : ",i);

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

if(s[i] > rempages)

printf("\nMemory is Full");

break;

rempages = rempages - s[i];

printf("Enter Page table for process P%d : ", i);

for(j=1; j<=s[i]; j++)

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

printf("\nEnter Process No. Page No. and Offset : ");

scanf("%d%d%d", &x, &y, &offset);

if(x>np || y>=s[i] || offset>=ps)

printf("\nInvalid Process or Page No. or offset");

else

pa = fno[x][y]* ps + offset;

printf("Physical Address is : %d",pa);

}
}
Output:
Enter Physical memory size : 4096

Enter Page size : 512

No. of Frames available are : 8

Enter no. of processes : 3

Enter no. of pages for process P1 : 3

Enter Page table for process P1 : 1 3 5

Enter no. of pages for process P2 : 3

Enter Page table for process P2 : 2 4 6

Enter no. of pages for process P3 : 2

Enter Page table for process P3 : 7 8

Enter Process No. Page No. and Offset : 2 1 120

Physical Address is : 2168

Result:
Thus Paging technique of memory management has been executed successfuly
Exp. No. 11 Implementation of memory allocation methods
Date: for fixed partition

a) First Fit Allocation


Aim:
To allocate memory requirements for processes using first fit allocation.

First fit
The first-fit, best-fit, or worst-fit strategy is used to select a free hole from the set of available
holes. Allocate the first hole that is big enough. Searching starts from the beginning of set of
holes.

Algorithm
1. Declare structures hole and process to hold information about set of holes and processes
respectively.

2. Get number of holes, say nh.

3. Get the size of each hole

4. Get number of processes, say np.

5. Get the memory requirements for each process.

6. Allocate processes to holes, by examining each hole as follows:

a. If hole size > process size then

i. Mark process as allocated to that hole.

ii. Decrement hole size by process size.

b. Otherwise check the next from the set of hole

7. Print the list of process and their allocated holes or unallocated status.

8. Print the list of holes, their actual and current availability.

9. Stop

Program
/* First fit allocation - ffit.c */

#include<stdio.h>

struct process

int size;
int flag;

int holeid;

p[10];

struct hole

int size;

int actual;

h[10];

main()

int i, np, nh, j;

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

scanf("%d", &nh);

for(i=0; i<= h[j].size)

p[i].flag = 1;

p[i].holeid = j;

h[j].size -= p[i].size;

}}}}

printf("\n\tFirst fit\n");

printf("\nProcess\tPSize\tHole");

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

if(p[i].flag != 1)

printf("\nP%d\t%d\tNot allocated", i, p[i].size);

else

printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);

printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh;i++)

printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size);

printf("\n");

Output:
Enter the number of Holes : 5

Enter size for hole H0 : 100

Enter size for hole H1 : 500

Enter size for hole H2 : 200

Enter size for hole H3 : 300

Enter size for hole H4 : 600

Enter number of process : 4

enter the size of process P0 : 212

enter the size of process P1 : 417

enter the size of process P2 : 112

enter the size of process P3 : 426

First fit
Process PSize Hole

P0 212 H1

P1 417 H4

P2 112 H1

P3 426 Not allocated

Hole Actual Available

H0 100 100

H1 500 176

H2 200 200

H3 300 300
H4 600 183

Result:
Thus processes were allocated memory using first fit method.
b) Best Fit Allocation
Aim :
To allocate memory requirements for processes using best fit allocation.
Best fit
Allocate the smallest hole that is big enough. The list of free holes is kept sorted according to
size in ascending order. This strategy produces smallest leftover holes
Worst fit
Allocate the largest hole. The list of free holes is kept sorted according to size in descending
order. This strategy produces the largest leftover hole.
Algorithm :
1. Declare structures hole and process to hold information about set of holes and processes
respectively.
2. 8Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.
9. Stop
Program:
/* Best fit allocation - bfit.c */
#include<stdio.h>
struct process
{
int size;
int flag;
int holeid;
}
p[10];
struct hole
{
int hid;
int size;
int actual;
}
h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh;i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &= h[j].size);
h[i].actual=h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
bsort(h, nh);
for(j=0; j<nh;j++)
{
If(p[i].flag!=1)
{
If(p[i].size<= h[j].size)
{
p[i].flag = 1;

p[i].holeid = h[j].hid;

h[j].size -= p[i].size;

printf("\n\tBest fit\n");

printf("\nProcess\tPSize\tHole");

for(i=0; i bh[j].size)

temp = bh[i];

bh[i] = bh[j];

bh[j] = temp;

}
Output:
Enter the number of Holes : 5

Enter size for hole H0 : 100

Enter size for hole H1 : 500

Enter size for hole H2 : 200

Enter size for hole H3 : 300

Enter size for hole H4 : 600

Enter number of process : 4

enter the size of process P0 : 212

enter the size of process P1 : 417

enter the size of process P2 : 112

enter the size of process P3 : 426

Best fit
Process PSize Hole

P0 212 H3

P1 417 H1

P2 112 H2

P3 426 H4

Hole Actual Available

H1 500 83

H3 300 88

H2 200 88

H0 100 100
H4 600 174

Result:
Thus processes were allocated memory using best fit method.
c) Worst Fit Allocation
Aim:
C Program To implement the Worst Fit Allocation.

Program:
#include<stdio.h>

#include< conio.h>

#define max

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp;

static int bf[max],ff[max];

clrscr();

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

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

getch();

INPUT

Enter the number of blocks: 3

Enter the number of files: 2

Enter the size of the blocks:-

Block 1: 5 Block 2: 2 Block 3: 7

Enter the size of the files:- File 1: 1 File 2: 4

OUTPUT
File No File Size Block No Block Size Fragment

1 1 1 5 4
2 4 3 7 3

Result:
Program to implement the worst fit allocation has been executed successfully.
Exp. No. 12 Implementation of page replacement algorithms
Date:
a) FIFO Page Replacement
Aim:
To implement demand paging for a reference string using FIFO method.

FIFO

Page replacement is based on when the page was brought into memory. When a page should be
replaced, the oldest one is chosen. Generally, implemented using a FIFO queue. Simple to
implement, but not efficient. Results in more page faults. The page-fault may increase, even if frame
size is increased (Belady's anomaly)

Algorithm
1. Get length of the reference string, say l.

2. Get reference string and store it in an array, say rs.

3. Get number of frames, say nf.

4. Initalize frame array upto length nf to -1.

5. Initialize position of the oldest page, say j to 0.

6. Initialize no. of page faults, say count to 0.

7. For each page in reference string in the given order, examine:

a. Check whether page exist in the frame array

b. If it does not exist then

i. Replace page in position j.

ii. Compute page replacement position as (j+1) modulus nf.

iii. Increment count by 1.

iv. Display pages in frame array.

8. Print count.

9. Stop

Program
/* FIFO page replacement - fifopr.c */

#include<stdio.h>

main()
{
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;

printf("Enter length of ref. string : ");

scanf("%d", &l);

printf("Enter reference string :\n");

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

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

printf("Enter number of frames : ");

scanf("%d", &nf);

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

frame[i] = -1; j = 0;

printf("\nRef. str Page frames");

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

printf("\n%4d\t", rs[i]);

avail = 0;

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

if(frame[k] == rs[i])

avail = 1;

if(avail == 0)

frame[j] = rs[i];

j = (j+1) % nf;

count++;

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

printf("\n\nTotal no. of page faults : %d\n",count);

Output :
Enter length of ref. string : 20

Enter reference string : 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6


Enter number of frames : 5

Ref. str Page frames

1 1 -1 -1 -1 -1
2 1 2 -1 -1 -1

3 1 2 3 -1 -1

4 1 2 3 4 -1

5 1 2 3 4 5

6 6 2 3 4 5

1 6 1 3 4 5

2 6 1 2 4 5

3 6 1 2 3 5

7 6 1 2 3 7

3
6

Total no. of page faults : 10

Result:
Thus page replacement was implemented using FIFO algorithm
b) LRU Page Replacement
Aim
To implement demand paging for a reference string using LRU method.

LRU
Pages used in the recent past are used as an approximation of future usage. The page that has not
been used for a longer period of time is replaced. LRU is efficient but not optimal. Implementation of
LRU requires hardware support, such as counters/stack.

Algorithm:
1. Get length of the reference string, say len.

2. Get reference string and store it in an array, say rs.

3. Get number of frames, say nf.

4. Create access array to store counter that indicates a measure of recent usage.

5. Create a function arrmin that returns position of minimum of the given array.

6. Initalize frame array upto length nf to -1.

7. Initialize position of the page replacement, say j to 0.

8. Initialize freq to 0 to track page frequency

9. Initialize no. of page faults, say count to 0.

10. For each page in reference string in the given order, examine:

a. Check whether page exist in the frame array.

b. If page exist in memory then

i. Store incremented freq for that page position in access array.

c. If page does not exist in memory then

i. Check for any empty frames.

ii. If there is an empty frame, Assign that frame to the page Store incremented freq for that page
position in access array. Increment count.

iii. If there is no free frame then Determine page to be replaced using arrmin function. Store
incremented freq for that page position in access array. Increment count.

iv. Display pages in frame array.

11. Print count.

12. Stop
Program
/* LRU page replacement - lrupr.c */

#include <stdio.h>

int arrmin(int[], int);

main()

{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;

int access[10], freq=0, dm;

printf("Length of Reference string : ");

scanf("%d", &len);

printf("Enter reference string :\n");

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

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

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

scanf("%d", &nf);

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

frame[i] = -1;

j = 0;

printf("\nRef. str Page frames");

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

printf("\n%4d\t", rs[i]);

avail = 0;

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

if(frame[k] == rs[i])

avail = 1;

access[k] = ++freq;

break;

}}
if(avail == 0)

dm = 0; for(k=0; k<nf;k++)

if(frame[k] == -1)

dm = 1;

break;

if(dm == 1)

frame[k] = rs[i];

access[k] = ++freq;

count++;

else

j = arrmin(access, nf);

frame[j] = rs[i];

access[j] = ++freq;

count++; }

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

printf("%4d", frame[k]);

}}

printf("\n\nTotal no. of page faults : %d\n", count); }

int arrmin(int a[], int n)

int i, min = a[0];

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

if(min>a[i])

min = a[i];

for(i=0; i<n;i++)
if min==a[i]

return I;}

Output
Length of Reference string : 20

Enter reference string : 1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6

Enter no. of frames : 5

Ref. str Page frames


1 1 -1 -1 -1 -1

2 1 2 -1 -1 -1

3 1 2 3 -1 -1

4 1 2 3 4 -1

5 1 2 3 4 5

6 1 2 6 4 5

3 1 2 6 3 5

7 1 2 6 3 7

3
6

Total no. of page faults : 8

Result
Thus page replacement was implemented using LRU algorithm.
C ) Implementation Of LFU Page Replacement Algorithm
AIM:
To write a c program to implement LFU page replacement algorithm

ALGORITHM :
1. Start the process

2. Declare the frame size

3. Get the number of pages to be inserted and enter the reference string

4. Get the value

5. Declare counter and stack

6. Select the least frequently used page by counter value

7. Stack them according the selection.

8. Display the values

9. Stop the process

PROGRAM

#include<stdio.h>

main()

int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");

scanf("%d",&n);

printf("Enter the reference string:");

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

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

printf("Enter no of frames:");

scanf("%d",&f);

q[k]=p[k];

printf("\n\t%d\n",q[k]);

c++;

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

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

if(p[i]!=q[j])

c1++; }

if(c1==f)

c++;

if(k <f)

q[k]=p[i];

k++;

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

printf("\t%d",q[j]);

printf("\n"); }

else

for(r=0;r<f;r++ )

c2[r]=0;

for(j=i-1;j <n;j--)

If(q[r]!=p[j])

c2[r]++;

else

break; } }

for(r=0;r<f;r++)

b[r]=c2[r];

for(r=0;r<f;r++)

for(j=r;j<f;j++)
{

If(b[r]< b[j] )

t=b[r];

b[r]=b[j];

b[j]=t; } } }

for(r=0;r <f;r++)

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]); }

printf("\n"); } } }

printf("\nThe no of page faults is %d",c); } /*

Output:
Enter no of pages:10

Enter the reference string:7 5 9 4 3 7 9 6 2 1

Enter no of frames:3

7
7 5

7 5 9

4 5 9

4 3 9

4 3 7

9 3 7

9 6 7

9 6 2

1 6 2
*/

Result
Thus page replacement was implemented using LFU algorithm
Exp. No. 13 Implementation of various file organization techniques
Date:
a) Single-Level Directory

Aim :
To create directory structure as a single level directory structure.

Single-Level Directory
All files are contained in the same directory, Easy to implement Filenames must be unique within a
directory Difficult to remember all filenames Leads to anamoly in a multi-user system

Algorithm :
1. Read number of directories

2. For each directory

a. Read directory name

b. Read number of files in that directory

c.Read filenames for that directory

3. Display directory name and their corresponding files

4. Stop

Program:
/* Single Level Directory - singlev.c */

#include<stdio.h>

int nod, nof[20];

char file[20][20][20];

char dir[20][20];

int i,j;

main()

{ printf("No. of Directories : ");

scanf("%d", &nod);

printf("\nEnter the directory details\n");

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

{ printf("\nDirectory Name : ");

scanf("%s", &dir[i]);
printf("No. of Files in the directory : ");

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

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

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

scanf("%s", &file[i][j]);}

printf("\nDirectory Filenames\n");

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

{printf("%s\t", dir[i]);

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

printf("%s ", file[i][j]);

printf("\n"); } }

Output :
No. of Directories : 3

Enter the directory details Directory Name : pds2

No. of Files in the directory : 3

Enter the filenames : inherit.cpp poly.cpp ovld.cpp

Directory Name : os

No. of Files in the directory : 4

Enter the filenames : fcfs.c pcsem.c deadlock.c lru.c

Directory Name : java

No. of Files in the directory : 2

Enter the filenames : hello.java

swing.java

Directory Filenames
pds2 inherit.cpp poly.cpp ovld.cpp

os fcfs.c pcsem.c deadlock.c lru.c

java hello.java swing.java

Result:
Thus single-level directory structure has been demonstrated
b) Two-Level Directory :
Aim:
To create directory structure as a two-level directory structure.

Two-Level Directory:
Each user has a user file directory (UFD) that lists folders and files of that user When a user refers to
a particular file, only his own UFD is searched. The two-level directory structure solves the
namecollision problem It isolates one user from another.

Algorithm :
1. Read number of users

2. For each user

i. Read directory name

ii. Read number of folders

iii. Read filenames for that folder

3. Display files and folders for that user

4. Stop

Program:
/* Two-level directory */

#include<stdio.h>

#include<conio.h>

struct st

char uname[10];

char dname[10][10];

char fname[10][10][15];

int ds,sds[10];

dir[10];

int main()

int i,j,k,n;

printf("No. of Users : ");


scanf("%d", &n);

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

printf("\nUser-%d Name : ", i+1);

scanf("%s", &dir[i].uname);

printf("No. of folders : ");

scanf("%d", &dir[i].ds);

for(j=0; j<dir[i].dss;j++)

printf("\nEnter folder name : ");

scanf("%s", &dir[i].dname[j]);

printf("No. of files : ");

scanf("%d", &dir[i].sds[j]);

printf("Enter filenames:\n");

for(k=0; k<dir[i].sds[j];k++)

scanf("%s", &dir[i].fname[j][k]);

}}

printf("\n\tTwo-Level Directory Structure\n");

printf("\nUser\tFolders\tFiles\n\n");

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

printf("%s", dir[i].uname);

for(j=0; j<dir[i].ds;j++)

printf("\t%s\t", dir[i].dname[j]);

for(k=0; k<dir[i].sds[j];k++)

printf("%-15s ", dir[i].fname[j][k]);

printf("\n");

printf("\n");

}}
Output:
No. of Users : 2

User-1 Name : vijai

No. of folders : 2

Enter folder name : network

No. of files : 2

Enter filenames: udpdns.java, tcpchat.java

Enter folder name : pds2

No. of files : 2

Enter filenames: inherit.cpp ,virtual.cpp

User-2 Name : anand

No. of folders : 2

Enter folder name : os

No. of files : 3

Enter filenames: sjf.c ,pcsem.c ,bankeralgo.c

Enter folder name : network

No. of files : 2

Enter filenames: tcpchat.java ,sniffdata.c

Two-Level Directory Structure

User Folders Files


vijai network udpdns.java tcpchat.java

pds2 inherit.cpp virtual.cpp

anand os sjf.c pcsem.c bankeralgo.c


network tcpchat.java sniffdata.c

Result :
Thus two-level directory structure has been demonstrated
c) Hirearchical Directory Structure
Aim:
To demonstrate tree-like hierarchical directory structure graphically.

Tree-Structured Directories:
A tree is the most common directory structure. Extends two-level directory structure to a tree of
arbitrary height. It allows users to create their own subdirectories and organize their files. The tree
has a root directory, and every file in the system has a unique path name. A directory (or
subdirectory) contains a set of files or subdirectories. Current directory contains files that are
required for that process. Path names can be of two types: absolute and relative.

Algorithm :
1. Define tree structure

2. Initialize graphics

3. Recursively obtain user files and folders under root in hierarchy

4. Display the directory structure graphically

5. Stop

Program:
/* Hierarchical directory structure - treedir.c */

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

struct tree_element

char name[20];

int x, y, ftype, lx, rx, nc, level;

struct tree_element *link[5]; };

typedef struct tree_element node;

main()

int gd=DETECT, gm;

node *root;

root = NULL;
clrscr();

create(&root, 0, "root", 0, 639, 320);

clrscr();

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

display(root);

getch();

closegraph();

create(node **root,int lev,char *dname,int lx,int rx,int x)

int i, gap;

if(*root == NULL)

(*root) = (node *)malloc(sizeof(node));

printf("Enter name of dir/file(under %s) : ", dname);

fflush(stdin);

gets((*root)->name);

printf("enter 1 for Dir / 2 for file : ");

scanf("%d", &(*root)->ftype);

(*root)->level = lev;

(*root)->y = 50 + lev * 50;

(*root)->x = x;

(*root)->lx = lx;

(*root)->rx = rx;

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

(*root)->link[i] = NULL;

if((*root)->ftype == 1)

{
printf("No of sub directories/files(for %s): ", (*root)->name);

scanf("%d", &(*root)->nc);

if((*root)->nc == 0)
gap = rx - lx;

else

gap = (rx - lx) / (*root)->nc;

for(i=0;i<(*root)->nc; i++)

create(&((*root)->link[i]), lev+1, (*root)->name, lx+gap* i, lx+gap*i+gap, lx+gap*i+gap/2);

else

(*root)->nc = 0; } }

display(node *root)

int i;

settextstyle(2, 0, 4);

settextjustify(1, 1);

setfillstyle(1, BLUE);

setcolor(14);

if(root != NULL) {

for(i=0; inc; i++)

line(root->x, root->y, root->link[i]->x, root>link[i]->y);

if(root->ftype == 1)

bar3d(root->x-20, root->y-10, root->x+20, root->y+10,0,0);

else

fillellipse(root->x, root->y,20, 20);

outtextxy(root->x, root->y,root ->name);

for(i=0; inc; i++)

display(root->link[i]); } }

Output:
Enter Name of dir/file(under root): ROOT

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for ROOT): 2

Enter Name of dir/file(under ROOT): USER1

Enter 1 for Dir / 2 for File: 1


No of subdirectories/files(for USER1): 1

Enter Name of dir/file(under USER1): SUBDIR1

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for SUBDIR1): 2

Enter Name of dir/file(under USER1): JAVA

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for JAVA): 0

Enter Name of dir/file(under SUBDIR1): VB

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for VB): 0

Enter Name of dir/file(under ROOT): USER2

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for USER2): 2

Enter Name of dir/file(under ROOT): A

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file(under USER2): SUBDIR2

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for SUBDIR2): 2

Enter Name of dir/file(under SUBDIR2): PPL

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for PPL): 2

Enter Name of dir/file(under PPL): B

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file(under PPL): C

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file(under SUBDIR): AI

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files(for AI): 2

Enter Name of dir/file(under AI): D

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file(under AI): E


Result:
Thus a hierarchical directory structure has been created and shown graphically.
Exp. No. 14 Implementation of File Allocation Strategies
Date:
a) Contiguous Allocation

Aim:
To implement file allocation on free disk space in a contiguous manner.

File Allocation:
The three methods of allocating disk space are:

1. Contiguous allocation

2. Linked allocation

3. Indexed allocation

Contiguous
Each file occupies a set of contiguous block on the disk. The number of disk seeks required is
minimal. The directory contains address of starting block and number of contiguous block (length)
occupied. Supports both sequential and direct access. First / best fit is commonly used for selecting a
hole.

Algorithm:
1. Assume no. of blocks in the disk as 20 and all are free.

2. Display the status of disk blocks before allocation.

3. For each file to be allocated:

a. Get the filename, start address and file length

b. If start + length > 20, then goto step 2.

c. Check to see whether any block in the range (start, start + length1) is allocated. If so, then go to
step 2.

d. Allocate blocks to the file contiguously from start block to start + length – 1.

4. Display directory entries.

5. Display status of disk blocks after allocation

6. Stop

Program :
/* Contiguous Allocation - cntalloc.c */

#include<stdio.h>

#include<string.h>
int num=0, length[10], start[10];

char fid[20][4], a[20][4];

void directory()

int i;

printf("\nFile Start Length\n");

for(i=0; i 20)

printf("Requirement exceeds range\n");

continue;

for(i=st; i<num;i++)

printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);

void display()

int i;

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

printf("%4d",i);

printf("\n");

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

printf("%4s", a[i]);

main()

int i,n,k,temp,st,nb,ch,flag;

char id[4];

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

strcpy(a[i], "");

printf("Disk space before allocation:\n");

display();
do

{ printf("\nEnter File name (max 3 char) : ");

scanf("%s", id);

printf("Enter start block : ");

scanf("%d", &st);

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

scanf("%d", &nb);

strcpy(fid[num], id);

length[num] = nb;

flag = 0;
if((st+nb) > 20)

printf("Requirement exceeds range\n");

continue;

for(i=st; i<st+nb);i++)

if(strcmp(a[i], "") != 0)

flag = 1;

if(flag == 1)

printf("Contiguous allocation not possible.\n");

continue;

start[num] = st;

for(i=st; i<(st+nb);i++)

strcpy(a[i], id);;

printf("Allocation done\n");

num++;

printf("\nAny more allocation (1. yes / 2. no)? : ");

scanf("%d", &ch);

}
while (ch == 1);

printf("\n\t\t\tContiguous Allocation\n");

printf("Directory:");

directory();

printf("\nDisk space after allocation:\n");

display();

printf("\n");

Output:
Disk space before allocation: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Enter File name (max 3 char) : ls

Enter start block : 3

Enter no. of blocks : 4

Allocation done Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : cp

Enter start block : 14

Enter no. of blocks : 3

Allocation done Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : tr

Enter start block : 18

Enter no. of blocks : 3

Requirement exceeds range

Enter File name (max 3 char) : tr

Enter start block : 10

Enter no. of blocks : 3


Allocation done Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : mv

Enter start block : 0

Enter no. of blocks : 2

Allocation done Any more allocation (1. yes / 2. no)? : 1

Enter File name (max 3 char) : ps


Enter start block : 12

Enter no. of blocks : 3

Contiguous allocation not possible.

Any more allocation (1. yes / 2. no)? : 2

Directory : Contiguous Allocation File Start Length


ls 3 4

cp 14 3

tr 10 3
mv 0 2

Disk space after allocation:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

my mv ls ls ls ls tr tr tr cp cp cp

Result:
Thus contiguous allocation is done for files with the available free blocks
b) Linked Allocation:
Aim
To implement file allocation on free disk space as a linked list of disk blocks.

Linked
Each file is a linked list of disk blocks. The directory contains a pointer to first and last blocks of the
file. The first block contains a pointer to the second one, second to third and so on. File size need not
be known in advance, as in contiguous allocation. No external fragmentation. Supports sequential
access only.

Indexed
In indexed allocation, all pointers are put in a single block known as index block. The directory
contains address of the index block. The ith entry in the index block points to ith block of the file.
Indexed allocation supports direct access. It suffers from pointer overhead, i.e wastage of space in
storing pointers.

Algorithm
1. Define file table as a linked list structure

2. Get number of files to be stored.

3. For each file:

a. Obtain number of disk blocks

b. Obtain randomly allocated disk blocks

c. Create a single linked list of nodes for the specified blocks.

4. Get the filename to be searched.

5. List disk blocks of that file as a linked list

6. Stop

Program
/* Linked list file allocation */

#include<stdio.h>

struct filetable
{

char name[20];

int nob;

struct block *sb;

}
ft[30];

struct block

int bno;

struct block *next;

};

main()

int i, j, n;

char str[20];

struct block *temp;

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

scanf("%d", &n);

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

{
printf("\nEnter file name %d : ",i+1);

scanf("%s", ft[i].name);

printf("Enter no of blocks in file %d : ", i+1);

scanf("%d", &ft[i].nob);

ft[i].sb = (struct block*)

malloc(sizeof(struct block));

temp = ft[i].sb;

printf("Enter the disk blocks : ");

scanf("%d", &temp ->bno);

temp->next = NULL;

for(j=1; j<ft[i].nob;j++){
temp->next = (struct block*)malloc(sizeof(structblock));

temp = temp->next;

scanf("%d", &temp->bno); }

temp->next = NULL; }

printf("\nEnter file name to be searched : ");


scanf("%s", str);

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

if(strcmp(str, ft[i].name)==0)

break;

if(i == n)

printf("\nFile Not Found");

else

{ printf("\nFilename No. of Blocks Blocks Occupied");

printf("\n %s\t\t%d\t", ft[i].name, ft[i].nob);

temp = ft[i].sb;

for(j=0; j<ft[i].nob;j++)

{ printf("%d -> ", temp->bno);

temp = temp->next; }

printf("NULL"); } }

Output :
Enter no. of files: 3

Enter file name 1 : hello.c

Enter no. of blocks in file 1 : 3

Enter the disk blocks : 12 23 34

Enter file name 2 : first.cpp

Enter no. of blocks in file 2 : 3

Enter the disk blocks : 22 33 44

Enter file name 3 : profile.doc

Enter no. of blocks in file 3 : 3

Enter the disk blocks : 87 76 65

Enter file name to be searched : first.cpp


Filename No. of Blocks Blocks Occupied

first.cpp 3 22 -> 33 -> 44 -> NULL

Result:
Thus linked list allocation is done for files with the available free blocks.
c) Indexed Allocation
Aim:
To implement Indexed file allocation technique in C.

Algorithm:
1. Start

2. Declare the index block no. and total no.of files in a block

3. Get the index block no. and total no.of files in a block from the user.

4.Allocate files based on the index block no.

5. Arrange the files based on indexes which are created for each fragment of the file such that each
and every similar indexed file is maintained by the primary index to provide the flow to file
fragments.

6. stop

PROGRAM:

#include<stdio.h>

#include<conio.h>

main()

{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;

clrscr();

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

scanf("%d",&n);

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

printf("Enter starting block and size of file%d:",i+1);

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

printf("Enter blocks occupied by file%d:",i+1);

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

printf("enter blocks of file%d:",i+1);

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

scanf("%d",&b[i][j]); }

printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)

printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);

printf("\nEnter file name:");

scanf("%d",&x);

printf("file name is:%d\n",x); i=x-1;

printf("Index is:%d",sb[i]);

printf("Block occupied are:");

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

printf("%3d",b[i][j]);

getch(); }

OUTPUT:
Enter no. of files:2

Enter starting block and size of file1: 2 5

Enter blocks occupied by file1:10

enter blocks of file1:3 2 5 4 6 7 2 6 4 7

Enter starting block and size of file2: 3 4

Enter blocks occupied by file2:5

enter blocks of file2: 2 3 4 5 6

File index length 1 2 10 2 3 5

Enter file name: venkat

file name is:12803

Index is:0Block occupied are:

Result :
Thus linked list allocation is done for files with the available free blocks.
Ex.no:15 Implementation Of Various Disk Scheduling Algorithm
Date:
A) FCFS
Aim :
Write C programs to simulate implementation FCFS Disk Scheduling Algorithm.

Algorithm:
1. 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.

2. Let us one by one take the tracks in default order and calculate the absolute distance of the track
from the head.

3. Increment the total seek count with this distance.

4. Currently serviced track position now becomes the new head position.

5. Go to step 2 until all tracks in request array have not been serviced.

Program:
#include<stdio.h>

int main()

{
int queue[20],n,head,i,j,k,seek=0,max,diff;

float avg;

printf("Enter the max range of disk\n");

scanf("%d",&max);

printf("Enter the size of queue request\n");

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

scanf("%d",&head);

queue[0]=head;

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

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

seek+=diff;

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 %f\n",avg); return 0;

OUTPUT:
Enter the max range of disk:200

Enter the size of queue request :8

Enter the queue of disk positions to be read

90 120 35 122 38 128 65 68

Enter the initial head position: 50

Disk head moves from 50 to 90 with seek: 40

Disk head moves from 90 to 120 with seek: 30

Disk head moves from 120 to 35 with seek: 85

Disk head moves from 35 to 122 with seek: 87

Disk head moves from 122 to 38 with seek :84

Disk head moves from 38 to 128 with seek :90

Disk head moves from 128 to 65 with seek :63

Disk head moves from 65 to 68 with seek :3

Total seek time is: 482

Average seek time is: 60.250000

RESULT:
Thus the program was executed and verified successfully
B] SSTF
Aim :
Write C programs to simulate implementation SSTF Disk Scheduling Algorithm.

Algorithm :

1. Let Request array represents an array storing indexes of tracks that have been requested. ‘head’ is
the position of disk head.

2. Find the positive distance of all tracks in the request array from head.

3. Find a track from requested array which has not been accessed/serviced yet and has minimum
distance from head.

4. Increment the total seek count with this distance.

5. Currently serviced track position now becomes the new head position.

6. Go to step 2 until all tracks in request array have not been serviced.

Program:
#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

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

float avg;

// clrscr();

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

printf("Enter the size of Queue\t");

scanf("%d",&n);

printf("Enter the Queue\t");

for(i=0;it[j])

temp=t[i];

t[i]=t[j];

t[j]=temp;

temp=queue[i];
queue[i]=queue[j];

queue[j]=temp; } } }

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

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

printf("Enter the initial head position\t");

scanf("%d",&head);

for(i=1;it[j])
{

temp=t[i];

t[i]=t[j];

t[j]=temp;

temp=queue[i] queue[i]=queue[j];

queue[j]=temp; } } }

for(i=1;i<n-1;i++)
{ seek=seek+abs(head-queue[i]);

head=queue[i]; }

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

avg=seek/(float)n;

printf("\nAverage Seek Time is %f\t",avg);

return 0; }

OUTPUT:
*** SSTF Disk Scheduling Algorithm ***

Enter the size of Queue 5

Enter the Queue 10 17 2 15 4

Enter the initial head position 3

Total Seek Time is14

Average Seek Time is 2.800000

RESULT:
Thus the program was executed and verified successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy