OS Lab Manual
OS Lab Manual
com
IV SEM CSE
www.Vidyarthiplus.com
www.Vidyarthiplus.com
OBJECTIVES:
The student should be made to:
Learn shell programming and the use of filters in the UNIX environment.
Be exposed to programming in C using system calls.
Learn to use the file system related system calls.
Be exposed to process creation and inter process communication.
Be familiar with implementation of CPU Scheduling Algorithms, page replacement algorithms
and Deadlock avoidance
LIST OF EXPERIMENTS:
1. Basics of UNIX commands.
2. Shell Programming.
3. Implement the following CPU scheduling algorithms
a) Round Robin b) SJF c) FCFS d) Priority
4. Implement all file allocation strategies
a) Sequential b) Indexed c) Linked
5. Implement Semaphores
6. Implement all File Organization Techniques
a) Single level directory b) Two level c) Hierarchical d) DAG
7. Implement Bankers Algorithm for Dead Lock Avoidance
8. Implement an Algorithm for Dead Lock Detection
9. Implement e all page replacement algorithms
a) FIFO b) LRU c) LFU
10. Implement Shared memory and IPC
11. Implement Paging Technique of memory management.
12. Implement Threading & Synchronization Applications
TOTAL: 45 PERIODS
OUTCOMES:
At the end of the course, the student should be able to
Implement deadlock avoidance, and Detection Algorithms
Compare the performance of various CPU Scheduling Algorithm
Critically analyze the performance of the various page replacement algorithms
Create processes and implement IPC
www.Vidyarthiplus.com
www.Vidyarthiplus.com
INDEX
1. Basic UNIX Commands
2. Shell Programming
3. Implementation of CPU Scheduling Algorithms
Non-preemptive CPU Scheduling
3. a. First come first serve Scheduling
3. b. Shortest Job first Scheduling
Preemptive CPU scheduling
3. c. Priority Scheduling
3. d. Round Robin Scheduling
4. Implementation of File allocation Strategies
4. a. Sequential allocation
4. b. Linked allocation
4. c. Indexed Allocation
5. Implementation of Semaphores
6. Implementation of File Organization Techniques
6.a. Single-level directory
6.b. Two-level directory
6.c. Hierarchial
6.d. DAG
7. Implementation of Bankers Algorithm for Deadlock Avoidance
8. Implementation of Deadlock Detection
9. Implementation of Page Replacement Algorithms
9.a. FIFO
9.b. LRU
www.Vidyarthiplus.com
www.Vidyarthiplus.com
9.c. LFU
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Directory Commands
Command
pwd
ls
cd
mkdir
Description
Shows the name and location of the directory where you are
curretly working.
Gives you a short list of the files in the directory where you
are currently working.
Moves you to another directory.
Creates a new subdirectory inside of the directory where
you are currently working
Command
CAT>FILE
more FILE
cat FILE
cat FILE1 FILE2 > NEW
cat FILE1 >> FILE2
sort FILE > NEWFILE
grep ITEM FILE(S)
wc FILE(S)
diff FILE1 FILE2 | more
cp
Description
Creates a file
Display contents of FILE, page by page.
Display a file.
Append FILE1 and FILE2 creating new file NEW.
Append FILE1 at the end of FILE2.
Sort FILE, putting sorted version into NEWFILE.
Display lines of FILE(S) which contain ITEM.
Count characters, words and lines in FILE(S).
Show differences between two versions of a file
Type cp followed by the name of an existing file and the
name of the new file.
Eg:
cp sourcefile destfile
To copy a file to a different directory specify the directory
instead of filename.
Eg:
cp newfile testdir
To copy a file to a different directory and create a new file
name, you need to specify a directory/a new file name.
Eg:
cp newfile testdir/newerfile
cp newfile ../newerfile
The .. represents one directory up in the hierarchy.
file
www.Vidyarthiplus.com
www.Vidyarthiplus.com
chmod
mv
rm
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Type rm -i followed by a filename if youd like to be
prompted before the file is actually removed.
Eg:
rm -i newfile
rm -i n*
find
Nl filename
print or set the system date and time to set date and time
date -s "11/20/2003 12:48:00" - Set the date to the date and time shown.
date '+DATE: %m/%d/%y%nTIME:%H:%M:%S' - Would list the time
and date in the below format:
bc
DATE: 02/08/01
TIME:16:44:55
Calculator
bc [-c] [-l] [file]
-c
Compile only. The output is dc commands that are sent to
the standard output.
-l
Define the math functions and initialize scale to 20,
instead of the default zero.
file
Name of the file that contains the bc commands to be
calculated this is not a necessary command.
Within the cal.txt file you could have a simple statement such as:
/* Add the value 1+2 /*
www.Vidyarthiplus.com
www.Vidyarthiplus.com
1+2
quit
bc cal.txt
When running the above command you will receive the results of the cal.txt
file. Which in this case would be 3.
echo
Echo's to the screen what you type after echo. Echo is useful for producing
diagnostics in command files, for sending known data into a pipe, and for
displaying the contents of environment variables.
echo [-n] text
-n
cal
who
man
clear
write
text
echo Hello world
The above example would return "Hello world" to the console
echo * | wc
The above example would list a count of all the files and directories in the
current directory.
Calendar for the month and the year.
cal [month] [year]
month
Specifies the month for you want the calendar to be
displayed. Must be the numeric representation of the
month. For example: January is 1 and December is 12.
year
Specifies the year that you want to be displayed.
Cal - Would give you the calendar for this month.
Cal 12 2000 - Would give you the calendar for December of 2000.
Displays who is on the system.
The man command is short for manual
Shows you online manuals on Unix commands.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
tty
Mail [email protected] - Starts a new e-mail, sending the email to the support at Computer Hope. When composing a message to
terminate the message type a period (.) and press enter.
To know terminal name
Filter commands
Grep
Prints all lines of the file except those that contain the pattern.
What the "-n" does is tell grep to print out the line number as well
as the line itself.
Search a file for a pattern using full regular expressions.
egrep "support|help|windows" myfile.txt - Would search for
patterns of support help and windows in the file myfile.txt.
Sorts the lines in a text file.
sort [-b] [-d] [-f] [-i] [-m] [-M] [-n] [-r] [-u] [+fields] filename [o outputfile]
Ignores spaces at beginning of the line.
-b
Uses dictionary sort order and ignores the
-d
punctuation.
Ignores caps
-f
Ignores nonprinting control characters.
-i
Merges two or more input files into one sorted
-m
output.
Treats the first three letters in the line as a month
-M
(such as may.)
Sorts by the beginning of the number at the
-n
beginning of the line.
egrep
sort
www.Vidyarthiplus.com
www.Vidyarthiplus.com
uniq
head
tail
cut
paste
www.Vidyarthiplus.com
10
www.Vidyarthiplus.com
tr
Translate characters.
tr [-c] [-d] [-s] [string1] [string2]
Complement the set of characters specified by
-c
string1.
Delete all occurrences of input characters that are
-d
specified by string1.
Replace instances of repeated characters with a
-s
single character.
First string or character to be changed.
string1
Second string or character to change the string1.
string2
EX:
Echo "12345678 9247" | tr 123456789 computerh - this
example takes an echo response of '12345678 9247' and pipes it
through the tr replacing the appropriate numbers with the letters.
In this example it would return computer hope.
Shell Commands
command
Pipe symbol (|).
Description
Pipes are a UNIX feature which allows you to connect several
commands together in one line and pass data from one to the next
much like a chain.
pipe connects one commands output to the next commands input
directs UNIX to connect stdout from the first command to the stdin
of the second command
line_count=`wc -l $filename | cut -c1-8`
the wc -l command counts the number of lines in the filename
contained in the variable $filename. This text string is then piped to
the cut command which snips off the first 8 characters and passes
them on to stdout, hence setting the variable line_count.
upper_case=`echo $lower_case | tr '[a-z]' '[A-Z]'`
redirection
tee
www.Vidyarthiplus.com
11
www.Vidyarthiplus.com
Ex.No.1.
Date:
PG
UG
Create files under CSE with extensions like *.txt, *.c etc.
Display all files with extension txt
Rename all files with extension txt to dat
Copy the files with extension C to UG directory
Delete all the files with extension C from CSE directory
Display all files with 2 character filename and extension .txt
Create a file with list of subject names and Display the first three and last three
Subjects stored in the file.
9. List the contents of the file from the fourth subject from the end of the file and also list the
contents of the file from the seventh subject from the beginning of the file
10. Create a file named INDIA which contains the following data
India is my country
All Indians are my
Brothers and Sister
I love my country
a) Use the filter command to search for the pattern my
b) Display all the lines that do not contain the above pattern
c) Count the total number of lines containing the pattern my
d) Display all the lines which contains the pattern country
11. Create a file with 15 lines of data. Using filter commands select top 10 lines, Last
8 lines and display them in two different files. Add line numbers to the files.
12. Create a file containing 10 lines of information. Count the number of lines,
number of words and number of character
13. Redirect the contents of ls p to a file called LISTING. Give the commands to
Redirect the output of a long listing of files and append it to the file LISTING
14. Change the modes of those files which begin with the letter s in such a way that the owner has
read and execute permission, the group has read and write permission and others only read
permission(use octaldecimal representation)
15. Create a file called MARK which contains the sample data as follows:
S001 Raja dbms 78
S002 Usha os 96
a) Display the contents of the file sorted according to the marks in the descending order
b) Display the names of the students in the alphabetic order ignoring the cases
c) Display the list of students who have scored marks between 50 and 80
d) Display the list of students and their registration numbers
e) Sort the files according to the third field and dump it to the file called SCODE
16. Create a file called EMP as given below:
E001:malar:mktg:5000
E002:balan:acct:7000
a) Sort the file on the employees department and display the name of the employee and the
department
2.
3.
4.
5.
6.
7.
8.
www.Vidyarthiplus.com
12
www.Vidyarthiplus.com
b) List the employees who earn between 4000 and 6000
c) Sort the file on the employee name in the reverse order and extract their codes and their names
d) Display the contents of the file without redundancy in sorted order
17. Display the permission of the group for the files whose names begin with p
18. Give the command to extract logins of the people whose login name starts with CSE
19. Display the entire text of the file in uppercase
20. Give the command to extract the links, the file owner and the file name only in the current
directory.
www.Vidyarthiplus.com
13
www.Vidyarthiplus.com
Ex.No.2.
Shell Programming
Date:
Shell programming is a group of commands grouped together under single filename. The shell interprets
the input, takes appropriate action, and finally displays the output. Shell scripts are dynamically
interpreted, not compiled.
Types of shell:
Bourne shell sh
C shell
csh
Korne Shell ksh
Creation and execution of shell scripts using command line editor:
1. creation
$ cat > greet
echo please enter your name:
read name
echo hi! Welcome to this session $name
Ctrl + D
2. Execution
$ sh greet
please enter your name: jaya
hi! Welcome to this session jaya
Valid shell variables:
n
area
a1
account
a_count
Assigning values to variable:
Variable=value
Displaying values of variables:
$ echo value of n is $n
Operators:
Arithmetic Operators provided by the shell are +,- * and /
Logical operators
-a and
-o or
! not
Relational operators
www.Vidyarthiplus.com
14
www.Vidyarthiplus.com
-eq
-ne
-gt
-lt
-ge
-le
-f
-d
-r
-w
-x
b) if..then..elif..else ..fi
2.fordodone
3.while..do..done
4.untildo..done
5.case esac
1) if construct
useful for executing a set of commands based on the condition being true and alternate set of
commands to be executed if the condition is false.
Ex. if (grep India countri.dat)
then
echo pattern found
else
echo pattern not found
fi
2) for construct
used to perform same set of operations on a list of values.
www.Vidyarthiplus.com
15
www.Vidyarthiplus.com
for variable in value1 value2 value3
do
Commands
done
Ex. for k in 1 2 3 4 5
do
echo the number is $k
echo the square of the number is `expr $k \* $k`
done
3)while construct
Repeatedly executing group of commands as long as the condition is true.
while condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
while [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
4) until construct
Repeatedly executing group of commands until a condition is true.
until condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
until [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
5) case construct:
case value in
choice1) commands;;
choice2)commands;;
.
esac
Ex.
www.Vidyarthiplus.com
16
www.Vidyarthiplus.com
read myval
case $myval in
0) echo zero;;
1) echo one;;
2) echo two;;
3) echo three;;
*) echo invalid argument;;
esac
Exercise:
1. Greatest among three numbers
Algorithm:
Step 1. Read 3 numbers n1,n2 and n3
Step 2. Check if n1 is greater than n2
check n1 is greater than n3 also
announce n1 as greatest
announce n3 as greatest
else, check whether n2 is greater than n3
if yes, announce n2 as greatest
else announce n3 as greatest
2. Factorial of a given number
Algorithm:
Step 1. Read n
Step 2. Initialize fact to 1 and i to n
Step 3. Repeat the following until i>0
Assign fact * i to fact
Decrement i by 1
3. Sum of Odd numbers upto n
Algorithm:
Step 1. Read n
Step 2. Initialize x=1 and sum=0
Step 3. Repeat the following until x < n
Assign sum + x to sum
Increment x by 2
4.Generation of Fibonacci numbers
Algorithm:
Step 1. Read n
Step 2. Initialize p=-1, q=1 and I=1
Step 3. Repeat the following until I < n
Assign p + q to r
Assign q to p
Assign r to q
Increment I by 1
5.Implement the Arithmetic calculator
Algorithm:
Step 1. Read a, b and option
Step 2. According to the option perform the operation
www.Vidyarthiplus.com
17
www.Vidyarthiplus.com
6.Write a Shell program to find the largest digit of a number
Algorithm:
Step 1: Get a number from the user
Step 2: Obtain individual digit for the above number using modulo operator
Step 3: Initialize variable max with first digit
Step 4; Compare the value of max with the other digits,
if the value of max is lesser update the value of max
Step 5: Display the value of max
7.Check whether given string is a palindrome or not.
Algorithm:
Step 1. Read a String
Step 2. Find the length of the string
Step 3. Start reading from the last character to the first character and store it as a new string
in temp
Step 4. Compare both the strings, if it is same the given string is palindrome
8.Write a Shell program to find out the reverse of a given number.
Algorithm:
Step 1: Get a number from the user
Step 2: Set a loop upto the number is not equal to zero
Step 3: reminder=number%10
Step 4:rnum=rnum*10+reminder
Step 5: number=number/10
Step 6: if number==rnum print both are same
www.Vidyarthiplus.com
18
www.Vidyarthiplus.com
Ex No: 3
Date:
Non-Pre-emptive Sheduling:
3.a. First Come First Serve
Aim:
To implement FCFS Scheduling algorithm using C.
Algorithm:
1.
Start
2.
Read the process name, Arrival time, its CPU burst from the user
3.
Calculate waiting time and turn around for each process on a first come first basis
Waiting time
Display the starting and finishing time of each process in their arrival time order
5.
Calculate the average waiting time and average turn around time
6.
Display the average waiting time and average turn around time
7.
Stop
www.Vidyarthiplus.com
19
www.Vidyarthiplus.com
Ex No: 3.b
Date:
Aim:
To implement SJF Scheduling algorithm in C.
Algorithm:
1. Start
2. Declare a structure Proc_stru with member variables such as process name, CPU burst, arrival
time, waiting time and turn around time
3. Read the process name, Arrival time, its CPU burst from the user and store it in the structure
Proc_stru
4. Sort the process in the ascending order of their CPU burst
5. Calculate waiting time and turn around time for each process which ordered by the CPU burst time
Waiting time
Display the average waiting time and average turn around time
9. Stop
www.Vidyarthiplus.com
20
www.Vidyarthiplus.com
Pre-emptive CPU Scheduling
Ex No: 3.c
Priority Scheduling
Date:
Aim:
To implement Priority Scheduling algorithm in C.
Algorithm
1. Start
2. Declare a structure Proc_stru with member variables such as process name, CPU burst, priority,
arrival time, waiting time and turn around time
3. Read the process name, Arrival time, its CPU burst, priority from the user and store it in the
structure Proc_stru
4. Sort the process in the ascending order of their Priority
5. Calculate waiting time and turn around time for each process which ordered by their Priority
Waiting time
Display the average waiting time and average turn around time
9. Stop
www.Vidyarthiplus.com
21
www.Vidyarthiplus.com
Ex No: 3.d
Date:
Aim:
To implement Round Robin Scheduling Algorithm in C.
Algorithm
1. Start
2. Declare a structure Proc_stru with member variables such as process name, CPU burst
3. Read the process name, its CPU burst from the user and store it in the structure Proc_stru
4. Get from the user CPU Time slice
5. Allot the process to CPU for given time slice
6. Switch the CPU to next process when time slice over
7. Display the currently allotted processs detail
8. Repeat the step 5, 6 till all the process are serviced completely.
9. Stop
www.Vidyarthiplus.com
22
www.Vidyarthiplus.com
Ex.No 4
Date:
Algorithm:
1. Start
2. Declare the starting block no. and the length of the file.
3. Get the Starting block no. and length of the file from the user.
4. Allocate files sequentially until end of the file.
5. Display the fragments of the file.
6. stop
www.Vidyarthiplus.com
23
www.Vidyarthiplus.com
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
www.Vidyarthiplus.com
24
www.Vidyarthiplus.com
Aim:
To allocate the files in the secondary storage using Linked allocation technique
Algorithm:
1. Start
2. Initialize the AVAIL linked list, where each node consist of starting address, size of the empty
block and a link for next available node
3. Initialialize the FAT ( File Allocation Table) which is implemented as array of pointers.
4. Display the AVAIL List
5. Read File allocation request which consist of File name, No of blocks and its contents
6. Traverse the AVAIL linked list from the starting node
7. Retrieve the required no of blocks from AVAIL List
8. Assign the contents of file to the retrieved blocks
9. Update the FAT by making an entry in FAT
10. Update the AVAIL LIST
11. Display the AVAIL List and FAT table
12. Stop
www.Vidyarthiplus.com
25
www.Vidyarthiplus.com
EX No: 5
Implementation of Semaphores
Date:
Aim:
To implement the Producer and Consumer Problem using semaphores
Algorithm:
1. Start
2. Initialize the semaphore variable S
3. In the producer function ,
3a)While s ==1 do nothing
3b)Produce the value
3c) Assign s=1
3d) Return
4. In the Consumer function
4a)While s==0 do nothing
4b)Display the consumed value
4c)Assign s=0
4d) Return
5. Create threads for producer and consumer function to make it run concurrently
6. Stop
www.Vidyarthiplus.com
26
www.Vidyarthiplus.com
Ex.No.6.a
Date:
Aim:
To implement Single level directory structure in C.
Algorithm:
1. Start
2. Declare the number, names and size of the directories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories.
5. Stop.
www.Vidyarthiplus.com
27
www.Vidyarthiplus.com
Ex.No. 6.b. Two-level directory Structure
Aim:
To implement Two-level directory structure in C.
Algorithm:
1. Start
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
www.Vidyarthiplus.com
28
www.Vidyarthiplus.com
Ex.No.6.c. Hierarchical directory Structure
Aim:
To implement hierarchial directory structure in C.
Algorithm:
1. Start
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
www.Vidyarthiplus.com
29
www.Vidyarthiplus.com
Ex.No.6.d. Directed Acyclic Graph (DAG)
Aim:
To implement Directed Acyclic Graph in C.
Algorithm:
1. Start
2. Collect set of nodes 1, 2, , n
3. Get the value of an edge (i,j) whenever i < j
4.
5. Stop.
www.Vidyarthiplus.com
30
www.Vidyarthiplus.com
Ex.No: 7 Implementation of Bankers Algorithm for Deadlock Avoidance
Date:
Aim:
To detect and prevent deadlock using Bankers algorithm.
Algorithm:
Safety algorithm
1. Start
2. Initialize a temporary vector W (Work) to equal the available vector A.
3. Find an index i (row i) such that
Need i = W
If no such row exists, the system will deadlock, since no process can run to completion.
4. If such a row is found, mark this process as finished, and add all its resources to W
Vector i,
W = W + Ci
Go to step 2, until either all processes are marked terminated (in this case initial state is safe), or
until a deadlock occurs, in which the state is not safe.
www.Vidyarthiplus.com
31
www.Vidyarthiplus.com
Ex. No: 8
Date:
Aim
Alg I: Simply detects the existence of a Cycle:
1. Start at any vertex finds all its immediate neighbors.
2. From each of these find all immediate neighbors, etc.
3. Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle).
4. Stop.
Alg 2: On a copy of the graph:
1. See if any Processes NEEDs can all be satisfied.
2. If so satisfy the needs with holds and remove that Process and all the Resources it holds from
3. the graph.
4. If any Process are left Repeat step a
5. If all Processes are finally removed by this procedure there is no Deadlock in the original graph, if
not there is.
6. Stop.
www.Vidyarthiplus.com
32
www.Vidyarthiplus.com
Ex. No: 9
Date:
Aim
To write a c program to implement FIFO page replacement algorithm
Algorithm
www.Vidyarthiplus.com
33
www.Vidyarthiplus.com
Ex.No.9.b. LRU page replacement algorithm
Aim:
To write a c program to implement LRU page replacement algorithm
Algorithm:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
www.Vidyarthiplus.com
34
www.Vidyarthiplus.com
Ex.No.9.c. LFU page replacement algorithm
Aim:
To write a c program to implement LFU page replacement algorithm
Algorithm:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least frequently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
www.Vidyarthiplus.com
35
www.Vidyarthiplus.com
Ex. No: 10
Date:
Aim:
To develop a client-server application program, this uses shared memory using IPC
Algorithm:
Server:
7. Define shared memory size of 30 bytes
8. Define the key to be 5600
9. Create a shared memory using shmget () system calls and gets the shared memory id in variable
shmid.
10. Attach the shared memory to server data space
11. Get the content to be placed in the shared memory from the user of the server.
12. Write the content in the shared memory, which will read out by the client.
13. stop
Client :
1. Define the key to be 5600
2. Attach the client to the shared memory created by the server.
3. Read the content from the shared memory.
4. Display the content on the screen.
5. stop
www.Vidyarthiplus.com
36
www.Vidyarthiplus.com
Ex. No: 11
Aim:
Date:
Algorithm:
1.
Start
2.
Declare the structure P_table with variables for page no and frame no
3.
4.
5.
6.
7.
If the physical memory is available then allot the pages of the process
8.
9.
10.
11.
12.
Stop
www.Vidyarthiplus.com
37
www.Vidyarthiplus.com
Ex. No: 12 Implementation of Threading and Synchronization for a Banking
Application using JAVA
Date:
Aim:
Algorithm:
1. Create a Bank Database
2. Create functions for adding a new user, depositing the amt, withdrawing the amt & viewing the
customer
3. Create a thread for each functionality
4. Implement the concurrency control among the threads
5. Function add new user
a. Get the user details namely, Acno, Name & Bank Balance
b. Write the user details to the bank database
6. Function Deposit
a. Get the Acno & Amt
b. Update the Bank balance for the given Acno
7. Function Withdraw
a. Get the Acno & Amt
b. Update the Bank balance for the given Acno
8. Function View
a. Get the Acno
b. Display the account details
www.Vidyarthiplus.com
38
www.Vidyarthiplus.com
Content beyond the Syllabus
Algorithm:
Safety algorithm
1. Start
2. Initialize a temporary vector W (Work) to equal the available vector A.
3. Find an index i (row i) such that
1. Need i = W
b. If no such row exists, the system will deadlock, since no process can run to completion.
4. If such a row is found, mark this process as finished, and add all its resources to W
a. Vector i,
1. W = W + Ci
b. Go to step 2, until either all processes are marked terminated (in this case initial state is
safe), or until a deadlock occurs, in which the state is not safe.
www.Vidyarthiplus.com
39
www.Vidyarthiplus.com
Ex. No: 14
Aim:
Date:
Algorithm:
1.
An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms (called
OPT or MIN). It is simply this:
1
2
1
2
3
1
2
3
4
1
2
3
6
1
2
3
5
7
1
2
3
7
1
2
3
6
8 Page Faults
o
o
o
o
The first three references cause faults that fill the three empty frames.
The reference to page 2 replaces page 7, because 7 will not be used until reference 18,
Whereas page 0 will be used at 5, and page 1 at 14.
The reference to page 3 replaces page 1, as page 1 will be the last of the three pages in
memory to be referenced again.
With only nine page faults, optimal replacement is much better than a FIFO algorithm, which
resulted in fifteen faults.
7. If we ignore the first three, which all algorithms must suffer, then optimal replacement is twice as
good as FIFO replacement.
8. Unfortunately, the optimal page-replacement algorithm is difficult to implement, because it
requires future knowledge of the reference string (similar situation with the SJF CPU-scheduling
algorithm).
9. As a result, the optimal algorithm is used mainly for comparison studies.
6.
www.Vidyarthiplus.com
40