185253 COMPUTER PRACTICE LAB-II

TABLE OF CONTENTS

S.NO.

NAME OF THE EXPERIMENT

PAGE NO.

1

STUDY OF UNIX OPERATING SYSTEM

2

VI EDITOR

3

BASIC UNIXCOMMANDS

4

SHELL PROGRAM ARITHMETIC OPERATION USING SHELL PROGRAM

5

GREATEST OF THREE NUMBERS

6

CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT

2

8

8.a. SWAPPING OF VALUES (Using 3rd Variable) 8.b. SWAPPING OF VALUES (Without using 3rd Variable)

9

NUMBER OF DIGITS IN AN INTEGER

10

ARITHMETIC OPERATIONS USING SHELL PROGRAMMING (Using SWITCH - CASE)

11

FACTORIAL OF A NUMBER

12

CHECKING FOR PRIME NUMBER

S.NO.

NAME OF THE EXPERIMENT

PAGE NO.

13

SUMS OF DIGITS OF AN INTEGER

C PROGRAMMING UNDER UNIX ENVIRONMENT

3

14 MATRIX ADDITION 15 SORTING OF INTEGERS 16 EMPLOYEE DETAILS USING STRUCTURES 17 FILE MANIPULATION 18 GREAEST AND SMALLEST NUMBER IN AN ARRAY 19 TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT 20 SYSTEM CALL FOR A READ ONLY FILE 21 SYSTEM CALL FOR WRITE-ONLY FILE 4 .

22 IMPLEMENTATION OF COPY COMMAND 23 IMPLEMENTATION OF WC COMMAND 24 FORK SYSTEM CALL 25 EXECL SYSTEM CALL 5 .

Unix stands for unplaced information computing system. 6 .No: 01 AIM: To understand the basic definitions and features of Unix operating system.STUDY OF UNIX OPERATING SYSTEM Ex.

UNIX SYSTEM I: It includes performance importance and enhanced process to process communication mostly one system III. VERSION: The first commercially licensed version. (ii) MULTI-TASKING: 7 . PWB/UNIX: Programmers were bench this version includes specialized utilities to managing software development/ UNIX SYSTEM III: The version includes as updated set of all the facilities of the passed two versions. It was the first Unix versions to be transported to a micro computer.1 Software development  BSD .4.VERSION OF UNIX: T&T Bell laboratories: The operating system originated at the Bell laboratories. UNIVERSITY OF CALIFORNIA:  BSD .3 (i) MULTIUSER: Unix is a multi-user OS which permits more than one user to utilize the computer simultaneously any one of the connected terminals.2  BSD .4.4.

TYPES OF USERS: User is the owner of the file 8 . FILE STRUCTURE: Unix has hierarchical file structure and imported free structure. 2. 1. PORTABLE: The application programs written for one system works on another system.The term task refers to a place initiated by the user to a process initiated by user Unix offer concept of classifying the task. 4. BACKGROUND TASK: It is the one whose the user can continue interaction with the system while one or more of the presently issued task if is still executing. 3. It maintains a list of users who are all allowed to access the system. MODULATOR: It means the various part of the Unix system can be adder on the reproved without editing performance of other component. SECURITY: Unix being a multi using operating system offers protection to user information from another. FOREGROUND TASK: It is the one where the user has to wait for the completion to present task before initiating the next task. 6. 5. The system also keep takes of that file and resources each user is authorized to renew user s name to be added to the list before they have an access to the system.

OTHERS: The other member who is neither the user nor the member of the group. KERNEL: Kernel has the direct interaction with the hardware. Unix is a communication operating system. GROUPS: The members of the group have the same group I.The owner of the file is the one who has created the file or the one to the ownership has been transferred by the creates of the file. VI EDITOR 9 . process management. security of file. SHELL: The shell is the Unix system command s inter plater. It controls functions like file management-1. RESULT: Thus the Unix environment has been studied. Communication between two computers at the remote location. Communication between two compare of some place which cannot use the same either same hardware as software through local area network.D. The two important shells are Bourse shell C shell 2. 1. scheduling management. but different user I. system. Communication between terminals connected to the computer. It is three level operating system.D.

No: 02 AIM: To understand the basic functions and features of the VI editor INSTRUCTIONS: VI EDITOR. THREE ASPECTS OF VI: VI is supplied with all Unix serpents VI can be used at other universities or any business with UNIX system. The VI editor has been Around for decade and is used daily by thousands of Linux and Unix users. Originally developed for Unix programmers. VI is a amount of memory allows efficient operation when the network is busy. DIFFERENT MODES IN VI EDITOR They are three different modes are available. VI uses standard alphabetic keys for commands. It is a model meaning.Ex.Visual editor VI stands for Visual The VI editor is the text editor. 10 . It is operating in the three different modes.

At this point.INSERT MODE Once we issue a VI editor insert append or open command we will be in VI insert mode. Command mode 2.Insert the word. Q . we can A. Insert task 3. other search and navigation commands the let you move around your file. In this mode we can issue many VI commands. Type text into our file B. Uses of the arrow keys we can easily move back to command mode by esc. 2. Cpy.1.Heading I . 11 . Last line or escape mode 1. including insert. append delete.Copying the current line. If we are working with a mode VI is typically configure to show the current mode of operation. COMMAND MODE When the first start editing a file with the VI editor we will be VI connected mode .

: q -to quit if you haven t left mode any edit.  After pressing the key. :wq -to quit and save edits. : w -to save our file but not quit VI. we will see a colon character appears at the beginning of the last line of our VI editor window and cursor will be moved to the position.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 command mode we get into this mode by pressing the colon key. 12 .

. Comes out of the directory arun [root@localhost root] Example: 13 . 1.RESULT: Thus the basic things about the editor and the 8 modes in VI editor had been studied. BASIC UNIXCOMMANDS Ex.No: 03 AIM: To execute and implement the basic Shell UNIX commands. DIRECTORY HANDLING COMMANDS AND THEIR OUTPUT: 1. [root@localhost root]# mkdir ram Creates a directory named ram 2. [root@localhost arun]# cd . [root@localhost root]# cd arun Enters into the directory arun Example: [ece@localhost ece]$ cd arun [ece@localhost arun]$ 3.

[root@localhost root]# pwd Shows the present working directory Example: [ece@localhost ece]$ pwd /home/ece 5.1 root -rw-r--r-.[root@localhost root]# ls -l List the permission of files / Directories total 4 -rw-r--r-.[ece@localhost ece]$ cd arun [ece@localhost arun]$ cd .[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 karthick karthik karti kat katheeja kathi 7.1 root root root 1093 Mar 11 2006 anaconda-ks.cfg 223 Mar 9 18:04 arith. [ece@localhost ece]$ 4..sh 14 .

-rw-r--r-. 3) [root@localhost root]# cat >> msb Appends the details in the file msb I'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 ece2 a b 15 .E ECE.1 root drwxr-xr-x 2 root root root 260 Mar 9 18:18 arm.sh 4096 Mar 9 19:24 barnabas 2. FILE HANDLING COMMANDS.E ECE. 1) [root@localhost root]# cat > msb Creates a New file msb I studying B. 2) [root@localhost root]# cat msb Opens the created file msb" I studying B.

of words 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 16 . 10 msb (iv)[root@localhost root]# wc -c msb Shows the no. words & characters. of lines alone. 2 10 65 msb (ii)[root@localhost root]# wc -l msb Shows the no.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 msb rm: remove regular file `msb'? y 8)(i) [root@localhost root]# wc msb Shows the no. of characters alone. 2 msb (iii)[root@localhost root]# wc -w msb Shows the no. of lines.

GENERAL PURPOSE COMMANDS. ece stands for electronic and conmmunication engineering 4.i am in ece dept [ece@localhost ece]$ cat ece9 cat: ece9: No such file or directory 10) [ece@localhost ece]$ find ece8 To find a file ece8 3. 1) [root@localhost root]# grep m msb Prints the sentence with the letter m My Name is sudha I'm studying in vel tech engineering college 2) [ece@localhost ece]$ egrep "are|our" yamuna1 to search for the multiple pattern we 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 the use of any regular expression. I ) SIMPLE COMMANDS. 1. [root@localhost root]# cal Prints the calendar if the present month March 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 17 . PATTERN SEARCHING COMMANDS.

... DESCRIPTION Copy SOURCE to DEST. [root@localhost root]# date Prints the current time..copy files and directories FSF CP(1) SYNOPSIS cp [OPTION].. or multiple SOURCE(s) to DIRECTORY. Thu Mar 9 19:26:27 IST 2006 3. SOURCE DEST cp [OPTION]..19 20 21 22 23 24 25 26 27 28 29 30 31 2. [root@localhost root]# time l Prints the System Run-time real 0m0.000s 4..000s sys 0m0. DIRECTORY cp [OPTION]. 18 .005s user 0m0. [root@localhost root]# man cal Opens manual on cal [ece@localhost ece]$ man cp CP(1) NAME cp . SOURCE.... --target-directory=DIRECTORY SOURCE.

06 Copyright 1991-1994. --archive same as -dpR --backup[=CONTROL] make a backup of each existing destination file -b : like --backup but does not accept an argument 5. 1998.This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1997.Mandatory arguments to long options are mandatory for short options too. -a. 2000 Free Software Foundation. Inc. 123 + 246 369 10 * 20 200 ii) STATUS INFORMATION COMMANDS 1. [root@localhost root]# bc Open the Binary Calculator bc 1. system boot Feb 23 23:02 19 . [ece@localhost ece]$ who b Displays the date & time the system was brought up.

[ece@localhost ece]$ ps a Display the status of all the process associated with u.0.0.0. [root@localhost root]# who am I Identifies the current working System root pts/0 Mar 9 19:21 (:0.0.0.0.135) Feb 23 23:04 (10.5) ece ece ece ece ece ece pts/1 pts/4 pts/6 pts/0 pts/3 pts/5 Feb 23 23:03 (10.36) 1998 (10. [ece@localhost ece]$ who u Restricts listing to users currently logged in.0.0.0.0.119) Feb 23 23:04 (10.137) consists of 3.0.0.terminal number and time fields. 1674 (10. Feb 23 23:05 . ece pts/2 Feb 23 23:03 00:02 1723 (10.10) 1993 (10.0.0.49) 2223 (10.0.0.0. Feb 23 23:04 .0.0.0) 5.0.5) ece ece ece ece ece ece pts/1 pts/4 pts/6 pts/0 pts/3 pts/5 Feb 23 23:03 00:02 Feb 23 23:03 .0.49) Feb 23 23:05 (10.0. ece pts/2 Feb 23 23:03 (10.10) Feb 23 23:03 (10.0.137) 4. [ece@localhost ece]$ who s Displays the short form listing which the username.0.0. Feb 23 23:04 00:01 Feb 23 23:04 .g options PID TTY TIME CMD 2311 pts/7 00:00:00 mail 20 .119) 2149 (10.0.0.135) 2101 (10.36) Feb 23 23:04 (10.2.0.

0. [ece@localhost ece]$ tty It will retun the pathname with your terminal /dev/pts/3 9.localdomain 2. [ece@localhost ece]$ ps l Lists the information using long format.4.20-6smp #1 SMP Thu Feb 27 09:59:40 EST 2003 i686 i686 i386 GNU/Linux 11. Login: ece Name: (null) Directory: /home/ece Shell: /bin/bash On since Tue Feb 23 23:03 (IST) on pts/2 from 10. [ece@localhost ece]$ ps g Display Inforamtion of all process of group loads specified in ggrplist PID TTY STAT TIME COMMAND 2152 pts/3 2344 pts/3 S R 0:00 -bash 0:00 ps g 7. F S UID PID PPID C PRI NI ADDR SZWCHAN TTY TIME CMD 4 S 502 2152 2149 0 75 0 . [ece@localhost ece]$ id To Display the Numerical value that corresponda to your login name. [ece@localhost ece]$ uname a To display the relevant details about the Operating System on the standard output.2312 pts/3 00:00:00 ps 6.1066 0 R 502 2348 2152 0 80 0 .0.10 21 .0.0.5 5 minutes 38 seconds idle On since Tue Feb 23 23:03 (IST) on pts/1 from 10.764 wait4 pts/3 00:00:00 bash pts/3 00:00:00 ps 8. Linux localhost. [ece@localhost ece]$ finger ece Gives you more Information about the User. uid=502(ece) gid=502(ece) groups=502(ece) 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.137 4 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 are currently logged in. Hai 2. [ece@localhost ece]$ mail ece To send the textual messages electrically transferred from one user to another. Subject: Hai. How are you.

22

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 ece@localhost.locald Tue Feb 23 23:06 18/720 "Hai." & Message 1: From ece@localhost.localdomain Tue Feb 23 23:06:23 2010 Date: Tue, 23 Feb 2010 23:06:22 +0530 From: ece@localhost.localdomain To: ece@localhost.localdomain 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 user To: ece@localhost.localdomain ece@localhost.localdomain Subject: Re: Hai. Thanks Cc: & cse Unknown command: "cse" c)& exit Exit from the mail d)&type Print the mail message e)&delete Delete from the mail box

23

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 [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 of a command to a file. [ece@localhost ece]$ cat<department

24

1 ece -rwx-----1 ece To list files & Directories in long format ece ece ece 30 Feb 9 23:47 lop 30 Feb 9 23:43 pal 65 Feb 23 23:16 science -rw-rw-r-.CSE EEE [ece@localhost ece]$ 4.1 ece 2.[ece@localhost ram]$ ls l total 3 -rw-rw-r-. [ece@localhost ece]$ < > To redirect the output and input command to a file [ece@localhost ece]$ cat<department>dnames [ece@localhost ece]$ cat dnames CSE EEE [ece@localhost ece]$ II ) SECURITY COMMANDS 1.1 ece ece 30 Feb 9 23:47 lop 25 . 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-.

g+w science (g)denotes group accessibility [ece@localhost ram]$ ls -l total 3 -rw-rw-r-.1 ece -rwx--------------1 ece 1 ece ece ece ece (=)denotes assigning absolutely 30 Feb 9 23:47 lop 30 Feb 9 23:43 pal 65 Feb 23 23:16 science 5) [ece@localhost ram]$ chmod g+r.1 ece ece 30 Feb 9 23:47 lop 26 .-rwx--------------- 1 ece 1 ece ece ece 30 Feb 9 23:43 pal 65 Feb 23 23:16 science shows Missing Permissions 3)[ece@localhost ram]$ chmod u+r.u+w science (+) denotes assigning Permissions (w) denotes writing permissions (r)denotes reading permissions [ece@localhost ram]$ ls -l total 3 -rw-rw-r-.1 ece -rwx-----1 ece ece ece 30 Feb 9 23:47 lop 30 Feb 9 23:43 pal 65 Feb 23 23:16 science the user now has -rw------1 ece ece reading writing permission 4) [ece@localhost ram]$ chmod u=x science [ece@localhost ram]$ ls -l total 3 -rw-rw-r-.

o+w science (o)denotes other user accessibility [ece@localhost ram]$ ls -l total 3 -rw-rw-r-.1 ece 7) [ece@localhost ram]$ chmod a+r.1 ece -rwx-----1 ece ece ece ece 30 Feb 9 23:47 lop 30 Feb 9 23:43 pal 65 Feb 23 23:16 science -r-xrwxrwx 1 ece III) WILD CARD PATTERNS 1.1 ece -rwx-----1 ece ece ece ece 30 Feb 9 23:47 lop 30 Feb 9 23:43 pal 65 Feb 23 23:16 science ----rw-rw. [ece@localhost raja]$ ls kanna mvj mvs student vikram vikram] [ece@localhost raja]$ ls m* 27 . [ece@localhost ansatz]$ ls m* To representing any number of characters used in the prefix or suffix.-rwx---------rw---- 1 ece 1 ece ece ece 30 Feb 9 23:43 pal 65 Feb 23 23:16 science the group has now permission to access 6) [ece@localhost ram]$ chmod o+r.a+x science (a)denotes all user accessibility [ece@localhost ram]$ ls -l total 3 -rw-rw-r-.

[ece@localhost raja]$ ls mv? To represent one character only either in the prefix or in the suffix 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.mvj: mvs: [ece@localhost raja]$ [ece@localhost raja]$ ls kanna mvj mvs student vikram vikram] 2. [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 28 .

0.0. [ece@localhost ece]$ who |wc The output of who is taken as the input for the wc and result is displayed.49) Feb 24 00:14 (10.0.1.0.119) Feb 24 00:25 (10.0.0.0.0.0.0.0.0.0.0.14) Feb 24 00:27 (10.0.0.120) Feb 24 00:24 (10.0.0.4) 29 .0.221) Feb 24 00:09 (10.0.34) Feb 24 00:22 (10.0. 9 54 426 2.0.0.0.137) Feb 24 00:19 (10. 16 [ece@localhost ece]$ cat names ece ece ece ece ece ece ece ece ece ece ece ece ece ece ece pts/4 pts/17 pts/10 pts/13 pts/1 pts/24 pts/3 pts/30 pts/7 pts/28 pts/20 pts/21 pts/31 pts/34 pts/32 Feb 23 23:56 (10.0.0.0.15) Feb 24 00:13 (10. [ece@localhost ece]$ who | tee names | wc l The Use of tee command is to store the intermediate results in a file.36) Feb 24 00:19 (10.6) Feb 24 00:17 (10.5) Feb 24 00:23 (10.0.0.0.3) Feb 24 00:25 (10.135) Feb 24 00:05 (10.180) Feb 24 00:14 (10.

[ece@localhost ece]$ tail -5 names To Display the Last Five lines from a File. (i)[root@localhost root]# sort msb1 Sorts in ascending order Aravind Balaji Dinesh Ganesh Kamalesh (ii) [root@localhost root]# sort -r msb1 Sorts in descending order Kamalesh 30 . jack arun prashant deepak naga [ece@localhost ece]$ 4.3. ajith superman spiderman goten vegetables 5. [ece@localhost ece]$ head -5 names To Display the First Five lines from a File.

[root@localhost root]# cut To cut the selected fields.File Creation date and along with the name of the file. [ece@localhost ece]$ pr g1 2010-02-25 02:38 echo ENTER THE NAME read n echo-n THE ENTERED NAME IS $n g1 Page 1 7. [ece@localhost ece]$ cat college diva vh2346 jan vh2345 kavi vh2387 naru vh2876 subi vh2498 [ece@localhost ece]$ cut -c2 college i a a 31 .[root@localhost root]# pr To display the Page number.Ganesh Dinesh Balaji Aravind 6.

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 college2 dhoni vh2378 divi vh2334 raina vh2397 raina vh2398 raj vh2270 raj vh2270 subi vh2498% jan vh2345 32 .

[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]$ join v1 college1 college2 dhoni vh2376 subi vh2498 jan vh2345 [ece@localhost ece]$ 10.9.[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]$ 33 .[root@localhost root]# join To extract the common lines from two sorted Files.

[cse@localhost cse]$ cat dept cse ece 34 .[root@localhost root]# nl Used to add Line Numbers to a File.[root@localhost root]# tr It is used to translate characters taken from the standard input. [cse@localhost cse]$ nl dept 1 cse 2 ece 3 eee 4 it 5 mech [cse@localhost cse]$ nl -v4 dept starts the numbering of each page by 4 4 cse 5 ece 6 eee 7 it 8 mech [cse@localhost cse]$ nl i3 dept increments number of lines by 3.11. 1 cse 4 ece 7 eee 10 it 13 mech 12.

eee it mech [cse@localhost cse]$ cat dept|tr "[a-z]" "[A-Z]" CSE ECE EEE IT MECH 13. 14.[root@localhost root]# more To Displays the output page by page [ece@localhost ece]$ ls | more 0 00 000123 003 0045 007 1 10 100 10001 101 35 .[root@localhost root]# pg It displays the output of a command on the screen page by page.

12 122 123 1234 1234567890 124 132 133 143 159 18 --More Press Enter to see the next page.

15.[root@localhost root]# cmp Compare any two files, including binary files. It takes two filenames as arguments. [ece@localhost ece]$ cat > college diva vh2346 jan vh2345 kavi vh2387 naru vh2876 subi vh2498 [ece@localhost ece]$ cat > college1 DHONI VH2876 RAINA VH2398 KAIF VH2657

36

HARI VH2765 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 vh2498 sachin vh2564 diva vh2346 [ece@localhost ece]$ diff -e college college1 1,5c dhoni vh2387 raina vh2389 subi vh2498

37

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 as unique to two files. [ece@localhost ece]$ comm -1 college college1 dhoni vh2387 raina vh2389 subi vh2498 sachin vh2564 diva vh2346 [ece@localhost ece]$ comm -13 college college1 dhoni vh2387

38

Division.raina vh2389 subi vh2498 sachin vh2564 diva 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. Multiplication.No: 04 AIM: To write a shell Program to do the Arithmetic Operations like Addition. 39 . Subtraction.

$b` 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: 40 . difference.ALGORITHM: Step 1: Start Step 2: Read the values of a and b Step 3: Write the expression to calculate sum. SHELL PROGRAM: echo "Enter Two Numbers" read a b c=`expr $a + $b` d=`expr $a . Quotient and Remainder. Product. Step 4: Print the Results Step 5: Stop.

ALGORITHM: Step 1: Start Step 2: Read the value of a .Enter Two Numbers 12 10 Sum = 22 Difference = 2 Product = 120 Quotient = 1 Remainder = 2 RESULT: Thus the shell Program to do arithmetic operation was executed without any errors. No.: 05 AIM: To write a shell program to find the greatest of three Numbers. 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 41 . GREATEST OF THREE NUMBERS Ex.

SHELL PROGRAM: echo "Enter Three Numbers" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "$a is Greater" elif [ $b -gt $c ] then echo "$b is Greater" else echo "$c is Greater" fi SAMPLE OUTPUT: Enter Three Numbers 12 14 16 16 is Greater 42 .Step 6: Then print B is Greater Step 7: Else Print C is greater Step 8: Stop.

num=num/10 Step 6: Close the While Loop 43 . z=y*y*y. sum=num+z.RESULT: Thus the program to find the greatest of three Numbers was executed and the output was verfied. Step 4: When the value of num is greater then 0. solve the following expressions Step 5: y = num/10. No.: 06 AIM: To check whether the given Number is Armstrong Number or not using Shell Programming. 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. CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT Ex.

Step 7: Check if x is equal to sum Step 8: Print the Result Step 9: Stop. SHELL PROGRAM: 44 .

echo "Enter a Number" read num x=$num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` z=`expr $y \* $y \* $y` sum=`expr $sum + $z` num=`expr $num / 10` done if [ $x -eq $sum ] then echo "$x is an armstrong Number" else echo "$x is not an armstrong Number Enter a Number 153 is an armstrong Number [root@localhost root]# sh arm.sh Enter a Number 123 123 is not an armstrong Number 45 .

ALGORITHM: Step 1: Start Step 2: Read the Value of n Step 3: Initialize i=2.: 07 AIM: To write a Shell Program to print the Fibonacci series.RESULT: Thus the Shell Program to check whether the given Number is Armstrong or not was executed and the output was verified. a=0. 46 . FIBONACCI SERIES Ex No. b=1.

Step 4: Print the Value of a and b Step 5: When i is lesser than n . 47 . 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.

SHELL PROGRAM: echo "Enter the Limit" read n i=2 echo "Fibonacci Series" echo "----------------" a=0 b=1 echo $a echo $b while [ $i -lt $n ] do. c=`expr $a + $b` echo $c a=$b b=$c 48 .

i=`expr $i + 1` done SAMPLE OUTPUT: Enter the Limit 8 Fibonacci Series ---------------0 1 1 2 3 5 8 13 49 .

: 08(a) AIM: To write a Shell Program to swap two values using 3rd variable. SWAPPING OF VALUES (Using 3rd Variable) Ex. SHELL PROGRAM: 50 . No. 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.RESULT: Thus the Shell Program to print the Fibonacci Series was executed and the output was verified.

echo "Enter Two Numbers" read a b temp=$a a=$b b=$temp echo $a $b SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12 RESULT: Thus the program to swap two values using 3rd variable was executed and the output was verified. SWAPPING OF VALUES 51 .

a=a + b.: 08(b) AIM: To write a Shell Program to swap two values without using 3rd Variable. No.(Without using 3rd Variable) Ex. 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 a=`expr $a + $b` 52 . b=a b. ALGORITHM: Step 1: Start Step 2: Read the values of a and b Step 3: Solve the expression.

b=`expr $a .$b` a=`expr $a .$b` echo $a $b SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12 53 .

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.: 09 AIM: To count the number of digits in an integer. 54 . solve n=a %10. using Shell Programming.RESULT: Thus the Shell Program to swap two values without using third variable was executed and the output was verified. No. NUMBER OF DIGITS IN AN INTEGER Ex.

Step 5: If the value of n is not equal to 0. SHELL PROGRAM: echo "Enter a Number" read a c=0 while [ $a -ne 0 ] do n=`expr $a % 10` c=`expr $c + 1` a=`expr $a / 10` done echo "The Number of Digits in the Integer is $c" SAMPLE OUTPUT: Enter a Number 123456789 The Number of Digits in the Integer is 9 RESULT: 55 . Step 10: Stop. 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.

ARITHMETIC OPERATIONS USING SHELL PROGRAMMING (Using SWITCH . 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 56 .Thus the Shell Program to calculate the number of digits of an integer was executed and the output was verified.CASE) Ex.

SHELL PROGRAM: echo "Enter Two Numbers" 57 .

$b`". 2) echo "The Difference between $a and $b is `expr $a . 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`"....read a b echo "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 n case "$n" in 1) echo "The Sum of $a and $b is `expr $a + $b`"..sh Enter Two Numbers 12 10 What do you want to do? (1 to 5) 1) Sum 2) Difference 58 . esac SAMPLE OUTPUT: [root@localhost Shell]# sh arith_switch. 5) echo "The Remainder of $a by $b is `expr $a % $b`"..

No. ALGORITHM: 59 .3) Product 4) Quotient 5) Remainder Enter your Choice 4 The Quotient of 12 by 10 is 1 RESULT: Thus the Shell program to perform arithmetic operations using Switch Case was executed and the output was verified. 11 AIM: To write a Shell Program to find the factorial of a Number. FACTORIAL OF A NUMBER Ex.

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. SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n .1` done echo "The Factorial of the given Number is $n" SAMPLE OUTPUT: 60 .1` p=1 while [ $i -ge 1 ] do n=`expr $n \* $i` i=`expr $i .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 7: Stop.

: 12 AIM: To write a Shell Program to check if the Number is a Prime Number or a Composite Number.[root@localhost Shell]# sh fact. No. CHECKING FOR PRIME NUMBER Ex. 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 61 .sh Enter a Number 5 The 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.

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 Step 9: If t is greater than 0. perform the following steps.Step 4: While i is greater than or equal to 2. print The Number is not a Prime Number Step 10: If the condition is not satisfied. print The Number is a Prime Number 62 .

1` t=0 while [ $i -ge 2 ] do p=`expr $n % $i` if [ $p -eq 0 ] then t=`expr $t + 1` fi i=`expr $i .1` done if [ $t -gt 0 ] then echo "The Number $n is not a Prime Number" 63 .SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n .

sh Enter a Number 4 The Number 4 is not a Prime Number RESULT: 64 .sh Enter a Number 2 The Number 2 is a Prime Number [root@localhost Shell]# sh prime.else echo "The Number $n is a Prime Number" fi SAMPLE OUTPUT: [root@localhost Shell]# sh prime.

Step 4: While the value of num is greater than 0. ALGORITHM: Step 1: Start Step 2: Read the Integer as num Step 3: Initialize the value of sum=0. 13 AIM: To write a Shell Program to calculate the sum of digits of an Integer. No. Step 5: Find the remainder of num by 10 and store in y Step 6: Add sum with y and store it in sum Step 7: Divide num by 10 and store the value in num Step 7: Close the while loop Step 8: Print the Result Step 9: Stop SHELL PROGRAM: echo "Enter a Number" 65 . SUMS OF DIGITS OF AN INTEGER Ex.Thus the Shell Program to check if the given number is prime or not. solve the following expressions. was executed and the output was verified.

sh Enter a Number 123456 The 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 and the output was verified.read num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` sum=`expr $sum + $y` num=`expr $num / 10` done echo "The Sum of the Digits of the Integer is $sum" SAMPLE OUTPUT: [root@localhost Shell]# sh digits_sum. C PROGRAMMING UNDER UNIX ENVIRONMENT MATRIX ADDITION 66 .

67 . 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. add[10][10]. m. j. No. 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. 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]. i.Ex.

m.C PROGRAM: #include<stdio.j.i.h> main() { int add[10][10]. scanf("%d%d".n.&n).a[10][10]. 68 . printf("\nEnter the Number of Rows and Columns:\n\n ").b[10][10].&m.

&a[i][j]). for(j=0.j<n.i++) { for(j=0.j<n.j++) add[i][j]=a[i][j]+b[i][j]. for(i=0. for(i=0.j++) scanf("%d". for(i=0. for(i=0.j<n.j<n.i<m.j++) scanf("%d". printf("Enter the 2nd Matrix: \n\n").&b[i][j]).printf("Enter the 1st Matrix: \n\n").i<n.i++) for(j=0.add[i][j]).i<m. } } } 69 .i<m. } printf("The Addition of the two matrices is: \n").i++) for(j=0.j++) { printf("%d\t".i++) { printf("\n\n").

SAMPLE OUTPUT: [root@localhost C_Program]# cc matadd./a.out Enter the Number of Rows and Columns: 2 2 Enter the 1st Matrix: 70 .c [root@localhost C_Program]# .

1 2 3 4 Enter the 2nd Matrix: 1 2 3 4 The Addition of the two matrices is: 2 4 6 8 71 .

No. SORTING OF INTEGERS Ex.RESULT: Thus the C Program to add two matrices under UNIX Environment was executed and the output was verified. get the n number of integers as a[] Step 4: Again using for loop check if the second number is greater than the first 72 . 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 .

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 5: If the above step is true. Step 8: Stop 73 .

n. printf("\nEnter the List of %d numbers:\n".n). x[j]=temp.i<n. scanf("%d".i++) 74 . } printf("\nThe Sorted list of Numbers is \n").i++) scanf("%d". for(i=0.h> main() { int x[50].i++) for(j=i+1.i. for(i=0.i<n.j++) if(x[i]>x[j]) { temp=x[i].C PROGRAM: #include<stdio. printf("How many Numbers ?\n"). for(i=0.temp.&x[i]).j<n.j.i<n-1.&n). x[i]=x[j].

out How many Numbers ? 5 Enter the List of 5 numbers: 4 8 3 6 2 The Sorted list of Numbers is 2 3 4 6 8 RESULT: Thus the C Program to sort the given array of integers under UNIX environment was executed and the output was verified.c [root@localhost C_Program]# .x[i]). EMPLOYEE DETAILS USING STRUCTURES 75 ./a.printf("%d\t". } SAMPLE OUTPUT: [root@localhost C_Program]# cc sort.

print the employee details Step 6: Stop 76 .: 16 AIM: To write a C Program to get and print the employee details using Structures under UNIX Platform..Ex. name and salary. Step 3: Inside the main function. No. get the number of employees Step 4: Using for loop. scan the employee details Step 5: Again using for loop. ALGORITHM: Step 1: Start Step 2: Initialize the structure with employee no.

C PROGRAM: #include<stdio.h> struct emp { char name[25]. 77 .

&a[i].: %d". int empno.i<n. printf("\n2. printf("\n2.int salary. } 78 . for(i=0. } for(i=0.i+1). }a[20]. printf("\n Enter the Number of Employees: "). scanf("%d".&a[i].i. printf("\n\n1.: ").Salary: %d".a[i].a[i].name).empno).i<n.empno). Ma in() { int n. printf("\n3.i++) { printf("\n\nEnter the %d Employee details".name).Employee No.salary).Employee Name: %s". scanf("%s".Name: "). scanf("%d".&n).i+1).Salary: ").&a[i]. scanf("%d".salary).i++) { printf("\n\nEmployee %d". printf("\n3. printf("\n1.Employee No.a[i].

out Enter the Number of Employees: 2 Enter the 1 Employee details 1.c [root@localhost C_Program]# .Employee No.} SAMPLE OUTPUT: [root@localhost C_Program]# cc employ./a.: 1234 79 .

: 1234 2.Employee Name: Ganesh 3.Salary: 12500 80 .Name: Ramesh 3.Name: Ganesh 3.2.Employee No.Employee No.Employee Name: Ramesh 3.Salary: 12500 Enter the 2 Employee details 1.Employee No.Salary: 12500 Employee 1 1.: 2314 2.Salary: 12500 Employee 2 1.: 2314 2.

C PROGRAM: #include<stdio. Read a file Copy a file Change the case of the entire file Count the number of words.: 17 AIM: To write a C Program to perform following file manipulation: 1. spaces and lines in a file. 4. 3.h> main() 81 .h> #include<string. 2. No. FILE MANIPULATION Ex.RESULT: Thus the C Program to get and print the employee details using Structures was executed and the output was verified.

printf("\n1. switch(choice) { case 1: while(!feof(fp1)) { ch=getc(fp1).tr. fseek(fp1.ct.Exit\n").Read the file\n2.letters=0.&choice). do { fp1=fopen("data1.lines=0.Copy the file\n3.txt". scanf("%d"."r"). case 2: fp2=fopen("data2.SEEK_SET). printf("\nEnter u'r Choice\n")."w").ch).0L.Change\n4. while(!feof(fp1)) { ct=fgetc(fp1). FILE *fp1. printf("%c".spaces=0.{ char ch. int choice. } break.txt".*fp2. 82 .words=0.Count\n5.

fp2=fopen("data2. case 3: fp2=fopen("data2. printf("%c". if((ch<91)&&(ch>=65)) { ch=ch+32. while(!feof(fp2)) { ch=fgetc(fp2). break."r").fp2).ch). fclose(fp2).ch). } else if((ch>=97)&&(ch<=122)) { 83 .tr=fputc(ct. printf("The Content of the copied file is "). } fclose(fp1). printf("%c".txt". while(!feof(fp2)) { ch=getc(fp2).txt". } fclose(fp2)."r").

"r"). } else { letters++.ch=ch-32.ch). while(!feof(fp2)) { ch=fgetc(fp2).txt". } else if (ch=='\n') { lines++. 84 . } } break. } else { printf("%c". printf("%c". if(ch==' ') { spaces++.ch). case 4: fp2=fopen("data2.

spaces+lines).} } printf("\nTotal lines: %d".lines). } SAMPLE OUTPUT: Enter u'r Choice 1.Change 4.Read the file 2.Count 5.Exit 1 Wisdom better than the Rubies. fclose(fp2).Copy the file 3.spaces). 85 . } } while(choice!=5). printf("\nTotal spaces: %d". printf("\nTotal words: %d". printf("\nTotal letters: %d". break.spaces+lines+words).

Exit 3 wISDOM BETTER THAN THE rUBIES.Change 4.Read the file 2. ?? Enter u'r Choice 1.Count 5.Read the file 2.Copy the file 3. ?? Enter u'r Choice 1.Copy the file 86 .Read the file 2.? Enter u'r Choice 1.Copy the file 3.Exit 2 The Content of the copied file is Wisdom better than the Rubies.Count 5.Change 4.

3.Exit 4 Total lines: 1 Total words: 5 Total spaces: 4 Total letters: 5 87 .Change 4.Count 5.

ALGORITHM: 88 . GREAEST AND SMALLEST NUMBER IN AN ARRAY Ex.RESULT: Thus the FILE MANIPULATION operation was performed and the output was verified.No.: 18 AIM: To write a C Program to find the greatest and smallest number in an array of integers.

Step 6: Swap the value of a[0] to temp .Step 1: Start Step 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. Step 7: Print the result Step 8: Stop 89 . sole the following conditions. perform the following swap conditions. a[j] to a[i] and a[i] to temp . Step 5: If the first digit is greater than the second.

i.j<n. for(i=0.i++) { for(j=i+1.n.h> main() { int a[100].i<n.&a[i]).n). printf("\nEnter %d numbers: \n".&n).PROGRAM: #include<stdio. for(i=0. scanf("%d".j++) 90 .j. printf("\nEnter the maximum limit: ").i<n.temp.i++) scanf("%d".

{ if(a[i]>a[j]) { temp=a[i].out Enter the maximum limit: 5 Enter 5 numbers: 9 6 2 10 15 The smallest number is 2 91 . } } } printf("\nThe smallest number is %d"./a. printf("\nThe biggest number is %d".c [root@localhost C_Program]# . } SAMPLE OUTPUT: [root@localhost C_Program]# cc maxmin.a[n-1]). a[i]=a[j]. a[j]=temp.a[0]).

TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT Ex. Solve b=(b*10)+a.: 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. Step 5: Store the remainder of n by 10 as a . solve the following expressions. and n=n/10 92 .The biggest number is 15 RESULT: Thus the C Program to find the greatest and smallest number in an array was executed and the output was verified. No.

print The number is a palindrome . print The number is not a palindrome .Step 6: If the value of c is equal to b . Step 8: Stop. 93 . Step 7: If the above condition is not satisfied.

b=0. n=n/10. c=n. printf("\nEnter a number: ").n. b=(b*10)+a. scanf("%d".h> main() { int a.c. } if(c==b) printf("\nThe number is a palindrome\n").PROGRAM: #include<stdio.&n). } 94 . while(n>=1) { a=n%10. else printf("\nThe number is not a palindrome\n").

:20 95 .SAMPLE OUTPUT: [root@localhost C_Program]# cc palin. No./a. SYSTEM CALL FOR A READ ONLY FILE Ex./a.out Enter a number: 12321 The number is a palindrome [root@localhost C_Program]# .out Enter a number: 123 The number is not a palindrome RESULT: Thus the C Program to check whether the number is a palindrome or not was executed and the output was verified.c [root@localhost C_Program]# .

Step 4: Read fd. 100 Step 5: Using for loop.h File control Options 96 .i and fd. Also initialize the string variable. buf. buf[i] Step 6: The data already stored in aa along with the garbage value will be printed as output. to avoid segmentation error. Header Files: fcntl. Step 7: Stop Note: A file aa with data must be created before the program is compiled and is run. buf[100]. print the character array. Step 3: Open the already created file aa as a read only and store its contents in fd.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.

h Standard symbolic constants and types.h> main() { int i.O_RDONLY).h> #include<fcntl. char buf[100].fd. 97 . fd=open("aa".h> #include<unistd.unistd. PROGRAM: #include<stdio.

out My name is Barnabas t B <@PB@.buf.X@&8Z@0 @ @c@TB 98 .i<100. } } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-r.i++) { printf("%c"./a.buf[i]).read(fd.100). for(i=0.c [root@localhost C_Program]# .

Step 3: Get the data form the user and store it as buf .RESULT: Thus the C Program for system call to open a read-only file was executed and the output was verified. ALGORITHM: Step 1: Start Step 2: Initialize the integer variables i and fd with a character variable buf[100] . 99 . No.: 21 AIM: To write a C Program to perform system call to open a write-only file. SYSTEM CALL FOR WRITE-ONLY FILE Ex.

Header Files: fcntl. open the file aa to find the garbage values stored in it.h File control Options unistd.h Standard symbolic constants and types. to avoid segmentation error. A file aa with data must be created before the program is compiled and is run.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. 100 .

/a.fd.O_WRONLY). printf("Give data: ").h> main() { int i.out Give data: I study in VEC [root@localhost C_Program]# cat aa 101 .h> #include<unistd. write(fd.buf.PROGRAM: #include<stdio. scanf("%s". fd=open("aa". } SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-w. char buf[100].c [root@localhost C_Program]# .sizeof(buf)).buf).h> #include<fcntl.

X@-8Z@0 @ @c@TBB RESULT: Thus the C Program to perform system call to opena write-only file was executed and the output was verified. IMPLEMENTATION OF COPY COMMAND 102 .IBe B B Bt <@PB@.

No. 103 . Step 5: Close all the files.Ex. Step 6: Stop. and store it in fp1 Step 3: Open a new file. Step 4: Copy all the characters one by one until the End of File. where the contents are to be copied and store it in fp2. ALGORITHM: Step 1: Start Step 2: Open the file to be copied.: 22 AIM: To write a C Program to implement copy command in UNIX.

h> main(int a.C PROGRAM: #include<stdio. char *av[2]) { char ch. 104 .

c [root@localhost C_Program]# cat>a Vel Tech Engineering College [root@localhost C_Program]# . fp1=fopen(av[1]./a. while((ch=getc(fp1))!=EOF) { putc(ch.*fp2.fp2). } fclose(fp1).FILE *fp1."w")."r"). } SMAPLE OUTPUT: [root@localhost C_Program]# cc copy. fclose(fp2). fp2=fopen(av[2].out a b [root@localhost C_Program]# cat b Vel Tech Engineering College 105 .

ALGORITHM: Step 1: Start 106 . No. IMPLEMENTATION OF WC COMMAND Ex.: 23 AIM: To write a C Program to implement wc command in UNIX.RESULT: Thus the C Program to implement copy command in UNIX was executed and the output was verified.

of characters. print the result with respect to the command given. Step 4: Using string compare function. w=0. l=0. 107 . Step 3: Using If Else condition count the no. letters and words.Step 2: Initialize the variables c=0. Step 5: Stop.

char ch."r").char *av[3]) { FILE *f. int w=0. if(ch==' '||ch=='\n') 108 .C PROGRAM: #include<stdio. else f=fopen(av[2]. if(a==2) f=fopen(av[1]. c=c++.h> main(int a."r").l=0.c=0. while(!feof(f)) { ch=getc(f).

else if(strcmp(av[1]. } fclose(f).c-1).out -l msb The number of lines are 7 [root@localhost C_Program]# .w). if(strcmp(av[1]./a. else if(strcmp(av[1]./a."-c")==0) printf("\nThe no.c-1./a. of words are 10 [root@localhost C_Program]# . of characters are 77 109 ./a. } SAMPLE OUTPUT: [root@localhost C_Program]# cc wc. of words are %d\n".c [root@localhost C_Program]# .w=w+1.l)."-w")==0) printf("\nThe no. else if(av[2]=='\0') printf("\n%d\t%d\t%d\n".l).w.out msb 77 10 7 [root@localhost C_Program]# . of characters are %d\n".out -c msb The no."-l")==0) printf("\nThe number of lines are %d\n". if(ch=='\n') l=l+1.out -w msb The no.

unistd.RESULT: Thus the C Program to implement wc commad in UNIX was executed and the output was verified. print the Child process ID. No. Parent ID and the process ID.h and store it in f Step 3: When f is equal to 0. 110 . using inbuilt ID functions. FORK SYSTEM CALL Ex.: 24 AIM: To write a C Program for FORK SYSTEM CALL ALGORITHM: Step 1: Start Step 2: Open the inbuilt function fork() inside the header file. Step 4: Stop.

111 .

h> main() { int f.getppid()).getppid()). } else { printf("\nProcess ID: %d\n".getpid()). printf("\nParent ID: %d\n".C PROGRAM: #include<stdio. f=fork().h> #include<unistd. printf("\nParent ID: %d\n".out 112 . } } SAMPLE OUTPUT: [root@localhost C_Program]# cc fork.c [root@localhost C_Program]# ./a. if(f==0) { printf("\nChild Process ID: %d\n".getpid()).

: 25 Date: 3rd May.Child Process ID: 3844 Parent ID: 3843 Process ID: 3843 Parent ID: 3673 RESULT: Thus the C Program for Fork System Call in UNIX was executed and the output was verified. No. 2006 113 . EXECL SYSTEM CALL Ex.

} SAMPLE OUTPUT: [root@localhost C_Program]# cc execl.AIM: To write a C Program for EXECL System Call in UNIX ALGORITHM: Step 1: Start Step 2: Give the command for Execl./a.h> main() { execl("/bin/date". to print the Date and Time.c [root@localhost C_Program]# . Step 3: Stop C PROGRAM: #include<stdio.h> #include<unistd.0).out Tue May 2 18:01:37 IST 2006 114 ."date".

[root@localhost C_Program]# RESULT: Thus the C Program for EXECL System Call in UNIX was executed and the output was verified. 115 .

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.