Você está na página 1de 32

Exp. No.

: 01
Date:
BASICS OF UNIX COMMANDS
AIM :
To study the basic unix commands and their usage
Directory Commands
Command
pwd
ls
cd
mkdir

File related commands


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
Shows the name and location of the directory where you are
currently 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

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.

Department of Information Technology

IT6412 Operating Systems Lab

file

chmod

Type file followed by the name of an existing file in the


directory.
Eg: file emergency3_demo.exe
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
full
read and write
read and execute
read only
write and execute
write only
execute only
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.

Department of Information Technology

IT6412 Operating Systems Lab

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.

rm

Eg:
rm n*
This command removes all files beginning with n.
Type rm -i followed by a filename if you would 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.
Inserts line number within the file

Nl filename

General Purpose commands


print or set the system date and time to set date and time
date
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
Department of Information Technology

IT6412 Operating Systems Lab

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 /*
1+2
quit
bc cal.txt
When running the above command you will receive the results of the cal.txt
file. 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
text

cal

On BSD and some variants derived from BSD does not


begin a new line after the echoed 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.
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.

Department of Information Technology

IT6412 Operating Systems Lab

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
write

Clears the screen


Send a message to another user.
write person [ttyname]

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 support@computerhope.com - 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.

tty

To know terminal name

Filter commands
Grep

grep -i 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.

grep -v pattern file

Prints all lines of the file except those that contain the pattern.

grep -c pattern file

Display only the number of matching lines

grep -n pattern file

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.

Department of Information Technology

IT6412 Operating Systems Lab

sort

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
-m
-M
-n
-r
-u
+fields
filename
-o
outputfile

uniq

Merges two or more input files into one sorted


output.
Treats the first three letters in the line as a month
(such as may.)
Sorts by the beginning of the number at the
beginning of the line.
Sorts in reverse order
If line is duplicated only display once
Sorts by fields , usually by tabs
The name of the file that needs to be sorted.
Sends the sorted output to a file.

in reverse
-r -r file.txt Sorts
Sort
- Would
sort theorder
file, file.txt in reverse order
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.
Suppress the writing of lines that are not
-d
repeated In the input.
Suppress the writing of lines that are
-u
repeated In the input.
Ignore the first fields fields on each input line
-f fields
when doing comparisons, where fields is a
positive Decimal integer.
Ignore the first chars characters when
s char
doing comparisons,
Equivalent to -f fields with fields set to n.
-n
Equivalent to -s chars with chars set to m.
+m
input_file A path name of the input file. If input_file is
specified,
if the
input_file
-, the
A path
name oforthe
output
file. Ifisoutput_file
is
output_file not
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.
-r

Department of Information Technology

Sorts in reverse order

IT6412 Operating Systems Lab

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 out selected fields of each line of a file.
Name=`who am i | cut -f1 -d' '` - set name to current login name.

cut
paste

tr

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 - - - Translate characters.
tr [-c] [-d] [-s] [string1] [string2]
-c
-d
-s
string1
string2

Complement the set of characters specified by


string1.
Delete all occurrences of input characters that are
specified by string1.
Replace instances of repeated characters with a
single character.
First string or character to be changed.
Second string or character to change the string1.

EX:
Echo "12345678 9247" | tr 123456789 computer hope - 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.

Department of Information Technology

IT6412 Operating Systems Lab

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

tee

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 (>).
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

Exercise:
1. Create the following directory structure in UNIX
CSE
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

2.
3.
4.
5.
6.
7.
8.

Department of Information Technology

IT6412 Operating Systems Lab

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.
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.

RESULT:
The various Unix operating system commands are studied and their usage are
understood.
Department of Information Technology

IT6412 Operating Systems Lab

Exp. No. : 02
Date:
SHELL PROGRAMMING
AIM:
To write shell scripts in Unix operating system using various programming constructs
Introduction:
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
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
-eq : check fro equality of integers
-ne : check for inequality
Department of Information Technology

IT6412 Operating Systems Lab

10

-gt
-lt
-ge
-le
-f
-d
-r
-w
-x

: check if one integer is greater than the other


: check if one integer is lesser than the other
: check if one integer is greater than or equal to the other
: check if one integer is lesser than or equal to the other.
: check if a file is an ordinary file
: check if a file is a directory
: check if a file is readable
: check if a file is write able
: 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..thenelsefi

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
Department of Information Technology

IT6412 Operating Systems Lab

11

2) for construct
Used to perform same set of operations on a list of values
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;;

.
Department of Information Technology

IT6412 Operating Systems Lab

12

esac
Ex. $echo enter a value
read myval
case $myval in
1) echo zero;;
2) echo one;;
3) echo two;;
4) 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
else 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
Department of Information Technology

IT6412 Operating Systems Lab

13

5.Implement the Arithmetic calculator


Algorithm:
Step 1. Read a, b and option
Step 2. According to the option perform the operation
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

RESULT:
Thus programs in Unix shell script are developed using various language
constructs and execueted successfully.

Department of Information Technology

IT6412 Operating Systems Lab

14

Exp. No. : 03
Date:
IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS
Non-Pre-emptive Scheduling:

3.a. First Come First Serve (FCFS)


Aim:
To implement FCFS Scheduling algorithm using C
Algorithm:
Step 1: Start
Step 2 : Read the process name, Arrival time, its CPU burst from the user
Step 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


Step 4 : Display the starting and finishing time of each process in their arrival time order
Step 5 : Calculate the average waiting time and average turn around time
Step 6 : Display the average waiting time and average turn around time
Step 7 : Stop

3.b. Shortest Job First (SJF)


Aim:
To implement SJF Scheduling algorithm in C
Algorithm:
Step 1: Start
Step 2: Declare a structure Proc_stru with member variables such as process name,
CPU burst, arrival time, waiting time and turn around time
Step 3: Read the process name, Arrival time, its CPU burst from the user and store it in the
structure Proc_stru
Step 4: Sort the process in the ascending order of their CPU burst
Step 5: Calculate waiting time and turn around time for each process which ordered by
the CPU burst time
Waiting time= starting time - arrival time
Department of Information Technology
IT6412 Operating Systems Lab

15

Turn around time= finishing time arrival time


Step 6: Display the starting and finishing time of each process in their execution order
Step 7: Calculate the average waiting time and average turn around time
Step 8: Display the average waiting time and average turn around time
Step 9: Stop

Pre-emptive CPU Scheduling:

3.c. Priority scheduling


Aim:
To implement Priority Scheduling algorithm in C
Algorithm:
Step 1: Start
Step 2: Declare a structure Proc_stru with member variables such as process name, CPU
burst, priority, arrival time, waiting time and turn around time
Step 3: Read the process name, Arrival time, its CPU burst, priority from the user and store
it in the structure Proc_stru
Step 4: Sort the process in the ascending order of their Priority
Step 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
Step 6: Display the starting and finishing time of each process in their execution order
Step 7: Calculate the average waiting time and average turn around time
Step 8: Display the average waiting time and average turn around time
Step 9: Stop

3.d. Round Robin scheduling


Aim:
To implement Round Robin Scheduling algorithm in C
Algorithm:
Step 1: Start
Step 2: Declare a structure Proc_stru with member variables such as process name, CPU
burst
Department of Information Technology

IT6412 Operating Systems Lab

16

Step 3: Read the process name, its CPU burst from the user and store it in the structure
Proc_stru
Step 4: Get from the user CPU Time slice
Step 5: Allot the process to CPU for given time slice
Step 6: Switch the CPU to next process when time slice over
Step 7: Display the currently allotted processs detail
Step 8: Repeat the step 5, 6 till all the process are serviced completely.
Step 9: Stop

RESULT:
Thus the for CPU scheduling algorthims are implemented in C language and
executed successfully.
Department of Information Technology

IT6412 Operating Systems Lab

17

Exp. No. : 04
Date:
IMPLEMENTATION OF FILE ALLOCATION STRATEGIES
4.a. Sequential File Allocation:
Aim:
To implement Sequential file allocation in C.
Algorithm:
Step 1: Start
Step 2: Declare the starting block no. and the length of the file.
Step 3: Get the Starting block no. and length of the file from the user. Allocate files
sequentially until end of the file.
Step 4: Display the fragments of the file.
Step 5: Stop
4.b. Indexed File Allocation:
Aim:
To implement Indexed file allocation technique in C.

Algorithm:
Step 1: Start
Step 2: Declare the index block no. and total no.of files in a block
Step 3: Get the index block no. and total no.of files in a block from the user.
Step 4: Allocate files based on the index block no.
Step 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.
Step 6: Stop

Department of Information Technology

IT6412 Operating Systems Lab

18

4.c. Linked File Allocation


Aim:
To allocate the files in the secondary storage using Linked allocation technique
Algorithm:
Step 1: Start
Step 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
Step 3: Initialize the FAT (File Allocation Table) which is implemented as array of
pointers.
Step 4: Display the AVAIL List
Step 5: Read File allocation request which consist of File name, No of blocks and its
contents.
Step 6: Traverse the AVAIL linked list from the starting node
Step 7: Retrieve the required no of blocks from AVAIL List
Step 8: Assign the contents of file to the retrieved blocks
Step 9: Update the FAT by making an entry in FAT
Step 10: Update the AVAIL LIST
Step 11: Display the AVAIL List and FAT table
Step 12: Stop

Result:
Thus the different file allocation strategies are implemented and executed successfully.

Department of Information Technology

IT6412 Operating Systems Lab

19

Exp. No. : 05
Date:
IMPLEMENTATION OF SEMAPHORES
Aim:
To implement the Producer and Consumer Problem using semaphores
Algorithm:
Step 1: Start
Step 2: Initialize the semaphore variable S
Step 3: In the producer function,
3a) While s ==1 do nothing
3b) Produce the value
3c) Assign s=1
3d) Return
Step 4: In the Consumer function
4a) While s==0 do nothing
4b) Display the consumed value
4c) Assign s=0
4d) Return
Step 5: Create threads for producer and consumer function to make it run concurrently
Step 6: Stop

Result:
Thus the implementation of Producer and Consumer problem is implemented and executed
successfully.

Department of Information Technology

IT6412 Operating Systems Lab

20

Exp. No. : 06
Date:
IMPLEMENTATION OF FILE ORGANIZATION TECHNIQUES
6.a. Implementation of Single level directory
Aim:
To implement Single level directory structure in C
Algorithm:
Step 1: Start
Step 2: Declare the number, names and size of the directories and file names.
Step 3: Get the values for the declared variables.
Step 4: Display the files that are available in the directories.
Step 5: Stop.

6.b. Two-level directory Structure


Aim:
To implement Two-level directory structure in C
Algorithm:
Step 1: Start
Step 2: Declare the number, names and size of the directories and subdirectories and file
names.
Step 3: Get the values for the declared variables.
Step 4: Display the files that are available in the directories and subdirectories.
Step 5: Stop.

Department of Information Technology

IT6412 Operating Systems Lab

21

6.c. Hierarchical directory Structure


Aim:
To implement hierarchical directory structure in C
Algorithm:
Step 1: Start
Step 2: Declare the number, names and size of the directories and subdirectories and file
names.
Step 3: Get the values for the declared variables.
Step 4: Display the files that are available in the directories and subdirectories.
Step 5: Stop.

6.d. Directed Acyclic Graph (DAG)


Aim:
To implement Directed Acyclic Graph in C
Algorithm:
Step 1: Start
Step 2: Collect set of nodes 1, 2, , n
Step 3: Get the value of an edge (i,j) whenever i < j
Step 4: Calculate N-choose-2 = n (n-1)/2 edges, but no cycles.
Step 5: Stop.

Result:
Thus the implementation of different file organization techniques are implemented
and executed successfully.
Department of Information Technology

IT6412 Operating Systems Lab

22

Exp. No. : 07
Date:
IMPLEMENTATION OF BANKERS ALGORITHM
Aim:
To detect and prevent deadlock using Bankers algorithm
Algorithm:
Safety algorithm
Step 1: Start
Step 2: Initialize a temporary vector W (Work) to equal the available vector A.
Step 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.
Step 4: If such a row is found, mark this process as finished, and add all
its resources to W Vector i,
W = W + Ci
Step 5: 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
Step 1: If

Request i < = Needi , go to step 2. Otherwise, error

Step 2: If Request i < = Available, go to step 3. Otherwise, Pi must wait, since resources
are not available
Step 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
Step 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.
Step 5: Stop
Result:
Thus the C program for Bankers algorithm is implemented and executed successfully.

Department of Information Technology

IT6412 Operating Systems Lab

23

Exp. No. : 08
Date:
IMPLEMENTATION OF DEADLOCK DETECTION
Aim:
To write a C program for implementing the deadlock detection algorithm
Algorithm:
Algorithm I: Simply detects the existence of a Cycle
Step 1: Start at any vertex finds all its immediate neighbors.
Step 2: From each of these find all immediate neighbors, etc.
Step 3: Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle).
Step 4: Stop.

Algorithm 2: On a copy of the graph


Step 1: See if any Processes NEEDs can all be satisfied.
Step 2: If so satisfy the needs with holds and remove that Process and all the Resources it
holds from the graph.
Step 3: If any Process are left Repeat step 1
Step 4: If all Processes are finally removed by this procedure there is no Deadlock in the
original graph, if not there is.
Step 5: Stop

Result:
Thus the program for Deadlock detection algorithm is implemented and executed
successfully.

Department of Information Technology

IT6412 Operating Systems Lab

24

Exp. No. : 09
Date:
IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS
9. a. FIFO page replacement algorithm
Aim:
To write a C program for implementing FIFO page replacement algorithm
Algorithm:
Step 1: Start the process
Step 2: Declare the size with respect to page length
Step 3: Check the need of replacement from the page to memory
Step 4: Check the need of replacement from old page to new page in memory
Step 5: Forma queue to hold all pages
Step 6: Insert the page require memory into the queue
Step 7: Check for bad replacement and page fault
Step 8: Get the number of processes to be inserted
Step 9: Display the values
Step 10: Stop the process

Department of Information Technology

IT6412 Operating Systems Lab

25

9.b. LRU page replacement algorithm


Aim:
To write a c program to implement LRU page replacement algorithm
Algorithm:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least recently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process

Department of Information Technology

IT6412 Operating Systems Lab

26

9.c. LFU page replacement algorithm


Aim:
To write a C program to implement LFU page replacement algorithm
Algorithm:
Step 1: Start the process
Step 2: Declare the size
Step 3: Get the number of pages to be inserted
Step 4: Get the value
Step 5: Declare counter and stack
Step 6: Select the least frequently used page by counter value
Step 7: Stack them according the selection.
Step 8: Display the values
Step 9: Stop the process

Result:
Thus the programs for Page Replacement algorithms are implemented and executed
successfully.
Department of Information Technology

IT6412 Operating Systems Lab

27

Exp. No. : 10
Date:
INTER PROCESS COMMUNICATION
Aim:
To develop a client-server application program that uses shared memory using IPC
Algorithm:
Client :
Step 1: Define the key to be 5600
Step 2: Attach the client to the shared memory created by the server.
Step 3: Read the content from the shared memory.
Step 4: Display the content on the screen.
Step 5: Stop
Server:
Step 1: Define shared memory size of 30 bytes
Step 2: Define the key to be 5600
Step 3: Create a shared memory using shmget () system calls and gets the shared
memory id in variable shmid.
Step 4: Attach the shared memory to server data space
Step 5: Get the content to be placed in the shared memory from the user of the server.
Step 6: Write the content in the shared memory, which will read out by the client.
Step 7: Stop

Result:
Thus the programs for Inter Process Communication (IPC) is implemented and executed
successfully.
Department of Information Technology

IT6412 Operating Systems Lab

28

Exp. No. : 11
Date:
IMPLEMENTATION OF PAGING
Aim:
To implement memory allocation with pages
Algorithm:
Step 1: Start
Step 2: Declare the structure P_table with variables for page no and frame no
Step 3: Display the status of physical memory
Step 4: Get the number of pages needed for a process
Step 5: Get the contents of the pages
Step 6: Display the contents of logical memory
Step 7: If the physical memory is available then allot the pages of the process
Step 8: Update the physical memory status
Step 9: Update the page table status
Step 10: Display the page table after allocation
Step 11: Display the physical memory after allocation
Step 12: Stop

Result:
Thus the C program for Paging concept is implemented and executed successfully.
Department of Information Technology

IT6412 Operating Systems Lab

29

Exp. No. : 12
Date:
IMPLEMENTATION OF THREADING AND SYNCHRONIZATION
Aim:
To implement Banking system involving Concurrency using Thread
Algorithm:
Step 1: Create a Bank Database
Step 2: Create functions for adding a new user, depositing the amt, withdrawing the amt
& viewing the customer
Step 3: Create a thread for each functionality
Step 4: Implement the concurrency control among the threads
Step 5: Function add new user
a) Get the user details namely, Acno, Name & Bank Balance
b) Write the user details to the bank database
Step 6: Function Deposit
a) Get the Acno & Amt
b) Update the Bank balance for the given Acno
Step 7: Function Withdraw
a) Get the Acno & Amt
b) Update the Bank balance for the given Acno
Step 8: Function View
a) Get the Acno
b) Display the account details

Result:
Thus the C program for concurrency and synchronization are implemented and executed
successfully.
Department of Information Technology

IT6412 Operating Systems Lab

30

Content beyond the Syllabus


Exp. No. : 13
Date:
DEADLOCK PREVENTION ALGORITHM FOR MULTIPLE RESOURCES
Aim:
To detect and prevent deadlock using Bankers algorithm
Algorithm:
Safety algorithm
Step 1: Start
Step 2: Initialize a temporary vector W (Work) to equal the available vector A.
Step 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.
Step 4: If such a row is found, mark this process as finished, and add all its resources to W
Vector i,
W = W + Ci
Step 5: 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


Step 1: If Request i < = Needi , go to step 2. Otherwise, error
Step 2: If Request i < = Available, go to step 3. Otherwise, Pi must wait, since resources
are not available
Step 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
Step 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.
Step 5: Stop.

Result:
Thus the C program for Deadlock Detection for multiple resources is implemented and
executed successfully.
Department of Information Technology

IT6412 Operating Systems Lab

31

Exp. No. : 14
Date:
DINING PHILOSOPHER PROBLEM
Aim:
To write a C program to implement the concept of the Dining Philosopher
problem
Algorithm:
Step 1: Get the total number of philosopher
Step 2: Get the number of philosopher who are hungry
Step 3: If all are hungry then deadlock occurs
Step 4: Get the philosopher position
Step 5: Display the menu
Step 6: Two can eat at a time
Step 7: One can eat at a time
Step 8: If choice is one, then
a)get the philosopher states of eating position
b)display the means of those who are waiting.

Result:
Thus the C program to implement the Dining philosopher problem was created, executed
successfully and the output was verified
Department of Information Technology

IT6412 Operating Systems Lab

32

Você também pode gostar