EASWARI ENGINEERING COLLEGE RAMAPURAM ... - Vidyarthiplus
Transcript of EASWARI ENGINEERING COLLEGE RAMAPURAM ... - Vidyarthiplus
1
EASWARI ENGINEERING COLLEGE
RAMAPURAM, CHENNAI-89
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
CS 6413- OPERATING SYSTEMS LABORATORY
ACADEMIC YEAR: 2014 - 2015
IV SEM CSE
www.Vidyarthiplus.com
www.Vidyarthiplus.com
2
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
3
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 Banker‟s 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
4
9.c. LFU
10. Interprocess communication using shared memory
11. Implementation of Paging technique
12. Implementation of Threading and Synchronization for a Banking Application using JAVA
Content beyond the Syllabus
1. Dead lock prevention algorithm for Multiple Resources.
2. Page Replacement Algorithm (Optimal)
www.Vidyarthiplus.com
www.Vidyarthiplus.com
5
Directory Commands
Command Description
pwd Shows the name and location of the directory where you are
curretly working.
ls Gives you a short list of the files in the directory where you
are currently working.
cd Moves you to another directory.
mkdir Creates a new subdirectory inside of the directory where
you are currently working
File related commands
Command Description
CAT>FILE Creates a file
more FILE Display contents of FILE, page by page.
cat FILE Display a file.
cat FILE1 FILE2 > NEW Append FILE1 and FILE2 creating new file NEW.
cat FILE1 >> FILE2 Append FILE1 at the end of FILE2.
sort FILE > NEWFILE Sort FILE, putting sorted version into NEWFILE.
grep ITEM FILE(S) Display lines of FILE(S) which contain ITEM.
wc FILE(S) Count characters, words and lines in FILE(S).
diff FILE1 FILE2 | more Show differences between two versions of a file
cp
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 Type file followed by the name of an existing file in the
directory.
Eg: file emergency3_demo.exe
www.Vidyarthiplus.com
www.Vidyarthiplus.com
6
chmod The chmod command allows you to alter access rights to
files and directories. All files and directories have security
permissions that grant the user particular groups‟ or all other
users‟ access.
Use chmod followed by the permission you are changing.
In very simple form this would be:
chmod 755 filename
The example above will grant you full rights, group rights to
execute and read, and all others access to execute the file.
# Permission
7 full
6 read and write
5 read and execute
4 read only
3 write and execute
2 write only
1 execute only
0 none
Use the table above to define the settings for the three
"users." In the command, the first number refers to your
permissions, the second refers to group, and the third refers
to general users.
Typing the command: chmod 751 filename
gives you full access, the group read and execute, and all
others execute only permission.
mv Type mv followed by the current name of a file and the new
name of the file.
Eg:
mv oldfile newfile
Type mv followed by the name of a file and the new
directory where you'd like to place the file.
Eg:
mv newfile testdir
This moves the file named newfile to an existing directory
named testdir. Be certain you‟re specifying a directory
name or the mv command alters the name of the file instead
of moving it.
rm
Type rm followed by the name of a file to remove the file.
Eg:
rm newfile Use the wildcard character to remove several files at once.
Eg:
rm n* This command removes all files beginning with n.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
7
Type rm -i followed by a filename if you‟d like to be
prompted before the file is actually removed.
Eg:
rm -i newfile
rm -i n*
By using this option, you have a chance to verify the
removal of each file. The -i option is very handy when
removing a number of files using the wildcard character *.
find Search for file with a specific name in a set of files find -name "rc.conf" -print
This command will search in the current directory and all
sub directories for a file named rc.conf.
Note: The -print option will print out the path of any file
that is found with that name. In general -print will print out
the path of any file that meets the find criteria.
How to search for a string in a selection of files (-exec grep
...). find. -exec grep "www.athabasca" '{}' \; -print
This command will search in the current directory and all
sub directories. All files that contain the string will have
their path printed to standard output.
Nl filename Inserts line number within the file
General Purpouse commands
date 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:
DATE: 02/08/01
TIME:16:44:55
bc 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
8
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 On BSD and some variants derived from BSD does not
begin a new line after the echoed text.
text The text that you want to echo to the screen.
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.
cal 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.
who Displays who is on the system.
man
The man command is short for manual
Shows you online manuals on Unix commands.
man [-] [-k keywords] topic
- Displays the manual without stopping.
-k keywords Searches for keywords in all of the manuals available.
topic Displays the manual for the topic or command typed in.
Man mkdir - Lists help information on the mkdir command.
Man -k irc - Quickly searches for manuals containing irc within them.
clear Clears the screen
write Send a message to another user.
write person [ttyname]
person If you wish to talk to someone on your own machine, then
person is just the person's login name. If you wish to talk
to a user on another host, then person is of the form
'user@host'.
ttyname If you wish to talk to a user who is logged in more than
www.Vidyarthiplus.com
www.Vidyarthiplus.com
9
once, the ttyname argument may be used to indicate the
appropriate terminal name, where ttyname is of the form
'ttyXX' or 'pts/X'
mail One of the ways that allows you to read/send E-Mail.
Mail - Opens the mail program with the first message in the mail (if
applicable).
Mail [email protected] - Starts a new e-mail, sending the e-
mail to the support at Computer Hope. When composing a message to
terminate the message type a period (.) and press enter.
tty To know terminal name
Filter commands
Grep
grep -i pattern file
grep -v pattern file
grep -c pattern file
grep -n pattern file
Finds text within a file.
Eg:
grep "unix" *.htm search all .htm files in the current directory for any reference of
unix and give results similar to the below example text
Prints all lines of file that contain the pattern, regardless of case.
For example, grep -i cg data prints any line of data that contains
"cg," "Cg," "cG," or "CG." The -i option ignores the case of the
string when searching.
Prints all lines of the file except those that contain the pattern.
Display only the number of matching lines
What the "-n" does is tell grep to print out the line number as well
as the line itself.
egrep
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.
sort Sorts the lines in a text file.
sort [-b] [-d] [-f] [-i] [-m] [-M] [-n] [-r] [-u] [+fields] filename [-
o outputfile]
-b Ignores spaces at beginning of the line.
-d Uses dictionary sort order and ignores the
punctuation.
-f Ignores caps
-i Ignores nonprinting control characters.
-m Merges two or more input files into one sorted
output.
-M Treats the first three letters in the line as a month
(such as may.)
-n Sorts by the beginning of the number at the
beginning of the line.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
10
-r Sorts in reverse order
-u If line is duplicated only display once
+fields Sorts by fields , usually by tabs
filename The name of the file that needs to be sorted.
-o outputfile Sends the sorted output to a file.
Sort -r file.txt - Would sort the file, file.txt in reverse order.
uniq Report or filter out repeated lines in a file.
uniq [-c | -d | -u ] [ -f fields ] [ -s char ] [-n] [+m] [input_file [
output_file ] ]
-c Precede each output line with a count of the number
Of times the line occurred in the input.
-d Suppress the writing of lines that are not repeated
In the input.
-u Suppress the writing of lines that are repeated
In the input.
-f fields Ignore the first fields fields on each input line
when doing comparisons, where fields is a positive
Decimal integer.
s char Ignore the first chars characters when doing
comparisons,
-n Equivalent to -f fields with fields set to n.
+m Equivalent to -s chars with chars set to m.
input_file A path name of the input file. If input_file is not
specified, or if the input_file is -, the
standard input will be used.
output_file A path name of the output file. If output_file is not
specified, the standard output will be used.
The results are unspecified if the file named by
output_file is the file named by input_file.
head Displays the first ten lines of a file, unless otherwise stated.
head [-number | -n number] filename
-number The number of the you wants to display.
-n number The number of the you wants to display.
filename The file that you want to display the x amount of
lines of.
Head -15 myfile.txt - Would display the first fifteen lines of
myfile.txt.
tail Delivers the last part of the file.
tail myfile.txt -n 100 The above example would list the last 100 lines in the file
myfile.txt.
cut Cut out selected fields of each line of a file.
Name=`who am i | cut -f1 -d' '` - set name to current login name.
paste Merge corresponding or subsequent lines of files.
The below example would take the input from ls and paste that
input into four columns.
ls | paste - - - -
www.Vidyarthiplus.com
www.Vidyarthiplus.com
11
tr Translate characters.
tr [-c] [-d] [-s] [string1] [string2]
-c Complement the set of characters specified by
string1.
-d Delete all occurrences of input characters that are
specified by string1.
-s Replace instances of repeated characters with a
single character.
string1 First string or character to be changed.
string2 Second string or character to change the string1.
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 Description
Pipe symbol (|). 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 Redirects work with files, not commands
tr '[a-z]' '[A-Z]' < $in_file > $out_file The command must come first, the in_file is directed in by the
less_than sign (<) and the out_file is pointed at by the greater_than
sign (>).
tee It is used to store output of a ling pipeline command to be stored for
later use.
$cat mast|sort|tee temp|cut –d “:” –f1
www.Vidyarthiplus.com
www.Vidyarthiplus.com
12
Ex.No.1. Basic UNIX Commands Date:
1. Create the following directory structure in UNIX
CSE
PG UG
2. Create files under CSE with extensions like *.txt, *.c etc.
3. Display all files with extension „txt‟
4. Rename all files with extension „txt‟ to „dat‟
5. Copy the files with extension C to UG directory
6. Delete all the files with extension C from CSE directory
7. Display all files with 2 character filename and extension „.txt‟
8. 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 employee‟s department and display the name of the employee and the
department
www.Vidyarthiplus.com
www.Vidyarthiplus.com
13
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
www.Vidyarthiplus.com
14
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
www.Vidyarthiplus.com
15
-eq : check fro equality of integers
-ne : check for inequality
-gt : check if one integer is greater than the other
-lt : check if one integer is lesser than the other
-ge : check if one integer is greater than or equal to the other
-le : check if one integer is lesser than or equal to the other.
-f : check if a file is an ordinary file
-d : check if a file is a directory
-r : check if a file is readable
-w : check if a file is write able
-x : check if a file is executable
String comparison operators
= equal to
!= not equal to
Logical operators
-a and
-o or
-! Not
Conditional execution operations
&& used to execute a command on successful execution of another command.
|| used to execute another command on failure of another command.
Read command
Used to read the value of the shell variable from a user.
Comment statement # this is a text program.
Programming language construct
1.a)if..then…else…fi b) if..then..elif..else ..fi
2.for…do…done
3.while..do..done
4.until…do..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
www.Vidyarthiplus.com
16
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. $echo “enter a value”
www.Vidyarthiplus.com
www.Vidyarthiplus.com
17
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
www.Vidyarthiplus.com
18
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
www.Vidyarthiplus.com
19
Ex No: 3 Implementation of Cpu Scheduling Algorithms 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 = starting time - arrival time
Turn around time= finishing time – arrival time
4. 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
www.Vidyarthiplus.com
20
Ex No: 3.b Shortest Job First 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 = starting time - arrival time
Turn around time= finishing time – arrival time
6. Display the starting and finishing time of each process in their execution order
7. Calculate the average waiting time and average turn around time
8. Display the average waiting time and average turn around time
9. Stop
www.Vidyarthiplus.com
www.Vidyarthiplus.com
21
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 = starting time - arrival time
Turn around time= finishing time – arrival time
6. Display the starting and finishing time of each process in their execution order
7. Calculate the average waiting time and average turn around time
8. Display the average waiting time and average turn around time
9. Stop
www.Vidyarthiplus.com
www.Vidyarthiplus.com
22
Ex No: 3.d Round Robin Scheduling 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 process‟s detail
8. Repeat the step 5, 6 till all the process are serviced completely.
9. Stop
www.Vidyarthiplus.com
www.Vidyarthiplus.com
23
Ex.No 4 Implementation of File Allocation Strategies Date:
4.a. Sequential File Allocation:
Aim:
To implement Sequential file allocation in C.
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
www.Vidyarthiplus.com
24
4.b. Indexed File 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
www.Vidyarthiplus.com
www.Vidyarthiplus.com
25
EX No: 4.c. Linked File Allocation
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
www.Vidyarthiplus.com
26
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
www.Vidyarthiplus.com
27
Ex.No.6.a Implementation of File Organization Techniques 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
www.Vidyarthiplus.com
28
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
www.Vidyarthiplus.com
29
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
www.Vidyarthiplus.com
30
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. Calculate N-choose-2 = n (n-1)/2 edges, but no cycles.
5. Stop.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
31
Ex.No: 7 Implementation of Banker’s Algorithm for Deadlock Avoidance Date:
Aim:
To detect and prevent deadlock using Banker‟s 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.
Resource – request algorithm
1. If Request i < = Needi , go to step 2. Otherwise, error
2. If Request i < = Available, go to step 3. Otherwise, Pi must wait, since resources are not available
3. Modify the state ( the system pretend to have allocated the requested resources to process Pi )
Available = Available - Requesti
Allocationi = Allocationi + Requesti
Needi = Needi - Requesti
4. If the resulting state is safe, the transaction is completed and process Pi is allocated its resources. If
new state is unsafe, then Pi must wait for Requesti and the old state is restored.
5. Stop.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
32
Ex. No: 8 Implementation of Deadlock Detection 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
www.Vidyarthiplus.com
33
Ex. No: 9 Page Replacement Algorithm Date:
Ex.No.9.a. FIFO page replacement algorithm
Aim
To write a c program to implement FIFO page replacement algorithm
Algorithm
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
www.Vidyarthiplus.com
www.Vidyarthiplus.com
34
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
www.Vidyarthiplus.com
35
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
www.Vidyarthiplus.com
36
Ex. No: 10 Inter Process Communication 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
www.Vidyarthiplus.com
37
Ex. No: 11 Implementation of Paging Techniques Date: Aim: To implement memory allocation with pages
Algorithm:
1. Start
2. Declare the structure P_table with variables for page no and frame no
3. Display the status of physical memory
4. Get the number of pages needed for a process
5. Get the contents of the pages
6. Display the contents of logical memory
7. If the physical memory is available then allot the pages of the process
8. Update the physical memory status
9. Update the page table status
10. Display the page table after allocation
11. Display the physical memory after allocation
12. Stop
www.Vidyarthiplus.com
www.Vidyarthiplus.com
38
Ex. No: 12 Implementation of Threading and Synchronization for a Banking
Application using JAVA Date:
Aim: To implement Banking system involving Concurrency (Thread)
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
www.Vidyarthiplus.com
39
Content beyond the Syllabus
Ex. No: 13 Deadlock Prevention Algorithm for Multiple Resources Date:
Aim: To detect and prevent deadlock using Banker‟s 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
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.
Resource – request algorithm
1. If Request i < = Needi , go to step 2. Otherwise, error
2. If Request i < = Available, go to step 3. Otherwise, Pi must wait, since resources are not available
3. Modify the state ( the system pretend to have allocated the requested resources to process Pi )
i. Available = Available - Requesti
ii. Allocationi = Allocationi + Requesti
iii. Needi = Needi - Requesti
4. If the resulting state is safe, the transaction is completed and process Pi is allocated its resources. If
new state is unsafe, then Pi must wait for Requesti and the old state is restored.
5. Stop.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
40
Ex. No: 14 PAGE REPLACEMENT ALGORITHM – Optimal Date:
Aim: To demonstrate page replacement algorithm.
Algorithm:
1. An optimal page-replacement algorithm has the lowest page-fault rate of all algorithms (called
OPT or MIN). It is simply this: 2. Replace the page that will not be used 3. for the longest period of time.
4. Use of this page-replacement algorithm guarantees the lowest possible page-fault rate for a fixed
number of frames.
5. For example, on our sample reference string, the optimal page-replacement algorithm would
yield nine page faults.
Optimal Page replacement
1 2 3 4 1 2 6 5 2 1 2 7 3 6 1 2 3 2 3 6
8 Page Faults
o The first three references cause faults that fill the three empty frames.
o The reference to page 2 replaces page 7, because 7 will not be used until reference 18,
o Whereas page 0 will be used at 5, and page 1 at 14.
o 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.
6. 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.
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3
4 6 5 7 6
www.Vidyarthiplus.com
www.Vidyarthiplus.com