Ex. No:1
UNIX BASIC
COMMANDS
S.NO
|
COMMAND
|
SYNTAX
|
DESCRIPTION
|
|
General
Purpose Utilities
|
||||
$ date
|
Displays current month, date
and
|
|||
time
|
||||
$ date +%D
|
Displays date in the format
|
|||
mm/dd/yy
|
||||
$date +%H
|
Display hour
|
|||
1
|
DATE
|
$date +%M
|
Display
minutes
|
|
$date +%$
|
Display
seconds
|
|||
$date +%h
|
Month name
|
|||
$date +%m
|
Month no
|
|||
$date +%T
|
Time in am /pm
|
|||
$date +%y
|
Last two
digits of the year
|
|||
$cal
|
Displays calendar for the
current
|
|||
month
|
||||
2
|
cal : calendar
|
$cal monthno year
|
Displays calendar for the
|
|
corresponding
month in the year
|
||||
$cal year
|
Displays the calendar for the
|
|||
whole year
|
||||
3
|
who : Login details
|
$ who
|
Gives the details of who all
have
|
|
logged into the system
currently.
|
||||
$who -Hu
|
Displays the
details with header
|
|||
4
|
who am I : who is on the
|
Tells who are you with the
|
||
system
|
terminal name, date & time
|
|||
5
|
type : to know the
|
$type command
|
To know the location of the
|
|
execution of the command
|
program the system will execute
|
|||
6
|
echo : to display messaae
|
$echo message
|
||
$echo
'message'
|
||||
7
|
man : online help
|
$man command
|
Online help about a particular
|
|
command
|
||||
uname : know your
|
$uname -r
|
Display the
version number
|
||
8
|
$uname -n
|
Display the
machine name
|
||
machine details
|
||||
$tty
|
Tells you the terminal number
|
|||
9
|
clear & tput clear
|
$tput clear
|
Clears the
screen
|
|
$clear
|
||||
Usage
of Directory Commands
|
||||
10
|
pwd: present working
|
$pwd
|
||
Directory
|
||||
$ls
|
Lists all the
files
|
|||
Lists files with their mode, no
of
|
||||
ls: displays the list of files
|
$ls -l
|
links, owner, file
size,modification
|
||
date and time and file name.
|
||||
11
|
in the current working
|
|||
Lists in order of last
modification
|
||||
directory.
|
$ls -t
|
|||
time.
|
||||
Sis -a
|
Lists all entries including the
|
|||
hidden files.
|
||||
COMMAND
|
SYNTAX
|
DESCRIPTION
|
||
$ls -d
|
Lists directory files instead
of its
|
|||
contents.
|
||||
$ls -p
|
Puts a slash
after each directory
|
|||
$is -u
|
Lists in order
of last access time
|
|||
$ls -x
|
To produce a multi-column
output
|
|||
$ls -F
|
To identify directories &
files . / -
|
|||
dir, * - exe files
|
||||
$ls -r
|
The files will be displayed in
the
|
|||
reverse order
|
||||
$ls -R
|
Recursive listing of all files
in sub
|
|||
directories
|
||||
$ls -I
|
Shows inode
no. of a file
|
|||
$ls -id dirname
|
Listing the attributes of a
particular
|
|||
directory
|
||||
12
|
mkdir: making
directory
|
$mkdir dirname
|
Creates home
directory
|
|
13
|
cd : change directory
|
$cd dirname
|
Change
directory
|
|
14
|
rmdir: removing
|
$rmdir dirname
|
||
directories
|
||||
15
|
HOME: to know the user
|
$echo SHOME
|
||
directory
|
||||
File Commands
|
||||
$cat > filename
|
Type the contents & press
ctrl-d to
|
|||
end
|
||||
16
|
cat:
displaying & creating
|
$cat filename or $cat <
|
Displays the contents of the
file.
|
|
files
|
filename
|
|||
$cat >> filename
|
To append the file content.Type
|
|||
the contents & press ctrl-d
to end
|
||||
$cp fl f2
|
Fl is copied
into f2.
|
|||
Interactive copy (warns the
user
|
||||
$cp -I f1 f2
|
before overwriting the
destination
|
|||
17
|
cp : copying a file
|
file)
|
||
Copying directory structures.(
|
||||
S cp - r dirl dir2
|
copies all files &
subdirectories in
|
|||
dirl to dir2)
|
||||
$rm filename
|
To delete a file
|
|||
$rm *
|
To delete all files
|
|||
18
|
rm : deleting
files
|
$rm -I f1
|
Interactive
deletion
|
|
$rm -f f1
|
Forcibly remove write-protected
|
|||
file also
|
||||
19
|
mv : renaming files
|
$mv f1 f2
|
Rename the file f1 as f2. (f1
no
|
|
longer exist)
|
||||
$more f1
|
Allows the user to view the
file f1,
|
|||
one screen at a time.(q- exit)
|
||||
20
|
more : paging output
|
|||
f
|
To forward one
screen
|
|||
b
|
To scroll back
|
|||
21
|
file : know the file types
|
$file f1
|
tell whether f1 is a
|
|
ordinary,directory or device
file.
|
||||
22
|
wc : line,word
and
|
$wc f1
|
Displays no.of
lines,words and
|
|
character counting
|
characters
|
|||
COMMAND
|
SYNTAX
|
DESCRIPTION
|
||
$wc -l f1
|
tells no.of
lines only
|
|||
$wc -w f1
|
No of words
only
|
|||
$wc -c f1
|
No of
characters only
|
|||
$od f1
|
display the file contents in
octal
|
|||
form
|
||||
23
|
od :
displaying the data in
|
$od -b f1
|
Displays the octal value of
each
|
|
octal
|
character in the file
|
|||
$od -bc f1
|
Displays each & every
character
|
|||
along with their octal equivalent.
|
||||
Splits into no. of files
containing
|
||||
split : spliting a file into
|
$split f1
|
'n' number of lines in each
file.
|
||
24
|
(filenames xaa,xab,..xyz)
|
|||
multiple files
|
||||
$split f1 f2
|
creates resultant files in the
names
|
|||
of f2aa, f2ab,..
|
||||
Files are compared byte by byte
I
|
||||
and the location of the 1-st
mistake
|
||||
$cmp fl f2
|
is echoed. (if 2 files are
same, cmp
|
|||
displays no message, simply
|
||||
25
|
cmp : comparing two files
|
returns S prompt)
|
||
Gives a detailed list of the
byte no.
|
||||
$cmp -l fl f2
|
& the directory bytes in
octal for
|
|||
each character that differ in
both
|
||||
files.
|
||||
comm : finding what is
|
Compares each line of fl with
its
|
|||
26
|
common (compares two
|
$comm f1 f2
|
||
corresponding line in f2
|
||||
sorted files)
|
||||
27
|
diff: to display the file
|
diff fl £2
|
display line-by-line
differences
|
|
differences
|
between pairs of text files
|
|||
chmod :
changing file
|
$chmod category operation
|
|||
permission (u - user, g -
|
permission filename
|
|||
group o -
others r- read, w-
|
Assigns execute permission to
user
|
|||
28
|
write & x- execute +
|
$chmod u+x,go+r fl
|
and read permission to group
and
|
|
addition of rights -
|
others.
|
|||
removal of
rights = assign
|
Assigns read & write
permissions
|
|||
permission in octal r-4 w
|
$chmod 644 fl
|
to user, read permission to
group
|
||
—2 x-1)
|
& others
|
|||
Environment
Variables
|
||||
$set
|
Displays a complete set of all
the
|
|||
environment variables
|
||||
$echo $PATH
|
It tells the route to locate
any
|
|||
executable command
|
||||
29
|
||||
$echo $HOME
|
Displays the
user directory
|
|||
$echo $LOGNAME
|
Tells the
login user
|
|||
$echo $TERM
|
Tells the
terminal type
|
|||
$echo $Ps1
|
Tells about
the shell
|
|||
Simple Filters
|
||||
30
|
head : displaying the
|
$head filename
|
Displays the first few lines of
the
|
|
beginning of a file
|
file'
|
|||
$head -n
filename
|
Displays the
first 'n' no. of lines
|
COMMAND
|
SYNTAX
|
DESCRIPTION
|
||
31
|
tail: displaying the end of a
|
$tail -n filename
|
Displays 'n' no. of lines from
the
|
|
file
|
end of a file.
|
|||
$tail +n
filename
|
n- th line
onwards, upto the end
|
|||
32
|
cut: slitting a file vertically
|
$cut -c range,range filename
|
To extract specific columns
from
|
|
the file.
|
||||
$cut -c
1-5,10-15 fl
|
||||
33
|
paste: pasting
files
|
$paste fl f2
|
To merge two
files
|
|
$sort filename
|
Sorts the contents based on
ASCII.
|
|||
sort : ordering a file (
|
Alphabetical order
|
|||
34
|
sorting is in
the order -
|
$sort -r
filename
|
Sorts in the
reverse order
|
|
numerals,
uppercase letters
|
$sort -n
filename
|
n- numerical
sort
|
||
&
lowercase letters)
|
$sort -c
filename
|
checks if file
is sorted
|
||
$sort -u
filename
|
removes
duplicate records
|
|||
uniq : locating repeated
|
It fetches one copy of each
record
|
|||
35
|
lines(it requires sorted
|
$uniq filename
|
||
& writes it to the standard
output.
|
||||
files as input)
|
||||
36
|
nl: line
numbering
|
$nl filename
|
It numbers
only logical lines.
|
|
tr: translating characters (tr
|
||||
doesn't accept a filename
|
Lower case to
uppercase
|
|||
37
|
as an argument, it takes its
|
$cat filename | tr '[a-z]'
'[A-Z]'
|
||
conversion
|
||||
input through redirection
|
||||
or a pipe)
|
||||
Pattern
Searching
|
||||
$grep pattern filename
|
Displays the lines that
containing
|
|||
the pattern
|
||||
$grep-v pattern filename
|
Displays the lines which are
not
|
|||
having that pattern
|
||||
grep: searching for a
|
||||
38
|
Displays the lines that
containing
|
|||
pattern
|
$grep -n pattern filename
|
|||
the pattern along with the line
no.
|
||||
$grep -i
pattern filename
|
Ignoring the
case
|
|||
$grep -c pattern filename
|
Displays count of number of
|
|||
occurrences
|
||||
eg rep : extending grep
|
Searches the two patterns in
the
|
|||
39
|
(searches more than one
|
$egrep 'pattern1|pattern2' f1
|
||
filefl
|
||||
pattern in a file)
|
||||
40
|
fgrep :
multiple string
|
$fgrep -f
filel file2
|
Searches
pattern from more than
|
|
searching
|
one file
|
|||
Process
|
||||
41
|
$$ : process no. of the sh
|
$echo $$
|
Displays the process number of
the
|
|
shell
|
current shell
|
|||
$ps
|
Displays the attributes of a
process
|
|||
such as PID TTY TTME CMD
|
||||
42
|
ps : process status
|
Displaying process ancestry
such
|
||
$ps -f
|
as UID PID PPID C STTME TTY
|
|||
TIME CMD
|
||||
$ps -a
|
Lists the
processes of all users
|
|||
$ps -e
|
Displays all system processes
|
COMMAND
|
SYNTAX
|
DESCRIPTION
|
||
Nohup permits execution of the
|
||||
43
|
nohup : log outsafely
|
$nohup command filename &
|
process even after the user has
|
|
logged out. It sends the
default
|
||||
output to a file nohup.out.
|
||||
44
|
kill: premature termination
|
$kill processidno
|
Terminates the job with the PID
|
|
of a process
|
||||
nice : job execution with
|
To run a job with a low
priority
|
|||
45
|
$nice command filename
|
(nice reduces the priority of
any
|
||
low priority
|
||||
process by 10 units)
|
||||
46
|
at & batch:
execute later
|
$at time
command filename
|
at the
particular time, that
|
|
$batch <
filename
|
||||
Interprocess
Communication
|
||||
Any user can read any message
|
||||
47
|
news : the bulletin board
|
$news
|
that is sent by the system
|
|
administrator
|
||||
write : two way
|
The message appears on user's
|
|||
terminal provided he is logged
|
||||
48
|
communication (o - over
|
$write username message 0
|
||
in.If he is not, the system
responds
|
||||
oo - over & out)
|
||||
with an error message.
|
||||
49
|
mesg : your willingness to
|
$mesg
|
User can receive messages (
|
|
talk
|
default option will be
displayed)
|
|||
$mesg n
|
to prevent
from others intrusions.
|
|||
talk : an alternative to
|
$talk username message ctrl-c
|
It splits the screen
horizontally into
|
||
50
|
2 windows; one is for receiving
|
|||
write
|
(or) press delete key to end)
|
|||
and the other is for
transmission
|
||||
It is used for later viewing,
and the
|
||||
51
|
mail : the universal mailer
|
$mail username message ctrl-d
|
user doesn't need to be logged
in
|
|
for the mail to reach her.
|
||||
Miscellaneous
Command
|
||||
$finger
|
It produces a list of all
logged
|
|||
finger or W: details of
|
users.
|
|||
52
|
||||
users
|
$finger username
|
Provide details of a particular
user
|
||
$ x=vl y=v2 $expr $x op $y
|
evaluate arguments as an
|
|||
53
|
expr : computation
|
(or) $z=`expr $x op $y` $echo
|
||
expression
|
||||
$z (op - any operator)
|
||||
Displays the result of the
|
||||
54
|
be : the calculator
|
$bc any calculation ctrl-d
|
calculation.(it is also used
for base
|
|
conversion)
|
||||
55
|
spell: check
your spellings
|
$spell
filename
|
Lists all
misspelled words
|
COMMAND
|
SYNTAX
|
DESCRIPTION
|
||
$touch mm/dd/hh mm file
|
Touch changes both modification
|
|||
name
|
time and access time
|
|||
56
|
touch :
changing the time
|
$touch - m mm dd hh min file
|
Only changes modification time
|
|
stamps
|
name
|
|||
$touch - a mm dd hh min file
|
Only access time
|
|||
name
|
||||
57
|
ln : linking of files
|
$ln f1 f2
|
The changes in fl also
reflected in
|
|
f2
|
||||
$chown owner name filename
|
Gives away the ownership of a
file
|
|||
chown & chgrp:changing
|
to any user
|
|||
58
|
||||
file ownership
|
$chgrp group name filename
|
Changes the group ownership of
|
||
the file
|
||||
$cc filename
|
To compile a C
programs
|
|||
59
|
cc : compiling C programs
|
$cc file1.c - o file2 or $cc -o
|
Compiles file1.c and places the
|
|
resulting executable program in
|
||||
file2 file1.c
|
||||
the file2 rather than a. out
|
||||
SHELL PROGRAMMING
EX NO: 2(a)
TO FIND GREATEST AMONG THREE NUMBERS
Date :
PRE LAB:
AIM:
To find the
greatest of three numbers using shell programming.
IN LAB:
ALGORITHM:
Step 1: Start the
program.
Step 2: Read the
three numbers a,b,c.
Step 3: If a is
greater than b and c.
Step 4: Print “A is
greatest”.
Step 5: If b is
greater than a and c.
Step 6: Print “B is
greater”.
Step 7: Else print “C
is greater”.
Step 8: Stop the
program.
PROGRAM:
echo "Enter three no"
read a
read b
read c
if test $a -gt $b
then
if test $a -gt $c
then
echo "a is greatest"
fi
fi
if test $b -gt $c
then
echo "b is greatest"
else
echo "c is greatest"
fi
OUTPUT :
[cse@unixSERVER ~]$ sh great.sh
Enter three no
10
12
13
C is greatest
RESULT:
Thus the
program to find greatest of three numbers using shell programming was executed and verified
EX NO: 2(b) TO PERFORM ARITHMETIC
OPERATION
Date :
PRE LAB:
AIM:
To perform
arithmetic operations using shell programming.
IN LAB:
ALGORITHM:
Step 1: Start the
program.
Step 2: Read the two
numbers a and b.
Step 3: Select the
arithmetic operation to be performed.
Step 4: Perform
“Addition” if case 1 is selected.
Step 5: Perform
“Subtraction” if case 2 is selected.
Step 6: Perform
“Multiplication” if case 3 is selected.
Step 7: Perform
“Division” if case 4 is selected.
Step 8: Print the
required result.
Step 9: Stop the
program.
PROGRAM:
echo "1.add"
echo "2.subtract"
echo "3.multiply"
echo "4.divide"
echo "Enter your choice"
read i
echo "Enter two numbers"
read a
read b
case $i in
1) x=`expr $a + $b`
echo "The sum is
$x";;
2) x=`expr $a - $b`
echo "The result
is $x";;
3) x=`expr $a \* $b`
echo "The
product is $x";;
4) x=`expr $a / $b`
echo "The result
is $x";;
esac
OUTPUT:
[cse@unixSERVER ~]$
sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
1
Enter two numbers
2
3
The sum is 5
[cse@unixSERVER ~]$
sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
2
Enter two numbers
5
1
The result is 4
[cse@unixSERVER ~]$
sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
3
Enter two numbers
6
5
The product is 30
[cse@unixSERVER ~]$
sh arit.sh
1.add
2.subtract
3.multiply
4.divide
Enter your choice
4
Enter two numbers
20
5
The result is 4
RESULT:
Thus the
program to perform arithmetic operations using shell programming was executed and verified.
Ex.no:2(c) FACTORIAL OF A GIVEN NUMBER
Date :
PRE LAB:
AIM:
To find the factorial of a given number using
shell programming.
IN LAB:
ALGORITHM:
Step 1: Start the
program.
Step 2: Declare a
variable a.
Step 3: Assume n is
equal to a.
Step 4: If a is o,
then print the factorial value as 1.
Step 5: Else,
subtract the value of a by 1.
Step 6: Multiply
with the previous value of a.
Step 7: Print the
required result.
Step 8: Stop the
program.
PROGRAM:
echo "Factorial of the given number"
echo "Enter the number"
read a
fact=1
n=$a
if [ $a -eq 0 ]
then
echo "Factorial of 0 is 1"
else
while [ $a -gt 0 ]
do
fact=`expr $fact \* $a`
a=`expr $a - 1`
done
echo "Factorial of $n is $fact"
fi
OUTPUT:
[cse@unixSERVER ~]$
sh fact.sh
Factorial of the
given number
Enter the number
3
Factorial of 3 is 6
RESULT:
Thus the program
to find the factorial of a given number using shell programming was executed
and verified.
CPU SCHEDULING ALGORITHMS
EX.NO:3 (a) ROUND ROBIN CPU SCHEDULING ALGORITHM
DATE:
PRE LAB:
AIM:
To
perform the round robin scheduling using a c program.
ALGORITHM:
Step1: Start the
program.
Step2: First make a
2-dimensional arry.
Step3: Enter the no
of process and quantum.
Step4: Enter the
processno,servicetime and arrivaltime.
Step5: In round robin
scheduling we execute the process for the given time quantum.
Step6: Calculate the
waiting time and turn around time using the giventime quantum.
Step7: Now calculate
the average waiting time and average turn around time.
Step8: Display the
process and respective time.
Step9: Display the
average waiting time and turn around time.
Step10: Stop the
program.
IN LAB:
PROGRAM:
#include<stdio.h>
main()
{
struct process
{
float at,tt,st,ft,bt,wt;
int pno;
}p[20],t[20],temp;
float stat=0,time=0,q=1,avgwt=0,avgtt=0;
int i,n;
int arr[50],arr1[50],k=1;
printf("ROUND ROBIN SCHEDULING\n");
printf("Enter the no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time of %d process:",i);
scanf("%f",&p[i].bt);
t[i].bt=p[i].bt;
p[i].pno=i+1;
p[i].at=0;
}
while(stat<n)
{
for(i=0;i<n;i++)
{
if(t[i].bt>q)
{
p[i].ft=time+q;
t[i].bt=t[i].bt-q;
time=time+q;
arr[k]=p[i].pno;
k++;
}
else if(t[i].bt>0&&t[i].bt<=q)
{
p[i].ft=t[i].bt+time;
arr1[k]=t[i].bt;
time=time+t[i].bt;
arr[k]=p[i].pno;
k++;
t[i].bt=0;
stat++;
}
}
}
printf("\n\npno\tat\tst\tbt\tft\twt\ttt\n");
for(i=0;i<n;i++)
{
p[i].wt=p[i].ft-p[i].bt;
p[i].tt=p[i].wt+p[i].bt;
avgwt=avgwt+p[i].wt;
avgtt=avgtt+p[i].tt;
printf("%d\t2.2f\t%2.2f\t%2.2f\t%2.2f\t%2.2f\t%2.2f\n",p[i].pno,p[i].at,p[i].st,p[i].bt,p[i].ft,p[i].wt,p[i].tt);
}
avgwt=(avgwt/n);
avgtt=(avgtt/n);
printf("\naverage waiting time:%2.2f",avgwt);
printf("\naverage turn around time:%2.2f",avgtt);
}
OUTPUT:
[cse@unixSERVER ~]$ vi roro.c
[cse@unixSERVER ~]$ cc roro.c
[cse@unixSERVER ~]$ ./a.out
ROUND ROBIN SCHEDULING
Enter the no of process: 3
Enter the burst time of 0 process: 5
Enter the burst time of 1 process: 7
Enter the burst time of 2 process: 3
pno at st
bt ft wt
tt
1 2.2f 0.00
0.00 5.00 12.00
7.00
2 2.2f 0.00
0.00 7.00 15.00
8.00
3 2.2f 0.00
0.00 3.00 9.00
6.00
average waiting time: 7.00
average turn around time: 12.00
RESULT:
Thus the program for round robin scheduling has been
executed and output is
verified successfully.
POST LAB:
VIVA VOCE:
1. What is time slice?
2. How turnaround time is calculated?
3. Define arrival time.
4. What is the unit of average waiting time?
5. Define entry section and exit section
EX.NO:3 (b) SJF CPU SCHEDULING ALGORITHM
DATE:
PRE LAB:
AIM:
To write
a program for implementing shortest job
first using c programming.
IN LAB:
ALGORITHM:
Step 1 :Start the
program.
Step 2:Declare the
variables.
Step 3:Initialize
total waiting time and total as 0.
Step 4:Pint the no of
process.
Step 5:Use two
dimensional array to print burst time, arrival time in a matrix form.
Step 6:Calculate
average waiting time and turn around time.
Step 7:Print the
result.
Step 8:Stop.
PROGRAM:
#include<stdio.h>
int main()
{
int m,a[10][6],i,j,k,te,p,q;
int twt=0,tot=0;
float awt,tat;
printf("SJF\n");
printf("Enter the no.of process:");
scanf("%d",&m);
printf("Enter %d *3 rowwise \n process no \nburst time \narrival
time\n",m);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=i+1;j<3;j++)
{
if(a[i+1][1]<a[i][1])
{
te=a[i][0];a[i][0]=a[i+1][0];a[i+1][0]=te;
q=a[i][1];a[i][1]=a[i+1][1];a[i+1][1]=q;
p=a[i][2];a[i][2]=a[i+1][2];a[i+1][2]=p;
}
}
}
for(i=0;i<m;i++)
for(j=0;j<m;j++)
printf("%d\t",a[i][j]);
printf("\nP.NO\tB.T\tA.T\tW.T\tR.T\tTAT\n");
a[0][3]=0;
for(i=1;i<m;i++)
{
a[i][3]=a[i-1][3]+a[i-1][1];
twt+=a[i][3];
}
for(i=0;i<m;i++)
{
a[i][4]=a[i][3]-a[i][2];
a[i][5]=a[i][1]+a[i][4];
tot+=a[i][5];
}
for(i=0;i<m;i++)
{
for(j=0;j<6;j++)
{
printf("%d\t",a[i][j]);
}
awt=twt/m;
tat=tot/m;
printf("\n");
}
printf("avg waiting time is %f\n",awt);
printf("avg turn around time is %f\n",tat);
}
OUTPUT:
[cse@unixSERVER ~]$ cc sjf.c
[cse@unixSERVER ~]$ ./a.out
SJF
Enter the no of process 3
Enter 3*3 rowwise
process no burst
time arrival time
1 3 5
2 4 6
3 5 7
1 3 5
2 4 6
3 5 7
PNO BT AT
WT RT TAT
1 3
5 0 5
8
2 4 6
3 9 13
3 5 7
7 14 19
average waiting time is 3.000000
average turnaround time is 13.000000[7133@unixSERVER ~]$SJF
RESULT:
Thus the program for SJFscheduling has been executed
and output is verified successfully.
POST LAB:
VIVA VOCE:
1. Define CPU scheduling
2. What is a Dispatcher?
3. Classify two types of SJF.
4. What is response time?
5. Delineate Gantt chart.
EX.NO:3 (c) FCFS
CPU SCHEDULING ALGORITHM
DATE:
PRE LAB:
AIM:
To write a program for
implementing first come first serve using
C programming.
IN LAB:
ALGORITHM:
Step1: Start the program.
Step2: Declare the variables.
Step3: Initialize
average waiting time and average turn around time as 0.
Step4: Print the no of process.
Step5: Based on the process print the burst time and arrival
time.
Step6: Check the arrival time of process I and j.
Step7: If process I is greater than j, then swap.
Step8: Calculate average waiting time and average turn
around time.
Step9: Display the result.
Step10: Stop
PROGRAM:
#include<stdio.h>
main()
{
struct process
{
float at,tt,st,ft,bt,wt;
}p[20],temp;
float avgwt=0.0,avgtt=0.0;
int n,i,j;
printf("FCFS\n");
printf("\nEnter the no of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the arrival time and burst time of %d
proce
scanf("%f%f",&p[i].at,&p[i].bt);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
p[0].st=p[0].at;
for(i=0;i<n;i++ )
{
p[i].ft=p[i].st+p[i].bt;
p[i+1].st=p[i].ft;
if(p[i].st>p[i].at)
p[i].wt=p[i].st-p[i].at;
else
p[i].wt=0;
p[i].tt=p[i].wt+p[i].bt;
avgwt=avgwt+p[i].wt;
avgtt=avgtt+p[i].tt;
}
printf("pno \t at\t
bt\t wt\t tt\n");
for(i=0;i<n;i++)
{
printf("%d\t%2.2f\t%2.2f\t%2.2f\t%2.2f\t%2.2f\t%2.2f\n",i
t,p[i].tt);
}
avgwt=(avgwt/n);
avgtt=(avgtt/n);
printf("avgwt is %2.2f\t%2.2f",avgwt);
printf("avgtt is %2.2f\t%2.2f",avgtt);
}
OUTPUT:
[cse@unixSERVER ~]$ cc fcfs.c
[cse@unixSERVER ~]$ ./a.out
FCFS
Enter the no of process: 3
Enter the arrival time and burst time of 0 process 1 4
Enter the arrival time and burst time of 1 process 2 5
Enter the arrival time and burst time of 2 process 4 7
Pno at bt
wt tt
0 1.00 4.00
0.00 4.00 0.00
0.00
1 2.00 5.00
3.00 8.00 0.00
0.00
2 4.00 7.00
6.00 13.00 0.00
0.00
avgwt is 3.00 0.00
avgtt is 8.33 0.00
RESULT:
Thus the program for implementing first come
first serve has been executed and verified.
POST LAB:
VIVA VOCE:
1. Whether FCFS is preemptive or non preemptive?
2. Define dispatcher and its functions.
3. Delineate context switch.
4. What is the default arrival time?
5. Define dispatch latency?
EX.NO:3 (d) PRIORITY CPU SCHEDULING ALGORITHM
DATE:
PRE LAB:
AIM:
To write a c program to implement the Priority Scheduling (PR).
IN LAB:
To write a c program to implement the Priority Scheduling (PR).
IN LAB:
ALGORITHM:
Step 1: Get the number of process from the user.
Step 2: Get also CPU service time for each process from the user.
Step 3: Sort the CPU time of process according to the process priority in ascending order
Step 4: Now for each process the waiting time is equivalent to CPU time of previous process.
Step 5: The ratio of waiting time of all the process to the number of process will give the average waiting time.
Step 6: Display the result.
Step 1: Get the number of process from the user.
Step 2: Get also CPU service time for each process from the user.
Step 3: Sort the CPU time of process according to the process priority in ascending order
Step 4: Now for each process the waiting time is equivalent to CPU time of previous process.
Step 5: The ratio of waiting time of all the process to the number of process will give the average waiting time.
Step 6: Display the result.
PROGRAM:
# include<stdio.h>
main()
{
int j,i,k=0,ptime[25],n,s=0,I,sum=0;
char name[25][25];
int t,p,time[10],pos[10],priority[25];
float avg;
printf ("enter the no. of process: \t");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name for processes: \t");
printf("%d \t",i+1);
scanf("%s",name[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the process time: \t");
printf("%s \t",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the priority for process: \t");
printf("%d \t",i+1);
scanf("%d",&priority[i]);
}
printf("\n process – name \t process – time \n");
for(i=0;i<n;i++)
{
printf("\t %s \t \t %d \t\t %d\n",name[i],ptime[i],priority[i]);
}
printf("\n \n PRIORITY SCHEDULING \n \n");
for(i=0;i<n;i++)
{
pos[i]=i;
time[i]=ptime[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
p=time[i];
time[i]=time[j];
time[j]=p;
t=pos[i];
pos[i]=pos[j];
pos[j]=t;
p=priority[i];
priority[i]=priority[j];
priority[j]=p;
}
}
for(i=0;i<n;i++)
{
printf("process %s from %d to %d \n",name,pos[i],k,(k+time[i]));
k+=time[i];
}
for(i=0;i<(n-1);i++)
{
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\n average waiting time: \t");
printf("%2fmsec",avg);
sum=avg=s=0;
for(i=0;i<n;i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n turn around time is \t");
printf("%2fmsec",avg);
return 0;
}
# include<stdio.h>
main()
{
int j,i,k=0,ptime[25],n,s=0,I,sum=0;
char name[25][25];
int t,p,time[10],pos[10],priority[25];
float avg;
printf ("enter the no. of process: \t");
scanf ("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the name for processes: \t");
printf("%d \t",i+1);
scanf("%s",name[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the process time: \t");
printf("%s \t",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n \n");
for(i=0;i<n;i++)
{
printf("enter the priority for process: \t");
printf("%d \t",i+1);
scanf("%d",&priority[i]);
}
printf("\n process – name \t process – time \n");
for(i=0;i<n;i++)
{
printf("\t %s \t \t %d \t\t %d\n",name[i],ptime[i],priority[i]);
}
printf("\n \n PRIORITY SCHEDULING \n \n");
for(i=0;i<n;i++)
{
pos[i]=i;
time[i]=ptime[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
p=time[i];
time[i]=time[j];
time[j]=p;
t=pos[i];
pos[i]=pos[j];
pos[j]=t;
p=priority[i];
priority[i]=priority[j];
priority[j]=p;
}
}
for(i=0;i<n;i++)
{
printf("process %s from %d to %d \n",name,pos[i],k,(k+time[i]));
k+=time[i];
}
for(i=0;i<(n-1);i++)
{
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\n average waiting time: \t");
printf("%2fmsec",avg);
sum=avg=s=0;
for(i=0;i<n;i++)
{
s+=ptime[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n turn around time is \t");
printf("%2fmsec",avg);
return 0;
}
RESULT :
Thus the program to implement the
Priority Scheduling was executed
successfully and output was verified.
POST LAB:
VIVA VOCE:
1. What are privileged instructions?
2. How is the protection for memory provided?
3. How can a user program disrupt the normal operations of a
system?
4. Justify, how priority is considered for job scheduling?
5. Write the formula to calculate waiting time.
IMPLEMENT ALL FILE ALLOCATION STRATEGIES
EX.NO:4(A) SEQUENTIAL FILE
ALLOCATION
DATE:
PRE LAB:
AIM:
To implement the sequential file allocation using C
programming.
IN LAB:
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size
for each process.
Step 4: First fit algorithm searches all the entire memory
block until a hole which is
big
enough is encountered. It allocates that memory block for the requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for
the smallest hole which can
be
allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for
the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques
and display the best
algorithm
which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int
f[50],i,st,j,len,c,k,count=0;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n
enter starting block & length of files");
scanf("%d%d",&st,&len);
printf("\n
file not allocated(yes-1/no-0)");
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[i]==0)
{
f[j]=1;
printf("\n%d\t%d",,j,f[j]);
if(j==(st+len-1))
printf("\n
the file is allocated to disk");
}
}
else
printf("file
is not allocated");
count=0;
printf("\n
if u want to enter more files(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto
X;
else
exit();
getch();
}
OUTPUT:
enter
starting block & length of files
4
5
file not allocated (y-1/n-0)
4 1
5 1
6 1
7 1
8 1
file
is allocated to disk
if u
want to enter more files(y-1/n-0)
0
POST LAB:
RESULT :
Thus the program to implement the sequential
file allocation was executed
successfully and output was verified.
EX.NO:4(B) INDEXED
FILE ALLOCATION
DATE:
PRE LAB:
AIM:
To implement the indexed file allocation
using C programming.
IN LAB:
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size
for each process.
Step 4: First fit algorithm searches all the entire memory
block until a hole which is
big
enough is encountered. It allocates that memory block for the requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for
the smallest hole which can
be
allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for
the largest hole and
allocates it to the process.
Step 7: Analyses all the three memory management techniques
and display the best
algorithm
which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],i,k,j,index[50],n,c,count=0;
for(i=0;i<50;i++)
f[i]=0;
X:
printf("enter
index block");
scanf("%d",&i);
if(f[i]!=1)
{
f[i]=1;
printf("enter
no of files on index");
scanf("%d",&n);
}
y:
for(i=0;i<n;i++)
scanf("%d",&index[i]);
if(f[index[i]==0)
count++;
if(count==n)
{
for(j=0;j<nj++)
f[index[j]=1;
printf("\nallocated");
printf("\n
file indexed");
for(k=0;k<n;k++)
printf("\n%d->%d:%d",i,index[k],f[index[k]);
}
else
{
printf("\n
file in the index already allocation");
printf("\nenter
another file indexed");
goto
y;
}
printf("\n
index is already allocated");
count=0;
printf("\n
if u enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto
x;
getch(
);
}
OUTPUT:
enter
index block 3
enter no of files on index
4 5 6
7 8
Allocated
file indexed
4->5:1
4->6:1
4->7:1
4->8:1
index
is already allocated
if u
enter one more block(1/0)
0
POST LAB:
RESULT :
Thus the program to implement the indexed
file allocation was executed successfully and output was verified.
EX.NO:4(C) LINKED FILE ALLOCATION
DATE:
PRE LAB:
AIM:
To implement the linked file allocation
using C programming.
IN LAB:
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size
for each process.
Step 4: First fit algorithm searches all the entire memory
block until a hole which is
big
enough is encountered. It allocates that memory block for the requesting
process.
Step 5: Best-fit algorithm searches the memory blocks for
the smallest hole which can
be
allocated to requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for
the largest hole and
allocates
it to the process.
Step 7: Analyses all the three memory management techniques
and display the best
algorithm
which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int
f[50],p,i,j,k,a,st,len,n;
for(i=0;i<50;i++)
f[i]=0;
printf("enter
how many blocks already allocated");
scanf("%d",&p);
printf("\nenter
the blocks nos");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("enter
index sarting block & length");
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n
%d->file is already allocated",j);
k++;
}
}
}
else
printf("\nif
u enter one more (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto
X;
else
exit();
getch(
);
}
OUTPUT:
enter
how many blocks already allocated 5
enter
block nos
3 7 9
10 14
enter
index starting block & length 4 10
4->1
5->1
6->1
7->file
is already allocated
8->1
9->file
is already allocated
10->file
is already allocated
11->1
12->1
13->1
14->file
is already allocated
15->1
16->1
17->1
POST LAB:
RESULT :
Thus the program to implement the linked
file allocation was executed successfully and output was verified.
EX.NO:5 PRODUCER – CONSUMER PROBLEM USING SEMAPHORES
DATE:
PRE LAB:
AIM:
To implement the producer- consumer problem using
semaphores in C programming.
IN LAB:
ALGORITHM:
1.
Start the program
2.
create semaphore using semget( ) system call
3. if
successful it returns positive value
4.
create two new processes
5.
first process will produce
6.
until first process produces second process cannot
consume
7.
End.
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define
num_loops 2
int
main(int argc,char* argv[])
{
int
sem_set_id;
int
child_pid,i,sem_val;
struct
sembuf sem_op;
int
rc;
struct
timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(“main:semget”);
exit(1);
}
printf(“semaphore
set created,semaphore setid‘%d’\n ”,
sem_set_id);
child_pid=fork();
switch(child_pid)
{
case
-1:
perror(“fork”);
exit(1);
case
0:
for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:’%d’\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(“consumer:’%d’\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return
0;
}
OUTPUT:
semaphore
set created
semaphore
set id ‘327690’
producer:
‘0’
consumer:’0’
producer:’1’
consumer:’1’
POST LAB:
RESULT :
Thus the program to implement the producer-consumer
problem was executed successfully and output was verified.
Ex.No:7 BANKERS
ALGORITHM FOR DEAD LOCK AVOIDANCE
Date:
PRE LAB:
AIM:
To
implement the bankers Algorithm for Dead Lock Avoidance
IN LAB:
PROGRAM:
#include<stdio.h>
int alloc[10][10];
int max[10][10];
int cneed[10][10];
int a[10];
int total[10];
void main()
{
int r,p,i,j,k,flag,f,count,x,flag1;
count=0;
flag1=0;
printf(“\nEnter number of resources:”);
scanf(“%d”,&r);
f=r;
printf(“\nEnter total memory of given resources sequentially:”);
for(i=0;i<r;i++)
scanf(“%d”,&total[i]);
printf(“\nEnter number of processes:”);
scanf(“%d”,&p);
printf(“\nEnter Allocated and Maxneed memory…”);
getch();
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf(“\nFor Process %d=”,i);
scanf(“%d%d”,&alloc[i][j],&max[i][j]);
//calculating current need
cneed[i][j]=max[i][j]-alloc[i][j];
}
}
printf(“\nSequence of execution is…\n”);
k=0;
for(j=0;j<r;j++)
{
for(i=0;i<p;i++)
{
a[k]+=alloc[i][j];
}
a[k]=total[k]-a[k];
k++;
}
while(count!=p)
{
for(i=0;i<p;i++)
{
flag=0;
if(cneed[i][f]!=1)
{
for(j=0;j<r;j++)
{
total[j]=a[j]-cneed[i][j];
}
for(k=0;k<r;k++)
{
if(total[k]<0)
{
flag=1;
}
}
}
if((flag==0)&&(cneed[i][f]!=1))
break;
}
x=i;
cneed[x][f]=1;
printf(“P%d->”,x);
count++;
for(i=0;i<r;i++)
{
total[i]+=max[x][i];
a[i]=total[i];
}
for(i=0;i<p;i++)
{
if((cneed[i][0]<a[0])&&(cneed[i][f]==0))
flag1=1;
}
if(flag1==0)
break;
}
if(flag1==0)
printf(“\nUnsafe…”);
else
printf(“\nSafe…”);
getchar();
}
int alloc[10][10];
int max[10][10];
int cneed[10][10];
int a[10];
int total[10];
void main()
{
int r,p,i,j,k,flag,f,count,x,flag1;
count=0;
flag1=0;
printf(“\nEnter number of resources:”);
scanf(“%d”,&r);
f=r;
printf(“\nEnter total memory of given resources sequentially:”);
for(i=0;i<r;i++)
scanf(“%d”,&total[i]);
printf(“\nEnter number of processes:”);
scanf(“%d”,&p);
printf(“\nEnter Allocated and Maxneed memory…”);
getch();
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf(“\nFor Process %d=”,i);
scanf(“%d%d”,&alloc[i][j],&max[i][j]);
//calculating current need
cneed[i][j]=max[i][j]-alloc[i][j];
}
}
printf(“\nSequence of execution is…\n”);
k=0;
for(j=0;j<r;j++)
{
for(i=0;i<p;i++)
{
a[k]+=alloc[i][j];
}
a[k]=total[k]-a[k];
k++;
}
while(count!=p)
{
for(i=0;i<p;i++)
{
flag=0;
if(cneed[i][f]!=1)
{
for(j=0;j<r;j++)
{
total[j]=a[j]-cneed[i][j];
}
for(k=0;k<r;k++)
{
if(total[k]<0)
{
flag=1;
}
}
}
if((flag==0)&&(cneed[i][f]!=1))
break;
}
x=i;
cneed[x][f]=1;
printf(“P%d->”,x);
count++;
for(i=0;i<r;i++)
{
total[i]+=max[x][i];
a[i]=total[i];
}
for(i=0;i<p;i++)
{
if((cneed[i][0]<a[0])&&(cneed[i][f]==0))
flag1=1;
}
if(flag1==0)
break;
}
if(flag1==0)
printf(“\nUnsafe…”);
else
printf(“\nSafe…”);
getchar();
}
OUTPUT:
//For Safe condition
Enter number of resources:3
Enter total memory of given resources sequentially:10 5 7
Enter number of processes:5
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5
For Process 0=0 3
For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
For Process 3=2 2
For Process 3=1 2
For Process 3=1 2
For Process 4=0 4
For Process 4=0 3
For Process 4=2 3
Sequence of execution is...
P1->P3->P0->P2->P4->
Safe...
//For Unsafe condition
Enter number of resources:3
Enter total memory of given resources sequentially:6 5 4
Enter number of processes:3
Enter Allocated and Maxneed memory...
For Process 0=0 7
For Process 0=1 5
For Process 0=0 3
For Process 1=2 3
For Process 1=0 2
For Process 1=0 2
For Process 2=3 9
For Process 2=0 0
For Process 2=2 2
Sequence of execution is...
P1->Unsafe...
Ex.No:8 BANKERS
ALGORITHM FOR DEAD LOCK DETECTION
Date:
PRE LAB:
AIM:
To
write a C program to implement the bankers Algorithm for Dead Lock Detection
IN LAB:
PROGRAM:
#include <stdio.h>;
#include <conio.h>;
void main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Enter total no of resources");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
OUTPUT:
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1
Enter allocation matrix :
1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0
Enter resource vector (Total resources) :
2 1 1 2 1
Enter availability vector (available resources) :
0 0 0 0 1
deadlock causing processes are : 2 3
#include <conio.h>;
void main()
{
int found,flag,l,p[4][5],tp,tr,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("Enter total no of processes");
scanf("%d",&tp);
printf("Enter total no of resources");
scanf("%d",&tr);
printf("Enter claim (Max. Need) matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&c[i][j]);
}
printf("Enter allocation matrix\n");
for(i=1;i<=tp;i++)
{
printf("process %d:\n",i);
for(j=1;j<=tr;j++)
scanf("%d",&p[i][j]);
}
printf("Enter resource vector (Total resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&r[i]);
}
printf("Enter availability vector (available resources):\n");
for(i=1;i<=tr;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
OUTPUT:
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1
Enter allocation matrix :
1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0
Enter resource vector (Total resources) :
2 1 1 2 1
Enter availability vector (available resources) :
0 0 0 0 1
deadlock causing processes are : 2 3
POSTLAB:
RESULT:
Thus the
program to implement the bankers Algorithm for
Dead Lock Detection has been executed successfully.
Ex.No:9(a) IMPLEMENTATION OF
FIFO PAGE REPLACEMENT ALGORITHM
Date:
PRE LAB:
Aim
To write a program to implement
FIFO page replacement algorithm
INLAB:
.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
Program:
#include<stdio.h>
int main()
{
int
i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE
NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE
PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE
NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]=
-1;
j=0;
printf("\tref
string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if
(avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page
Fault Is %d",count);
return
0;
}
Output
ENTER THE NUMBER OF PAGES:
20
ENTER THE PAGE NUMBER :
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
ENTER THE NUMBER OF FRAMES :3
ref string page frames
7 7
-1 -1
0 7 0
-1
1 7 0
1
2 2 0
1
0
3 2 3
1
0 2 3
0
4 4 3
0
2 4 2
0
3 4 2
3
0 0 2
3
3
2
1 0 1
3
2 0 1
2
0
1
7 7 1
2
0 7 0
2
1 7 0
1
Page Fault Is 15
POSTLAB:
Result
The program for FIFO page
replacement was implemented and hence verified.
Ex.No:9(b) IMPLEMENTATION
OF LRU PAGE REPLACEMENT ALGORITHM
Date:
PRELAB:
Aim
To write a program a program to
implement LRU page replacement algorithm
INLAB:
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
Program
#include<stdio.h>
main()
{
int
q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of
pages:");
scanf("%d",&n);
printf("Enter the reference
string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of
frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
POSTLAB:
Output:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5
9
4 5
9
4 3
9
4
3 7
9 3
7
9 6
7
9 6
2
1 6
2
The no of page faults is 10
POSTLAB:
Result:
The program for LRU page
replacement was implanted and hence verified.
Ex.No:9(c)
OPTIMAL(LFU) PAGE REPLACEMENT
ALGORITHM
Date:
PRELAB:
AIM:
To implement page replacement algorithms Optimal (The page
which is not used for longest time) Optimal
algorithm. Here we select the page that will not be used for the longest
period of time.
INLAB:
ALGORITHM:
OPTIMAL:
Step 1: Create a
array
Step 2: When the page
fault occurs replace page that will not be used for the longest
period
of time
PROGRAM:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n
OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter
the no.of reference string");
scanf("%d",&nor);
printf("Enter
the reference string");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
clrscr();
printf("\n
OPTIMAL PAGE REPLACEMENT ALGORITHM");
printf("\n................................");
printf("\nThe
given string");
printf("\n....................\n");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=0;i<nof;i++)
{
frm[i]=-1;
optcal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
flag=0;
printf("\n\tref no %d ->\t",ref[i]);
for(j=0;j<nof;j++)
{
if(frm[j]==ref[i])
{
flag=1;
break;
}
}
if(flag==0)
{
count++;
if(count<=nof)
victim++;
else
victim=optvictim(i);
pf++;
frm[victim]=ref[i];
for(j=0;j<nof;j++)
printf("%4d",frm[j]);
}
}
printf("\n
Number of page faults: %d",pf);
getch();
}
int optvictim(int index)
{
int
i,j,temp,notfound;
for(i=0;i<nof;i++)
{
notfound=1;
for(j=index;j<nor;j++)
if(frm[i]==ref[j])
{
notfound=0;
optcal[i]=j;
break;
}
if(notfound==1)
return i;
}
temp=optcal[0];
for(i=1;i<nof;i++)
if(temp<optcal[i])
temp=optcal[i];
for(i=0;i<nof;i++)
if(frm[temp]==frm[i])
return i;
return 0;
}
OUTPUT:
OPTIMAL PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..6 5 4 2 3 1
OPTIMAL PAGE REPLACEMENT ALGORITHM
The given reference
string:
…………………. 6 5
4 2 3 1
Reference NO
6-> 6 -1 -1
Reference NO
5-> 6 5 -1
Reference NO
4-> 6 5 4
Reference NO
2-> 2 5 4
Reference NO 3-> 2
3 4
Reference NO
1-> 2 3 1
No.of page
faults...6
POST LAB:
RESULT :
Thus the program to implement the paging technique of memory management was
executed successfully and output was verified.
Ex.No:10 Implement Shared memory and IPC
Date:
PRELAB:
AIM:
To write a c program to develop an application using Inter
process Communication (IPC)
using pipes.
INLAB:
ALGORITHM:
1. Create the child process using Fork()
2. Create the pipe structure using pipe()
3. Now close the read end of the parent process
using close()
4. Write the data in the pipe using write()
5. Now close the write end of child process
using close()
6. Read y the data in the pipe using read()
7. Display the string.
PROGRAM:
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10],b[10];
printf("\nEnter the string to enter into the
pipe");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],b,5);
printf("\nThe string retrived from the pipe
is %s\n",b);
}
SENDER:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define size 32
int main()
{
int shmid;
char *s[100],*str;
printf("\nipc message passing using shared memory
sender");
shmid=shmget(60,size,IPC_CREAT|0666);
str=shmat(shmid,0,0);
printf("\neneter the message to be sent");
gets(s);
strcpy(str,s);
printf("\nyour mesage has been sent");
return 0;
}
RECEIVER:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define size 32
int main()
{
printf("\nipc message passing using shared
memory-receiver");
int shmid;
char *str;
shmid=shmget(60,size,IPC_CREAT|0666);
str=shmat(shmid,0,0);
printf("\nreceived message is....");
puts(str);
return 0;
}
OUTPUT:
Sender
ipc message passing using shared memory sender
enter the message to be sentsuccess
your mesage has been sent
Receiver
ipc message passing using shared memory-receiver
received message is....success
POSTLAB:
RESULT:
The C program to develop an application using
Inter process Communication (IPC)
using pipes is implemented.
Ex.No:11 Implement Paging Technique of memory management
Date:
PRELAB:
AIM:
To implement the Memory management policy- Paging.
INLAB:
ALGORITHM:
Step 1: Read all the necessary input from the keyboard.
Step 2: Pages - Logical memory is broken into fixed - sized
blocks.
Step 3: Frames – Physical memory is broken into fixed –
sized blocks.
Step 4: Calculate the physical address using the following
Physical
address = ( Frame number * Frame size ) + offset
Step 5: Display the physical address.
Step 6: Stop the process.
PROGRAM :
#include <stdio.h>
struct pstruct
{
int fno;
int pbit;
}ptable[10];
int pmsize,lmsize,psize,frame,page,ftable[20],frameno;
void info()
{
printf("\n\nMEMORY
MANAGEMENT USING PAGING\n\n");
printf("\n\nEnter
the Size of Physical memory: ");
scanf("%d",&pmsize);
printf("\n\nEnter
the size of Logical memory: ");
scanf("%d",&lmsize);
printf("\n\nEnter
the partition size: ");
scanf("%d",&psize);
frame =
(int) pmsize/psize;
page =
(int) lmsize/psize;
printf("\nThe
physical memory is divided into %d no.of frames\n",frame);
printf("\nThe
Logical memory is divided into %d no.of pages",page);
}
void assign()
{
int i;
for
(i=0;i<page;i++)
{
ptable[i].fno
= -1;
ptable[i].pbit=
-1;
}
for(i=0;
i<frame;i++)
ftable[i]
= 32555;
for
(i=0;i<page;i++)
{
printf("\n\nEnter
the Frame number where page %d must be placed: ",i);
scanf("%d",&frameno);
ftable[frameno]
= i;
if(ptable[i].pbit
== -1)
{
ptable[i].fno
= frameno;
ptable[i].pbit
= 1;
}
}
printf("\n\nPAGE
TABLE\n\n");
printf("PageAddress FrameNo. PresenceBit\n\n");
for
(i=0;i<page;i++)
printf("%d\t\t%d\t\t%d\n",i,ptable[i].fno,ptable[i].pbit);
printf("\n\n\n\tFRAME
TABLE\n\n");
printf("FrameAddress PageNo\n\n");
for(i=0;i<frame;i++)
printf("%d\t\t%d\n",i,ftable[i]);
}
void cphyaddr()
{
int
laddr,paddr,disp,phyaddr,baddr;
printf("\n\n\n\tProcess
to create the Physical Address\n\n");
printf("\nEnter
the Base Address: ");
scanf("%d",&baddr);
printf("\nEnter
theLogical Address: ");
scanf("%d",&laddr);
paddr =
laddr / psize;
disp =
laddr % psize;
if(ptable[paddr].pbit == 1 )
phyaddr
= baddr + (ptable[paddr].fno*psize) + disp;
printf("\nThe
Physical Address where the instruction present: %d",phyaddr);
}
void main()
{
info();
assign();
cphyaddr();
}
OUTPUT:
MEMORY MANAGEMENT USING PAGING
Enter the Size of
Physical memory: 16
Enter the size of Logical memory: 8
Enter the partition size: 2
The physical memory is divided into 8 no.of frames
The Logical memory is divided into 4 no.of pages
Enter the Frame number where page 0 must be
placed: 5
Enter the Frame number where page 1 must be
placed: 6
Enter the Frame number where page 2 must be
placed: 7
Enter the Frame number where page 3 must be
placed: 2
PAGE TABLE
PageAddress
FrameNo.
PresenceBit
0 5 1
1 6 1
2 7 1
3 2 1
FRAME TABLE
FrameAddress PageNo
0 32555
1 32555
2 3
3 32555
4 32555
5 0
6 1
7 2
Process to create the Physical Address
Enter the Base
Address: 1000
Enter theLogical
Address: 3
The Physical Address where the instruction present: 1013
POSTLAB:
RESULT:
The Memory management policy using
Paging is implemented.
Ex.No:12 Implement Threading &
Synchronization Applications
Date:
PRELAB:
AIM:
To write a program to implement threading and synchronization
applications.
INLAB:
ALGORITHM:
Step 1: Start the program.
Step 2: Enter the logical memory
address.
Step 3: Enter the page table
which has offset and page frame.
Step 4: The corresponding
physical address can be calculate by,
PA = [ pageframe* No. of page
size ] + Page offset.
Step 5: Print the physical
address for the corresponding logical address.
Step 6: Terminate the program.
PROGRAM:
$gcc
thread.c -lpthread
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
void* doSomeThing(void *arg)
{
unsigned
long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0]))
{
printf("\n First thread processing\n");
}
else
{
printf("\n Second thread processing\n");
}
for(i=0;
i<(0xFFFFFFFF);i++);
return
NULL;
}
int main(void)
{
int i =
0;
int err;
while(i
< 2)
{
err
= pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if
(err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
else
printf("\n Thread created successfully\n");
i++;
}
sleep(5);
return
0;
}
OUTPUT:
First thread processing 1
Thread created successfully
Second thread processing 2
can't create thread
POSTLAB:
RESULT:
Thus the
program has been executed and verified successfully.