036 - Os - Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

EX. NO.

: 1 UNIX COMMANDS
DATE :

AIM
To execute different Unix commands.

COMMANDS
1) Command to print the login name of the user
[cs15090@CSDB312 ~]$ logname
cs15090

2) To display the users


[cs15090@CSDB312 ~]$ who
cs15090 tty1 2016-02-03 22:06 (:0)
cs15090 pts/0 2016-02-03 22:06 (:0.0)
cs15090 pts/1 2016-02-03 23:14 (:0.0)

3) To display the present user alone


[cs15090@CSDB312 ~]$ who am i
cs15090 pts/0 2016-02-03 22:06 (:0.0)

4) Print the current date


[cs15090@CSDB312 ~]$ date
Wed Feb 3 23:18:30 IST 2016

5) Print the present working directory


[cs15090@CSDB312 ~]$ tty
/dev/pts/0

6) Print the calendar


[cs15090@CSDB312 ~]$ cal
February 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29

7) Print the calendar for october 2003


[cs15090@CSDB312 ~]$ cal 10 2003
October 2003
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

8) Print the name of the unix system


[cs15090@CSDB312 ~]$ uname
Linux

9) Create a file
[cs15090@CSDB312 ~]$ cat>f1
this is file1
[cs15090@CSDB312 ~]$ cat f1
this is file1

10) Print the terminal name


Reg. No. : 190501036 Page No. :
[cs15090@CSDB312 ~]$ tty
/dev/pts/0

11) Create multiple files


[cs15090@CSDB312 ~]$ touch f2 f3 f4

12) List all the files and directories in the current directory
[cs15090@CSDB312 ~]$ ls
bst.cpp elen.c factorial.cpp filename.c fribin.cpp Music
Pictures six.c test2.cpp time.cpp two.c
a.out Desktop eleven.c fifteen.c five.c friunary.cpp nine.c
Public sixteen.c test.cpp timefinal.cpp upper.cpp
armstrong.cpp Documents f1 fifty.c forty.c matrixadd.cpp
nineteen.c sam streverse.cpp thirteen.c twelve.cVideos
avg.cpp Downloads f2 fiftyone.c fortyone.c maximum.cpp one.c
seven.c student.cpp thirty twenty virtual.cpp
binary.cpp eight.c f3 file1name.c four.c multilevel.cpp
opover.cpp seven.cvi Templates thirty.c twenty.c
book.cpp eighteen.c f4 filename2.c fourteen.c multiple.cpp
palindrome.cpp seventeen.c ten.c three.c twentyfive.c

13) Create a file in write mode


[cs15090@CSDB312 ~]$ cat>fn
this is file fn

14) To display the contents of the file fn


[cs15090@CSDB312 ~]$ cat fn
this is file fn

15) Copy the contents of two files into a third file


[cs15090@CSDB312 ~]$ cat fn
this is file fn
[cs15090@CSDB312 ~]$ cat>fm
this is file fm
cat fm fn>fw
[cs15090@CSDB312 ~]$ cat fw
this is file fm
this is file fn

16) Append the contents of two file into a third file


[cs15090@CSDB312 ~]$ cat fm
this is file fm
[cs15090@CSDB312 ~]$ cat fn
this is file fn
[cs15090@CSDB312 ~]$ cat>fw
this is file fw
[cs15090@CSDB312 ~]$ cat fm fn>>fw
[cs15090@CSDB312 ~]$ cat fw
this is fw
this is file fm
this is file fn

17) Display the first n lines of the file


[cs15090@CSDB312 ~]$ head -1 fn
this is file fn

18) Display the last n lines of the file


[cs15090@CSDB312 ~]$ tail -1 fn
this is file fn

19) Create a file


[cs15090@CSDB312 ~]$ vi file1.txt

20) Copy the contents of one file into another


[cs15090@CSDB312 ~]$ cat os1.txt

Reg. No. : 190501036 Page No. :


this is file 1
[cs15090@CSDB312 ~]$ cp os1.txt os2.txt
[cs15090@CSDB312 ~]$ cat os1.txt
this is file 1

21) Create a directory


[cs15090@CSDB312 ~]$ mkdir pav

22) Copy files into the dictionary


[cs15090@CSDB312 ~]$ cp f1 d1
[cs15090@CSDB312 ~]$ cd d1
[cs15090@CSDB312 ~]$ ls
f1 f2 f3 f4

23) To change the directory


[cs15090@CSDB312 pav]$ cd d1
[cs15090@CSDB312 d1]$

24) To move to the parent directory


[cs15090@CSDB312 d1]$ cd..
[cs15090@CSDB312 ~]$

25) Copy a file from one directory to another


[cs15090@CSDB312 pav]$ cd
[cs15090@CSDB312 ~]$cp pav/f1 d1
[cs15090@CSDB312 ~]$cd d1
[cs15090@CSDB312 ~]$ ls
f1 f2 f3 f4

26) Remove a file


[cs15090@CSDB312 ~]$ rm f1
[cs15090@CSDB312 ~]$ ls
f2 f3 f4

27) Remove with confirmation


[cs15090@CSDB312 ~]$ rm f2 -i
rm: remove regular empty file `f2'? y
[cs15090@CSDB312 d1]$ ls
f3 f4

28) Remove without confirmation(forcibly)


[cs15090@CSDB312 ~]$ rm f3 -f
[cs15090@CSDB312 ~]$ ls
f4

29) Move contents from one file to another


[cs15090@CSDB312 ~]$ mv fm fn
[cs15090@CSDB312 ~]$ cat fn
this is file fm

30) To create a hidden file


[cs15090@CSDB312 ~]$ cat>.f11
this is hidden file

31) To view the hidden file


[cs15090@CSDB312 ~]$ ls -a
. bst.cpp .f11 forty.c .gtk-bookmarks one.c
sam test2.cpp two.c
.. .cache f3 fortyone.c .gvfs
opover.cpp .serverauth.3076 test.cpp upper.cpp
] .ccache f4 four.c .ICEauthority os1.txt
seven.c thirteen.c Videos
a.out .config f5 fourteen.c .imsettings.log os2.txt
seven.cvi thirty .viminfo
armstrong.cpp .dbus factorial.cpp fribin.cpp .local

Reg. No. : 190501036 Page No. :


palindrome.cpp seventeen.c thirty.c virtual.cpp
avg.cpp Desktop fifteen.c friunary.cpp matrixadd.cpp
Pictures six.c three.c .Xauthority
.bash_history Documents fifty.c fv maximum.cpp Public
sixteen.c .thumbnails .xsession-errors
.bash_logout Downloads fiftyone.c fw .mozilla .pulse
.sixteen.c.swo time.cpp .xsession-errors.old
.bash_profile eight.c file1name.c fw1 multilevel.cpp .pulse-
cookie .sixteen.c.swp .time.cpp.swp .zshrc
.bashrc eighteen.c filename2.c .gconf multiple.cpp
.recently-used.xbel streverse.cpp timefinal.cpp
binary.cpp elen.c .filename2.c.swp .gconfd Music
.recently-used.xbel.D3B8OX student.cpp twelve.c
book.cpp eleven.c filename.c .gnome2 .nautilus
.recently-used.xbel.H7GEPX Templates twenty
.book.cpp.swo .esd_auth five.c .gnome2_private nine.c
.recently-used.xbel.P7I7OX ten.c twenty.c
.book.cpp.swp .ex1.txt.swp fn .gstreamer-0.10 nineteen.c
.recently-used.xbel.ZTYBPX .ten.c.swp twentyfive.c

32) To view all the files starting with the given letter
[cs15090@CSDB312 ~]$ cat>f3
this is file f3
[cs15090@CSDB312 ~]$ cat>f33
this is file f33
[cs15090@CSDB312 ~]$ cat f3
this is file f3
[cs15090@CSDB312 ~]$ cat f33
this is file f33
[cs15090@CSDB312 ~]$ ls f3*
f3 f33

33) To view all the files starting with either of the given letters (aeiou)
[cs15090@CSDB312 ~]$ ls [aeiou]*
a a1 a.out armstrong.cpp avg.cpp eight.c eighteen.c elen.c eleven.c one.c
opover.cpp os1.txt os2.txt upper.cpp

34) To view all the files starting with the letters other than (aeiou)
[cs15090@CSDB312 ~]$ls –L[a,e,i,o,u]*
bst.cpp factorial.cpp filename.c fribin.cpp Music Pictures
six.c test2.cpp time.cpp two.c Desktop fifteen.c five.c
friunary.cpp nine.c Public sixteen.c test.cpp timefinal.cpp
Documents f1 fifty.c forty.c matrixadd.cpp nineteen.c sam
streverse.cpp thirteen.c twelve.c Videos
Downloads f2 fiftyone.c fortyone.c maximum.cpp seven.c
student.cpp thirty twenty virtual.cpp binary.cpp f3
file1name.c four.c multilevel.cpp seven.cvi Templates thirty.c
twenty.c book.cpp f4 filename2.c fourteen.c multiple.cpp
palindrome.cpp seventeen.c ten.c three.c twentyfive.c

35) Long listing


[cs15090@CSDB312 [cs15090@CSDB312 ~]$ ls -l
total 304
-rwxr-xr-x 1 cs15090 cs14 1476 Jul 11 2015 ]
-rw-r--r-- 1 cs15090 cs14 10 Feb 3 11:49 a
-rw-r--r-- 1 cs15090 cs14 3 Feb 3 11:51 a1
-rwxr-xr-x 1 cs15090 cs14 10312 Aug 22 15:07 a.out
-rwxr-xr-x 1 cs15090 cs14 327 Jul 9 2015 armstrong.cpp
-rwxr-xr-x 1 cs15090 cs14 1227 Jul 25 2015 avg.cpp
-rwxr-xr-x 1 cs15090 cs14 781 Aug 8 14:48 binary.cpp
-rwxr-xr-x 1 cs15090 cs14 409 Jul 25 2015 book.cpp
-rwxr-xr-x 1 cs15090 cs14 1767 Sep 12 15:18 bst.cpp
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Desktop
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Documents
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Downloads
Reg. No. : 190501036 Page No. :
-rwxr-xr-x 1 cs15090 cs14 234 Nov 8 2014 eight.c
-rwxr-xr-x 1 cs15090 cs14 341 Nov 15 2014 eighteen.c
-rwxr-xr-x 1 cs15090 cs14 0 Nov 10 2014 elen.c
-rwxr-xr-x 1 cs15090 cs14 262 Nov 10 2014 eleven.c
-rw-r--r-- 1 cs15090 cs14 16 Feb 3 11:45 f3
-rw-r--r-- 1 cs15090 cs14 17 Feb 3 11:46 f33
-rw-r--r-- 1 cs15090 cs14 0 Feb 3 10:56 f4
-rw-r--r-- 1 cs15090 cs14 0 Feb 3 11:03 f5
-rwxr-xr-x 1 cs15090 cs14 293 Jul 9 2015 factorial.cpp
-rwxr-xr-x 1 cs15090 cs14 229 Nov 11 2014 fifteen.c
-rwxr-xr-x 1 cs15090 cs14 286 Nov 25 2014 fifty.c
-rwxr-xr-x 1 cs15090 cs14 269 Nov 25 2014 fiftyone.c
-rwxr-xr-x 1 cs15090 cs14 123 Sep 22 2014 file1name.c
-rwxr-xr-x 1 cs15090 cs14 0 Nov 8 2014 filename2.c
-rwxr-xr-x 1 cs15090 cs14 57 Sep 22 2014 filename.c
-rwxr-xr-x 1 cs15090 cs14 365 Nov 8 2014 five.c
-rw-r--r-- 1 cs15090 cs14 29 Feb 3 11:19 fn
-rwxr-xr-x 1 cs15090 cs14 346 Nov 26 2014 forty.c
-rwxr-xr-x 1 cs15090 cs14 267 Nov 26 2014 fortyone.c
-rwxr-xr-x 1 cs15090 cs14 157 Nov 8 2014 four.c
-rwxr-xr-x 1 cs15090 cs14 608 Nov 10 2014 fourteen.c
-rwxr-xr-x 1 cs15090 cs14 781 Aug 8 14:52 fribin.cpp
-rwxr-xr-x 1 cs15090 cs14 807 Aug 8 15:14 friunary.cpp
-rw-r--r-- 1 cs15090 cs14 32 Feb 3 11:11 fv
-rw-r--r-- 1 cs15090 cs14 88 Feb 3 11:21 fw
-rw-r--r-- 1 cs15090 cs14 45 Feb 3 11:22 fw1
-rwxr-xr-x 1 cs15090 cs14 679 Jul 9 2015 matrixadd.cpp
-rwxr-xr-x 1 cs15090 cs14 302 Jul 9 2015 maximum.cpp
-rwxr-xr-x 1 cs15090 cs14 874 Aug 22 14:10 multilevel.cpp
-rwxr-xr-x 1 cs15090 cs14 760 Aug 22 13:27 multiple.cpp
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Music
-rwxr-xr-x 1 cs15090 cs14 249 Nov 8 2014 nine.c
-rwxr-xr-x 1 cs15090 cs14 554 Nov 15 2014 nineteen.c
-rwxr-xr-x 1 cs15090 cs14 122 Nov 8 2014 one.c
-rwxr-xr-x 1 cs15090 cs14 631 Aug 8 14:19 opover.cpp
-rw-r--r-- 1 cs15090 cs14 8 Feb 3 11:37 os1.txt
-rw-r--r-- 1 cs15090 cs14 8 Feb 3 11:38 os2.txt
-rwxr-xr-x 1 cs15090 cs14 323 Jul 10 2015 palindrome.cpp
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Pictures
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Public
-rwxr-xr-x 1 cs15090 cs14 6422 Sep 22 2014 sam
-rwxr-xr-x 1 cs15090 cs14 446 Nov 8 2014 seven.c
-rwxr-xr-x 1 cs15090 cs14 0 Nov 8 2014 seven.cvi
-rwxr-xr-x 1 cs15090 cs14 221 Nov 15 2014 seventeen.c
-rwxr-xr-x 1 cs15090 cs14 320 Nov 8 2014 six.c
-rwxr-xr-x 1 cs15090 cs14 2 Nov 15 2014 sixteen.c
-rwxr-xr-x 1 cs15090 cs14 190 Jul 9 2015 streverse.cpp
-rwxr-xr-x 1 cs15090 cs14 506 Jul 25 2015 student.cpp
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Templates
-rwxr-xr-x 1 cs15090 cs14 288 Nov 10 2014 ten.c
-rwxr-xr-x 1 cs15090 cs14 110 Jul 11 2015 test2.cpp
-rwxr-xr-x 1 cs15090 cs14 263 Jul 11 2015 test.cpp
-rwxr-xr-x 1 cs15090 cs14 588 Nov 10 2014 thirteen.c
-rwxr-xr-x 1 cs15090 cs14 289 Nov 25 2014 thirty
-rwxr-xr-x 1 cs15090 cs14 625 Nov 25 2014 thirty.c
-rwxr-xr-x 1 cs15090 cs14 149 Nov 8 2014 three.c
-rwxr-xr-x 1 cs15090 cs14 611 Jul 24 2015 time.cpp
-rwxr-xr-x 1 cs15090 cs14 477 Jul 24 2015 timefinal.cpp
-rwxr-xr-x 1 cs15090 cs14 396 Nov 10 2014 twelve.c
-rwxr-xr-x 1 cs15090 cs14 286 Nov 24 2014 twenty
-rwxr-xr-x 1 cs15090 cs14 465 Nov 17 2014 twenty.c
-rwxr-xr-x 1 cs15090 cs14 542 Nov 17 2014 twentyfive.c
-rwxr-xr-x 1 cs15090 cs14 304 Nov 8 2014 two.c
-rwxr-xr-x 1 cs15090 cs14 403 Jul 9 2015 upper.cpp
drwxr-xr-x 2 cs15090 cs14 4096 Nov 8 2014 Videos
Reg. No. : 190501036 Page No. :
-rwxr-xr-x 1 cs15090 cs14 582 Aug 22 15:09 virtual.cpp

36) To list file types


[cs15090@cs4210 ~]$ file *
]: ASCII C++ program text
a: ASCII text
a1: ASCII text
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.32, not stripped
armstrong.cpp: ASCII English text, with CRLF line terminators
avg.cpp: ASCII C++ program text
binary.cpp: ASCII C++ program text
book.cpp: ASCII C++ program text
bst.cpp: ASCII C program text
client.c: ASCII C program text
Desktop: directory
Documents: directory
Downloads: directory
eight.c: ASCII English text
eighteen.c: ASCII English text
elen.c: empty
eleven.c: ASCII English text
ex1proper.txt: ASCII text
ex1.txt: ASCII text
f3: ASCII text
f33: ASCII text
f4: empty
f5: empty
factorial.cpp: ASCII English text, with CRLF line terminators
fifteen.c: ASCII English text
fifty.c: ASCII English text
fiftyone.c: ASCII English text
file1name.c: ASCII English text
filename2.c: empty
filename.c: ASCII text
five.c: ASCII C program text
fn: ASCII text
forty.c: ASCII English text
fortyone.c: ASCII C++ program text
four.c: ASCII English text
fourteen.c: ASCII English text
fribin.cpp: ASCII C++ program text
friunary.cpp: ASCII C++ program text
fv: ASCII text
fw: ASCII text
fw1: ASCII text
matrixadd.cpp: ASCII English text, with CRLF line terminators
maximum.cpp: ASCII English text, with CRLF line terminators
multilevel.cpp: ASCII C++ program text
multiple.cpp: ASCII C++ program text
Music: directory
nine.c: ASCII English text
nineteen.c: ASCII C program text
one.c: ASCII C program text
opover.cpp: ASCII C++ program text
os1.txt: ASCII text
os2.txt: ASCII text
palindrome.cpp: ASCII C program text, with CRLF line terminators
Pictures: directory
Public: directory
sam: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.32, not stripped
servcli.c: ASCII C program text
seven.c: ASCII English text
seven.cvi: empty
Reg. No. : 190501036 Page No. :
seventeen.c: ASCII C program text
six.c: ASCII English text
sixteen.c: ASCII text
streverse.cpp: ASCII C program text, with CRLF line terminators
student.cpp: ASCII C++ program text
Templates: directory
ten.c: ASCII English text
test2.cpp: ASCII text
test.cpp: ASCII C++ program text
thirteen.c: ASCII English text
thirty: ASCII C program text
thirty.c: ASCII C program text
three.c: ASCII English text
time.cpp: ASCII C++ program text, with CRLF line terminators
timefinal.cpp: ASCII C++ program text, with CRLF line terminators
twelve.c: ASCII English text
twenty: ASCII English text
twenty.c: ASCII English text
twentyfive.c: ASCII English text
two.c: ASCII English text
upper.cpp: ASCII C program text, with CRLF line terminators
Videos: directory
virtual.cpp: ASCII C++ program text

37) To list the files with sizes in the particular directory


[cs15090@cs4210 ~]$ ls -s
total 332
4 ] 4 Desktop 4 f33 4 five.c 4 fw1
4 os1.txt 4 six.c 4 thirty.c 4 Videos
4 a 4 Documents 0 f4 4 fn 4 matrixadd.cpp
4 os2.txt 4 sixteen.c 4 three.c 4 virtual.cpp
4 a1 4 Downloads 0 f5 4 forty.c 4 maximum.cpp
4 palindrome.cpp 4 streverse.cpp 4 time.cpp
8 a.out 4 eight.c 4 factorial.cpp 4 fortyone.c 4 multilevel.cpp
4 Pictures 4 student.cpp 4 timefinal.cpp
4 armstrong.cpp 4 eighteen.c 4 fifteen.c 4 four.c 4 multiple.cpp
4 Public 4 Templates 4 twelve.c
4 avg.cpp 0 elen.c 4 fifty.c 4 fourteen.c 4 Music
8 sam 4 ten.c 4 twenty
4 binary.cpp 4 eleven.c 4 fiftyone.c 4 fribin.cpp 4 nine.c
4 servcli.c 4 test2.cpp 4 twenty.c
4 book.cpp 12 ex1proper.txt 4 file1name.c 4 friunary.cpp 4 nineteen.c
4 seven.c 4 test.cpp 4 twentyfive.c
4 bst.cpp 12 ex1.txt 0 filename2.c 4 fv 4 one.c
0 seven.cvi 4 thirteen.c 4 two.c
4 client.c 4 f3 4 filename.c 4 fw 4 opover.cpp
4 seventeen.c 4 thirty 4 upper.cpp

38) Give all rights for the user,for group give only write and execute permission
[cs15090@cs4210 ~]$ chmod 730 f11
[cs15090@cs4210 ~]$ls –l f11
-rwx-wx---l cs15090 cs14 feb 10 10:30 f11

39) Give only execute permission for others


[cs15090@cs4210 ~]$ chmod o+x file1.txt
[cs15090@cs4210 ~]$ ls –l file1.txt
-rwx-wx--x 1 cs15090 cs14 14 Feb 10 09:59 file1.txt

40) To list files with inode number


[cs15090@cs4210 ~]$ ls -i file1.txt
794658 file1.txt

41) Calculator
[cs15090@cs4210 ~]$ bc
bc 1.06.95
Reg. No. : 190501036 Page No. :
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+3
5
quit

42) Calculator with trigonometric library


[cs15090@cs4210 ~]$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
S(30)
-.98803162409286178998
quit

43) Evaluating expression


[cs15090@cs4210 ~]$ expr 3 + 2
5

44) Factor of a number


[cs15090@cs4210 ~]$ factor 5
5: 5
[cs15090@cs4210 ~]$ factor 6
6: 2 3

45) Word count(lines,words and characters)


[cs15090@cs4210 ~]$ wc -lc file1.txt
1 14 file1.txt

46) Sort a file


[cs15090@cs4210 ~]$ cat>f2
def
abc
[cs15090@cs4210 ~]$sort f2
abc
def

47) Sort a file in reverse order


[cs15090@cs4210 ~]$sort -r f2
def
abc

48) Display unique output of a file


[cs15090@cs4210 ~]$cat>f1
abi
priya
kp
pavi
pavi
abi
[cs15090@cs4210 ~]$sort –u f1
abi
priya
kp
pavi

49) To append contents to a file


[cs15090@cs4210 ~]$cat>file3
How
[cs15090@cs4210 ~]$cat>file4
Are
cat file3 file4>>file5
How are

Reg. No. : 190501036 Page No. :


50) Sort ignoring case
[cs15090@cs4210 ~]$cat>file f6
With
Hai
Jkl
[cs15090@cs4210 ~]$sort f6 –f
Hai
Jkl
With

51) Sort the numbers


[cs15090@cs4210 ~]$ cat>f7
123
256
[cs15090@cs4210 ~]$ sort f7 –n
9
123
256

52) Sort the contents of two files and store it in another file
[cs15090@cs4210 ~]$ cat>l1
Ghj
Uio
[cs15090@cs4210 ~]$ cat>l2
How r u
[cs15090@cs4210 ~]$ sort l1l2>>l3
[cs15090@cs4210 ~]$ cat l3
Ghj
How r u
Uio

53) Sort and merge two files


[cs15090@cs4210 ~]$ sort l1 l2 –m
Ghj
How r u
Uio

54) Sort the standard input


[cs15090@cs4210d4~]$sort-
l
o
a
b
k
a
b
k

55) Sort the standard input with the file content


[cs15090@cs4210 ~]$ sort f1-
d
a
g
a
d
g

56) Rename a directory


[cs15090@cs4210 ~]$mv d2 d3
[cs15090@cs4210 ~]$cd d2
[cs15090@cs4210
d2~]$ Directory d3 is renamed
as d2

Reg. No. : 190501036 Page No. :


57) Move files in a directory

Reg. No. : 190501036 Page No. :


[cs15090@cs4210 pav~]$ls
f1 f2
[cs15090@cs4210 pav~]$cd
[cs15090@cs4210 ~]$mv pav/f1 d1
[cs15090@cs4210 ~]$cd d1
[cs15090@cs4210 ~]$ls
f1 f2 f3 f4
[cs15090@cs4210 ~]$ls
f2

58) Display the selected fields in the file


[cs15090@cs4210 ~]$ cat s1

1 a 100
2 b 101
3 c 102
4 d 103
[cs15090@cs4210 ~]$cut –c 1 s1
1
2
3
4

59) Search for the given pattern and print it


[cs15090@cs4210 ~]$cat>f6
hai friends
hai.how r u?
fine
[cs15090@cs4210 ~]$grep ha*f6
hai friends
hai.how r u?

60) Search for the pattern ignoring case and starting with either of [a-f] in the file and print it
[cs15090@cs4210 ~]$grep [a-f]?h –i –n f6:
f6:hai friends.1
f6:hai.how r u?2
f6:fine 3

61) Search for the pattern ending with either of [s-z] in the file and print it along with the line numbers
[cs15090@cs4210 ~]$grep –n [s-z]$f6
f6:hai friends.1
f6:hai.how r u?2

62) Ignore duplicate data and print it


[cs15090@cs4210 ~]$cat>f7
hai
hai
fine
[cs15090@cs4210 ~]$uniq –d f7
hai
fine

63) Print the unique data


[cs15090@cs4210 ~]$cat f7
hai
hai
fine
[cs15090@cs4210 ~]$uniq –d f7
hai
fine

64) Count the number of times the particular data is present in the file
[cs15090@cs4210 ~]$cat f7
hai
hai
Reg. No. : 190501036 Page No. :
fine
[cs15090@cs4210 ~]$uniq –d f7
hai 2
fine 1

65) Create link between two files


[cs15090@cs4210 ~]$cat f7
hai
hai
fine
[cs15090@cs4210 ~]$ln f7 f10
[cs15090@cs4210 ~]$cat f10
hai
hai
fine

RESULT
Thus the Unix commands has been executed successfully

Reg. No. : 190501036 Page No. :


EX. NO. : 2 SHELL PROGRAMMING
DATE :

A) SUM OF TWO NUMBERS

AIM
To find sum of two numbers using Shell programming.

ALGORITHM
i) START
ii) Read two values
iii) c is equals to a + b
iv) then print c
v) STOP

PROGRAM
#!/bin/sh
echo Enter two numbers :
read a b
c=`expr $a + $b`
echo Sum of $a and $b is $c

SAMPLE INPUT AND OUTPUT


Enter two numbers :
2
3
Sum of 2 and 3 is 5

RESULT
Hence, the program to find sum of two numbers using Shell programming has been executed successfully.

B) SWAPPING TWO NUMBERS

AIM
To swap two numbers using Shell Programming.

ALGORITHM
i) START
ii) Read two numbers
iii) temp is equal to a
iv) a is equal to b
v) b is equal to temp
vi) Then print both values
vii) STOP

PROGRAM
#!/bin/sh
echo Enter two numbers :
read a b
Reg. No. : 190501036 Page No. :
temp=$a
a=$b
b=$temp
echo Numbers after swapping : $a $b

SAMPLE INPUT AND OUTPUT


Enter two numbers :
3
6
Numbers after swapping : 6 3

RESULT
Hence, the program to swap two numbers using Shell Programming has been executed successfully.

C) LARGEST OF THREE NUMBERS

AIM
To find the largest of three numbers using Shell Programming.

ALGORITHM
i) START
ii) Read three values
iii) Check if, a is greater than b and a is greater than c
iv) If yes, print a is greatest
v) Else check if, b is greater than c
vi) If yes, print b is greatest
vii) Else, print c is greatest
viii) STOP

PROGRAM
#!/bin/sh
echo Enter 3 numbers :
read a b c
if(( ($a > $b) && ($a > $c) ))
then
echo $a is greatest
elif(($b > $c))
then
echo $b is greatest
else
echo $c is greatest
fi

SAMPLE INPUT AND OUTPUT


Enter 3 numbers :
3 9 7
9 is greatest

RESULT
Hence, the program to find the largest of three numbers using Shell Programming has been executed
successfully.

D) FACTORIAL OF A NUMBER

AIM
To find the factorial of a number using Shell Programming.
Reg. No. : 190501036 Page No. :
ALGORITHM
i) START
ii) Read a value
iii) Initialize f is equal to 1
iv) Open loop for condition i is less than equal to n
v) When i is equal to 0 and increment i
vi) f is equal to f * i
vii) then print f
viii) STOP

PROGRAM
#!/bin/sh
echo Enter a number :
read n
f=1
for((i=1; i<=n; i++))
do
f=`expr $f \* $i`
done
echo "The factorial of $n is $f"

SAMPLE INPUT AND OUTPUT


Enter a number :
4
The Factorial of 4 is 120

RESULT
Hence, the program to find the factorial of a number using Shell Programming has been executed successfully.

E) DAYS IN A WEEK

AIM
To find days in a week using Shell Programming.

ALGORITHM
i) START
ii) Read a value
iii) Check if, n is equal to 1
iv) Then print Sunday
v) Check if, n is equal to 2
vi) Then print Monday
vii) Check if, n is equal to 3
viii) Then print Tuesday
ix) Check if, n is equal to 4
x) Then print Wednesday
xi) Check if, n is equal to 5
xii) Then print Thursday
xiii) Check if, n is equal to 6
xiv) Then print Friday
xv) Check if, n is equal to 7
xvi) Then print Saturday
xvii) else, print invalid
xviii) STOP

PROGRAM
Reg. No. : 190501036 Page No. :
#!/bin/sh
echo Enter a number :
read n
if (( $n == 1))
then
echo $n=sunday
elif (( $n == 2 ))
then
echo $n=monday
elif (( $n == 3 ))
then
echo $n=tuesday
elif (( $n == 4 ))
then
echo $n=wednesday
elif (( $n == 5 ))
then
echo $n=Thursday
elif (( $n == 6 ))
then
echo $n=friday
elif (( $n == 7 ))
then
echo $n=saturday
else
echo $n=invalid
fi

SAMPLE INPUT AND OUTPUT


Enter a number :
7
Saturday

RESULT
Hence, the program to find days in a week using Shell Programming has been executed successfully.

F) FIBONACCI SERIES

AIM
To find Fibonacci series using Shell Programming.

ALGORITHM
i) START
ii) Read a value
iii) Initialize a is equal 0 and b is equal to 1
iv) Print a and b
v) Enter loop, i is less than or equal to n
vi) When i = 3 and i++
vii) print c
viii) c is equal to a + b
ix) a is equal to b and b is equal to c
x) STOP

PROGRAM
#!/bin/sh
echo Enter the limit
read n
a=0
Reg. No. : 190501036 Page No. :
b=1
echo The fibonacci series
echo $a
echo $b
for((i=3; i<=n; i++))
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
done

SAMPLE INPUT AND OUTPUT


Enter the limit
5
The Fibonacci series
0 1 1 2 3 5

RESULT
Hence, the program to find Fibonacci series using Shell Programming has been executed successfully.

G) AMSTRONG NUMBER

AIM
To find whether the number is Armstrong or not using Shell Programming.

ALGORITHM
i) START
ii) Read a value
iii) m is equal to n and s is equal to 0
iv) enter loop for condition n is less than 0
v) if yes, p is equal to n % 10
vi) c is equal to p*p*p
vii) s is equal to s + c
viii) n is equal to n / 10
ix) check if, s is equal to m
x) if yes, print Armstrong
xi) else, print Not Armstrong
xii) STOP

PROGRAM
#!/bin/sh
echo Enter the number
read n
m=$n
s=0
while(($n > 0))
do
p=`expr $n % 10`
c=`expr $p \* $p \* $p`
s=`expr $s + $c`
n=`expr $n / 10`
done
if(($s == $m))
then
echo Armstrong
else
echo Not Armstrong
fi

Reg. No. : 190501036 Page No. :


SAMPLE INPUT AND OUTPUT
Enter the number
153
Armstrong

RESULT
Hence, the program to find whether the number is Armstrong or Not using Shell Programming has been
executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 3 SYSTEM CALLS OF UNIX OPERATING SYSTEM

DATE :

AIM
To implement system calls of UNIX operating system fork, exec, getpid, exit, wait, close, stat,
opendir, readdir.

ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.

PROGRAM

Stat()

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
int main(void)
{
char *path,path1[10];
struct stat *nfile;
nfile=(struct stat *) malloc (sizeof(struct stat));
printf("enter name of file whose stsistics has to");
scanf("%s",path1);
stat(path1,nfile);
printf("user id %d\n",nfile->st_uid);
printf("block size :%d\n",nfile->st_blksize);
printf("last access time %d\n",nfile->st_atime);
printf("time of last modification %d\n",nfile->st_atime);
printf("porduction mode %d \n",nfile->st_mode);
printf("size of file %d\n",nfile->st_size);
printf("nu,mber of links:%d\n",nfile->st_nlink);
}

Reg. No. : 190501036 Page No. :


Wait()
#include<stdio.h>
#include<unistd.h>
int main(void)
{
int pid,status,exitch;
if((pid=fork())==-1)
{
perror("error");
exit (0);
}
if(pid==0)
{
sleep(1);
printf("child process");
exit (0);
}
else
{
printf("parent process\n");
if((exitch=wait(&status))==-1)
{
perror("during wait()");
exit (0);
}
printf("parent existing\n");
exit (0);
}
}

Getpid()
#include<stdio.h>
int main()
{
int pid;
pid=getpid();
printf("process ID is %d\n",pid);
pid=getppid();
printf("parent process ID id %d\n",pid);
}

Fork()
#include<stdio.h>
int main(void)
{
int fork(void),value;
value=fork();
printf("main:value =%d\n",value);
return 0;
}

Reg. No. : 190501036 Page No. :


Exec()
#include<stdio.h>
main()
{
int pid;
char *args[]={"/bin/ls","-l",0};
printf("\nParent Process");
pid=fork();
if(pid==0)
{
execv("/bin/ls",args);
printf("\nChild process");
}
else
{
wait();
printf("\nParent process");
exit(0);
}
}

Opendir,readdir
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc,char *argv[])
{
char buff[256];
DIR *dirp;
printf("\n\nEnter directory name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("Error");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}

SAMPLE INPUT AND OUTPUT

STAT::
OUTPUT:::
enter name of file whose stsistics has toALGO

user id 0
block size :0
last access time 0
Reg. No. : 190501036 Page No. :
time of last modification 0
porduction mode 0
size of file 0
number of links:0

WAIT:
enter name of file whose stsistics has toSE ALL DIAGRAMS
user id 0
block size :0
last access time 0

time of last modification 0


porduction mode 0
size of file 0
number of links:0

GETPID:::
process ID is 461
parent process ID id 460

FORK::::
process ID is 461
parent process ID id 460

Exec()::::
total 16
-rwxr-xr-x 1 runner2 runner2 8769 Sep 16 04:23 a.out
-rwxrwxrwx 1 root root 249 Sep 16 04:23 main.c

Parent Process

Parent process

Opendir,readdir:::
Enter directory nameCoding
Error

RESULT
Hence, the programs to implement system calls of UNIX operating system has been executes
successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 4 SIMULATE UNIX COMMANDS

DATE :

AIM
To simulate UNIX commands such as cp, mv, ls, grep.

A) cp

ALGORITHM
i) START
ii) Create file pointers fp1,fp2
iii) Open the first file in read mode to read the data from file.
iv) Open the second file in write mode to write the date to the file.
v) Check end of file of f1 and write the data into the second file.
vi) Close both the files.
vii) STOP

PROGRAM
#include<stdio.h>
void main()
{
char temp[200];
FILE *fp1,*fp2;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
fgets(temp,200,fp1);
while(!feof(fp1))
{
fputs(temp,fp2);
fgets(temp,200,fp1);
}
fclose(fp1);
fclose(fp2);
}

SAMPLE INPUT AND OUTPUT


In file1 In file2
Aaa Aaa
Bbb Bbb
Cccc Cccc

B) MOVE (mv)

ALGORITHM
i) START
ii) Create file pointers fp1,fp2
iii) Open the first file in read mode to read the data from file.
iv) Open the second file in write mode to write the date to the file.
v) Check end of file of f1 and write the data into the second file.
vi) Close both the files
vii) Remove the first file.
viii) STOP

Reg. No. : 190501036 Page No. :


PROGRAM
#include<stdio.h>
void main()
{
char temp[200];
FILE *fp1,*fp2;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
while(!feof(fp1))
{
fgets(temp,200,fp1);
fputs(temp,fp2);
}
fclose(fp1);
fclose(fp2);
remove("file1.txt");
}

SAMPLE INPUT AND OUTPUT

In file2
Xxx
Ttt
Yyyy
//file1 gets deleted

C) GREP

ALGORITHM
i) START
ii) Create a file pointer f1
iii) Declare i=0,temp[100],b[30]
iv) Ask the user to enter a pattern
v) Open the file in read mode.
vi) If the pattern matches in the file then print the line number and the string.
vii) Close the file
viii) STOP

PROGRAM
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1;
int i=0;
char temp[100],b[30];
printf("Enter pattern\n");
scanf("%s",b);
f1=fopen("file1.txt","r");
fgets(temp,100,f1);
while(!feof(f1))
{
i++;
if(strstr(temp,b))
{
printf("sub string is %s \n line number is %d ",temp,i);
}
fgets(temp,100,f1);
}
Reg. No. : 190501036 Page No. :
fclose(f1);
}

SAMPLE INPUT AND OUTPUT


Enter pattern
Shr
Sub string is my name is shruthakeerthy
Line number is 1.

D) LIST

ALGORITHM
i) START
ii) Create a DIR pointer p and a struct dirent pointer x
iii) Open the required directory.
iv) Do x=readdirectory(p).
v) In a loop while(x!=NULL)
vi) Print the name of directory
vii) STOP

PROGRAM
#include<stdio.h>
#include<dirent.h>
void main()
{
DIR *p;
struct dirent *x;
p=opendir("oslab");
x=readdir(p);
while(x!=NULL)
{
printf("%s",x->d_name);
x=readdir(p);
}
}

SAMPLE INPUT AND OUTPUT


...a.out aaa.txt fff.txt grep.c list.c sss.txt

RESULT
Thus the basic UNIX commands were successfully simulated.

Reg. No. : 190501036 Page No. :


Reg. No. : 190501036 Page No. :
EX. NO. : 5 CPU SCHEDULING ALGORITHMS

DATE :

AIM
To implement CPU Scheduling Algorithm.

A) FCFS – WITHOUT ARRIVAL TIME

ALGORITHM
1. Start
2. Define a structure to initialize the variables pro,b,f,w,t
main()
3. Input the number of processes, process name, burst time
4. s[0].w=0, s[0].t=s[0].b, s[0].f=s[0].b
5. wavg=s[0].w, tavg=s[0].t
6. Repeat steps 6 to 8 till i<n, s[i].f=s[i].b+s[i-1].f
7. s[i].w=s[i].f-s[i].a-s[i].b, s[i].t=s[i].f-s[i].a
8. wavg+=s[i].w, tavg+=s[i].t
9. Find the wavg and tavg and print
10.Stop

PROGRAM
#include<stdio.h>

struct fcfs
{
char pro[10];
int b,f,w,t;
}s[10];

main()
{
int n,i;
float wavg,tavg;
printf("Enter the number of process:");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the process name:");
scanf("%s",&s[i].pro);
printf("Enter the burst time:");
scanf("%d",&s[i].b);
}
s[0].w=0;
s[0].t=s[0].b;
s[0].f=s[0].b;
wavg=s[0].w;
tavg=s[0].t;
for(i=1;i<n;i++)
{
s[i].f=s[i].b+s[i-1].f;
s[i].w=s[i].f-s[i].a-s[i].b;
s[i].t=s[i].f-s[i].a;
wavg+=s[i].w;
tavg+=s[i].t;

Reg. No. : 190501036 Page No. :


}
wavg=wavg/n;
tavg=tavg/n;

printf("\nPN\tBT\tFT\tWT\tTAT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d",s[i].pro,s[i].b,s[i].f,s[i].w,s[i].t);
}

printf("\n\nAverage waiting time:%f",wavg);


printf("\nAverage turnover time:%f",tavg);
}

SAMPLE INPUT AND OUTPUT


Enter the number of process:3

Enter the process name:P1


Enter the burst time:24

Enter the process name:P2


Enter the burst time:3

Enter the process name:P3

Enter the burst time:3

PN BT FT WT TAT

P1 24 24 0 24

P2 3 27 24 27

P3 3 30 27 30

Average waiting time:17.000000

Average turnover time:27.000000

B) FCFS – WITH ARRIVAL TIME

ALGORITHM
1. Start
2. Define a structure fcfs to initialize the variables pro,a,b,f,w,t
main()
3. Input the number of processes, process name, arrival time, and burst time
4. If min.a>s[j].a, swap s[i] and s[j]
5. s[0].w=0, s[0].t=s[0].b
6. s[0].f=s[0].b+s[0].a
7. wavg=s[0].w, tavg=s[0].t;
8. s[i].w=s[i].f-s[i].a-s[i].b;
9. s[i].t=s[i].f-s[i].a;
10. wavg+=s[i].w, tavg+=s[i].t;
11. Find the wavg and tavg and print
12. Stop

PROGRAM
#include<stdio.h>

Reg. No. : 190501036 Page No. :


struct fcfs
{
char pro[10];
int a,b,f,w,t;
}s[10],min;

main()
{
int n,i,j;
float wavg,tavg;
printf("Enter the number of process:");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\nEnter the process name:");
scanf("%s",&s[i].pro);
printf(" Enter the arrival time:");
scanf("%d",&s[i].a);
printf("Enter the burst time:");
scanf("%d",&s[i].b);
}

for(i=0;i<=n;i++)
{
min=s[i];
for(j=i+1;j<=n-1;j++)
{
if(min.a>s[j].a)
{
min=s[j];
s[j]=s[i];
s[i]=min;
}
}
}

s[0].w=0;
s[0].t=s[0].b;
s[0].f=s[0].b+s[0].a;
wavg=s[0].w;
tavg=s[0].t;
for(i=1;i<n;i++)
{
if(s[i-1].f>=s[i].a)
{
s[i].f=s[i].b+s[i-1].f;
}
else
{
s[i].f=s[i].a+s[i].b;
}
s[i].w=s[i].f-s[i].a-s[i].b;
s[i].t=s[i].f-s[i].a;
wavg+=s[i].w;
tavg+=s[i].t;
}

wavg=wavg/n;
tavg=tavg/n;

printf("\nPN\tAT\tBT\tFT\tWT\tTAT");
Reg. No. : 190501036 Page No. :
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",s[i].pro,s[i].a,s[i].b,s[i].f,s[i].w,s
[i].t);
}

printf("\n\nAverage waiting time:%f",wavg);


printf("\nAverage turnover time:%f",tavg);
}

SAMPLE INPUT AND OUTPUT


Enter the number of process:5

Enter the process name:p1


Enter the arrival time:0
Enter the burst time:10

Enter the process name:p2

Enter the arrival time:5

Enter the burst time:1

Enter the process name:p3

Enter the arrival time:2

Enter the burst time:3

Enter the process name:p4

Enter the arrival time:3

Enter the burst time:4

Enter the process name:p5

Enter the arrival time:1

Enter the burst time:5

PN AT BT FT WT TAT

p1 0 10 10 0 10

p5 1 5 15 9 14

p3 2 3 18 13 16

p4 3 4 22 15 19

p2 5 1 23 17 18

Reg. No. : 190501036 Page No. :


Average waiting time:10.800000

Average turnover time:15.400000

C) PRIORITY SCHEDULING

ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.

PROGRAM
#include<stdio.h>
main()
{
int i,j,k,n,t,pv,priority, a[10],b[10],p[10],w[10],f[10],ta[10],flag[10];
printf("Enter the number of jobs : ");
scanf("%d",&n);
printf("\nEnter the arrival & burst times of the jobs along with the priority of
execution\n\n");
for(i=0;i<n;i++)
{
printf("Job %d : ",i+1);
flag[i]=0;
scanf("%d", &a[i]);
scanf("%d", &b[i]);
scanf("%d", &p[i]);
}
for(i=0;i<n;i++) /* SORTING */
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;

t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
Reg. No. : 190501036 Page No. :
i=0;
do
{
if(i == 0)
{
f[i] = b[i];
flag[i] = 1;
w[i] = f[i] - (a[i] + b[i]);
ta[i] = f[i] - a[i];
pv = i++;
continue;
}
j = 0;
k = 0;
priority = n+1;

while((f[pv] >= a[j]) && (j < n))


{
if((priority > p[j]) && (flag[j] == 0))
{
priority = p[j];
k = j;
}
j++;
}
if(k != 0)
{
f[k] = f[pv] + b[k];
flag[k] = 1;
pv = k;
}
else
{
f[i] = f[pv] + b[i];
if(f[pv] < a[i]) /* IDLE TIME */
{
f[i]+=a[i]-f[pv];
}
flag[i] = 1;
pv = i;
}
w[pv] = f[pv] - (a[pv] + b[pv]);
ta[pv] = f[pv] - a[pv];
i++;
}while(i<n);
printf("\n\t\t\t\t OUTPUT \n");
printf("\t\t\t\t------------- \n");
printf("\n\tAT\tBT\tPRIORITY\tFT\tWT\tTT\t\n\n");for(i=0;i<n;i++)
{
printf("\t%d\t%d\t %d\t%d\t%d\t%d\n",a[i],b[i],p[i],f[i],w[i],ta[i]);
}
}

SAMPLE INPUT AND OUTPUT

Enter the number of jobs : 5

Enter the arrival & burst times of the jobs along with the priority of execution

Job 1 : 0 11 2

Reg. No. : 190501036 Page No. :


Job 2 : 5 28 0
Job 3 : 12 2 3
Job 4 : 2 10 1
Job 5 : 9 16 4
OUTPUT

AT BT PRIORITY FT WT

0 11 2 11 0 11
2 10 0 21 9 19
5 28 3 65 32 60
9 16 1 37 12 28
12 2 4 67 53 55

D) ROUND ROBIN

ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.

PROGRAM
#include<stdio.h>
main()
{
int n, i, k = 0, count = 0, bt[10], rt[10], ft[10], flag[10], TS;
char pn[10][10];

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


scanf("%d", &n);

printf("Enter Process name and burst time :: \n");


for(i = 0; i < n; i++)
{
scanf("%s %d", pn[i], &bt[i]);
flag[i] = 0;
rt[i] = bt[i];
}

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


scanf("%d", &TS);
do
{
for(i = 0; i < n; i++)
{
if(flag[i] == 0)
{
Reg. No. : 190501036 Page No. :
if(rt[i] <= TS)
{
ft[i] = count + rt[i];
rt[i] = 0;
flag[i] = 1;
k++;
}
else
{
ft[i] = count + TS;
rt[i] = rt[i] - TS;
}
count = ft[i];
}
}
}while(k<n);

printf("PN\t BT\t FT\n");


for(i = 0; i < n; i++)
{
printf("%s\t %d\t %d\n",pn[i], bt[i], ft[i]);
}
}

SAMPLE INPUT AND OUTPUT


Enter the number of processes :: 5

Enter Process name and burst time ::

P1 8
P2 2
P3 7
P4 3
P5 5

Enter the time slice :: 3


PN BT FT
P1 8 24
P2 2 5
P3 7 25
P4 3 11
P5 5 22

E) SJF

ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.

Reg. No. : 190501036 Page No. :


11)Call sem_post with mutex as a parameter.
12)Stop.

PROGRAM
#define maxno 1000
#include<stdio.h>
#include<string.h>
main()
{
int i,j,k,n,t,pv,flg,smallbt,a[10],b[10],w[10],f[10],ta[10],flag[10];
char p[10][4],temp[10];
printf("Enter the number of jobs : ");
scanf("%d",&n);
printf("\nEnter the process name, arrival & burst times of the jobs\n\n");
for(i=0;i<n;i++)
{
printf("Job %d : ",i+1);
flag[i]=0;
scanf("%s", p[i]);
scanf("%d", &a[i]);
scanf("%d", &b[i]);
}
for(i=0;i<n;i++) /* SORTING */
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
t=a[i]; a[i]=a[j]; a[j]=t;

t=b[i]; b[i]=b[j]; b[j]=t;

strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
i=0,f[0]=0,pv=0;
do
{
flg = 0;
j = 0;
k = 0;
smallbt = maxno;
while((f[pv] >= a[j]) && (j < n))
{
if((smallbt > b[j]) && (flag[j] == 0))
{
flg = 1;
smallbt = b[j];
k = j;
}
j++;
}
if(flg == 0)
{
f[i] = a[i] + b[i]; /* IDLE TIME */
flag[i] = 1;
pv = i;
}
else
Reg. No. : 190501036 Page No. :
{
f[k] = f[pv] + b[k];
flag[k] = 1;
pv = k;
}
w[pv] = f[pv] - (a[pv] + b[pv]);
ta[pv] = f[pv] - a[pv];
i++;
}while(i<n);
printf("\n\n\t\t\t\t OUTPUT \n");
printf("\nPN\tAT\tBT\tFT\tWT\tTT\t\n\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\n",p[i],a[i],b[i],f[i],w[i],ta[i]);
}

SAMPLE INPUT AND OUTPUT

Enter the number of jobs : 4

Enter the process name, arrival & burst times of the jobs

Job 1 : P1 4 4
Job 2 : P2 0 3
Job 3 : P3 2 7
Job 4 : P4 5 6

OUTPUT

PN AT BT FT WT TT

P2 0 3 3 0 3

P3 2 7 10 1 8

P1 4 4 14 6 10

P4 5 6 20 9 15

RESULT
Hence, the programs to implement CPU Scheduling Algorithm has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 6 FILE ALLOCATION STRATEGIES

DATE :

AIM
To implement file allocation strategies.

A) SEQUENTIAL

ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.

PROGRAM
#include<stdio.h>
#include<stdlib.h>

struct all
{
int pgno, status, linkno;
}a[10];

print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d :: %d\n",i, a[i].pgno, a[i].linkno);
}
}

main()
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac, k;
printf("Enter the number of frames :: ");
scanf("%d", &n);
printf("Enter the frame size :: ");
scanf("%d", &fz);

printf("Enter the status of the frame (Empty - (0)/Full - (1))\n");


for(i = 1; i <= n; i++)
{
printf("%d :: ",i);
scanf("%d", &a[i].status);
if(a[i].status == 0)
{
a[i].pgno = 0;
}

Reg. No. : 190501036 Page No. :


else
{
a[i].pgno = 1;
a[i].linkno = -999999;
occ++;
}
}

do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
nf--;
occ++;
a[i].status = 1;

a[i].pgno = pg * 10 + cnt;
if((cnt == 1) && ( nf == 0))
a[i].linkno = -1;
else if(cnt == 1)
a[i].linkno = 0;
else if(nf == 0)
{
a[i].linkno = -1;
a[k].linkno = i;
break;
}

else
a[k].linkno = i;
cnt++;
k = i;

printf("\tk=%d\n",k);
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
Reg. No. : 190501036 Page No. :
}while(ch == 1);
}

SAMPLE INPUT AND OUTPUT

Enter the number of frames :: 5

Enter the frame size :: 4

Enter the status of the frame (Empty - (0)/Full - (1))

1 :: 2

2 :: 3

3 :: 4

4 :: 1

5 :: 2

Enter the size of the block to be allocated :: 4

All frames are occupied

B) INDEXED

ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.

PROGRAM
#include<stdio.h>
#include<stdlib.h>

struct all
{
int pgno, status;
}a[10];

print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d\n",i, a[i].pgno);
}
}

main()
Reg. No. : 190501036 Page No. :
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac;
printf("Enter the number of frames :: ");
scanf("%d", &n);
printf("Enter the frame size :: ");
scanf("%d", &fz);

printf("Enter the status of the frame (Empty - (0)/Full - (1))\n");


for(i = 1; i <= n; i++)
{
printf("%d :: ",i);
scanf("%d", &a[i].status);
if(a[i].status == 0)
{
a[i].pgno = 0;
}
else
{
a[i].pgno = 1;
occ++;
}
}

do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
a[i].pgno = pg * 10 + cnt;
occ++;
cnt++;
nf--;
a[i].status=1;
}
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
}while(ch == 1);
}

Reg. No. : 190501036 Page No. :


SAMPLE INPUT AND OUTPUT

Enter the number of frames :: 3

Enter the frame size :: 3

Enter the status of the frame (Empty - (0)/Full - (1))

1 :: 2

2 :: 1

3 :: 3

Enter the size of the block to be allocated :: 3

Frames unavailable

C) LINKED

ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.

PROGRAM
#include<stdio.h>
#include<stdlib.h>

struct all
{
int pgno, status;
}a[10];

print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d\n",i, a[i].pgno);
}
}

main()
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac;
printf("Enter the number of frames :: ");
scanf("%d", &n);
Reg. No. : 190501036 Page No. :
printf("Enter the frame size :: ");
scanf("%d", &fz);

printf("Enter the status of the frame (Empty - (0)/Full - (1))\n");


for(i = 1; i <= n; i++)
{
printf("%d :: ",i);
scanf("%d", &a[i].status);
if(a[i].status == 0)
{
a[i].pgno = 0;
}
else
{
a[i].pgno = 1;
occ++;
}
}

do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
a[i].pgno = pg * 10 + cnt;
occ++;
cnt++;
nf--;
a[i].status=1;
}
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
Reg. No. : 190501036 Page No. :
}while(ch == 1);
}

SAMPLE INPUT AND OUTPUT

Enter the number of frames :: 3

Enter the frame size :: 3

Enter the status of the frame (Empty - (0)/Full - (1))

1 :: 1

2 :: 1

3 :: 2

Enter the size of the block to be allocated :: 2

Frames unavailable

RESULT
Hence, the programs for file allocation strategies has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 7 PRODUCER CONSUMER PROBLEM USING SEMAPHORES
DATE :

AIM
To implement Producer Consumer Problem using Semaphores

ALGORITHM
i) START
ii) Initialise the semaphone and all the values needs to be used.
iii) From main call the producer function.
iv) In producer function we need to run an infinite loop.
v) Initialise i has 0 and call wait function with empty and mutex as a parameter.
vi) Increment i and assign it to put.
vii) Then assign p to who and print put value.
viii) Call the signal function with mutex and full as parameter.
ix) Call wait function with full and mutex as a parameter.
x) Assign put to get and c to who then print get.
xi) Call the signal function with mutex and empty as parameter.
xii) In wait function check whether s is less than or equal to 0 and who is equal to p then call customer orelse call
producer.
xiii) Orelse s is subtracted by 1 itself.
xiv) In signal function we increment s by 1.
xv) STOP

PROGRAM
#include<stdio.h>
typedef int semaphore;
semaphore mutex=1,full=0,empty=1;
char who;
int put,get;

producer()
{
do
{
static int i=0;
wait(&empty);
wait(&mutex);
i++;
put=i;
who='p';
printf("PUT: %d/n",put);
signal(&mutex);
signal(&full);
}
while(1);
}

consumer()
{
do
{
wait(&full);
wait(&mutex);
get=put;
who='c';
printf("GET: %d/n",get);
Reg. No. : 190501036 Page No. :
signal(&mutex);
signal(&empty);
}
while(1);
}
wait(semaphore *s)
{
if(*s<=0)
{
if(who=='p')
consumer();
else
producer();
}
else
*s=*s-1;
}
signal(semaphore *s)
{
*s=*s+1;
}
main()
{
printf("\t\tproducer consumer problem using semaphore\n");
printf("\n\npress ctrl+z to break execution\n\n");
producer();
}

SAMPLE INPUT AND OUTPUT

press ctrl+z to break execution

PUT: 1/nGET: 1/nPUT: 2/nGET: 2/nPUT: 3/nGET: 3/nPUT: 4/nGET: 4/nPUT: 5/nGET: 5/nPUT: 6/nGET:
6/nPUT: 7/nGET: 7/nPUT: 8/nGET: 8/nPUT: 9/nGET: 9/nPUT: 10/nGET: 10/nPUT: 11/nGET: 11/nPUT:
12/nGET: 12/nPUT: 13/nGET: 13/nPUT: 14/nGET: 14/nPUT: 15/nGET: 15/nPUT: 16/nGET: 16/nPUT:
17/nGET: 17/nPUT: 18/nGET: 18/nPUT: 19/nGET: 19/nPUT: 20/nGET: 20/nPUT: 21/nGET: 21/nPUT:
22/nGET: 22/nPUT: 23/nGET: 23/nPUT: 24/nGET: 24/nPUT: 25/nGET: 25/nPUT: 26/nGET: 26/nPUT:
27/nGET: 27/nPUT: 28/nGET: 28/nPUT: 29/nGET: 29/nPUT: 30/nGET: 30/nPUT: 31/nGET: 31/nPUT:
32/nGET: 32/nPUT: 33/nGET: 33/nPUT: 34/nGET: 34/nPUT: 35/nGET: 35/nPUT: 36/nGET: 36/nPUT:
37/nGET: 37/nPUT: 38/nGET: 38/nPUT: 39/nGET: 39/nPUT: 40/nGET: 40/nPUT: 41/nGET: 41/nPUT:
42/nGET: 42/nPUT: 43/nGET: 43/nPUT: 44/nGET: 44/nPUT: 45/nGET: 45/nPUT: 46/nGET: 46/nPUT:
47/nGET: 47/nPUT: 48/nGET: 48/nPUT: 49/nGET: 49/nPUT: 50/nGET: 50/nPUT: 51/nGET: 51/nPUT:
52/nGET: 52/nPUT: 53/nGET: 53/nPUT: 54/nGET: 54/nPUT: 55/nGET: 55/nPUT: 56/nGET: 56/nPUT:
57/nGET: 57/nPUT: 58/nGET: 58/nPUT: 59/nGET: 59/nPUT: 60/nGET: 60/nPUT: 61/nGET: 61/nPUT:
62/nGET: 62/nPUT: 63/nGET: 63/nPUT: 64/nGET: 64/nPUT: 65/nGET: 65/nPUT: 66/nGET: 66/nPUT:
67/nGET: 67/nPUT: 68/nGET: 68/nPUT: 69/nGET: 69/nPUT: 70/nGET: 70/nPUT: 71/nGET: 71/nPUT:
72/nGET: 72/nPUT: 73/nGET: 73/nPUT: 74/nGET: 74/nPUT: 75/nGET: 75/nPUT: 76/nGET: 76/nPUT:
77/nGET: 77/nPUT: 78/nGET: 78/nPUT: 79/nGET: 79/nPUT: 80/nGET: 80/nPUT: 81/nGET: 81/nPUT:
82/nGET: 82/nPUT: 83/nGET: 83/nPUT: 84/nGET: 84/nPUT: 85/nGET: 85/nPUT: 86/nGET: 86/nPUT:
87/nGET: 87/nPUT: 88/nGET: 88/nPUT: 89/nGET: 89/nPUT: 90/nGET: 90/nPUT: 91/nGET: 91/nPUT:
92/nGET: 92/nPUT: 93/nGET: 93/nPUT: 94/nGET: 94/nPUT: 95/nGET: 95/nPUT: 96/nGET: 96/nPUT:
97/nGET: 97/nPUT: 98/nGET: 98/nPUT: 99/nGET: 99/nPUT: 100/nGET: 100/nPUT: 101/nGET: 101/nPUT::
2289/

RESULT
Hence, the program to implement Producer Consumer Problem using Semaphores has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 8 FILE ORGANIZATION TECHNIQUES
DATE :

AIM
To implement File Organization Techniques.

A) SINGLE LEVEL

ALGORITHM

1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the files to store in directory.
5)Store the files in directory.

PROGRAM

#include<stdio.h>

void main()
{
int i, j, k, n, x;
char d[10][10];
char fn[10][10][10];
int s[10];

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


scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter the name of the directories :: ");
scanf("%s",d[i]);
}

printf("Enter the size of the directories :: ");


for(i = 0; i < n; i++)
scanf("%d", &s[i]);

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


{
printf("Enter File names of directory %s", d[i]);
for(j = 0; j < s[i]; j++)
{
scanf("%s", fn[i][j]);
}
}

printf("\n\nDIRECTORY\tSIZE\tFILENAMES\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);

Reg. No. : 190501036 Page No. :


for(j = 0; j < s[i]; j++)
{
printf("\t%s\n",fn[i][j]);
printf("\t\t");
}
}
}

SAMPLE INPUT AND OUTPUT

No of Directories 1
size of Directory is 1
Name of Directory is Dir
Enter the file names F1

In Directory Dir Filesnames


F1

B) TWO LEVEL

ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5)Get the Names of each sub Directory.
6)Get the file names.
7)Store the files in Sudirectory.

PROGRAM
#include<stdio.h>
void main()
{
int i, j, k, n, x;
char d[10][10];
char sd[10][10][10];
char fn[10][10][10][10];
int s[10], ss[10][10];

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


scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter the name of the directories :: ");
scanf("%s",d[i]);
}

printf("Enter the size of the directories :: ");


for(i = 0; i < n; i++)
scanf("%d", &s[i]);

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


{
printf("Enter Sub Directory names and size of directory %s", d[i]);
for(j = 0; j < s[i]; j++)
{
scanf("%s %d", sd[i][j], &ss[i][j]);
}
Reg. No. : 190501036 Page No. :
}

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


{
for(j = 0; j < s[i]; j++)
{
printf("Enter the file names of Sub Directory %s :: ",sd[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
scanf("%s", fn[i][j][k]);
}
}
}

printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t", fn[i][j][k]);
printf("\t\t\t\t");
}
printf("\n\t\t");
}
}

SAMPLE INPUT AND OUTPUT

No of Directories 1
size of Directory is 1
Name of Directory is Dir
Enter the size of sub Directory 1
Enter the name of Sub Directory Sub1
Enter the file names F1

In Directory Dir
In Sub Directory sub1
F1

C) HIERARCHICAL

ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5) Get the Names of each sub Directory.
6) Get the sub directory size and name of sub Directory
7)Get the file names.
8)Store the files.

PROGRAM
#include<stdio.h>
#include<conio.h>

Reg. No. : 190501036 Page No. :


void main()
{
int i, j, k, l, n, x;
char d[10][10];
char sd[10][10][10];
char sdd[10][10][10][10], fn[4][4][4][4][10];
int s[10], ss[10][10], sss[10][10][10];

clrscr();
printf("Enter the number of directories :: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter the name of the directories :: ");
scanf("%s",d[i]);
}

printf("Enter the size of the directories :: ");


for(i = 0; i < n; i++)
scanf("%d", &s[i]);

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


{
printf("Enter Sub Directory names and size of directory %s", d[i]);
for(j = 0; j < s[i]; j++)
{
scanf("%s %d", sd[i][j], &ss[i][j]);
}
}

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


{
for(j = 0; j < s[i]; j++)
{
printf("Enter the sub Directory and size of Sub Directory %s ::
",sd[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
scanf("%s %d", sdd[i][j][k], &sss[i][j][k]);
printf("Enter the file names of sub Directory %s",
sdd[i][j][k]);
for(l = 0; l < sss[i][j][k]; l++)
{
scanf("%s",fn[i][j][k][l]);
}
}
}
}

printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\n")
;
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t%d\t", sdd[i][j][k], sss[i][j][k]);
for(l = 0; l < sss[i][j][k]; l++)
{
Reg. No. : 190501036 Page No. :
printf("%s\n",fn[i][j][k][l]);
printf("\t\t\t\t\t\t\t");
}
printf("\t\t\t\t\t\t");
}
printf("\n\t\t");
}
printf("\n\t");
}
getch();
}

SAMPLE INPUT AND OUTPUT


No of Directories 1
size of Directory is 1
Name of Directory is Dir
Enter the size of sub Directory 1
Enter the name of Sub Directory Sub1
Enter the size of Sub Directory 1
Enter the name of sub Directory SSub1
Enter the file names F1

In Directory Dir
In Sub Directory sub1
In Sub Directory SSub1
F1

D) DAG

ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5)Get the Names of each sub Directory.
6)Get the file names.
7) Ask Whether the files connected to anyother sub directory.
8) If yes means connect files to that sub directory,No means store NULL.
9)Store the files.

PROGRAM
#include<stdio.h>

void main()
{
int i, j, k, n, x;
char d[10][10];
char sd[10][10][10];
char fn[10][10][10][10], lk[10][10][10][10];
int s[10], ss[10][10];

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


scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter the name of the directories :: ");
scanf("%s",d[i]);
}

Reg. No. : 190501036 Page No. :


printf("Enter the size of the directories :: ");
for(i = 0; i < n; i++)
scanf("%d", &s[i]);

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


{
printf("Enter Sub Directory names and size of directory %s", d[i]);
for(j = 0; j < s[i]; j++)
{
scanf("%s %d", sd[i][j], &ss[i][j]);
}
}

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


{
for(j = 0; j < s[i]; j++)
{
printf("Enter the file names of Sub Directory %s :: ",sd[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
scanf("%s", fn[i][j][k]);
printf("Is the file linked to any other directory :: ");
scanf("%d", &x);
if(x)
{
printf("Enter the subdirectory name :: ");
scanf("%s",lk[i][j][k]);
}
else
{
strcpy(lk[i][j][k],"NULL");
}
}
}
}

printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\tLINK\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t", fn[i][j][k]);
printf("%s\n",lk[i][j][k]);
printf("\t\t\t\t");
}
printf("\n\t\t");
}
}

SAMPLE INPUT AND OUTPUT


No of Directories 1
size of Directory is 2
Name of Directory is Dir
Enter the size of sub Directory 1

Reg. No. : 190501036 Page No. :


Enter the name of Sub Directory Sub1
Enter the file names F1
Is the file linked to anyother directory 0
Enter the size of sub Directory 2
Enter the name of Sub Directory Sub2
Enter the file names F2
Is the file linked to anyother directory 1
Enter the subdirectory name Sub1

In Directory Dir
In Sub directory sub1
Files are F1->NULL
In Sub directory sub2
Files are F2->sub1

RESULT
Hence, the programs to implement File Organization Techniques has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 9 BANKERS ALGORITHM FOR DEAD LOCK AVOIDANCE
DATE :

AIM
To implement Bankers algorithm for Deadlock Avoidance.

ALGORITHM
1) Start
2) Initialise all the values required for the algorithm.
3) In input function collect all the number process and its resources.
4)Then get the allocation matrix,maximum matrix and availabe matrix
from the user
5)In show function display all the values we got from the input function
6)In safety function run the for loop from 0 to n and assign finish of
i as 0.
7)Run the for loop from 0 to r and initialise work of i as avail of i.
8)Run a while loop with statement i is less than n and initialise flag
as 0 and again a loop from 0 to r.
9) Check if finish of i is equal to 0 and need is less than avialable then
add available with the allocated values.
10) Then break the loop if the flag is equal to 0.
11) Check the count of values which is equal to finish of i and if it is
equal to the value of n then the system is safe or else its not.
12) Check the process number and get the value of request from the user.
13)Check whether the request is greater than need.
14) If so request cant be formed or else the request is formed.
15) Run the form loop from 0 to r and calculate availabem allocation and
need of the page and check whether it is safe or not.
16) Stop.

PROGRAM
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int work[100];
int finish[100];
int request[100];
int n,r;

void input()
{
int i,j;
printf("Enter the number of processes :: ");
scanf("%d",&n);
printf("Enter the number of resources :: ");
scanf("%d",&r);
printf("Enter the allocation matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter the max matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);

Reg. No. : 190501036 Page No. :


printf("Enter the available matrix :: ");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);

for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j] = max[i][j] - alloc[i][j];
}

void show()
{
int i,j;
printf("Allocation Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",alloc[i][j]);
}
}
printf("\nMax Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",max[i][j]);
}
}
printf("\nAvailable Matrix\n");
for(i=0;i<r;i++)
printf("%d\t",avail[i]);
printf("\nNeed Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",need[i][j]);
}
}
}

int safety()
{
int i,j, k, c1 = 0, flag, count = 0;
for(i=0;i<n;i++)
{
finish[i] = 0;
}
for(i=0;i<r;i++)
{
work[i] = avail[i];
}

printf("\n");
while(count < n)
{
Reg. No. : 190501036 Page No. :
flag = 0;
for(i = 0; i < n; i++)
{
int c=0;
for(j = 0; j < r; j++)
{
if((finish[i] == 0) && ( need[i][j] <= work[j]))
{
c++;
if(c==r)
{
for(k = 0; k < r; k++)
{
work[k] +=
alloc[i][k];
}
finish[i] = 1;
count++;
printf("P%d -> ",i);
flag = 1;
}
}
}
}
if(flag == 0)
break;
}

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


{
if(finish[i] == 1)
{
c1++;
}
}

printf("%d", c1);

if(c1 == n)
{
printf("\n The system is in safe state!!!\n\n");
return 1;
}
else
{
printf("\n The system is in unsafe state since there is a
deadlock!!!\n\n");
return 0;
}
}

void resourcerequest()
{
int p, i;
printf("Enter the process number requesting for resources :: ");
scanf("%d", &p);

printf("Enter the request :: ");


for(i = 0; i < r; i++)
{
scanf("%d", &request[i]);
}
Reg. No. : 190501036 Page No. :
for(i = 0; i < r; i++)
{
if(request[i] > need[p][i])
{
printf("Request can't exceed Need!!!\n");
return;
}
if(request[i] > avail[i])
{
printf("Process %d should wait for Resources!!!\n",p);
return;
}
}

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


{
avail[i] = avail[i] - request[i];
alloc[p][i] = alloc[p][i] + request[i];
need[p][i] = need[p][i] - request[i];
}

show();
int x = safety();
if(x)
printf("The new system is safe!!! Request can be granted!!!\n");
else
printf("The new system is unsafe!!! Request can't be granted!!!\n");
}
main()
{
int x;
input();
show();
x = safety();
if(x) resourcerequest();
else
printf("RESOURCE REQUEST CANT BE PERFORMED \n");
}

SAMPLE INPUT AND OUTPUT


Enter the number of processes :: 5
Enter the number of resources :: 3
Enter the allocation matrix :: 1 1 2
2 1 2

4 0 1

0 2 0

1 1 2
Enter the max matrix :: 4 3 3
3 2 2

9 0 2

7 5 3

Reg. No. : 190501036 Page No. :


1 1 2

Enter the available matrix :: 2 10 0


Allocation Matrix
1 1 2

2 1 2

4 0 1

0 2 0

1 1 2
Max Matrix
4 3 3

3 2 2

9 0 2

7 5 3

1 1 2
Available Matrix
2 1 0
Need Matrix

3 2 1

1 1 0

5 0 1

7 3 3

0 0 0

P1 -> P4 -> P0 -> P2 -> P3 -> 5

The system is in safe state!!!

RESULT
Hence the program to implement Bankers algorithm for Deadlock Avoidance has been executes successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 10 DEAD LOCK DETECTION
DATE :

AIM
To implement an algorithm for Deadlock Detection

ALGORITHM
1) Start
2) Initialise all the values required for the algorithm.
3) In input function collect all the number process and its resources.
4)Then get the allocation matrix,maximum matrix and availabe matrix
from the user
5)In show function display all the values we got from the input function
6)In safety function run the for loop from 0 to n and assign finish of
i as 0.
7)Run the for loop from 0 to r and initialise work of i as avail of i.
8)Run a while loop with statement i is less than n and initialise flag
as 0 and again a loop from 0 to r.
9) Check if finish of i is equal to 0 and need is less than avialable then
add available with the allocated values.
10) Then break the loop if the flag is equal to 0.
11) Check the count of values which is equal to finish of i and if it is
equal to the value of n then the system is safe or else its not.
12) Stop.

PROGRAM
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int work[100];
int finish[100];
int n,r;

void input()
{
int i,j;
printf("Enter the number of processes :: ");
scanf("%d",&n);
printf("Enter the number of resources :: ");
scanf("%d",&r);
printf("Enter the allocation matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter the max matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("Enter the available matrix :: ");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);

for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j] = max[i][j] - alloc[i][j];
Reg. No. : 190501036 Page No. :
}

void show()
{
int i,j;
printf("Allocation Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",alloc[i][j]);
}
}
printf("\nMax Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",max[i][j]);
}
}
printf("\nAvailable Matrix\n");
for(i=0;i<r;i++)
printf("%d\t",avail[i]);
printf("\nNeed Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",need[i][j]);
}
}
}

void safety()
{
int i,j, k, c1 = 0, flag, count = 0;
for(i=0;i<n;i++)
{
finish[i] = 0;
}
for(i=0;i<r;i++)
{
work[i] = avail[i];
}

printf("\n");
while(count < n)
{
flag = 0;
for(i = 0; i < n; i++)
{
int c=0;
for(j = 0; j < r; j++)
{
if((finish[i] == 0) && ( need[i][j] <= work[j]))
Reg. No. : 190501036 Page No. :
{
c++;
if(c==r)
{
for(k = 0; k < r; k++)
{
work[k] +=
alloc[i][k];
}
finish[i] = 1;
count++;
printf("P%d -> ",i);
flag = 1;
}
}
}
}
if(flag == 0)
break;
}

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


{
if(finish[i] == 1)
{
c1++;
}
}

printf("%d", c1);

if(c1 == n)
{
printf("\n The system is in safe state!!!\n\n");
}
else
{
printf("\n The system is in unsafe state since there is a
deadlock!!!\n\n");
}
}

main()
{
int x;
input();
show();
safety();
}

SAMPLE INPUT AND OUTPUT

Enter the number of processes :: 5

Enter the number of resources :: 3

Enter the allocation matrix :: 1 1 2

212

401
Reg. No. : 190501036 Page No. :
020

112

Enter the max matrix :: 4 3 3

322

902

753

112

Enter the available matrix :: 2 10 0


Allocation Matrix
1 1 2

2 1 2

4 0 1

0 2 0

1 1 2
Max Matrix
4 3 3

3 2 2

9 0 2

7 5 3

1 1 2

Available Matrix
2 1 0
Need Matrix
3 2 1

1 1 0

5 0 1

7 3 3

0 0 0

P1 -> P4 -> P0 -> P2 -> P3 -> 5

The system is in safe state!!!

RESULT
Hence the program to implement an algorithm for Deadlock Detection has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 11 PAGE REPLACEMENT ALGORITHMS
DATE :

AIM
To implement page replacement algorithms.

A) FIFO

ALGORITHM
1) Start.
2) Get the number of pages from the user and the page of i.
3)Get the frame size of initialise each frame as -1.
4) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
5) If avail is equal to 0 then frame of j is equal to page of i.
6) J is equal to j added by 1 with a module of f and increment count.
7)Run the loop from 0 to f and get the frame value
8)Print the number of page fault.
9)Stop.

PROGRAM
#include<stdio.h>

main()
{
int avail, count, i, j, n, f, k, page[10], frame[10];

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


scanf("%d",&n);

printf("Enter the pages :: ");


for(i = 0; i < n; i++)
scanf("%d", &page[i]);

printf("Enter the number of frames in memory :: ");


scanf("%d", &f);

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


frame[i] = -1;
j = 0;
printf("\tPages\t Page Allocated to Frame\n");
for(i = 0; i < n; i++)
{
printf("\t%d\t\t",page[i]);
avail=0;
for(k = 0; k < f; k++)
if(frame[k] == page[i])

avail=1;
if(avail == 0)
{
frame[j] = page[i];
j = (j + 1) % f;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
Reg. No. : 190501036 Page No. :
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}

SAMPLE INPUT AND OUTPUT

Enter the number of pages :: 5

Enter the pages :: 4


1
2
4
5

Enter the number of frames in memory :: 3

Pages Page Allocated to Frame

4 4 -1 -1

1 4 1 -1

2 4 1 2

5 5 1 2

Number of Page Fault is 3

B) LRU

ALGORITHM
1) Start.
2) In struct initialise the values.
3) Get the maximum page number and get the values of pno and cnt from the
loop.
4the number of pages from the user and the page of i.
5)Get the frame size of initialise each frame as -1.
6) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
7) If frame of k is equal to page of i then avail if equal to q and m of
frame of k with count incremented.
8) If avail is equal to 0 then small is equal to n added by 1.
9)Run the loop from 0 to f and get the frame value
10) If it is equal to -1 then sind is equal to k and then break.
11) If small is greater then m of x.count small is equal to m of x.count
then sind is equal to k and then break.
12) Then print the sind value and frame of sind as page of i.
13)Initialise x as page of i and m of x with count incremented.
14)Run the for loop from 0 to f and print the frame of k.
15)Print the number of page fault .
16)Stop.

PROGRAM
#include<stdio.h>

Reg. No. : 190501036 Page No. :


struct page
{
int pno;
int cnt;
};

main()
{
struct page m[10];
int avail, small, sind, c, count = 0, x, i, j, n, f, k, pmax, page[25], frame[10];

printf("Enter the maximum page number :: ");


scanf("%d",&pmax);

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


{
m[i].pno = i;
m[i].cnt = 0;
}

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


scanf("%d",&n);

printf("Enter the pages :: ");


for(i = 0; i < n; i++)
{
scanf("%d", &page[i]);
}

printf("Enter the number of frames in memory :: ");


scanf("%d", &f);

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


frame[i] = -1;
j = 0;
printf("\tPages\t Page Allocated to Frame\n");
for(i = 0; i < n; i++)
{
printf("\t%d\t\t",page[i]);
c++;
avail=0;
for(k = 0; k < f; k++)
{
if(frame[k] == page[i])
{
avail=1;
m[frame[k]].cnt = c;
}
}

sind = 0;
if(avail == 0)
{
small = n + 1;
for(k = 0; k < f; k++)
{
x = frame[k];
if(x == -1)
{
sind = k;
break;
}
Reg. No. : 190501036 Page No. :
if(small > m[x].cnt)
{
small = m[x].cnt;
sind = k;
}
}

//printf("\nsind :: %d",sind);
frame[sind] = page[i];
x = page[i];
m[x].cnt = c;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}

SAMPLE INPUT AND OUTPUT


Enter the maximum page number :: 10

Enter the number of pages :: 10

Enter the pages :: 7


5
9
4
3
7
9
6
2
1

Enter the number of frames in memory :: 3

Pages Page Allocated to Frame

7 7 -1 -1

5 7 5 -1

9 7 5 9

4 4 5 9

3 3 5 9

7 7 5 9

6 6 5 9

2 2 5 9

1 1 5 9
Reg. No. : 190501036 Page No. :
Number of Page Fault is 9

C) LFU

ALGORITHM
1) Start.
2) In struct initialise the values.
3) Get the maximum page number and get the values of pno and cnt from the
loop.
4the number of pages from the user and the page of i.
5)Get the frame size of initialise each frame as -1.
6) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
7) If frame of k is equal to page of i then avail if equal to q and m of
frame of k with count incremented.
8) If avail is equal to 0 then small is equal to n added by 1.
9)Run the loop from 0 to f and get the frame value
10) If it is equal to -1 then sind is equal to k and then break.
11) If small is greater then m of x.count small is equal to m of x.count
then sind is equal to k and then break.
12) Then print the sind value and frame of sind as page of i.
13)Initialise x as page of i and m of x with count incremented.
14)Run the for loop from 0 to f and print the frame of k.
15)Print the number of page fault .
16)Stop.

PROGRAM
#include<stdio.h>

struct page
{
int pno;
int cnt;
};

main()
{
struct page m[10];
int avail, small, sind, count = 0, x, i, j, n, f, k, pmax, page[25], frame[10];

printf("Enter the maximum page number :: ");


scanf("%d",&pmax);

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


{
m[i].pno = i;
m[i].cnt = 0;
}

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


scanf("%d",&n);

printf("Enter the pages :: ");


for(i = 0; i < n; i++)
{
scanf("%d", &page[i]);
}

printf("Enter the number of frames in memory :: ");


scanf("%d", &f);
Reg. No. : 190501036 Page No. :
for(i = 0; i < f; i++)
frame[i] = -1;
j = 0;
printf("\tPages\t Page Allocated to Frame\n");
for(i = 0; i < n; i++)
{
printf("\t%d\t\t",page[i]);
avail=0;
for(k = 0; k < f; k++)
{
if(frame[k] == page[i])
{
avail=1;
m[frame[k]].cnt++;
}
}

if(avail == 0)
{
small = n + 1;
for(k = 0; k < f; k++)
{
x = frame[k];
if(x == -1)
{
sind = k;
break;
}
if(small > m[x].cnt)
{
small = m[x].cnt;
sind = k;
}
}

//printf("\nsind :: %d",sind);
frame[sind] = page[i];
x = page[i];
m[x].cnt++;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}

SAMPLE INPUT AND OUTPUT

Enter the maximum page number :: 12

Enter the number of pages :: 12

Enter the pages :: 1

Reg. No. : 190501036 Page No. :


4

Enter the number of frames in memory :: 3

Pages Page Allocated to Frame

1 1 -1 -1

2 1 2 -1

3 1 2 3

4 4 2 3

5 5 2 3

1 5 2 1

4 5 2 4

3 5 2 3

Number of Page Fault is 8

RESULT
Hence the program to implement a page replacement algorithms has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 12 SHARED MEMORY, MESSAGE PASSING AND PIPES
DATE :

AIM
To implement Shared memory, message passing and pipes.

A) SHARED MEMORY

ALGORITHM

Client:
1) Start.
2) Initialise all the values.
3) Check if shmid is equal shmget of key, SHMSZ, 066 which are less then
0 then print Cannot attach the data and exit.
4) Check if shm is equal shmat of shmid, NULL, 0 which is equal to char of *
subtracted by 1 0 then print Cannot attach the data and exit.
5) Assign s as shm and run for loop from shm to NULL and increment s
6)Putchar of s and add space then shm as *.
7)Atlast exit with 0.
8)Stop.

Server:
1) Start.
2) Initialise all the values.
3) Check if shmid is equal shmget of key, SHMSZ, IPC_CREAT or of 0666
then print Cannot attach the data and exit.
4) Check if shm is equal shmat of shmid, NULL, 0 which is equal to char of *
subtracted by 1 0 then print Cannot attach the data and exit.
5) Assign c as shm and run for loop from a to less than or equal to z and
increment c
6) S increment is equal to c.
7) While shm is not equal to * the sleep of 1.
8)Stop.

PROGRAM

a.) Client.c

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>

#define SHMSZ 27

main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;

if((shmid = shmget(key, SHMSZ,0666)) < 0)


{
printf("Cannot Allocate Shared Memory\n");

Reg. No. : 190501036 Page No. :


exit(1);
}

if((shm = shmat(shmid,NULL,0))==(char *)-1)


{
printf("Cannot Attach Data in Shared Memory\n");
exit(1);
}

s=shm;

for(s=shm; *s != '\0'; s++)


{
putchar(*s);
putchar(' ');
}
putchar('\n');

*shm='*';

exit(0);
}

b.) Server.c

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>

#define SHMSZ 27

main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;

if((shmid = shmget(key, SHMSZ,IPC_CREAT | 0666)) < 0)


{
printf("Cannot Allocate Shared Memory\n");
exit(1);
}

if((shm = shmat(shmid,NULL,0))==(char *)-1)


{
printf("Cannot Attach Data in Shared Memory\n");
exit(1);
}

s=shm;

for(c = 'a'; c <= 'z'; c++)


{
*s++ = c;
}

*s='\0';

while(*shm != '*')
sleep(1);
Reg. No. : 190501036 Page No. :
exit(0);
}

SAMPLE INPUT AND OUTPUT:

Hello

Hello

B) MESSAGE PASSING

ALGORITHM

Sender
Algo:
1) Start
2) Initialise a tydef of structure and values.
3)And then gt all the values in it.
4)Check if msqid is equal to mssget of key msgflg which is less then 0.
5)Else the fprintf of stderr.
6)Initialise mtype as 1 and print the message.
7)Then string ccopy the value and print it.
8) Then calculate the buffer length and check if msgsnd of msqid and sbuf
buffer length and IPC_NOWAIT is less than 0 then print all th values.
9) perror of msgsnd the exit and print the message of sbuf of mtext.
10)Stop

Receiver
Algo:
1) Start.
2) Check is msqid is equal to msgget of key 0666 which is less then 0 and
perror of mssgget then exit.
3) Check if msgrcv of msqid and rbuf MSGSZ, 1,0 which is less then 0 and perror
of msgrcv then exit.
4) Print the rbuf of mtext then exit of 0.
5)Stop
PROGRAM
Message Passing

a.) Message send.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf
{long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
Reg. No. : 190501036 Page No. :
message_buf sbuf;
size_t buf_length;
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n", key,
msgflg);
if ((msqid = msgget(key, msgflg )) < 0)
{perror("msgget");
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);

sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Did you get this?");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
exit(0);
}

b.) Message receive.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ 128
typedef struct msgbuf
{long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid; key_t
key; message_buf
rbuf;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
{perror("msgget");
exit(1);
}
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0)
{
perror("msgrcv");
exit(1);
}
printf("%s\n", rbuf.mtext);
exit(0);
}

SAMPLE INPUT AND OUTPUT

C) PIPES

ALGORITHM
Reg. No. : 190501036 Page No. :
1) Start
2) Get the string from the pipe.
3) Initialise fork to child check if child is not equal then close the fd
of 10 .
4) Then write fd of 1, a,5 and wait as 0.
5) Else close the fd of 1, read fd of 0, a,5 then print the retrived valu\
for pipe.
6) Stop.

PROGRAM
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf("\n enter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{ close(fd[10]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("\n the string retrived from pipe is %s\n",a);
}
return 0;
}

SAMPLE INPUT AND OUTPUT

enter the string to enter into the pipe:5

the string retrived from pipe is 5

RESULT
Hence the program to implement Shared memory, message passing and pipes has been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 13 PAGING AND SEGMENTATION TECHNIQUE
DATE :

AIM
To implement Paging and Segmentation Technique of memory management.

A) PAGING

ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7) physical addres is equal to page table of pageno. multiplied by page
size and add it with ofset.
8) Then print physical address.
9)Stop.

PROGRAM
#include <stdio.h>
int main()
{
int pgtable[10];
int psz, nop, i, la, pgno, off, pa;

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


scanf("%d", &nop);

printf("Enter the frame numbers in the page table :: ");


for(i = 0; i < nop; i++)
{
scanf("%d", &pgtable[i]);
}

printf("Enter the page size :: ");


scanf("%d", &psz);

printf("Enter the logical address :: ");


scanf("%d", &la);

pgno = la / psz;
off = la % psz;

pa = pgtable[pgno] * psz + off;

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


}

SAMPLE INPUT AND OUTPUT


Enter the number of pages :: 4
Enter the frame numbers in the page table :: 4 5 6 7
Enter the page size :: 4
Enter the logical address :: 13
Physical Address :: 29

Reg. No. : 190501036 Page No. :


B) SEGMENTATION

ALGORITHM
1) Start.
2) Get the value of number of segements.
3) Run the for loop till nos and get the base and limit value of the segment.
4)Then get the logical address of the segment.
5) Check is s of sno. limit is greater then equal to offset.
6) Then calculate pa as s of sno. base and add offset then print pa.
7)Orelse print Invalid address.
8)Stop

PROGRAM
#include <stdio.h>

struct segtable
{
int base;
int limit;
};

int main()
{
struct segtable s[10];
int nos, i, sno, off, pa;

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


scanf("%d", &nos);

printf("Enter the base and limit in the segment table :: ");


for(i = 0; i < nos; i++)
{
scanf("%d", &s[i].base);
scanf("%d", &s[i].limit);
}

printf("Enter the logical address (Segment No and Offset) :: ");


scanf("%d %d", &sno, &off);

if(s[sno].limit >= off)


{
pa = s[sno].base + off;
printf("Physical Address :: %d\n", pa);
}
else
printf("Invalid Address!!!\n");
}

SAMPLE INPUT AND OUTPUT

Enter the number of segments :: 4


Enter the base and limit in the segment table :: 1000 100
2000 200
3000 300
4000 400
Enter the logical address (Segment No and Offset) :: 0 20
Physical Address :: 1020

Reg. No. : 190501036 Page No. :


Enter the number of segments :: 4
Enter the base and limit in the segment table :: 1000 100
2000 200
3000 300
4000 400
Enter the logical address (Segment No and Offset) :: 3 450
Invalid Address!!!

RESULT
Hence the program to implement Paging and Segmentation Technique of memory management has been
executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 14 THREADING & SYNCHRONIZATION APPLICATIONS
DATE :

AIM
To implement Threading & Synchronization Applications for Reader Writer Problem

ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.

PROGRAM

#include<stdio.h>
#include<sys/types.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
int b;
int readcount;
sem_t mutex,wrt;
void *w()
{
sem_wait(&wrt);
printf("Enter the value :: ");
scanf("%d",&b);
printf("Shared Value (Writer) = %d\n",b);
sem_post(&wrt);
}
void *r(void *a)
{
int *rr=(int *)a;
sem_wait(&mutex);
readcount++;
if(readcount==1)
sem_wait(&wrt);
sem_post(&mutex);
printf("I am Reader %d\t",*rr);
printf("\n Last Updated Shared value (Reader) =%d\n",b);
sem_wait(&mutex);
readcount--;
if(readcount==0)
sem_post(&wrt);
sem_post(&mutex);
Reg. No. : 190501036 Page No. :
}
int main()
{
pthread_t w1,w2,r1,r2;
int rr1=1,rr2=2;
b=20;
readcount=0;

sem_init(&wrt,0,1);
sem_init(&mutex,0,1);

pthread_create(&w1,NULL,w,NULL);
pthread_create(&r1,NULL,r,(void *)&rr1);
pthread_create(&r2,NULL,r,(void *)&rr2);
pthread_create(&w2,NULL,w,NULL);

pthread_join(w1,NULL);
pthread_join(r1,NULL);
pthread_join(r2,NULL);
pthread_join(w2,NULL);
}

SAMPLE INPUT AND OUTPUT

[cs15001@co297 ~]$ cc -pthread readwrite.c


[cs15001@co297 ~]$ ./a.out
Enter the value :: 4
Shared Value (Writer) = 4
I am Reader 2
Last Updated Shared value (Reader) =4
I am Reader 1
Last Updated Shared value (Reader) =4
Enter the value :: 2
Shared Value (Writer) = 2

RESULT
Hence the program to implement Threading & Synchronization Applications for Reader Writer Problem has
been executed successfully.

Reg. No. : 190501036 Page No. :


EX. NO. : 15 STUDY OF MINIX OPERATING SYSTEM
DATE :

AIM
To Study Minix Operating System

MINIX

Minix (from "mini-Unix") is a POSIX-compliant (since version 2.0), Unix-like operating system based on a
microkernel architecture.

Early versions of MINIX were created by Andrew S. Tanenbaum for educational purposes. Starting with
MINIX 3, the primary aim of development shifted from education to the creation of a highly reliable and self-
healing microkernel OS. MINIX is now developed as open-source software.

MINIX was first released in 1987, with its complete source code made available to universities for study in
courses and research. It has been free and open-source software since it was re-licensed under the BSD-3-
Clause license in April 2000

Implementation
Minix 1.0
Andrew S. Tanenbaum created MINIX at Vrije Universiteit in Amsterdam to exemplify the principles conveyed
in his textbook, Operating Systems: Design and Implementation (1987).

An abridged 12,000 lines of the C source code of the kernel, memory manager, and file system of MINIX 1.0
are printed in the book. Prentice-Hall also released MINIX source code and binaries on floppy disk with a
reference manual. MINIX 1 was system-call compatible with Seventh Edition Unix.[11]

Tanenbaum originally developed MINIX for compatibility with the IBM PC and IBM PC/AT 8088
microcomputers available at the time.

Minix 1.5
MINIX 1.5, released in 1991, included support for MicroChannel IBM PS/2 systems and was also ported to the
Motorola 68000 and SPARC architectures, supporting the Atari ST, Commodore Amiga, Apple Macintosh[12]
and Sun SPARCstation computer platforms. There were also unofficial ports to Intel 386 PC compatibles (in
32-bit protected mode), National Semiconductor NS32532, ARM and Inmos transputer processors. Meiko
Scientific used an early version of MINIX as the basis for the MeikOS operating system for its transputer-based
Computing Surface parallel computers. A version of MINIX running as a user process under SunOS and Solaris
was also available, a simulator named SMX (operating system) or just SMX for short.[13][14]

Reg. No. : 190501036 Page No. :


Minix 2.0
MINIX 2.0.4 system startup and login prompt

MINIX 2.0.4 shell interaction


Demand for the 68k-based architectures waned, however, and MINIX 2.0, released in 1997, was only available
for the x86 and Solaris-hosted SPARC architectures. It was the subject of the second edition of Tanenbaum's
textbook, cowritten with Albert Woodhull and was distributed on a CD-ROM included with the book. MINIX
2.0 added POSIX.1 compliance, support for 386 and later processors in 32-bit mode and replaced the Amoeba
network protocols included in MINIX 1.5 with a TCP/IP stack.

Version 2.0.3 was released in May 2001. It was the first version after MINIX had been relicensed under the
BSD-3-Clause license, which was retroactively applied to all previous versions.[15]

Minix-vmd
Main article: Minix-vmd
Minix-vmd is a variant of MINIX 2.0 for Intel IA-32-compatible processors, created by two Vrije Universiteit
researchers, which adds virtual memory and support for the X Window System.

Minix 3
Main article: MINIX 3

Minix 3.2 running the "top" system monitoring command

Minix 3 running X11 with the twm window manager


Minix 3 was publicly announced on 24 October 2005 by Tanenbaum during his keynote speech at the
Association for Computing Machinery (ACM) Symposium on Operating Systems Principles (SOSP). Although
it still serves as an example for the new edition of Tanenbaum's textbook -coauthored by Albert S. Woodhull-, it
is comprehensively redesigned to be "usable as a serious system on resource-limited and embedded computers
and for applications requiring high reliability."[16]

Minix 3 currently supports IA-32 and ARM architecture systems. It is available in a Live CD format that allows
it to be used on a computer without installing it on the hard drive, and in versions compatible with hardware
emulating and virtualizing systems, including Bochs, QEMU, VMware Workstation/Fusion, VirtualBox, and
Microsoft Virtual PC.

Version 3.1.2 was released on 18 April 2006. It was the first version after MINIX had been relicensed under the
BSD-3-Clause license with a new fourth clause.[17]

Version 3.1.5 was released on 5 November 2009. It contains X11, emacs, vi, cc, gcc, perl, python, ash, bash,
zsh, ftp, ssh, telnet, pine, and over 400 other common Unix utility programs. With the addition of X11, this

Reg. No. : 190501036 Page No. :


version marks the transition away from a text-only system. In many cases it can automatically restart a crashed
driver without affecting running processes. In this way, MINIX is self-healing and can be used in applications
demanding high reliability. MINIX 3 also has support for virtual memory management, making it suitable for
desktop OS use.[18] Desktop applications such as Firefox and OpenOffice.org are not yet available for MINIX
3 however.

As of version 3.2.0, the userland was mostly replaced by that of NetBSD and support from pkgsrc became
possible, increasing the available software applications that MINIX can use. Clang replaced the prior compiler
(with GCC now having to be manually compiled), and GDB, the GNU debugger, was ported.[19][20]

Minix 3.3.0, released in September 2014, brought ARM support.

Minix 3.4.0RC, Release Candidates became available in January 2016;[21] however, a stable release of MINIX
3.4.0 is yet to be announced.

Minix supports many programming languages, including C, C++, FORTRAN, Modula-2, Pascal, Perl, Python,
and Tcl.

Minix 3 still has an active development community with over 50 people attending MINIXCon 2016, a
conference to discuss the history and future of MINIX.[22]

All Intel chipsets post-2015 are running MINIX 3 internally as the software component of the Intel
Management Engine.[23][24]

Relationship with Linux


Early influence
Linus Torvalds used and appreciated Minix,[25] but his design deviated from the Minix architecture in
significant ways, most notably by employing a monolithic kernel instead of a microkernel. This was
disapproved of by Tanenbaum in the Tanenbaum–Torvalds debate. Tanenbaum explained again his rationale for
using a microkernel in May 2006.[26]

Early Linux kernel development was done on a Minix host system, which led to Linux inheriting various
features from Minix, such as the Minix file system.

RESULT
Hence, the Study Minix Operating System has been performed successfully.

Reg. No. : 190501036 Page No. :

You might also like