Download - Cpl 2 Manual
-
7/31/2019 Cpl 2 Manual
1/62
STUDY OF UNIX OPERATING SYSTEMEx. No: 01
AIM:
To understand the basic definitions and features of UNIX operating system.UNIX stands for unplaced information computing system.
FEATURES OF UNIX:
MULTIUSER:
Unix is a multi-user OS which permits more than one user to utilize the computersimultaneously any one of the connected terminals.
MULTI-TASKING:The term task refers to a place initiated by the user to a process initiated by user Unix offer
concept of classifying the task.
FOREGROUND TASK:
It is the one where the user has to wait for the completion to present task before initiatingthe next task.
BACKGROUND TASK:
It is the one whose the user can continue interaction with the system while one or more ofthe presently issued task if is still executing.
PORTABLE:
The application programs written for one system works on another system.
MODULATOR:
It means the various part of the Unix system can be adder on the reproved without editingperformance of other component.
HIERARCHICAL FILE STRUCTURE:
Unix has hierarchical file structure and imported free structure.
SECURITY:
Unix being a multi using operating system offers protection to user information from
another. It maintains a list of users who are all allowed to access the system. The system alsokeep takes of that file and resources each user is authorized to renew users name to be added tothe list before they have an access to the system.
ARCHITECTURE OF UNIX/LAYERS OF UNIX
A unix architecture is a computer operating system architecture that demonstrate the unixphilosophy.In unix, several layers of interaction are occurring between the computer hardware andthe user.
-
7/31/2019 Cpl 2 Manual
2/62
The first layer is the KERNAL, which is the heart of the unix os.The second layer,the SHELL translates the commands given by the application programs.The final layer is the APPLICATIONS given by the user.
THE KERNAL:The KERNAL is the core of the operating system.It runs on the actual machine hardware
and manages all interaction with the hardware.The kernel provides services to start and stopprograms,handles the file system and other common low level tasks that most programs share.Italso handles the information exchange between the terminals such as tape drives,printersetc.All applications and commands in UNIX interact with the kernel, rather than the hardwaredirectly.OPERATIONS OF THE KERNAL:
It is responsible for scheduling and enabling the user and other process.
It is responsible for allocating memory.
It is responsible for managing the swapping between memory and disk.
It is also used to move the data to and from the peripherals.THE SHELL:
The shell is both a command language interpreter and a programming language thatprovides a traditional user interface for the unix operating system.
UNIX has a variety of shells,they are
Bourne shell:
C shell
Korn shellADVANTAGES OF UNIX
Programmable operating system
Full multitasking with protected memory
Very efficient virtual memory
Access controls and security
Ability to handle string commands
A powerfully unified file systemBASIC UNIX ELEMENTS:
commands
files
directories
processes
RESULT:
Thus the Unix environment has been studied.
VI EDITOREx.No: 02
AIM:
-
7/31/2019 Cpl 2 Manual
3/62
To understand the basic functions and features of the VI editor
INSTRUCTIONS:
VI EDITOR- Visual editor
VI stands for Visual
The VI editor is the text editor. Originally developed for Unix programmers.It is a model meaning. It is operating in the three different modes. The VI editor has beenAround for decade and is used daily by thousands of Linux and Unix users.THREE ASPECTS OF VI:
VI is supplied with all Unix serpents VI can be used at other universitiesor any business with UNIX system.
VI is a amount of memory allows efficient operation when the network is busy.
VI uses standard alphabetic keys for commands.
DIFFERENT MODES IN VI EDITOR
They are three different modes are available.
1. Command mode2. Insert task3. Last line or escape mode
1. COMMAND MODE
When the first start editing a file with the VI editor we will be VI connected
mode. In this mode we can issue many VI commands, including insert, append delete, othersearch and navigation commands the let you move around your file.
2.INSERT MODE
Once we issue a VI editor insert append or open command we will be in VI insertmode. If we are working with a mode VI is typically configure to show the current mode ofoperation.
At this point, we can
A. Type text into our fileB. Uses of the arrow keys
we can easily move back to command mode by esc.
Cpy- Copying the current line.Q - Heading
-
7/31/2019 Cpl 2 Manual
4/62
I - Insert the word.
3.LAST LINE MODE OR ESCAPE MODE:
The last VI mode is known as VI last line mode we can only get to last line mode from commandmode we get into this mode by pressing the colon key.
After pressing the key, we will see a colon character appears at the beginning of the last line of ourVI editor window and cursor will be moved to the position.
: w -to save our file but not quit VI.
: q -to quit if you havent left mode any edit.
:wq -to quit and save edits.
RESULT:
Thus the basic things about the editor and the 8 modes in VI editor had been studied.
BASIC UNIXCOMMANDS
Ex.No: 03
AIM:
-
7/31/2019 Cpl 2 Manual
5/62
To execute and implement the basic Shell UNIX commands.
1. DIRECTORY HANDLING COMMANDS AND THEIR OUTPUT:
1. [root@localhost root]# mkdir ram Creates a directory named ram
2. [root@localhost root]# cd arun Enters into the directory arunExample:
[ece@localhost ece]$ cd arun
[ece@localhost arun]$
3. [root@localhost arun]# cd .. Comes out of the directory arun[root@localhost root]
Example:
[ece@localhost ece]$ cd arun
[ece@localhost arun]$ cd ..
[ece@localhost ece]$
4. [root@localhost root]# pwd Shows the present working directory
Example:[ece@localhost ece]$ pwd
/home/ece
5.[root@localhost root]# rmdir Used to remove a directory
6.[root@localhost root]# ls Shows the list of files & directories EXAMPLE:[ece@localhost ece]$ ls
kanth
kar
kart
karth
karthickkarthik
karti
kat
katheeja
kathi
7.[root@localhost root]# ls -l List the permission of files / Directoriestotal 4
-rw-r--r-- 1 root root 1093 Mar 11 2006 anaconda-ks.cfg
-rw-r--r-- 1 root root 223 Mar 9 18:04 arith.sh
-rw-r--r-- 1 root root 260 Mar 9 18:18 arm.sh
drwxr-xr-x 2 root root 4096 Mar 9 19:24 barnabas
2. FILE HANDLING COMMANDS.
1) [root@localhost root]# cat > msb Creates a New file msbI studying B.E ECE.
-
7/31/2019 Cpl 2 Manual
6/62
2) [root@localhost root]# cat msb Opens the created file msb"I studying B.E ECE.
3) [root@localhost root]# cat >> msb Appends the details in the file msbI'm studying in Veltech Engineering college
4) [root@localhost root]# cat msb
My Name is Barnabas
I'm studying in Veltech Engineering college
5) [ece@localhost ece]$ cp ece1 ece2 To create duplicate copies[ece@localhost ece]$ cat ece1
a
b
c
d
[ece@localhost ece]$ cat ece2a
b
c
d
6) [ece@localhost ece]$ ln ece7 ece8 To link two files[ece@localhost ece]$ cat ece8
Iam studying in vel tech college
i am in ece dept
7) [root@localhost root]# rm msb Removes the file msbrm: remove regular file `msb'? y
8)(i) [root@localhost root]# wc msb Shows the no. of lines, words & characters.2 10 65 msb
(ii)[root@localhost root]# wc -l msb Shows the no. of lines alone.2 msb
(iii)[root@localhost root]# wc -w msb Shows the no. of words alone.10 msb
(iv)[root@localhost root]# wc -c msb Shows the no. of characters alone.65 msb
9) [ece@localhost ece]$ mv ece9 ece0 ls to move ordinary and directory files[ece@localhost ece]$ cat ece0
Iam studying in vel tech college
i am in ece dept
[ece@localhost ece]$ cat ece9
-
7/31/2019 Cpl 2 Manual
7/62
cat: ece9: No such file or directory
10) [ece@localhost ece]$ find ece8 To find a fileece8
3. PATTERN SEARCHING COMMANDS.
1) [root@localhost root]# grep m msb Prints the sentence with the letter mMy Name is sudha
I'm studying in vel tech engineering college
2) [ece@localhost ece]$ egrep "are|our" yamuna1 to search for the multiple patternwe belong to our vel tech tech college
we are of ece students
3) [ece@localhost ece]$ fgrep "stands for" yamuna1 to extract only the fixed string without theuse of any regular expression.ece stands for electronic and conmmunication engineering
4. GENERAL PURPOSE COMMANDS.
I ) SIMPLE COMMANDS.
1. [root@localhost root]# cal Prints the calendar if the present monthMarch 2006
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
2. [root@localhost root]# date Prints the current time.Thu Mar 9 19:26:27 IST 2006
3. [root@localhost root]# time l Prints the System Run-timereal 0m0.005s
user 0m0.000s
sys 0m0.000s
4. [root@localhost root]# man cal Opens manual on cal
[ece@localhost ece]$ man cpCP(1) FSF CP(1)
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
-
7/31/2019 Cpl 2 Manual
8/62
cp [OPTION]... --target-directory=DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short optionstoo.
-a, --archive
same as -dpR
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
:
5. [root@localhost root]# bc
Open the Binary Calculatorbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is
free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
123 + 246
369
10 * 20
200
ii) STATUS INFORMATION COMMANDS
1. [ece@localhost ece]$ who b Displays the date & time the system was
brought up.system boot Feb 23 23:02
2. [ece@localhost ece]$ who s Displays the short form listing which consists ofthe username,terminal number and time fields.
ece pts/2 Feb 23 23:03 (10.0.0.5)ece pts/1 Feb 23 23:03 (10.0.0.10)
ece pts/4 Feb 23 23:03 (10.0.0.36)
ece pts/6 Feb 23 23:04 (10.0.0.135)
ece pts/0 Feb 23 23:04 (10.0.0.119)
ece pts/3 Feb 23 23:04 (10.0.0.49)
ece pts/5 Feb 23 23:05 (10.0.0.137)
-
7/31/2019 Cpl 2 Manual
9/62
3. [ece@localhost ece]$ who u Restricts listing to users currently logged in.
ece pts/2 Feb 23 23:03 00:02 1723 (10.0.0.5)
ece pts/1 Feb 23 23:03 00:02 1674 (10.0.0.10)ece pts/4 Feb 23 23:03 . 1993 (10.0.0.36)
ece pts/6 Feb 23 23:04 00:01 1998 (10.0.0.135)
ece pts/0 Feb 23 23:04 . 2101 (10.0.0.119)
ece pts/3 Feb 23 23:04 . 2149 (10.0.0.49)
ece pts/5 Feb 23 23:05 . 2223 (10.0.0.137)
4. [root@localhost root]# who am I Identifies the current working System
root pts/0 Mar 9 19:21 (:0.0)
5. [ece@localhost ece]$ ps a Display the status of all the process associated with u,goptions
PID TTY TIME CMD
2311 pts/7 00:00:00 mail
2312 pts/3 00:00:00 ps
6. [ece@localhost ece]$ ps g Display Inforamtion of all process of group loads specifiedin ggrplist
PID TTY STAT TIME COMMAND
2152 pts/3 S 0:00 -bash
2344 pts/3 R 0:00 ps g
7. [ece@localhost ece]$ ps lLists the information using long format.
F S UID PID PPID C PRI NI ADDR SZWCHAN TTY TIME CMD
4 S 502 2152 2149 0 75 0 - 1066 wait4 pts/3 00:00:00 bash
0 R 502 2348 2152 0 80 0 - 764 - pts/3 00:00:00 ps
8. [ece@localhost ece]$ ttyIt will retun the pathname with your terminal
/dev/pts/3
9. [ece@localhost ece]$ idTo Display the Numerical value that corresponda to your login
-
7/31/2019 Cpl 2 Manual
10/62
name.
uid=502(ece) gid=502(ece) groups=502(ece)
10.[ece@localhost ece]$ uname a To display the relevant details about the OperatingSystem on the standard output.
Linux localhost.localdomain 2.4.20-6smp #1 SMP Thu Feb 27 09:59:40 EST 2003 i686 i686
i386 GNU/Linux
11.[ece@localhost ece]$ finger eceGives you more Information about the User.
Login: ece Name: (null)
Directory: /home/ece Shell: /bin/bash
On since Tue Feb 23 23:03 (IST) on pts/2 from 10.0.0.55 minutes 38 seconds idle
On since Tue Feb 23 23:03 (IST) on pts/1 from 10.0.0.10
5 minutes 38 seconds idle
On since Tue Feb 23 23:03 (IST) on pts/4 from 10.0.0.36
7 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/6 from 10.0.0.135
4 minutes 54 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/0 from 10.0.0.119
3 minutes 32 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/3 from 10.0.0.49
On since Tue Feb 23 23:05 (IST) on pts/5 from 10.0.0.1374 minutes 2 seconds idle
On since Tue Feb 23 23:06 (IST) on pts/7 from 10.0.0.119
7 seconds idle
On since Tue Feb 23 23:07 (IST) on pts/8 from 10.0.0.34
2 minutes 12 seconds idle
New mail received Tue Feb 23 23:08 2010 (IST)
Unread since Tue Feb 23 22:44 2010 (IST)
No Plan.
iii ) COMMUNICATION COMMANDS
1. [root@localhost root]# wall Hai To send message hai to all users there who arecurrently logged in.
Hai
2. [ece@localhost ece]$ mail ece To send the textual messages electrically
-
7/31/2019 Cpl 2 Manual
11/62
transferred from one user to another.
Subject: Hai.
How are you.
All the best for your exams.
Have a nice day.
3. [ece@localhost ece]$ mail Read your mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/ece": 1 messages 1 new
a) >N 1 [email protected] Tue Feb 23 23:06 18/720 "Hai."
&
Message 1:
From [email protected] Tue Feb 23 23:06:23 2010
Date: Tue, 23 Feb 2010 23:06:22 +0530
From: [email protected]: [email protected]
Subject: Hai.
How are you.Have you prepared for your exams.
ALL THE BEST FOR YOUR EXAMS.
Have a nice day.
b) & reply To send the reply to the specified userTo: [email protected] [email protected]
Subject: Re: Hai.
Thanks
Cc: & cseUnknown command: "cse"
c)& exit Exit from the mail
d)&type Print the mail message
e)&delete Delete from the mail box
OTHER SHELL COMMANDS
I ) INPUT AND OUTPUT REDIRECTION COMMANDS
1. [root@localhost root]# > To redirect the output of a cammmand to a file.
[ece@localhost ece]$ date>a1
[ece@localhost ece]$ cat a1
Tue Feb 23 22:59:03 IST ece@localhost ece]$
2. [ece@localhost ece]$ >> To append the output at the end of a file
-
7/31/2019 Cpl 2 Manual
12/62
[ece@localhost ece]$ cat science
date
time
[ece@localhost ece]$ cat tech
one
two[ece@localhost ece]$ cat science>>tech
[ece@localhost ece]$ cat tech
one
two
date
time
[ece@localhost ece]$
3. [ece@localhost ece]$To redirect the output and input command to a file[ece@localhost ece]$ catdnames
[ece@localhost ece]$ cat dnames
CSE
EEE
[ece@localhost ece]$
II ) SECURITY COMMANDS
1.[ece@localhost ram]$ ls l To list files & Directories in long formattotal 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-rw-rw-r-- 1 ece ece 65 Feb 23 23:16 science
2. ece@localhost ram]$ chmod -ux science (-) denotes removing the permission
(u)denotes the user acess(x) denotes executing permissions
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
---------- 1 ece ece 65 Feb 23 23:16 scienceshowsMissing Permissions
-
7/31/2019 Cpl 2 Manual
13/62
3)[ece@localhost ram]$ chmod u+r,u+w science(+) denotes assigningPermissions
(w) denotes writing permissions(r)denotes reading permissions
[ece@localhost ram]$ ls -ltotal 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-rw------- 1 ece ece 65 Feb 23 23:16 science the user now has reading writingpermission
4) [ece@localhost ram]$ chmod u=x science (=)denotes assigning absolutely[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal---------- 1 ece ece 65 Feb 23 23:16 science
5) [ece@localhost ram]$ chmod g+r,g+w science(g)denotes group accessibility[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
----rw---- 1 ece ece 65 Feb 23 23:16 sciencethe grouphas now permission to access
6) [ece@localhost ram]$ chmod o+r,o+w science(o)denotes other useraccessibility
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
----rw-rw- 1 ece ece 65 Feb 23 23:16 science
7) [ece@localhost ram]$ chmod a+r,a+x science(a)denotes all user accessibility[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-r-xrwxrwx 1 ece ece 65 Feb 23 23:16 science
III) WILD CARD PATTERNS
1. [ece@localhost ansatz]$ ls m* To representing any number of characters used in the prefixor suffix.
-
7/31/2019 Cpl 2 Manual
14/62
[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
[ece@localhost raja]$ ls m*
mvj:
mvs:
[ece@localhost raja]$[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
2. [ece@localhost raja]$ ls mv?To represent one character only either in the prefix or in thesuffix of the file name[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
[ece@localhost raja]$ ls mv?
mvj:
mvs:
[ece@localhost raja]$ ls
3. [ece@localhost raja]$ ls sh[1-4]To access a subset of related files[ece@localhost raja]$ ls
kanna mvj mvs sh1 sh2 sh3 sh4 sh5 student vikram vikram]
[ece@localhost raja]$ ls sh[1-4]
sh1:
sh2:
sh3:
sh4:
[ece@localhost raja]$
IV ) PIPES AND FILTERS COMMANDS
1. [ece@localhost ece]$ who |wc The output of whois taken as the input for thewc and result is displayed.
9 54 426
2. [ece@localhost ece]$ who | tee names | wc l The Use of tee command is tostore the intermediate results in a file.
16
[ece@localhost ece]$ cat names
ece pts/4 Feb 23 23:56 (10.0.0.135)ece pts/17 Feb 24 00:05 (10.0.0.221)
ece pts/10 Feb 24 00:09 (10.0.0.15)
ece pts/13 Feb 24 00:13 (10.0.0.180)
ece pts/1 Feb 24 00:14 (10.0.0.49)
ece pts/24 Feb 24 00:14 (10.0.0.6)
ece pts/3 Feb 24 00:17 (10.0.0.36)
ece pts/30 Feb 24 00:19 (10.0.0.137)
-
7/31/2019 Cpl 2 Manual
15/62
ece pts/7 Feb 24 00:19 (10.0.0.34)
ece pts/28 Feb 24 00:22 (10.0.0.5)
ece pts/20 Feb 24 00:23 (10.0.0.120)
ece pts/21 Feb 24 00:24 (10.0.0.3)
ece pts/31 Feb 24 00:25 (10.0.0.119)
ece pts/34 Feb 24 00:25 (10.0.0.14)ece pts/32 Feb 24 00:27 (10.0.0.4)
3. [ece@localhost ece]$ head -5 names To Display the First Five lines from aFile.
jack
arun
prashant
deepak
naga
[ece@localhost ece]$
4. [ece@localhost ece]$ tail -5 names To Display the Last Five lines from aFile.
ajith
superman
spiderman
goten
vegetables
5. (i)[root@localhost root]# sort msb1 Sorts in ascending orderAravind
BalajiDinesh
Ganesh
Kamalesh
(ii) [root@localhost root]# sort -r msb1 Sorts in descending order Kamalesh
Ganesh
Dinesh
Balaji
Aravind
6.[root@localhost root]# pr To display the Page number,File Creation date andalong with the name of the file.
[ece@localhost ece]$ pr g1
2010-02-25 02:38 g1 Page 1
echo ENTER THE NAME
read n
echo-n THE ENTERED NAME IS $n
-
7/31/2019 Cpl 2 Manual
16/62
7.[root@localhost root]# cut To cut the selected fields.[ece@localhost ece]$ cat college
diva vh2346
jan vh2345
kavi vh2387naru vh2876
subi vh2498
[ece@localhost ece]$ cut -c2 college
i
a
a
a
u
[ece@localhost ece]$
8.[root@localhost root]# paste To paste the contenet from one to another file.[ece@localhost ece]$ cat college1
dhoni vh2376
raina vh2398
raj vh2270
subi vh2498
jan vh2345
[ece@localhost ece]$ cat college2
divi vh2334
raina vh2398
raj vh2270
[ece@localhost ece]$ paste -d% college1 college2dhoni vh2378
divi vh2334
raina vh2397
raina vh2398
raj vh2270
raj vh2270
subi vh2498%
jan vh2345
9.[root@localhost root]# join To extract the common lines from two sorted Files.
[ece@localhost ece]$ cat college1dhoni vh2376
raina vh2398
raj vh2270
subi vh2498
jan vh2345
[ece@localhost ece]$ cat college2
divi vh2334
-
7/31/2019 Cpl 2 Manual
17/62
raina vh2398
raj vh2270
[ece@localhost ece]$ join v1 college1 college2
dhoni vh2376
subi vh2498
jan vh2345[ece@localhost ece]$
10.[root@localhost root]# uniq To Displays the adjacent duplicate lines in a existing file[ece@localhost ece]$ cat tech3
nithu vh2272
uma vh2297
nithu vh2272[ece@localhost ece]$
[ece@localhost ece]$ uniq -c2 tech3
3 nithu vh2272
[ece@localhost ece]$
11.[root@localhost root]# nl Used to add Line Numbers to a File.[cse@localhost cse]$ nl dept
1 cse
2 ece
3 eee
4 it
5 mech
[cse@localhost cse]$ nl -v4 deptstarts the numbering of each pageby 4
4 cse
5 ece6 eee
7 it
8 mech
[cse@localhost cse]$ nl i3 dept increments number of lines by3.1 cse
4 ece
7 eee
10 it
13 mech
12.[root@localhost root]# tr It is used to translate characters taken from the standard input.
[cse@localhost cse]$ cat deptcse
ece
eee
it
mech
[cse@localhost cse]$ cat dept|tr "[a-z]" "[A-Z]"
CSE
-
7/31/2019 Cpl 2 Manual
18/62
ECE
EEE
IT
MECH
13.[root@localhost root]# pg It displays the output of a command on the screen
page by page.
14.[root@localhost root]# more To Displays the output page by page[ece@localhost ece]$ ls | more
0
00
000123
003
0045
007
1
10100
10001
101
12
122
123
1234
1234567890
124
132
133143
159
18
--More Press Enter to see the next page.
15.[root@localhost root]# cmp Compare any two files, including binary files. Ittakes two filenames as arguments.
[ece@localhost ece]$ cat > college
diva vh2346
jan vh2345
kavi vh2387naru vh2876
subi vh2498
[ece@localhost ece]$ cat > college1
DHONI VH2876
RAINA VH2398
KAIF VH2657
HARI VH2765
-
7/31/2019 Cpl 2 Manual
19/62
SRI VH4567
[ece@localhost ece]$ cmp college college1
college college1 differ: byte 1, line 1
[ece@localhost ece]$ cat college[ece@localhost ece]$ cmp -b college college1
college college1 differ: byte 1, line 1 is 144 d 104 D
16.[root@localhost root]# diff Files are compared on line by line basis.[ece@localhost ece]$ cat college
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
[ece@localhost ece]$ cat college1
dhoni vh2387
raina vh2389
subi vh2498sachin vh2564
diva vh2346
[ece@localhost ece]$ diff -e college college1
1,5c
dhoni vh2387
raina vh2389
subi vh2498
sachin vh2564
diva vh2346
[ece@localhost ece]$ diff -b college college1
1,4c1,2< diva vh2346
< jan vh2345
< kavi vh2387
< naru vh2876
---
> dhoni vh2387
> raina vh2389
5a4,5
> sachin vh2564
> diva vh2346
17.[root@localhost root]# comm Compare and displays lines common as well asunique to two files.
[ece@localhost ece]$ comm -1 college college1
dhoni vh2387
raina vh2389
subi vh2498
sachin vh2564
-
7/31/2019 Cpl 2 Manual
20/62
diva vh2346
[ece@localhost ece]$ comm -13 college college1
dhoni vh2387
raina vh2389
subi vh2498
sachin vh2564diva vh2346
[ece@localhost ece]$ comm -23 college college1
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
RESULT:
Thus the Basic Shell Commands were executed.
ARITHMETIC OPERATION USING SHELL PROGRAM
Ex.No: 04
AIM:
To write a shell Program to do the Arithmetic Operations like Addition, Subtraction,Multiplication, Division.
ALGORITHM:
Step 1: Start
Step 2: Read the values of a and b
Step 3: Write the expression to calculate sum, difference, Product, Quotient and Remainder.
Step 4: Print the Results
Step 5: Stop.
SHELL PROGRAM:
echo "Enter Two Numbers"read a bc=`expr $a + $b`d=`expr $a - $b`
-
7/31/2019 Cpl 2 Manual
21/62
e=`expr $a \* $b`f=`expr $a / $b`g=`expr $a % $b`echo "Sum = $c"echo "Difference = $d"
echo "Product = $e"echo "Quotient = $f"echo "Remainder = $g"
SAMPLE OUTPUT:
Enter Two Numbers12 10Sum = 22Difference = 2Product = 120Quotient = 1
Remainder = 2
RESULT:
Thus the shell Program to do arithmetic operation was executed without any errors.GREATEST OF THREE NUMBERS
Ex. No.: 05
AIM:
To write a shell program to find the greatest of three Numbers.
ALGORITHM:
Step 1: Start
Step 2: Read the value of a, b and c
Step 3: Check if a is greater than b and a is greater than c
Step 4: Then print A is Greater
Step 5: If Step 3 is not satisfied check if b is greater than c
Step 6: Then print B is Greater
Step 7: Else Print C is greater
Step 8: Stop.
SHELL PROGRAM:
echo "Enter Three Numbers"read a b c
-
7/31/2019 Cpl 2 Manual
22/62
if [ $a -gt $b -a $a -gt $c ]thenecho "$a is Greater"elif [ $b -gt $c ]then
echo "$b is Greater"elseecho "$c is Greater"fi
SAMPLE OUTPUT:
Enter Three Numbers12 14 1616 is Greater
RESULT:Thus the program to find the greatest of three Numbers was executed and the output wasverfied.
CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT
Ex. No.: 06
AIM:
To check whether the given Number is Armstrong Number or not using ShellProgramming.
ALGORITHM:
Step 1: Start
Step 2: Read the value of num
Step 3: Store the value of num in x and initialize the value of sum = 0.
Step 4: When the value of num is greater then 0, solve the following expressions
Step 5: y = num/10; z=y*y*y; sum=num+z; num=num/10
Step 6: Close the While Loop
Step 7: Check if x is equal to sum
Step 8: Print the Result
-
7/31/2019 Cpl 2 Manual
23/62
Step 9: Stop.
SHELL PROGRAM:
echo "Enter a Number"read numx=$numsum=0while [ $num -gt 0 ]doy=`expr $num % 10`z=`expr $y \* $y \* $y`sum=`expr $sum + $z`num=`expr $num / 10`doneif [ $x -eq $sum ]thenecho "$x is an armstrong Number"elseecho "$x is not an armstrong Number"fi
SAMPLE OUTPUT:
Enter a Number
-
7/31/2019 Cpl 2 Manual
24/62
153153 is an armstrong Number[root@localhost root]# sh arm.shEnter a Number123
123 is not an armstrong Number
RESULT:Thus the Shell Program to check whether the given Number is Armstrong or not wasexecuted and the output was verified.
FIBONACCI SERIES
Ex No.: 07
AIM:To write a Shell Program to print the Fibonacci series.
ALGORITHM:
Step 1: Start
Step 2: Read the Value of n
Step 3: Initialize i=2; a=0; b=1.
Step 4: Print the Value of a and b
Step 5: When i is lesser than n, solve the expression c=a+b
Step 6: Print c
Step 7: Swap the Values of b to a and c to b
Step 8: Solve the expression i=i+1
Step 9: Stop.
-
7/31/2019 Cpl 2 Manual
25/62
SHELL PROGRAM:
echo "Enter the Limit"read ni=2echo "Fibonacci Series"echo "----------------"a=0b=1echo $aecho $bwhile [ $i -lt $n ]doc=`expr $a + $b`echo $ca=$bb=$ci=`expr $i + 1`done
SAMPLE OUTPUT:
Enter the Limit8
-
7/31/2019 Cpl 2 Manual
26/62
Fibonacci Series----------------011
235813
RESULT:
Thus the Shell Program to print the Fibonacci Series was executed and the output wasverified.
SWAPPING OF VALUES
(Using 3rd Variable)
Ex. No.: 08(a)
AIM:
To write a Shell Program to swap two values using 3rd variable.
ALGORITHM:
Step 1: Read the Values of a and b
Step 2: Swap the values of a to the third variable temp
Step 3: Swap the value of b to a and b to temp
Step 4: Print the swapped values of a and b
Step 5: Stop.
SHELL PROGRAM:
echo "Enter Two Numbers"read a btemp=$a
-
7/31/2019 Cpl 2 Manual
27/62
a=$bb=$tempecho $a $b
SAMPLE OUTPUT:
Enter Two Numbers12 1010 12
RESULT:
Thus the program to swap two values using 3rd variable was executed and the output wasverified.
SWAPPING OF VALUES
(Without using 3rd Variable)
Ex. No.: 08(b)
AIM:
To write a Shell Program to swap two values without using 3rd Variable.
ALGORITHM:
Step 1: Start
Step 2: Read the values of a and b
Step 3: Solve the expression, a=a + b; b=a b; a=a b
Step 4: Print the swapped values of a and b
Step 5: Stop
SHELL PROGRAM:
echo "Enter Two Numbers"read a b
-
7/31/2019 Cpl 2 Manual
28/62
a=`expr $a + $b`b=`expr $a - $b`a=`expr $a - $b`echo $a $b
SAMPLE OUTPUT:
Enter Two Numbers12 1010 12
RESULT:
Thus the Shell Program to swap two values without using third variable was executed andthe output was verified.NUMBER OF DIGITS IN AN INTEGER
Ex. No.: 09
AIM:
To count the number of digits in an integer, using Shell Programming.
ALGORITHM:
Step 1: Start
Step 2: Read the value of a
Step 3: Initialize the value of c as 0
Step 4: When the value of a is not equal to 0, solve n=a %10.
Step 5: If the value of n is not equal to 0, solve c=c + 1(increment)
Step 6: End if condition
Step 7: Solve the expression a=a / 10
Step 8: Repeat steps 4 to 7 until a is equal to 0
Step 9: Print the value of c as the number of digits in an integer.
Step 10: Stop.
SHELL PROGRAM:
echo "Enter a Number"read a
-
7/31/2019 Cpl 2 Manual
29/62
c=0while [ $a -ne 0 ]don=`expr $a % 10`if [ $n -ne 0 ]
thenc=`expr $c + 1`fia=`expr $a / 10`doneecho "The Number of Digits in the Integer is $c"
SAMPLE OUTPUT:
Enter a Number123456789The Number of Digits in the Integer is 9
RESULT:
Thus the Shell Program to calculate the number of digits of an integer was executed and theoutput was verified.ARITHMETIC OPERATIONS USING SHELL PROGRAMMING
(Using SWITCH - CASE)
Ex. No. 10
AIM:
To write a Shell Program to perform Arithmetic Operations using Switch Case
ALGORITHM:
Step 1: Start
Step 2: Read the two Numbers.
Step 3: Get the operation choice from the User
Step 4: Give the expressions for each case and solve them.
Step 5: Print the Result
Step 6: Stop
-
7/31/2019 Cpl 2 Manual
30/62
SHELL PROGRAM:
echo "Enter Two Numbers"read a becho "What do you want to do? (1 to 5)"echo "1) Sum"echo "2) Difference"echo "3) Product"echo "4) Quotient"echo "5) Remainder"echo "Enter your Choice"read ncase "$n" in1) echo "The Sum of $a and $b is `expr $a + $b`";;2) echo "The Difference between $a and $b is `expr $a - $b`";;3) echo "The Product of the $a and $b is `expr $a \* $b`";;4) echo "The Quotient of $a by $b is `expr $a / $b`";;5) echo "The Remainder of $a by $b is `expr $a % $b`";;esac
SAMPLE OUTPUT:
[root@localhost Shell]# sh arith_switch.shEnter Two Numbers12 10What do you want to do? (1 to 5)
-
7/31/2019 Cpl 2 Manual
31/62
1) Sum2) Difference3) Product4) Quotient5) Remainder
Enter your Choice4The Quotient of 12 by 10 is 1
RESULT:
Thus the Shell program to perform arithmetic operations using Switch Case was executedand the output was verified.
FACTORIAL OF A NUMBER
Ex. No. 11
AIM:
To write a Shell Program to find the factorial of a Number.
ALGORITHM:
Step 1: Start
Step 2: Read the Number as n
Step 3: Assign the value of p as 0 and solve the expression i = n-1
Step 4: While the value of i is greater than or equal to 1 do the following steps.
Step 5: Solve the expression n=n*1 and i=i-1 (Decrementing Operation)
Step 6: Then print the value of n as the factorial of the given number.
Step 7: Stop.
SHELL PROGRAM:
echo "Enter a Number"read ni=`expr $n - 1`p=1
-
7/31/2019 Cpl 2 Manual
32/62
while [ $i -ge 1 ]don=`expr $n \* $i`i=`expr $i - 1`done
echo "The Factorial of the given Number is $n"
SAMPLE OUTPUT:
[root@localhost Shell]# sh fact.shEnter a Number5The Factorial of the given Number is 120
RESULT:
Thus the Shell Program to find the factorial of the Number was executed and the output
was verified.
CHECKING FOR PRIME NUMBER
Ex. No.: 12
AIM:
To write a Shell Program to check if the Number is a Prime Number or a CompositeNumber.
ALGORITHM:
Step 1: Start
Step 2: Read the Number as n
Step 3: Initialize the value of t to 0 and solve the expression i=n-1
Step 4: While i is greater than or equal to 2, perform the following steps.
Step 5: Solve p=n%i
Step 6: Check the condition (p=0) and then solve t=t+1
Step 7: End the If Condition
Step 8: Solve the expression i=i-1
-
7/31/2019 Cpl 2 Manual
33/62
Step 9: If t is greater than 0, print The Number is not a Prime Number
Step 10: If the condition is not satisfied, print The Number is a Prime Number
SHELL PROGRAM:
echo "Enter a Number"read ni=`expr $n - 1`t=0while [ $i -ge 2 ]dop=`expr $n % $i`if [ $p -eq 0 ]thent=`expr $t + 1`fii=`expr $i - 1`doneif [ $t -gt 0 ]thenecho "The Number $n is not a Prime Number"elseecho "The Number $n is a Prime Number"fi
-
7/31/2019 Cpl 2 Manual
34/62
SAMPLE OUTPUT:
[root@localhost Shell]# sh prime.shEnter a Number2
The Number 2 is a Prime Number[root@localhost Shell]# sh prime.shEnter a Number4The Number 4 is not a Prime Number
RESULT:
Thus the Shell Program to check if the given number is prime or not, was executed and theoutput was verified.SUMS OF DIGITS OF AN INTEGER
Ex. No. 13
AIM:
To write a Shell Program to calculate the sum of digits of an Integer.
ALGORITHM:
Step 1: StartStep 2: Read the Integer as numStep 3: Initialize the value of sum=0.Step 4: While the value of num is greater than 0, solve the following expressions.Step 5: Find the remainder of num by 10 and store in yStep 6: Add sum with y and store it in sumStep 7: Divide num by 10 and store the value in numStep 7: Close the while loopStep 8: Print the ResultStep 9: Stop
SHELL PROGRAM:
echo "Enter a Number"read numsum=0
-
7/31/2019 Cpl 2 Manual
35/62
while [ $num -gt 0 ]doy=`expr $num % 10`sum=`expr $sum + $y`num=`expr $num / 10`
doneecho "The Sum of the Digits of the Integer is $sum"
SAMPLE OUTPUT:
[root@localhost Shell]# sh digits_sum.shEnter a Number123456The Sum of the Digits of the Integer is 21
RESULT:
Thus the Shell program to calculate the sum of the digits of the integer was executed andthe output was verified.C PROGRAMMING UNDER UNIX ENVIRONMENT
MATRIX ADDITION
Ex. No. 14
AIM:
To write a C Program to add two matrices under UNIX Environment
ALGORITHM:
Step 1: Start
Step 2: Declare a[10][10], b[10][10], add[10][10], i, j, m, n.
Step 3: Get the number of rows and columns as m and n respectively
Step 4: Scan the values of the matrices according to the values of m and n.
Step 5: Add the two matrices directly and store the values in add[10][10]
Step 6: Using for loop print the values of add[10][10]
Step 7: End the loop
Step 8: Stop.
-
7/31/2019 Cpl 2 Manual
36/62
C PROGRAM:
#includemain(){int add[10][10],a[10][10],b[10][10],i,j,m,n;printf("\nEnter the Number of Rows and Columns:\n\n ");scanf("%d%d",&m,&n);printf("Enter the 1st Matrix: \n\n");for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
printf("Enter the 2nd Matrix: \n\n");for(i=0;ifor(j=0;jscanf("%d",&b[i][j]);
for(i=0;i{for(j=0;jadd[i][j]=a[i][j]+b[i][j];}printf("The Addition of the two matrices is: \n");for(i=0;i{printf("\n\n");for(j=0;j
-
7/31/2019 Cpl 2 Manual
37/62
{printf("%d\t",add[i][j]);}}
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc matadd.c[root@localhost C_Program]# ./a.out
Enter the Number of Rows and Columns:
22Enter the 1st Matrix:
1234Enter the 2nd Matrix:
1234The Addition of the two matrices is:
2 4
-
7/31/2019 Cpl 2 Manual
38/62
6 8
RESULT:
Thus the C Program to add two matrices under UNIX Environment was executed and theoutput was verified.
SORTING OF INTEGERS
Ex. No. 15
AIM:
To write a C Program to sort an array of integers under UNIX environment
ALGORITHM:
Step 1: Start
Step 2: Get the number of array integers as n
Step 3: Using for loop, get the n number of integers as a[]
Step 4: Again using for loop check if the second number is greater than the first
Step 5: If the above step is true, swap the value of second to first
Step 6: Repeat Steps 4 and 5 for all the integers scanned using for loop
Step 7: Again using for loop print the sorted list of values.
Step 8: Stop
-
7/31/2019 Cpl 2 Manual
39/62
C PROGRAM:
#includemain(){int x[50],n,i,j,temp;printf("How many Numbers ?\n");scanf("%d",&n);printf("\nEnter the List of %d numbers:\n",n);for(i=0;iscanf("%d",&x[i]);for(i=0;ifor(j=i+1;jif(x[i]>x[j]){temp=x[i];x[i]=x[j];x[j]=temp;}
printf("\nThe Sorted list of Numbers is \n");for(i=0;iprintf("%d\t",x[i]);
}
SAMPLE OUTPUT:
-
7/31/2019 Cpl 2 Manual
40/62
[root@localhost C_Program]# cc sort.c[root@localhost C_Program]# ./a.outHow many Numbers ?5
Enter the List of 5 numbers:48362
The Sorted list of Numbers is2 3 4 6 8
RESULT:
Thus the C Program to sort the given array of integers under UNIX environment wasexecuted and the output was verified.EMPLOYEE DETAILS USING STRUCTURES
Ex. No.: 16
AIM:
To write a C Program to get and print the employee details using Structures under UNIXPlatform.
ALGORITHM:
Step 1: Start
Step 2: Initialize the structure with employee no., name and salary.
Step 3: Inside the main function, get the number of employees
Step 4: Using for loop, scan the employee details
Step 5: Again using for loop, print the employee details
Step 6: Stop
-
7/31/2019 Cpl 2 Manual
41/62
C PROGRAM:
#includestruct emp{char name[25];int salary;int empno;}a[20];main(){int n,i;printf("\n Enter the Number of Employees: ");scanf("%d",&n);for(i=0;i{printf("\n\nEnter the %d Employee details",i+1);printf("\n\n1.Employee No.: ");scanf("%d",&a[i].empno);printf("\n2.Name: ");scanf("%s",&a[i].name);printf("\n3.Salary: ");scanf("%d",&a[i].salary);}for(i=0;i{
-
7/31/2019 Cpl 2 Manual
42/62
printf("\n\nEmployee %d",i+1);printf("\n1.Employee No.: %d",a[i].empno);printf("\n2.Employee Name: %s",a[i].name);printf("\n3.Salary: %d",a[i].salary);}
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc employ.c[root@localhost C_Program]# ./a.out
Enter the Number of Employees: 2
Enter the 1 Employee details
1.Employee No.: 1234
2.Name: Ramesh
3.Salary: 12500
Enter the 2 Employee details
1.Employee No.: 2314
2.Name: Ganesh
3.Salary: 12500
Employee 11.Employee No.: 1234
-
7/31/2019 Cpl 2 Manual
43/62
2.Employee Name: Ramesh3.Salary: 12500
Employee 21.Employee No.: 2314
2.Employee Name: Ganesh3.Salary: 12500
RESULT:
Thus the C Program to get and print the employee details using Structures was executedand the output was verified.FILE MANIPULATION
Ex. No.: 17
AIM:
To write a C Program to perform following file manipulation:
1. Read a file2. Copy a file3. Change the case of the entire file4. Count the number of words, spaces and lines in a file.
C PROGRAM:
#include#includemain(){char ch,ct;int choice,tr,letters=0,words=0,spaces=0,lines=0;FILE *fp1,*fp2;do{fp1=fopen("data1.txt","r");
-
7/31/2019 Cpl 2 Manual
44/62
printf("\nEnter u'r Choice\n");printf("\n1.Read the file\n2.Copy the file\n3.Change\n4.Count\n5.Exit\n");scanf("%d",&choice);switch(choice){
case 1:while(!feof(fp1)){ch=getc(fp1);printf("%c",ch);}break;case 2:fp2=fopen("data2.txt","w");fseek(fp1,0L,SEEK_SET);while(!feof(fp1))
{ct=fgetc(fp1);tr=fputc(ct,fp2);}fclose(fp1);fclose(fp2);fp2=fopen("data2.txt","r");printf("The Content of the copied file is ");while(!feof(fp2)){ch=fgetc(fp2);printf("%c",ch);}fclose(fp2);break;case 3:fp2=fopen("data2.txt","r");while(!feof(fp2)){ch=getc(fp2);if((ch=65)){ch=ch+32;printf("%c",ch);}else if((ch>=97)&&(ch
-
7/31/2019 Cpl 2 Manual
45/62
else{printf("%c",ch);}}
break;case 4:fp2=fopen("data2.txt","r");while(!feof(fp2)){ch=fgetc(fp2);if(ch==' '){spaces++;}else if (ch=='\n')
{lines++;}else{letters++;}}printf("\nTotal lines: %d",lines);printf("\nTotal words: %d",spaces+lines);printf("\nTotal spaces: %d",spaces);printf("\nTotal letters: %d",spaces+lines+words);fclose(fp2);break;}}while(choice!=5);}
SAMPLE OUTPUT:
Enter u'r Choice
1.Read the file2.Copy the file3.Change4.Count5.Exit1
-
7/31/2019 Cpl 2 Manual
46/62
Wisdom better than the Rubies.?Enter u'r Choice
1.Read the file
2.Copy the file3.Change4.Count5.Exit2The Content of the copied file is Wisdom better than the Rubies.??Enter u'r Choice
1.Read the file2.Copy the file
3.Change4.Count5.Exit3wISDOM BETTER THAN THE rUBIES.??Enter u'r Choice
1.Read the file2.Copy the file3.Change4.Count5.Exit4
Total lines: 1Total words: 5Total spaces: 4Total letters: 5
-
7/31/2019 Cpl 2 Manual
47/62
RESULT:
Thus the FILE MANIPULATION operation was performed and the output was verified.
GREAEST AND SMALLEST NUMBER IN AN ARRAY
Ex.No.: 18
AIM:
To write a C Program to find the greatest and smallest number in an array of integers.
ALGORITHM:
Step 1:StartStep 2: Get the maximum limit for the array as n
Step 3: Using for loop, get the array of integers to the value of n
Step 4: Again using for loop, sole the following conditions.
Step 5: If the first digit is greater than the second, perform the following swap conditions.
Step 6: Swap the value of a[0] to temp, a[j] to a[i] and a[i] to temp.
Step 7: Print the result
Step 8: Stop
-
7/31/2019 Cpl 2 Manual
48/62
PROGRAM:
#includemain(){int a[100],i,j,n,temp;printf("\nEnter the maximum limit: ");scanf("%d",&n);printf("\nEnter %d numbers: \n",n);for(i=0;iscanf("%d",&a[i]);for(i=0;i{for(j=i+1;j{if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}printf("\nThe smallest number is %d",a[0]);printf("\nThe biggest number is %d",a[n-1]);}
-
7/31/2019 Cpl 2 Manual
49/62
SAMPLE OUTPUT:
[root@localhost C_Program]# cc maxmin.c[root@localhost C_Program]# ./a.out
Enter the maximum limit: 5Enter 5 numbers:9621015The smallest number is 2The biggest number is 15
RESULT:
Thus the C Program to find the greatest and smallest number in an array was executed andthe output was verified.
TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT
Ex. No.: 19
AIM:
To write a C Program to check if the given number is a palindrome or not
ALGORITHM:
Step 1: Start
Step 2: Get the number as n
Step 3: Swap the value of n to c.
Step 4: While the value of n is greater than or equal to 1, solve the following expressions.
Step 5: Store the remainder of n by 10 as a; Solve b=(b*10)+a; and n=n/10
Step 6: If the value of c is equal to b, print The number is a palindrome.
Step 7: If the above condition is not satisfied, print The number is not a palindrome.
Step 8: Stop.
-
7/31/2019 Cpl 2 Manual
50/62
PROGRAM:
#includemain(){int a,c,n,b=0;printf("\nEnter a number: ");scanf("%d",&n);c=n;while(n>=1){a=n%10;b=(b*10)+a;n=n/10;}if(c==b)
printf("\nThe number is a palindrome\n");else
printf("\nThe number is not a palindrome\n");}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc palin.c
-
7/31/2019 Cpl 2 Manual
51/62
[root@localhost C_Program]# ./a.outEnter a number: 12321The number is a palindrome
[root@localhost C_Program]# ./a.out
Enter a number: 123The number is not a palindrome
RESULT:
Thus the C Program to check whether the number is a palindrome or not was executed andthe output was verified.
SYSTEM CALL FOR A READ ONLY FILE
Ex. No.:20
AIM:
To write C Program to perform system call to open for a read-only file.
ALGORITHM:
Step 1: Start
Step 2: Initialize the integer variables- i and fd. Also initialize the string variable, buf[100].
Step 3: Open the already created file aa as a read only and store its contents in fd.
Step 4: Read fd, buf, 100
Step 5: Using for loop, print the character array, buf[i]
Step 6: The data already stored in aa along with the garbage value will be printed as output.
Step 7: Stop
Note:
A file aa with data must be created before the program is compiled and is run, to avoidsegmentation error.
-
7/31/2019 Cpl 2 Manual
52/62
Header Files:
fcntl.h File control Options
unistd.h Standard symbolic constants and types.
PROGRAM:
#include#include#includemain(){int i,fd;char buf[100];
fd=open("aa",O_RDONLY);read(fd,buf,100);for(i=0;i
-
7/31/2019 Cpl 2 Manual
53/62
RESULT:
Thus the C Program for system call to open a read-only file was executed and the outputwas verified.
SYSTEM CALL FOR WRITE-ONLY FILE
Ex. No.: 21
AIM:
To write a C Program to perform system call to open a write-only file.
ALGORITHM:
Step 1: Start
Step 2: Initialize the integer variables i and fd with a character variable buf[100].
Step 3: Get the data form the user and store it as buf.
Step 4: Open the file aa as write-only and store it in fd.
Step 5: Write the data given by the user to the file and fd
Step 6: Stop
Note:
After the program is run, open the file aa to find the garbage values stored in it.A file aa with data must be created before the program is compiled and is run, to avoid
segmentation error.
Header Files:
-
7/31/2019 Cpl 2 Manual
54/62
fcntl.h File control Options
unistd.h Standard symbolic constants and types.
PROGRAM:
#include#include#includemain(){int i,fd;char buf[100];printf("Give data: ");
scanf("%s",buf);fd=open("aa",O_WRONLY);write(fd,buf,sizeof(buf));}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc syscall-w.c[root@localhost C_Program]# ./a.outGive data: I study in VEC[root@localhost C_Program]# cat aa
IBeBBBt
-
7/31/2019 Cpl 2 Manual
55/62
RESULT:
Thus the C Program to perform system call to opena write-only file was executed and theoutput was verified.
IMPLEMENTATION OF COPY COMMAND
Ex. No.: 22
AIM:
To write a C Program to implement copy command in UNIX.
ALGORITHM:
Step 1: Start
Step 2:Open the file to be copied, and store it in fp1Step 3: Open a new file, where the contents are to be copied and store it in fp2.
Step 4: Copy all the characters one by one until the End of File.
Step 5: Close all the files.
Step 6: Stop.
-
7/31/2019 Cpl 2 Manual
56/62
C PROGRAM:
#includemain(int a, char *av[2]){char ch;FILE *fp1,*fp2;fp1=fopen(av[1],"r");fp2=fopen(av[2],"w");while((ch=getc(fp1))!=EOF){putc(ch,fp2);}fclose(fp1);fclose(fp2);}
SMAPLE OUTPUT:
[root@localhost C_Program]# cc copy.c[root@localhost C_Program]# cat>aVel Tech Engineering College[root@localhost C_Program]# ./a.out a b[root@localhost C_Program]# cat bVel Tech Engineering College
-
7/31/2019 Cpl 2 Manual
57/62
RESULT:
Thus the C Program to implement copy command in UNIX was executed and the outputwas verified.
IMPLEMENTATION OF WC COMMAND
Ex. No.: 23
AIM:
To write a C Program to implement wc command in UNIX.
ALGORITHM:
Step 1: Start
Step 2: Initialize the variables c=0; w=0; l=0.
Step 3: Using If Else condition count the no. of characters, letters and words.
Step 4: Using string compare function, print the result with respect to the command given.
Step 5: Stop.
-
7/31/2019 Cpl 2 Manual
58/62
C PROGRAM:
#includemain(int a,char *av[3]){FILE *f;char ch;int w=0,l=0,c=0;if(a==2)f=fopen(av[1],"r");elsef=fopen(av[2],"r");while(!feof(f)){ch=getc(f);c=c++;if(ch==' '||ch=='\n')w=w+1;if(ch=='\n')l=l+1;}fclose(f);if(strcmp(av[1],"-c")==0)printf("\nThe no. of characters are %d\n",c-1);else if(strcmp(av[1],"-w")==0)printf("\nThe no. of words are %d\n",w);else if(strcmp(av[1],"-l")==0)printf("\nThe number of lines are %d\n",l);else if(av[2]=='\0')printf("\n%d\t%d\t%d\n",c-1,w,l);}
-
7/31/2019 Cpl 2 Manual
59/62
SAMPLE OUTPUT:
[root@localhost C_Program]# cc wc.c[root@localhost C_Program]# ./a.out msb77 10 7[root@localhost C_Program]# ./a.out -l msb
The number of lines are 7[root@localhost C_Program]# ./a.out -w msbThe no. of words are 10[root@localhost C_Program]# ./a.out -c msbThe no. of characters are 77
RESULT:
Thus the C Program to implement wc commad in UNIX was executed and the output wasverified.FORK SYSTEM CALL
Ex. No.: 24
AIM:
To write a C Program forFORK SYSTEM CALL
ALGORITHM:
Step 1: Start
Step 2: Open the inbuilt function fork() inside the header file, unistd.h and store it in f
Step 3: When f is equal to 0, print the Child process ID, Parent ID and the process ID, using inbuiltID functions.
Step 4: Stop.
-
7/31/2019 Cpl 2 Manual
60/62
C PROGRAM:
#include#includemain(){int f;f=fork();if(f==0){printf("\nChild Process ID: %d\n",getpid());printf("\nParent ID: %d\n",getppid());}else{printf("\nProcess ID: %d\n",getpid());printf("\nParent ID: %d\n",getppid());}}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc fork.c[root@localhost C_Program]# ./a.out
Child Process ID: 3844
Parent ID: 3843
Process ID: 3843
-
7/31/2019 Cpl 2 Manual
61/62
Parent ID: 3673
RESULT:
Thus the C Program for Fork System Call in UNIX was executed and the output was
verified.
EXECL SYSTEM CALL
Ex. No.: 25Date: 3rd May, 2006
AIM:
To write a C Program for EXECL System Call in UNIX
ALGORITHM:
Step 1: Start
Step 2: Give the command for Execl, to print the Date and Time.
Step 3: Stop
C PROGRAM:
#include#includemain(){execl("/bin/date","date",0);}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc execl.c[root@localhost C_Program]# ./a.outTue May 2 18:01:37 IST 2006[root@localhost C_Program]#
-
7/31/2019 Cpl 2 Manual
62/62
RESULT:
Thus the C Program for EXECL System Call in UNIX was executed and the output wasverified.