OS Rec

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 125

SMVEC

Page No.

1. Calendar
(i) TASK
SYNTAX

: To display the calendar of the current month.


: cal (or)cal -1

EXPLANATION : Displays the calendar of the current month on the screen.


-1

Display single month output. (This is the default.)

-s

Display Sunday as the first day of the week. (This is

the default.)
-m
-j

Display Monday as the first day of the week.


Display Julian dates (days one-based, numbered from

January 1).
-y
OUTPUT

Display a calendar for the current year.

: [smvec8@localhost aravindan~]$ cal


July 2016
Su Mo Tu We Th Fr Sa
1 2
3

8 9

10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
(ii) TASK
SYNTAX

: To display the calendar of the specified year.


: cal year

EXPLANATION : Displays the calendar of the specified year on the screen.


OUTPUT

: [smvec8@localhost aravindan~]$ cal 2016


Displays all the months of the year 2016.

SMVEC
Page No.

(iii). TASK
year.

: To display the calendar of the corresponding month and

SYNTAX

: cal month year

EXPLANATION : Displays the calendar of the corresponding month and year


on the screen.
OUTPUT
2013

: [smvec8@localhost aravindan~]$ cal November


November 2013
Su Mo Tu We Th Fr Sa
1 2
3

8 9

10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
(iv). TASK
month.

: To display the calendar of the previous, current & next

SYNTAX

: cal -3

EXPLANATION : Displays the calendar of the previous, current & next month
on the screen.
OUTPUT

: [smvec8@localhost aravindan~]$ cal -3


June 2016

July 2016

Aug 2016
Su Mo Tu We Th Fr Sa
We Th Fr Sa
1
1

5 6 7
10 11 12 13

Su Mo Tu We Th Fr Sa
4

Su Mo Tu
1

6
8

9 10 11

12 13 14 15 16 17 18
16 17 18 19 20

10 11 12 13 14 15 16

14 15

19 20 21 22 23 24 25
24 25 26 27

17 18 19 20 21 22 23

21 22 23

26 27 28 29 30
30 31

24 25 26 27 28 29 30

28 29

31

SMVEC
Page No.

2. Date
(i). TASK
SYNTAX

: To display the system date and time


: date

EXPLANATION : This command displays the current system date and time on
the screen.
OUTPUT

: [smvec8@localhost aravindan~]$ date


Mon July 4 10:50:45 IST 2016

(ii). TASK

: To display the current month.

SYNTAX

: date +%m

EXPLANATION
screen.

: This command displays the current month on the

OUTPUT

: [smvec8@localhost aravindan~]$ date+%m


07

(iii). TASK

: To display the name of the current month.

SYNTAX

: date +%h

EXPLANATION
month on the screen
OUTPUT

: This command displays the name of the current


: [smvec8@localhost aravindan~]$ date +%h

July
(iv). TASK

: To display the current system date.

SYNTAX

: date +%d

EXPLANATION
the screen.

: This command displays the current system date on

OUTPUT

: [smvec8@localhost aravindan~]$ date +%d


04

SMVEC
Page No.

(v). TASK

: To display the current system (year)

SYNTAX

: date +%Y

EXPLANATION : This command displays the current system year on the


screen.
OUTPUT

: [smvec8@localhost aravindan~]$ date +%Y


2016

(vi). TASK

: To display the current system time.

SYNTAX

: date +%H

EXPLANATION
hour) on the screen.
OUTPUT

: This command displays the current system time (in


: [smvec8@localhost aravindan~]$ date +%H

10
(vii). TASK

: To display the current system weekday.

SYNTAX

: date +%a

EXPLANATION
: This command displays the current system
weekday (in abbreviated
form) on the screen.
OUTPUT

: [smvec8@localhost aravindan~]$ date +%a


Mon

(viii). TASK

: To display the current system second.

SYNTAX

: date +%S

EXPLANATION
: This command displays the current system
second on the screen.
OUTPUT

: [smvec8@localhost aravindan~]$ date +%S


32

SMVEC
Page No.

(ix). TASK

: To display the current system year.

SYNTAX

: date +%y

EXPLANATION
on the screen.
OUTPUT

: This command displays the current system year


: [smvec8@localhost aravindan~]$ date +%y

16

3. List
(i). TASK

: To list information about the files in a directory.

SYNTAX

: ls

EXPLANATION
by default) on the

: Lists information about the files (The current directory


screen.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt

line.sh
(ii). TASK

pqr.txt

sort.sh
a.txt

level.txt
b.txt

: To list information about the files in a directory.

SYNTAX

: ls - l

EXPLANATION
the screen.
OUTPUT

xyz.txt

: Lists information about the files in listing format on


: [smvec8@localhost aravindan~]$ ls - l

Total 28
-rw-rw-r--1 smvec8

smvec8

0 Jul 4 16:22 abc.txt

-rw-rw-r--1 smvec8

smvec8 26 Jul 4 17:13

f1.sh

SMVEC
Page No.

(iii). TASK

: To list information about the files in a directory.

SYNTAX

: ls - r

EXPLANATION
the screen.
OUTPUT

: Lists information about the files in reverse order on


: [smvec8@localhost aravindan~]$ ls - r

xyz.txt sort.sh
abc.txt b.txt
(iv). TASK

pqr.txt line.sh level.txt


a.txt
aravind

: To list information about the files in a directory.

SYNTAX

: ls - s

EXPLANATION

: Lists information about the files its size on the screen.

OUTPUT

: [smvec8@localhost aravindan~]$ ls - s

4 abc.txt 8 b.txt
(v). TASK

8 pqr.txt 8 line.sh

: To list information about the files in a directory.

SYNTAX
EXPLANATION
screen.
OUTPUT

8 a.txt

: ls - S
: Lists information about the files in sorted order on the
: [smvec8@localhost aravindan~]$ ls - S

aravind abc.txt a.txt b.txt level.txt


line.sh
pqr.txt sort.sh xyz.txt

SMVEC
Page No.

4. Copy
TASK

: To copy files and directories

SYNTAX

: cp f1 f2

EXPLANATION

: Copies f1 to f2

OUTPUT

: [smvec8@localhost aravindan~]$ cp a.txt b.txt


[smvec8@localhost aravindan~]$

cat b.txt

Hello guys!
How do you do?
5. Script/Edit
TASK

: To typescript a file that can be printed later/edit the existing

file
SYNTAX

: script scriptname
..
..
..
Exit.
/vi filename

EXPLANATION

: To open and type a script

OUTPUT

: [smvec8@localhost aravindan~]$ vi a.txt


~Hello all!
~How is it going?
-- INSERT--

SMVEC
Page No.

6. Remove
TASK

: To remove files.

SYNTAX

: rm filename

EXPLANATION

: This command removes the specified file.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt
line.sh

xyz.txt
pqr.txt

sort.sh
a.txt

level.txt
b.txt

[smvec8@localhost aravindan~]$

rm

level.txt
[smvec8@localhost aravindan~]$ ls
aravind

abc.txt
pqr.txt

xyz.txt
a.txt

sort.sh

line.sh

b.txt

7. Rename
TASK

: To rename files.

SYNTAX

: mv f1 f2

EXPLANATION

: This command renames source to destination.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt
line.sh

xyz.txt
pqr.txt

sort.sh
a.txt

level.txt
b.txt

[smvec8@localhost aravindan~]$

mv pqr.txt

rqp.txt
[smvec8@localhost aravindan~]$ ls
aravind

abc.txt
rqp.txt

xyz.txt
a.txt

sort.sh
b.txt

line.sh

SMVEC
Page No.

8. Make Directory
TASK

: To make a new directory.

SYNTAX

: mkdir

EXPLANATION

: This command creates a new directory.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt
line.sh

xyz.txt
pqr.txt

sort.sh
a.txt

level.txt
b.txt

[smvec8@localhost aravindan~]$

mkdir

student
[smvec8@localhost aravindan~]$ ls
aravind

abc.txt
rqp.txt

xyz.txt
a.txt

sort.sh
b.txt

line.sh
student

9. Remove Directory
TASK

: To remove/delete an existing directory.

SYNTAX

: rmdir

EXPLANATION

: This command deletes the directory.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt
line.sh

xyz.txt
pqr.txt

sort.sh
a.txt

level.txt
b.txt

[smvec8@localhost aravindan~]$

rmdir

student
[smvec8@localhost aravindan~]$ ls
aravind

abc.txt
rqp.txt

xyz.txt
a.txt

sort.sh
b.txt

line.sh

SMVEC
Page No.

10. Pwd
TASK

: To display the name of the current/working directory.

SYNTAX

: pwd

EXPLANATION
directory.

: This command displays the name of the current

OUTPUT

: [smvec8@localhost aravindan~]$ pwd


/home/smvec8/aravindan

11. Change Directory


TASK

: To change the working directory.

SYNTAX

: cd

EXPLANATION

: This command changes to the working directory.

OUTPUT

: [smvec8@localhost aravindan~]$ cd aravind


[smvec8@smvec/aravindan/aravind~]$ cd\
[smvec8@localhost aravindan~]$

12. Cat command


(i). TASK

: To open new file and print the contents on the screen.

SYNTAX

: cat>filename

EXPLANATION

: This command opens a new file.

OUTPUT

: [smvec8@localhost aravindan~]$ cat>a.txt


Hello guys!
How is it going?

SMVEC
Page No.

(ii). TASK

: To view the contents of a file on the screen.

SYNTAX

: cat filename

EXPLANATION

: This command is used to view the contents of a file.

OUTPUT

: [smvec8@localhost aravindan~]$ cat a.txt


Hello guys!
How is it going?

(iii). TASK

: To concatenate the contents of two files.

SYNTAX

: cat f1 f2>f3

EXPLANATION

: Concatenates the contents of f1 and f2 and save in f3.

OUTPUT
abc.txt >b.txt

: [smvec8@localhost aravindan~]$ cat a.txt


[smvec8@localhost aravindan~]$ cat b.txt
Hello guys!
How is it going?
Welcome to all
This is Aravindan.

(iv). TASK

: To display the contents of a file with line number.

SYNTAX

: cat n filename

EXPLANATION

: Displays the contents of file with line numbers.

OUTPUT

: [smvec8@localhost aravindan~]$ cat n b.txt


1 Hello guys!
2 How is it going?
3 Welcome to all
4 This is Aravindan.

SMVEC
Page No.

(v). TASK

: To redirect the contents of a file to another file.

SYNTAX

: cat f1>>f2

EXPLANATION
to f2.

: This command is used redirect the contents of file f1

OUTPUT
a.txt>>xyz.txt

: [smvec8@localhost aravindan~]$ cat


[smvec8@localhost aravindan~]$ cat xyz.txt
Hello guys!
How is it going?

13. Whoami
TASK

: To display the current user login and identity

SYNTAX

: whoami

EXPLANATION
login.
OUTPUT

: This command is used to display the current user


: [smvec8@localhost aravindan~]$ whoami
aravindan/smvec8

14. Who
TASK

: To display all the users currently logged in.

SYNTAX

: who

EXPLANATION

: This command is used to display all the users logged

OUTPUT

: [smvec8@localhost aravindan~]$ who

in.

smvec8

pts/89

2016-07-18

smvec36

pts/63

2016-07-18

smvec92

pts/103

2016-07-14

16:04
15:28(172.17.10.75)
21:13

SMVEC
Page No.

15. Finger
TASK
users.

: To display the detailed information about system

SYNTAX

: finger

EXPLANATION
: This command is used to display detailed info
about all the system users.
OUTPUT
Office

: [smvec8@localhost aravindan~]$ finger

Login
Name
OfficePhone
Host
smvec8

Tty

Idle

LoginTime

pts/89

Jul 18 16:04

(172.17.25.58)
smvec36

pts/63

smvec92

pts/103

Jul 18 15:28

(172.17.10.75)
3d

Jul 14 21:13

16. Last
TASK

: To display the list of last logged-in users for a month.

SYNTAX

: last

EXPLANATION
: This command is used to display the list of last
logged-in users for a
month.
OUTPUT

: [smvec8@localhost aravindan~]$ last

smvec16
still logged in

pts/49

smvec8
still logged in

pts/89

smvec21
16:04(00:00)

pts/78

172.17.10.69

Mon

smvec75
20:02(00:30)

pts/5

172.17.10.31

Thu

wtmp

begins

Thu

172.17.25.51

Mon
Mon

Jul 14 14:56:28 2016

Jul 18 16:09
Jul 18 16:04
Jul 18 16:04Jul 14 19:31-

SMVEC
Page No.

17. Head
TASK

: To display the first ten lines of a file.

SYNTAX

: head filename

EXPLANATION

: Displays the first ten lines of the file.

OUTPUT

: [smvec8@localhost aravindan~]$ head -3 b.txt


Hello guys!
How is it going?
Welcome to all

18. Tail
TASK

: To display the last ten lines of a file.

SYNTAX

: tail filename

EXPLANATION

: Displays the last ten lines of the file.

OUTPUT

: [smvec8@localhost aravindan~]$ last -3 b.txt


How is it going?
Welcome to all
This is Aravindan.

19. Clear
TASK

: To clear the content of the command prompt.

SYNTAX

: clear

EXPLANATION

: Clears the content of the command prompt.

OUTPUT

: [smvec8@localhost aravindan~]$ clear


(In a Clear Window)
[smvec8@smvec/aravindan~]$

SMVEC
Page No.

20. Sort
(i)

TASK
order.

: To sort the content of the file in ascending

SYNTAX

: sort filename

EXPLANATION

: Sorts the content of the file in ascending order.

OUTPUT

: [smvec8@localhost aravindan~]$ sort abc.txt


Arrival Time
Sorting
Welcome

(ii)

TASK
order.

: To sort the content of the file in descending

SYNTAX

: sort r filename

EXPLANATION
order.
OUTPUT

: Sorts the content of the file in descending


: [smvec8@localhost aravindan~]$ sort r abc.txt
Welcome
Sorting
Arrival Time

21. And/Semicolon
TASK

: Used to combine more than one commands.

SYNTAX

: cmd1 && cmd2(cmd1;cmd2)

EXPLANATION

: Combines more than one command.

OUTPUT
date (whoami;date)

: [smvec8@localhost aravindan~]$ whoami &&


smvec8
Mon

July

11:05:15

IST

2016

SMVEC
Page No.

22. Or
TASK

: To display the output for one command which is true.

SYNTAX

: cmd1 || cmd2

EXPLANATION

: Outputs the command which is true.

OUTPUT

: [smvec8@localhost aravindan~]$ whoami ||

date
smvec8
23. Alias
TASK
commands

: To create simple names or abbreviations for

SYNTAX

: alias name=value

EXPLANATION

: Creates the abbreviations for commands

OUTPUT
p=pwd

: [smvec8@localhost aravindan~]$ allias


/home/smvec8/aravindan

24. Cut
(i)

TASK

: To cut a set of columns from a file.

SYNTAX

: cut -c no_of_characters filename

EXPLANATION
: This command displays the characters of a
particular column in the
specified field.
OUTPUT
abc.txt

: [smvec8@localhost aravindan~]$ cut c -3


Wel
Sor
Arr

SMVEC
Page No.

(ii)

TASK

: To cut a column from a file.

SYNTAX

: cut -c filename

EXPLANATION
: This command displays the characters of a
particular column in the
specified field.
OUTPUT
abc.txt

: [smvec8@localhost aravindan~]$ cut c 3


l
r
r

25. Touch
TASK

: To create an empty file.

SYNTAX

: touch filename

EXPLANATION

: This command creates a new empty file.

OUTPUT

: [smvec8@localhost aravindan~]$ ls
aravind

abc.txt
line.sh

xyz.txt
pqr.txt

sort.sh
a.txt

level.txt
b.txt

[smvec8@localhost aravindan~]$

touch

hello.txt
[smvec8@localhost aravindan~]$ ls
aravind

abc.txt
rqp.txt

xyz.txt
a.txt

sort.sh
b.txt

line.sh

hello.txt

SMVEC
Page No.

26. Uniq
TASK

: To delete the lines that are present in the given file

SYNTAX

: uniq option f1 f2

EXPLANATION
f2 that are in f1.
OUTPUT

: This command deletes the identical lines from


: [smvec8@localhost aravindan~]$ cat abc.txt
Welcome
Sorting
Arrival Time
[smvec8@localhost aravindan~]$

cat

xyz.txt
Welcome
This is OS lab
[smvec8@localhost aravindan~]$ uniq d
abc.txt xyz.txt
[smvec8@localhost aravindan~]$

cat

xyz.txt
This is OS lab
27. Echo
TASK

: Used to display a line of text.

SYNTAX

: echo

EXPLANATION

: Displays the statement within double quotes.

OUTPUT

: [smvec8@localhost aravindan~]$ echo Hello

all
Hello all

SMVEC
Page No.

28. Word Count

(i)

TASK
: Used to display the number of lines, words and
characters in file.

SYNTAX

: wc filename

EXPLANATION

: Displays the number of lines, words and characters in

OUTPUT

: [smvec8@localhost aravindan~]$ wc a.txt

file.

2
(ii) TASK

23

a.txt

: Used to display the number of lines in file.

SYNTAX

: wc l filename

EXPLANATION

: Displays the number of lines in file.

OUTPUT

: [smvec8@localhost aravindan~]$ wc l a.txt


2

(iii) TASK

: Used to display the number of characters in file.

SYNTAX

: wc m filename

EXPLANATION

: Displays the number of characters in file.

OUTPUT

: [smvec8@localhost aravindan~]$ wc m a.txt


23

(iv) TASK

: Used to display the number of words in file.

SYNTAX

: wc w filename

EXPLANATION

: Displays the number of words in file.

OUTPUT

: [smvec8@localhost aravindan~]$ wc w a.txt


6

SMVEC
Page No.

29. Grep
(i)

TASK

: Used to search for a pattern in a file.

SYNTAX

: grep pattern filename

EXPLANATION

: Displays the lines that have the given pattern in the

file.
OUTPUT
sub.txt

: [smvec8@localhost aravindan~]$ grep System


OS Operating System
SS System Software

(ii)

TASK
: Used to search for a pattern in a file and displays the
no of times it
repeated in the file.

SYNTAX

: grep -c pattern filename

EXPLANATION
in the file.

: Displays the no of times the given pattern is repeated

OUTPUT
sub.txt

: [smvec8@localhost aravindan~]$ grep System

(iii) TASK
with line number.
SYNTAX

: Used to search for a pattern in a file and display along

: grep -n pattern filename

EXPLANATION
file with line number.

: Displays the lines that have the given pattern in the

OUTPUT
System sub.txt

: [smvec8@localhost aravindan~]$ grep n


1: OS Operating System
8: SS System Software

SMVEC
Page No.

(iv) TASK
the case.

: Used to search for a pattern in a file irrespective of

SYNTAX

: grep -i pattern filename

EXPLANATION
: Displays the lines that have the given pattern
irrespective of case in the file.
OUTPUT
sub.txt

: [smvec8@localhost aravindan~]$ grep i System


OS Operating System
SS System Software

Ss Sound system
30. Read
TASK

: Used to read a value for a variable.

SYNTAX

: read identifier

EXPLANATION

: Reads the value for the identifier

OUTPUT

: [smvec8@localhost aravindan~]$ read n


10
[smvec8@localhost aravindan~]$

31. Fgrep
(i) TASK

: Used to search for a pattern in two files.

SYNTAX

: fgrep pattern f1 f2

EXPLANATION
the two files.

: Displays the lines that have the given pattern in

OUTPUT
sub.txt lmn.txt

: [smvec8@localhost aravindan~]$ fgrep System


sub.txt : OS Operating System
sub.txt : SS System Software

lmn.txt: System of Education

SMVEC
Page No.

(ii) TASK
: Used to search for a pattern in the two files and
displays the no of times
it repeated in the files.
SYNTAX

: fgrep -c pattern f1 f2

EXPLANATION
: Displays the no of times the given pattern is
repeated in the files.
OUTPUT
sub.txt lmn.txt

: [smvec8@localhost aravindan~]$ fgrep System

sub.txt : 2
lmn.txt : 1
(iii) TASK
: Used to search for a pattern in two files and
display along with line no.
SYNTAX

: fgrep -n pattern f1 f2

EXPLANATION
the files with line

: Displays the lines that have the given pattern in


number.

OUTPUT
: [smvec8@localhost aravindan~]$ fgrep n
System sub.txt lmn.txt
sub.txt: 1: OS Operating System
sub.txt: 8: SS System Software

lmn.txt: 3:System of Education


(iv) TASK
: Used to search for a pattern in the files
irrespective of the case.
SYNTAX

: fgrep -i pattern f1 f2

EXPLANATION
: Displays the lines that have the given pattern
irrespective of case in the
file.
OUTPUT
System sub.txt

: [smvec8@localhost aravindan~]$ grep i

sub.txt : OS Operating System


sub.txt : SS System Software
sub.txt : Ss Sound system
lmn.txt : System of Education

SMVEC
Page No.

32. Pipe
TASK
command.

: Used to output one command as input for another

SYNTAX

: cmd1|cmd2|cmd3

EXPLANATION
another command
OUTPUT

: Makes the output of one command as input to


: [smvec8@localhost aravindan~]$ whoami | date
Mon Jul 18 16:50:11 IST 2016

33. Tee
TASK
: Used to read the standard input and then write to
standard output of file.
SYNTAX

: cmd1| tee filename

EXPLANATION
specified file

: Reads the input of one command to the

OUTPUT
pqr.txt

: [smvec8@localhost aravindan~]$ date | tee


Mon Jul 18 18:07:36 IST 2016
[smvec8@localhost aravindan~]$ cat pqr.txt
Mon Jul 18 18:07:36 IST 2016

34. Write
TASK

: Used to communicate with other users.

SYNTAX

: write login_name

EXPLANATION
logged in

: Used to communicate with other users who are

OUTPUT
pqr.txt

: [smvec8@localhost aravindan~]$ date | tee


Hello
How are you?
Bye

SMVEC
Page No.

35. Mail(& Type)


(i)

TASK

: Used to send mail to another user.

SYNTAX

: mail username

EXPLANATION

: Creates a new mail and sends to the specified

OUTPUT

: [smvec8@localhost aravindan~]$ mail smvec2

user

Subject: First mail


Hello smvec2.
How is it going?
(ii)

TASK

: Used to read mail from another user.

SYNTAX

: mail

EXPLANATION

: Used to read the mail received from a user

OUTPUT

: [smvec8@localhost aravindan~]$ mail


/var/spool/mail/smvec8: 1 message 1 new
>N 1 smvec2@localhost.loc Mon Jul 4 17:06
Re:First mail

14/651

& type smvec2


Message 1:
From smvec2@localhost.loc Mon Jul 4 17:06:35 2016
Date: Mon, 4 Jul 2016 17:06:35 +0530
From: smvec2@localhost.localdomain
To: smvec8@localhost.localdomain
Subject: Re:First mail
Hello smvec2.
How is it going?

& Saved 1 message in mbox

SMVEC
Page No.

36. Terminal Name


TASK

: Used to display the current terminal path.

SYNTAX

: tty

EXPLANATION

: Displays the terminal path name

OUTPUT

: [smvec8@localhost aravindan~]$ tty


/dev/pts/3

37. Expression
TASK

: Used to evaluate an expression.

SYNTAX

: expr expression

EXPLANATION

: Evaluates the expression and displays the result.

OUTPUT
+ 10

: [smvec8@localhost aravindan~]$ echo expr 10


20

38. Free
TASK
: Used to display the total amount of free and used
physical and swap
memory in the system.
SYNTAX

: free

EXPLANATION
: Displays the total amount of free and used
physical and swap
memory in the system.
OUTPUT

: [smvec8@localhost aravindan]$ free

total

used

free

shared

buffers

cached
Mem:
0

232936

2074968

1530188

544780

962024
-/+ buffers/cache:
Swap:

4128760

335228

1739740

80

4128680

SMVEC
Page No.

39. Ping
TASK
to the user.

: Ping is used to ensure that the host computer is connected

SYNTAX

: ping Ip - address

EXPLANATION
: Pings the ip address and checks if the system is
connected or not to host.
OUTPUT
172.17.28.123

: [smvec8@localhost aravindan~]$ ping


PING 172.17.28.123 (172.17.28.123) 56(84) bytes of
data.
64 bytes from 172.17.28.123: icmp_seq=1 ttl=128
time=1.79 ms
64 bytes from 172.17.28.123: icmp_seq=2 ttl=128
time=0.379 ms
64 bytes from 172.17.28.123: icmp_seq=3 ttl=128
time=0.368 ms
64 bytes from 172.17.28.123: icmp_seq=4 ttl=128
time=0.413 ms

^Z
[1]+ Stopped

ping 172.17.28.123

40. Password
TASK

: To change the password of the user

SYNTAX

: passwd

EXPLANATION

: Change the password of the current user

OUTPUT

: [smvec8@localhost aravindan~]$ passwd


Changing password for user smvec8.
Changing password for smvec8
(current) UNIX password:
New UNIX password:
Retype new UNIX password:
Successfully Changed your password

SMVEC
Page No.

41. Uname
TASK

: Used to display the name of the system being used.

SYNTAX

: uname

EXPLANATION

: Displays the system name.

OUTPUT

: [smvec8@localhost aravindan~]$ uname


Linux

42. Sequence
TASK

: Used to generate a sequence.

SYNTAX

: seq starting_value ending_value

EXPLANATION
ending value.

: Generates a sequence from the starting value till the

OUTPUT

: [smvec8@localhost aravindan~]$ seq 1 4


1
2
3
4

43. Open and fill


TASK
line.

: Used to open a file and position the cursor at the first

SYNTAX
EXPLANATION
the first line.
OUTPUT

: vi +filename
: Opens an existing file and places the cursor at
: [smvec8@localhost aravindan~]$ vi +a.txt
Welcome
Hello all!
How is it going?

SMVEC
Page No.

44. Compare
TASK

: Compares the sorted files line by line

SYNTAX

: comm a.txt b.txt

EXPLANATION
the lines that match
OUTPUT

: Compares the sorted files line by line and prints


: [smvec8@localhost aravindan~]$ comm a.txt

b.txt
Hello all!
How is it going?
45. Basic Calculator
TASK
calculator

: To perform simple arithmetic operations using basic

SYNTAX

: bc

EXPLANATION

: Opens the basic calculator

OUTPUT

: [smvec8@localhost aravindan~]$ bc
10 + 10
20
[smvec8@localhost aravindan~]$ bc
10 \* 10
100

46. Exit
TASK

: To close the current terminal

SYNTAX

: logout

EXPLANATION

: Logs out of the current user and exits the terminal

OUTPUT

: [smvec8@localhost aravindan~]$ logout

SMVEC
Page No.

47. Man
TASK

: To open help window for any command

SYNTAX

: man command_name

EXPLANATION

: Opens the help menu for the corresponding command

OUTPUT

: [smvec8@localhost aravindan~]$ man echo

ECHO(1)

User Commands

Name
echo display a line of text
Synopsis
echo [OPTION]. [STRING].
Description
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
.
.
.
(END)

ECHO(1)

SMVEC
Page No.

ODD OR EVEN
Code:
[smvec8@localhost aravindan]$ vi odev.sh
echo "Enter the number : "
read n
rem=$(($n % 2))
if [ $rem -eq 0 ];
then
echo "even"
else
echo "odd"
fi
Output:
[smvec8@localhost aravindan]$ sh odev.sh
Enter the number :
5
odd
[smvec8@localhost aravindan]$ sh odev.sh
Enter the number :
8
even

SMVEC
Page No.

LINUX COMMANDS USING ECHO


Code:
[smvec8@localhost aravindan]$ vi lecho.sh
echo "1. WHO AM I ?"
echo "2. WHO IS LOGGED IN?"
echo "3. DATE"
read n
case $n in
1)whoami ;;
2)who ;;
3)date ;;
esac

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ sh lecho.sh
1. WHO AM I ?
2. WHO IS LOGGED IN?
3. DATE
1
smvec8
[smvec8@localhost aravindan]$ sh lecho.sh
1. WHO AM I ?
2. WHO IS LOGGED IN?
3. DATE
2
root

:0

2016-08-22 15:24

smvec44 pts/11
smvec8 pts/1
smvec45 pts/22

2016-09-01 20:36
2016-09-06 16:40 (172.17.25.57)
2016-08-25 20:05

[smvec8@localhost aravindan]$ sh lecho.sh


1. WHO AM I ?
2. WHO IS LOGGED IN?
3. DATE
3
Tue Sep 6 16:49:55 IST 2016

SMVEC
Page No.

PRIME NUMBER OR NOT


Code:
[smvec8@localhost aravindan]$ vi prime.sh
echo "Enter the number"
read n
i=2
while [ $i -lt $n ]
do
x=$(( $n % $i ))
if [ $x -eq 0 ];
then
echo "$n is not a prime number"
exit
fi
i=$(( $i + 1))
done
echo "$n is a Prime number"

Output:
[smvec8@localhost aravindan]$ sh prime.sh
Enter the number
45
45 is not a prime number
[smvec8@localhost aravindan]$ sh prime.sh
Enter the number
23
23 is a Prime number

SMVEC
Page No.

STRING COMPARE
Code:
[smvec8@localhost aravindan]$ vi strcp.sh
echo "STRING 1"
read str1
echo "STRING 2"
read str2
if [ $str1 = $str2 ]
then
echo "The Strings are Equal"
else
echo "The Strings are Not Equal"
fi

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
Hello
The Strings are Equal
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
Bye
The Strings are Not Equal
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
hello
The Strings are Not Equal

SMVEC
Page No.

GREATEST OF THREE NUMBERS


Code:
[smvec8@localhost aravindan]$ vi gttn.sh
echo "Enter the values"
read a
read b
read c
if [ $a -gt $b -a $a -gt $c ]
then
echo "A is greatest"
elif [ $b -gt $c ]
then
echo "B is greatest"
else
echo "C is greatest"
fi

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
4
5
9
C is greatest
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
74
15
54
A is greatest
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
75
98
25
B is greatest

SMVEC
Page No.

SWAPPING WITHOUT TEMPORARY VARIABLE


Code:
[smvec8@localhost aravindan]$ vi swapt.sh
echo "Enter two numbers"
read a
read b
echo "Before Swap"
echo "a= $a"
echo "b= $b"
a=$(( $a + $b ))
b=$(( $a - $b ))
a=$(( $a - $b ))
echo "After Swap"
echo "a= $a"
echo "b= $b"
Output:
[smvec8@localhost aravindan]$ sh swapt.sh
Enter two numbers
9
5
Before Swap
a= 9
b= 5
After Swap
a= 5
b= 9

SMVEC
Page No.

PASSWORD CHECK
Code:
[smvec8@localhost aravindan]$ vi pass.sh
echo "Password check:"
read pw
clear
echo "Password check:"
echo "Enter the password"
read n
if [ $n = $pw ]
then
echo "Valid password"
else
echo "Invalid password"
fi

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ sh pass.sh
Password check:
Password
Password check:
Enter the password
Password
Valid password
[smvec8@localhost aravindan]$ sh pass.sh
Password check:
Password
Password check:
Enter the password
password
Invalid password

SMVEC
Page No.

Fork System Call


Code:
#include<sys/types.h>
#include<stdio.h>
int main()
{
int pid, p1,p2;
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
wait(10);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
printf("\n New fork id %d ",pid);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ cc fork.c
[smvec8@localhost aravindan]$ ./a.out
After Fork Called
Current Process1 id :10538
After Fork Called
Current Process2 id :16520
New fork id 16571

SMVEC
Page No.

Stat System Call


Code:
#include<sys/stat.h>
int main()
{
int s;
struct stat st;
s=stat("re.c",&st);
if(s<0)
{
printf("\n use of stat() is unsuccessful ");
exit(0);
}
else
{
printf("\n INODE NUMBER IS %i",st.st_ino);
printf("\n IDENTIFIER OF THE DEVICE IS %d",st.st_dev);
printf("\n USER IDENTIFIER OF THE OWNER IS %u",st.st_uid);
printf("\n BLOCK SIZE FOR FILE SYSTEM IS %o",st.st_blksize);
printf("\n LAST FILE MODIFY IS %s\n",ctime(&st.st_mtime));
}
}

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ cc stat.c
[smvec8@localhost aravindan]$ ./a.out
INODE NUMBER IS 16254000
IDENTIFIER OF THE DEVICE IS 64768
USER IDENTIFIER OF THE OWNER IS 547
BLOCKSIZE FOR THE SYSTEM IS 10000
LAST FILE MODIFY IS Tue Aug 8 14:30:50 2016

SMVEC
Page No.

Read System Call


Code:
/* File Read - fread.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
main(int argc, char *argv[])
{
int fd,i;
char buf[100];
if (argc < 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
printf("%s file does not exist\n", argv[1]);
exit(-1);
}
printf("Contents of the file %s is : \n", argv[1]);
while(read(fd, buf, sizeof(buf)) > 0)
{
printf("%s", buf);
}
close(fd);
}

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ read.c
[smvec8@localhost aravindan]$./a.out <filename.c>
[smvec8@localhost aravindan]$ ./a.out fork.c

#include<sys/types.h>
#include<stdio.h>
int main()
{
int pid, p1,p2;
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
wait(10);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
printf("\n New fork id %d ",pid);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}

SMVEC
Page No.

Write System Call


Code:
/* File append - fappend.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
main(int argc, char *argv[])
{
int fd, n, len;
char buf[100];
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT, 0644);
if (fd < 0)
{
perror(argv[1]);
exit(-1);
}
while((n = read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
close(fd);
}

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ cc append.c
[smvec8@localhost aravindan]$ usage : ./a.out<filename.c>
[smvec8@localhost aravindan]$ ./a.out fork.c
#include<sys/types.h>
#include<stdio.h>
int main()
{
int pid, p1,p2;
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
wait(10);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
printf("\n New fork id %d ",pid);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}Appended text

SMVEC
Page No.

Execution System Call


Code:
#include <unistd.h>
#include <stdio.h>
main (int argc, char *argv[])
{
char *args[2];
args[0] = "-l";
args[1] = NULL;
execv("/bin/ls",args) ;
}
Output:
[smvec8@localhost aravindan]$ cc exec.c
[smvec8@localhost aravindan]$ ./a.out
a.out

dp

great.sh

append.c

evenodd

jj

even.sh

john

[smvec8@localhost aravindan]$

SMVEC
Page No.

Directory System Call


Code:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main()
{
char buff[10];
DIR *dirp;
printf("\n\n ENTER THE DIRECTORY NAME:");
scanf("%s", buff);
dirp =opendir(buff);
if(dirp==NULL)
{
printf("The given directory does not exist");
}
while(dptr=readdir(dirp))
{
printf("%d\n",dptr->d_ino);
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}

SMVEC
Page No.

Output:
[smvec8@localhost aravindan]$ cc dir.c
[smvec8@localhost aravindan]$ ./a.out
d:\
new.c
primeornot.sh
fork.c
third.c

SMVEC
Page No.

Simulation of ls Command
Code:
#include<dirent.h>
// Main Program
int main()
{
struct dirent **namelist;
int status,i;
char pathname[100];
getwd(pathname);
status=scandir(pathname,&namelist,0,alphasort); //to scan a directory
if(status <0)
printf("cannot find the current directory");
else
{
for(i=0;i<status;i++)
{
printf("%s\n",namelist[i]->d_name);
}
}
}

SMVEC
Page No.

Output :
[smvec8@localhost aravindan]$ cc ls.c
[smvec8@localhost aravindan]$ ./a.out
Apple.c
Direct.c
Echo
File1.doc
Ring.h

SMVEC
Page No.

Simulation of Grep Command


Code:
#include<stdio.h>
#include<string.h>
#define buffer 1024
int main(int argc,char *argv[])
{
FILE *fp;
char fline[buffer];
char *newline;
int line=1;
if(argc!=3)
{
printf("Please use the following syntax \n");
printf("./a.out filename searchstring \n");
printf("Example: ./a.out test1.c print \n");
exit(1);
}
// argv[1] is filename
if(!(fp=fopen(argv[1],"r")))
{
printf("grep : Couldnot open file : %s\n",argv[1]);
exit(1);
}

SMVEC
Page No.

while(fgets(fline,buffer,fp)!=NULL)
{
// argv[2] is search string
if(strstr(fline,argv[2])!=NULL)
{
printf("Line number: %d text: %s\n",line,fline);
}
line++;
}
}

SMVEC
Page No.

Output:

[smvec8@localhost aravindan]$ cc grep.c


[smvec8@localhost aravindan]$ ./a.out dirent.c searchstring
[smvec8@localhost aravindan]$ ./a.out dirent.c print
Line number : 8 text : printf(\n\n Enter the directory name);
Line number : 13 text : printf(the given directory does not exist);
Line number :17 text : printf(%d\n,dptr->d_ino);
Line number : 17 text : printf(%s\ndptr->d_name);
[smvec8@localhost aravindan]$

SMVEC
Page No.

FCFS
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt;
}pr[10];
void main()
{
int i,j,n,prevtt;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t FIRST COME FIRST SERVE ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO \tBURST TIME \tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~ \t~~~~~~~~~~ \t~~~~~~~~~\t~~~~~~~~~~~~~~~~");

SMVEC
Page No.

for(i=1;i<=n;i++)
{
printf("\n%10d\t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

SJF
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt;
}pr[10];
void main()
{
int i,j,n,prevtt,t;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t FIRST COME FIRST SERVE ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].bt>pr[j].bt)
{
t=pr[j].pid;
pr[j].pid=pr[i].pid;
pr[i].pid=t;
t=pr[j].bt;
pr[j].bt=pr[i].bt;
pr[i].bt=t;
}
}
}

SMVEC
Page No.

prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO \tBURST TIME \tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~ \t~~~~~~~~~~ \t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n%10d \t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f" ,avgwt ,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

PRIORITY SCHEDULING
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt,pt;
}pr[10];
void main()
{
int i,j,n,prevtt,t;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t PRIORITY ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time & Arrival time:",pr[i].pid);
scanf("%d %d",&pr[i].bt,&pr[i].pt);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].pt>pr[j].pt)
{
t=pr[j].pid;
pr[j].pid=pr[i].pid;
pr[i].pid=t;
t=pr[j].bt;
pr[j].bt=pr[i].bt;
pr[i].bt=t;
t=pr[j].pt;
pr[j].pt=pr[i].pt;
pr[i].pt=t;
}
}
}

SMVEC
Page No.

prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO\tBURST TIME\tPRIORITY\tWAIT TIME\tTURN AROUND
TIME");
printf("\n~~~~~~~~~~\t~~~~~~~~~~\t~~~~~~~~\t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n%10d\t%10d\t%8d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].pt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

ROUND ROBIN
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt,pt,rem;
}pr[10];
void main()
{
int i,j,n,ts,t,max;
double avgwt=0,avgtt=0;
double tottt,totwt,totbt;
clrscr();
printf("\n\t\t\t\t ROUND ROBIN ");
printf("\n\n Enter the number of process and time slice : ");
scanf("%d %d",&n,&ts);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
totbt=0;
for(i=1;i<=n;i++)
{
pr[i].rem = pr[i].bt;
totbt = totbt+pr[i].bt;
}
while(tottt<totbt)
{
for(i=1;i<=n;i++)
{
if(pr[i].rem==0)
{
continue;
}
else if(pr[i].rem>ts)
{
tottt=tottt+ts;
pr[i].rem=pr[i].rem-ts;
}

SMVEC
Page No.

else
{
tottt=tottt+pr[i].rem;
pr[i].rem=0;
pr[i].wt=tottt-pr[i].bt;
pr[i].tt=tottt;
}
}
};
printf("\nPROCESS NO\tBURST TIME\tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~\t~~~~~~~~~~\t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n\n%10d\t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
max=0;
for(j=0;j<=n;j++)
{
if(max<pr[j].tt)
{
max=pr[j].tt;
}
}
for(j=0;j<=max;j++)
{
printf("-");
}
printf("\n0");

SMVEC
Page No.

for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<=max;j++)
{
printf("-");
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

DEADLOCK -BANKER'S ALGORITHM PROGRAM:


Code:
#include<stdio.h>
#include<conio.h>
void matrix_get(int[5][5],int,int);
void main()
{
int max[5][5],all[5][5],total[1][5];
int avail[1][5],f[5],need[5][5],state[5];
int s,i,j,p,r,c,ch,ch1,k=0;
clrscr();
printf("\t\t DEADLOCK -BANKER'S ALGORITHM \n");
printf("ENTER THE NUMBER OF PROCESS:");
scanf("%d",&p);
ch=p;
printf("ENTER THE NUMBER OF RESOURCE TYPE:");
scanf("%d",&r);
printf("ENTER THE MAX MATRIX:\n");
matrix_get(max,p,r);
printf("\nENTER THE ALLOCATED MATRIX:\n");
matrix_get(all,p,r);
printf("ENTER THE TOTAL RESOURCE PRESENT:\n");
matrix_get(total,1,r);
for(i=0;i<r;i++)
{
for(j=0;j<p;j++)
{
if(max[j][i]>total[0][i] || all[j][i]>total[0][i])
{
printf("\nINPUT IS > TOTAL RESOURCE
PRESENT\n");
exit(0);
}
}

SMVEC
Page No.

s=total[0][i];
for(j=0;j<p;j++)
{
s=s-all[j][i];
}
avail[0][i]=s;
}
printf("\n\n AVAILABLE MATRIX \n\n");
for(i=0;i<r;i++)
{
printf("%d\t",avail[0][i]);
}
printf("\n\n NEED MATRIX:\n\n");
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-all[i][j];
if(need[i][j]<0)
{
need[i][j]=0;
}
printf("%d\t",need[i][j]);
}
printf("\n");
}

SMVEC
Page No.

while(ch>0)
{
ch1=ch;
for(i=0;i<p;i++)
{
if(f[i]==0)
{
c=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[0][j])
{
c++;
}
}
if(c==r)
{
f[i]=1;
state[k++]=i;
ch--;
for(j=0;j<r;j++)
{
avail[0][j]=avail[0][j]+all[i][j];
need[i][j]=0;
}
}
}
}
if(ch==p)
{
printf("\n ALREADY DEADLOCK OCCURRED");
exit(0);
}
if(ch1==ch)
{
printf("\n ALLOCATION LEADS TO DEADLOCK");
exit(0);
}
}

SMVEC
Page No.

printf("\n\n SAFE STATE S:{");


for(i=0;i<p;i++)
{
printf("P%d ",state[i]);
}
printf("}");
}
void matrix_get(int a[5][5],int p,int r)
{
int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&a[i][j]);
}
}
}

SMVEC
Page No.

Output:

SMVEC
Page No.

SMVEC
Page No.

First fit program:


Code:
#include<stdio.h>;
#include<process.h>;
void main()
{
int m[20],p[20],i,j,np,nm;
clrscr();
printf("\n\n\t\t\t******* First Fit******\n");
// Read Process details
printf("Enter number of Process\n");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("Enter the size of %d Process: ",i+1);
scanf("%d",&p[i]);
}
// Read Memory details
printf("\nEnter number of Memory Blocks\n");
scanf("%d",&nm);
for(i=0;i<nm;i++)
{
printf("Enter the %d Block size: ",i+1);
scanf("%d",&m[i]);
}

SMVEC
Page No.

// Allocate Memory
for(i=0;i<np;i++)
{
for(j=0;j<nm;j++)
{
if(p[i]<=m[j])
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,j+1);
m[j] = m[j] - p[i];
p[i]=10000;
break;
}
}
}

for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

Best fit program:


Code:
#include<stdio.h>;
#include<process.h>;
void main()
{
int m[20],p[20],i,j,np,nm, pos, min;
//clrscr();
printf("\t\t\t******* Best Fit******\n");
// Read Process details
printf("Enter number of Process\n");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("Enter the size of %d Process: ",i+1);
scanf("%d",&p[i]);
}
// Read Memory details
printf("\nEnter number of Memory Blocks\n");
scanf("%d",&nm);
for(i=0;i<nm;i++)
{
printf("Enter the %d Block size: ",i+1);
scanf("%d",&m[i]);
}
for(i=0;i<np;i++)
{
min = 9999;
for(j = 0; j < nm; j++)
{
if(min > m[j] && p[i] <= m[j])
{
min = m[j];
pos = j;
}
}

SMVEC
Page No.

if (min != 9999)
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,pos+1);
m[pos] = m[pos] - p[i];
p[i]=10000;
}
}
for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

Worst fit program:


Code:
#include<stdio.h>;
#include<process.h>;
void main()
{
int m[20],p[20],i,j,np,nm, pos, max;
//clrscr();
printf("\t\t\t******* Worst Fit******\n");
// Read Process details
printf("Enter number of Process\n");
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("Enter the size of %d Process: ",i+1);
scanf("%d",&p[i]);
}
// Read Memory details
printf("\nEnter number of Memory Blocks\n");
scanf("%d",&nm);
for(i=0;i<nm;i++)
{
printf("Enter the %d Block size: ",i+1);
scanf("%d",&m[i]);
}
for(i=0;i<np;i++)
{
max = -1;
for(j = 0; j < nm; j++)
{
if(max < m[j] && p[i] <= m[j])
{
max = m[j];
pos = j;
}
}

SMVEC
Page No.

if (max != -1)
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,pos+1);
m[pos] = m[pos] - p[i];
p[i]=10000;
}
}
for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

FIFO Program:
Code:
#include<stdio.h>
#include<conio.h>
int nf;
int ne;
int hit,miss;
int ref[50];
int fr[15];
void getdata();
void fifo();
int large(int []);
void getdata()
{
int i;
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void fifo()
{
int i,j,found=0,rear=-1;
miss=hit=0;
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
printf("\nThe no to be inserted :%d",ref[i]);
found=search(ref[i]);
if(found==0)
{
miss++;

SMVEC
Page No.

if(rear==nf-1)
{
rear=-1;
}
rear++;
fr[rear]=ref[i];
}
else
{
hit++;
}
printf("\n");
for(j=0;j<nf;j++)
printf("%d ",fr[j]);
}
printf("\n\n No. of page faults=%d\n",miss);
printf("\n No. of Hits=%d",hit);
}
int search(int item)
{
int i;
for(i=0;i<nf;i++)
{
if(fr[i]==item)
{
return(1);
}
}
return 0;
}
void main()
{
clrscr();
printf("\n\t\t\t FIFO PAGE REPLACEMENT");
getdata();
printf("\t\t\t FIFO PAGE REPLACEMENT");
fifo();
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

SMVEC
Page No.

LRU program:
Code:
#include<stdio.h>
int nf;
int i,ne;
int miss=0;
int ref[50];
int fr[15];
void getdata();
void lru();
void getdata()
{
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void lru()
{
int check,j,k;
int count=0,index[5];
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
check=0;
printf(The number to be inserted %d,ref[i]);

SMVEC
Page No.

for(j=0;j<nf;j++)
{
if(fr[j]==ref[i])
{
check=1;
}
}
if(check==0)
{
miss+=1;
if(count<nf)
{
fr[count++] = ref[i];
}
else
{
for(j=0;j<i;j++)
{
for(k=0;k<nf;k++)
{
if(ref[j]==fr[k])
{
index[k] = j;
}
}
}
for(j=0;j<nf;j++)
{
if(index[check]>index[j])
{
check=j;
}
}
fr[check]=ref[i];
}
printf("\n");
}

SMVEC
Page No.

for(j=0;j<nf;j++)
{
printf("%d\t",fr[j]);
}
}
printf("\n Number of page faults:%d",miss);
}
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT");
getdata();
printf("\t\t\t LRU PAGE REPLACEMENT");
lru();
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

SMVEC
Page No.

FCFS Disk Scheduling


Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int q[20],r,h,i,j,k,sk=0,d;
clrscr();
printf("\t\t\tFirst Come First Serve Disk Scheduling Algorithm\n");
printf("\nEnter the number of request:\t");
scanf("%d",&r);
printf("Enter the Request:\t\n");
for(i=1;i<=r;i++)
{
scanf("%d",&q[i]);
}
printf("Enter the head position\t");
scanf("%d",&h);
q[0]=h;
printf("\n");
for(j=0;j<=r-1;j++)
{
d=abs(q[j+1]-q[j]);
sk+=d;
printf("Move from %d to %d with Seek %d\n",q[j],q[j+1],d);
}
printf("\nTotal Seek Time is %d\t",sk);
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

C-Scan Program
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define cymax 199
int i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp;
void main()
{
clrscr();
printf("\n\t\t\t C-SCAN Algorithm ");
printf("\n ENTER THE NUMBER OF REQUEST : ");
scanf("%d",&req);
printf("\n ENTER THE CURRENT HEAD POSITION : ");
scanf("%d",&cpo);
printf("\n ENTER THE ELEMENTS OF THE REQUEST : ");
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyp[j] > cyp[j+1])
{
temp = cyp[j];
cyp[j] = cyp[j+1];
cyp[j+1] = temp;
}
}
}

SMVEC
Page No.

cp=0;
do
{
if(cyp[cp] == cpo)
break;
cp++;
}while(cp!=req);
i=0,j=cp;
cpo = cyp[cp];
do
{
if(cpo == cyp[req])
{
npo = cymax; cp = -1;
}
else
{
npo = cyp[++cp];
}
printf("Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo == cymax ? 0 : npo;
}while(npo != cyp[j-1]);
printf(" Total Tracks Displaced : %d",tmov);
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

LOOK ALGORITHM:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int ind,i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp,tmp;
void main()
{
clrscr();
printf(\n\t\t\t LOOK ALGORITHM \n);
printf("\n Enter the number of requests : ");
scanf("%d",&req);
printf("\n Enter the current header position : ");
scanf("%d",&cpo);
printf("\n Enter the Requests : ",req);
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
ind = 0;
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{

SMVEC
Page No.

if(cyp[j] > cyp[j+1])


{
temp = cyp[j];
cyp[j] = cyp[j+1];
cyp[j+1] = temp;
}
}
}
cp=0;
do
{
if(cyp[cp] == cpo)
break;
cp++;
}while(cp!=req);
tmp = cp;
i=0;
cpo = cyp[cp];do
{
if(ind == 0)
{
if(cp == 0)
{
cp = tmp;
npo = cyp[++cp]; ind = 1;
}
else
npo = cyp[--cp];
}

SMVEC
Page No.

else
npo = cyp[++cp];
printf(" Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo;
}while(npo!=cyp[req]);
printf(" Total Tracks Displaced : %d",tmov);
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

C-Look Program
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp;
void main()
{
clrscr();
printf("\n\t\t\t C-LOOK ");
printf("\n Enter the number of requests : ");
scanf("%d",&req);
do
{
printf("\n Enter the current header position : ");
scanf("%d",&cpo);
}while(cpo>199 || cpo <=0);
printf("\n Enter the Requests : ",req);
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyp[j] > cyp[j+1])
{
temp = cyp[j];
cyp[j] = cyp[j+1];
cyp[j+1] = temp;
}
}
}

SMVEC
Page No.

cp=0;
do
{
if(cyp[cp] == cpo)
break;
cp++;
}while(cp!=req);
i=0,j=cp;
cpo = cyp[cp];
do
{
if(cp == req)
{
npo = cyp[0];
cp = 0;
}
else
{
npo = cyp[++cp];
}
printf("Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo == cyp[req] ? cyp[0] : npo ;
}while(npo != cyp[j-1]);
printf(" Total Movement : %d",tmov);
getch();
}

SMVEC
Page No.

Output:

SMVEC
Page No.

Dining Philosopher Problem:


Code:
#include<stdio.h>
#include<conio.h>
#define N 5
#define LEFT (j-1)%N
#define RIGHT (j+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[N];
void test(int);
int lt, rt;
void main()
{
int f,ch=0,i,a,b;
int take_sticks(int);
int put_sticks(int);
clrscr();
printf("\n \t *** DINING PHILOSOPHER PROBLEM**\n");
printf("ENTER THE NUMBER OF PHILOSOPHER IN THE TABLE IS %d:",N);
printf("\n STATUS OF THE PHILOSOPHER:\n");
for(i=0;i<N;i++)
{
state[i]=THINKING;
printf("\n P%d IS THINKING",i);
}

SMVEC
Page No.

do
{
printf("\n\t\t MENU\n\n");
printf("\n\t1.START EATING\n\n");
printf("\n\t2.STOP EATING\n\n");
printf("\n\t3.EXIT\n\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE PHILOSOPHER WHO IS GOING TO EAT:");
scanf("%d",&a);
f=take_sticks(a);
if(f==EATING)
{
printf("\n THE PHILOSOPHER HAS STARTED

EATING...\n\n");

}
else
{
printf("\nTHE PHILOSOPHER IS HUNGRY!!..\n\n");
printf("\n THE CHOPSTICK IS ENGAGED\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}

SMVEC
Page No.

else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 2:
printf("\n ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK");
scanf("%d",&b);
if(state[b]==EATING)
{
f=put_sticks(b);
printf("\n THE PHILOSOPHER HAS STOPPED EATING!!..\n\n");
}
else
{
printf("\n THE PHILOSOPHER IS NOT EATING!!\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}

SMVEC
Page No.

else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
getch();
}
int take_sticks(int j)
{
state[j]=HUNGRY;
test(j);
return state[j];
}

SMVEC
Page No.

int put_sticks(int j)
{
state[j]=THINKING;
if(j==0) test(N-1); else test(LEFT);
if(j==N-1) test(0); else test(RIGHT);
return state[j];
}
void test(int j)
{
if(j==0) rt = N-1; else rt = LEFT;
if(j==N-1) lt = 0; else lt = RIGHT;
if(state[j]==HUNGRY && state[lt]!=EATING && state[rt]!=EATING)
{
state[j]=EATING;
}
}

SMVEC
Page No.

Output:

SMVEC
Page No.

SMVEC
Page No.

SMVEC
Page No.

SMVEC
Page No.

SMVEC
Page No.

You might also like