You are on page 1of 105

OPERATING SYSTEMS

Basic Unix Commands


LOGIN AND AUTHENTICATION
///////////////////// LOGIN//////////////////// FeroozKhanKM@Chintu:~$ login login: student Password: Session setup problem, abort. FeroozKhanKM@Chintu:~$ login -f login: student Password: Session setup problem, abort. FeroozKhanKM@Chintu:~$ login -n login: invalid option -- n usage: login *-fp+ *username+ FeroozKhanKM@Chintu:~$ login -h root login: -h for super-user only.

//////////////////////LOGOUT///////////////////// FeroozKhanKM@Chintu:~$ exit exit There are stopped jobs. /////////////////PASSWORD/////////////////////// FeroozKhanKM@Chintu:~$ passwd Changing password for user student. Changing password for student (current) UNIX password: New UNIX password: BAD PASSWORD: it is based on your username New UNIX password: *2++ Stopped passwd FeroozKhanKM@Chintu:~$ passwd a passwd: bad argument -a: unknown option FeroozKhanKM@Chintu:~$ passwd -d Only root can do that. FeroozKhanKM@Chintu:~$ passwd -l Only root can do that. FeroozKhanKM@Chintu:~$ passwd -h passwd: bad argument -h: unknown option FeroozKhanKM@Chintu:~$ passwd -r passwd: bad argument -r: unknown option

///////////////////kinit//////////////// FeroozKhanKM@Chintu:~$ kinit FeroozKhanKM@Chintu:~$ kinit -p FeroozKhanKM@Chintu:~$ kinit -s 10 FeroozKhanKM@Chintu:~$ FeroozKhanKM@Chintu:~$ kinit -s kinit: option requires an argument -- s FeroozKhanKM@Chintu:~$ kinit -v kinit(v5): No credentials cache found while validating credentials FeroozKhanKM@Chintu:~$ ////////////kdestroy//////////////// FeroozKhanKM@Chintu:~$ kdestroy kdestroy: No credentials cache found while destroying cache FeroozKhanKM@Chintu:~$ kdestroy -p kdestroy: invalid option -- p Usage: kdestroy *-5+ *-4+ *-q+ *-c cache_name+ -5 Kerberos 5 (available) -4 Kerberos 4 (available) (Default is Kerberos 5 and Kerberos 4) -q quiet mode -c specify name of credentials cache FeroozKhanKM@Chintu:~$ kdestroy -q kdestroy: No credentials cache found while destroying cache FeroozKhanKM@Chintu:~$ kdestroy -5 kdestroy: No credentials cache found while destroying cache

INFORMATION
///////////DATE/////////////

FeroozKhanKM@Chintu:~$ date Mon Dec 23 18:07:35 IST 2013 FeroozKhanKM@Chintu:~$ date -u Mon Dec 23 12:37:41 UTC 2013 FeroozKhanKM@Chintu:~$ date -d+"fri" Fri Dec 27 00:00:00 IST 2013 //////////HISTORY///////////// FeroozKhanKM@Chintu:~$ history 1 ping 2 ipconfig 3 lynx google.com 4 lynx google.com 5 terminal 6 bye 7 LISTPART 8 ls 9 clear 10 :qw 11 :qw 12 ynx google.com 13 lynx google.com 14 gedit elan 15 lynx google.com 16 lynx google.com 17 openmanager 18 datamanager 19 man available 20 ping

21 Datamanager 22 ping*-/

FeroozKhanKM@Chintu:~$ history -r FeroozKhanKM@Chintu:~$ history ! bash: history: !: numeric argument required FeroozKhanKM@Chintu:~$ history !10 history :qw bash: history: :qw: numeric argument required FeroozKhanKM@Chintu:~$ history !! history history :qw bash: history: history: numeric argument required FeroozKhanKM@Chintu:~$ FeroozKhanKM@Chintu:~$ history !?kdestroy?

////////////MAN///////////// FeroozKhanKM@Chintu:~$ man What manual page do you want? FeroozKhanKM@Chintu:~$ man No manual entry for FeroozKhanKM@Chintu:~$ man -k What manual page do you want?

FeroozKhanKM@Chintu:~$ man What manual page do you want? kdestroy FeroozKhanKM@Chintu:~$ man kdestroy *3++ Stopped man kdestroy FeroozKhanKM@Chintu:~$ man man

////////////INFO/////////////////// student@localhost ~+$ info *4++ Stopped info File: dir Node: Top This is the top of the INFO tree This (the Directory node) gives a menu of major topics. Typing "q" exits, "?" lists all Info commands, "d" returns here, "h" gives a primer for first-timers, "mEmacs<Return>" visits the Emacs topic, etc. In Emacs, you can click mouse button 2 on a menu item or cross reference to select it. * Menu: Texinfo documentation system * Pinfo: (pinfo). curses based lynx-style info browser. * Texinfo: (texinfo). The GNU documentation format. * info standalone: (info-stnd). Read Info documents without Emacs. * infokey: (info-stnd)Invoking infokey. Compile Info customizations. * install-info: (texinfo)Invoking install-info. Update info/dir entries. * makeinfo: (texinfo)Invoking makeinfo. Translate Texinfo source. * texi2dvi: (texinfo)Format with texi2dvi. Print Texinfo documents. * texi2pdf: (texinfo)PDF Output. PDF output for Texinfo. * texindex: (texinfo)Format with tex/texindex. Sort Texinfo index files.

FeroozKhanKM@Chintu:~$ info -d+"home" info: dir: No such file or directory

////////////////WHO//////////////// FeroozKhanKM@Chintu:~$ who student :0 2013-12-23 17:44 student pts/1 2013-12-23 17:45 (:0.0) FeroozKhanKM@Chintu:~$ who -b system boot 2013-12-23 17:43 FeroozKhanKM@Chintu:~$ who d 2013-12-23 17:43 2013-12-23 17:44

428 id=si term=0 exit=0 1767 id=l5 term=0 exit=0

FeroozKhanKM@Chintu:~$ who -m student pts/1 2013-12-23 17:45 (:0.0)

////////////////WHOAMI//////////////

FeroozKhanKM@Chintu:~$ whoami student ///////////////TOP///////////////////////

FeroozKhanKM@Chintu:~$ top -d"10" top - 18:18:38 up 36 min, 2 users, load average: 0.39, 0.24, 0.20 Tasks: 154 total, 3 running, 144 sleeping, 7 stopped, 0 zombie Cpu(s): 6.4%us, 3.0%sy, 0.0%ni, 48.7%id, 38.3%wa, 2.8%hi, 0.7%si, 0.0%st Mem: 2066012k total, 1000520k used, 1065492k free, 36528k buffers Swap: 4192924k total, 0k used, 4192924k free, 547436k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3657 student 15 0 216m 55m 20m S 10.3 2.8 0:06.08 firefox 3158 root 15 0 363m 16m 7844 S 5.0 0.8 0:13.49 Xorg 16 root 11 -5 0 0 0 R 3.8 0.0 0:02.06 kblockd/1 3435 student 15 0 95032 14m 9372 R 1.4 0.7 0:03.01 gnometerminal 3535 student 15 0 207m 67m 47m S 1.1 3.3 0:04.38 swriter.bin 9 root 10 -5 0 0 0 S 0.4 0.0 0:00.64 events/1 427 root 10 -5 0 0 0 D 0.4 0.0 0:02.63 kjournald 5 root RT -5 0 0 0 S 0.3 0.0 0:00.32 migration/1 3316 student 15 0 17708 8404 6104 S 0.3 0.4 0:01.09 metacity 2679 root 18 0 1976 632 556 S 0.1 0.0 0:00.73 hald-addon-stor 3420 student 15 0 90344 10m 7868 S 0.1 0.5 0:00.12 mixer_applet2 3726 student 15 0 2336 1060 804 R 0.1 0.1 0:00.02 top 1 root 15 0 2072 624 540 S 0.0 0.0 0:00.45 init 2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0 3 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0 4 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/0 6 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/1 7 root RT -5 0 0 0 S 0.0 0.0 0:00.00 watchdog/1 8 root 10 -5 0 0 0 S 0.0 0.0 0:00.01 events/0 10 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 khelper 11 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kthread 15 root 10 -5 0 0 0 S 0.0 0.0 0:00.07 kblockd/0 17 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 kacpid 125 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/0 126 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 cqueue/1 129 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 khubd 131 root 10 -5 0 0 0 S 0.0 0.0 0:00.00 kseriod 200 root 15 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd 201 root 19 0 0 0 0 S 0.0 0.0 0:00.00 pdflush 202 root 15 0 0 0 0 S 0.0 0.0 0:00.05 pdflush 203 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 kswapd0 204 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 aio/0 205 root 14 -5 0 0 0 S 0.0 0.0 0:00.00 aio/1 371 root 11 -5 0 0 0 S 0.0 0.0 0:00.00 kpsmoused

402 root 403 root 404 root 414 root 458 root

14 15 14 15 10

-5 -5 -5 -5 -5

0 0 0 0 0

0 0 0 0 0

0S 0S 0S 0S 0S

0.0 0.0 0.0 0.0 0.0

0.0 0.0 0.0 0.0 0.0

0:00.00 ata/0 0:00.00 ata/1 0:00.00 ata_aux 0:00.00 kstriped 0:00.00 kauditd

FeroozKhanKM@Chintu:~$ top -p+"1" top - 18:21:47 up 39 min, 2 users, load average: 0.35, 0.31, 0.23 Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie Cpu(s): 1.2%us, 0.2%sy, 0.0%ni, 98.3%id, 0.2%wa, 0.0%hi, 0.2%si, 0.0%st Mem: 2066012k total, 994916k used, 1071096k free, 36724k buffers Swap: 4192924k total, 0k used, 4192924k free, 547792k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 15 0 2072 624 540 S 0.0 0.0 0:00.45 init

FeroozKhanKM@Chintu:~$ top h top: procps version 3.2.7

/////////////////UPTIME////////////////

FeroozKhanKM@Chintu:~$ uptime 18:22:56 up 40 min, 2 users, load average: 0.29, 0.32, 0.24 FeroozKhanKM@Chintu:~$ uptime -v usage: uptime *-V+ -V display version

FILE MANAGEMENT:
/////////////////////CAT//////////////// FeroozKhanKM@Chintu:~$ cat fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ cat fer.c n 1 Sample Document 2 3 Commands FeroozKhanKM@Chintu:~$ cat fer.c b 1 Sample Document 2 Commands FeroozKhanKM@Chintu:~$ cat fer.c -u FeroozKhanKM@Chintu:~$

//////////////COPY///////////////

FeroozKhanKM@Chintu:~$ cp fer.c fer1.c FeroozKhanKM@Chintu:~$ cp fer.c fer1.c -i cp: overwrite `fer1.c'? y FeroozKhanKM@Chintu:~$ cp fer.c fer1.c -l cp: cannot create link `fer1.c': File exists FeroozKhanKM@Chintu:~$ cp fer.c fer2.c FeroozKhanKM@Chintu:~$ cp fer.c fer2.c -l

cp: cannot create link `fer2.c': File exists FeroozKhanKM@Chintu:~$ cp fer.c fer3.c -l FeroozKhanKM@Chintu:~$

//////////////////MOVE OR RENAME/////////

FeroozKhanKM@Chintu:~$ mv fer.c fer1.c FeroozKhanKM@Chintu:~$ mv fer.c fer1.c -f mv: cannot stat `fer.c': No such file or directory FeroozKhanKM@Chintu:~$ mv fer1.c fer.c FeroozKhanKM@Chintu:~$ mv fer.c fer1.c -f FeroozKhanKM@Chintu:~$ mv fer.c fer1.c -i mv: cannot stat `fer.c': No such file or directory FeroozKhanKM@Chintu:~$ mv fer1.c fer.c FeroozKhanKM@Chintu:~$ mv fer.c fer1.c -i

///////////////////////LIST/////////////////// FeroozKhanKM@Chintu:~$ ls Desktop ex1.html ex1.html~ ex2.html ex2.html~ feed fer1.c fer2.c fer3.c Unsaved Document 1 Unsaved Document 1~

FeroozKhanKM@Chintu:~$ ls -a . .bash_profile .dmrc .gtkrc-1.2-gnome2 .mozilla .evolution ex2.html~ fer3.c .recently-used.xbel .Trash .gnome .zshrc

.. .bashrc .eggcups ex1.html feed .fontconfig .gnome2 .ICEauthority .nautilus .redhat Unsaved Document 1 .bash_history .config .gnome2_private .kde Document 1~ .emacs ex1.html~ fer1.c .openoffice.org .ssh .gconf Unsaved

.bash_logout Desktop .esd_auth ex2.html fer2.c .gstreamer-0.10 .metacity .qt .thumbnails

.gconfd .xsession-errors

FeroozKhanKM@Chintu:~$ ls -A .bash_history .config .emacs ex1.html~ fer1.c .gnome2_private .kde .openoffice.org .ssh Document 1~ .bash_logout Desktop .esd_auth ex2.html fer2.c .gstreamer-0.10 .metacity .qt .thumbnails .gconf Unsaved

.gconfd .xsession-errors

.bash_profile .dmrc .evolution ex2.html~ fer3.c .gnome .gtkrc-1.2gnome2 .mozilla .recently-used.xbel .Trash .zshrc .bashrc .eggcups ex1.html feed .fontconfig .gnome2 .ICEauthority .nautilus .redhat Unsaved Document 1

FeroozKhanKM@Chintu:~$ ls -t fer2.c Desktop fer1.c fer3.c feed Unsaved Document 1 Unsaved Document 1~ ex2.html ex2.html~ ex1.html ex1.html~

////////////////////REMOVE//////////////// FeroozKhanKM@Chintu:~$ rm fer1.c FeroozKhanKM@Chintu:~$ rm -f FeroozKhanKM@Chintu:~$ rm fer1.c -f FeroozKhanKM@Chintu:~$ rm fer2.c -f FeroozKhanKM@Chintu:~$ rm fer3.c -i rm: remove regular file `fer3.c'? y

///////////////////LINK////////////////////

FeroozKhanKM@Chintu:~$ ln -f fer.c fer1.c ln: accessing `fer.c': No such file or directory FeroozKhanKM@Chintu:~$ ln -s fer.c fer1.c ln: creating symbolic link `fer1.c' to `fer.c': File exists

////////////////CHMOD////////////////// [student@localhos ~]$ chmod 4 fer.c [student@localhost jaya]$ cat fer.c cat: os2: PermiFeroozKhanKM@Chintu:~$ ln -f fer.c fer1.c FeroozKhanKM@Chintu:~$ ln -n fer.c fer1.c ln: creating hard link `fer1.c' to `fer.c': File exists ssion denied

[student@localhost jaya]$ chmod 777 fer.c [student@localhost jaya]$ cat fer.c Sample Documents Commands FeroozKhanKM@Chintu:~$ chmod 7 fer.c [student@localhost jaya]$ cat fer.c Sample Documents Commands ////////////////////FIND//////////////

FeroozKhanKM@Chintu:~$ find fer.c fer.c FeroozKhanKM@Chintu:~$ find *.c fer1.c FeroozKhanKM@Chintu:~$ find -atime 10 fer,c find: paths must precede expression Usage: find *-H+ *-L+ *-P+ *path...+ *expression+ FeroozKhanKM@Chintu:~$ find -atime 10 fer.c find: paths must precede expression Usage: find *-H+ *-L+ *-P+ *path...+ *expression+ FeroozKhanKM@Chintu:~$ find fer.c -atime 10 FeroozKhanKM@Chintu:~$ find fer.c -atime 0 fer.c FeroozKhanKM@Chintu:~$ find fer.c -ctime 0 fer.c FeroozKhanKM@Chintu:~$ find fer.c -ctime 1

/////////////gzip/////////////////////

gzip: fer.c has 1 other link -- unchanged FeroozKhanKM@Chintu:~$ gzip fer1.c gzip: fer1.c has 1 other link -- unchanged FeroozKhanKM@Chintu:~$ gzip fer.c gzip: fer.c has 1 other link -- unchanged FeroozKhanKM@Chintu:~$ gzip ex11.html gzip: ex11.html: No such file or directory FeroozKhanKM@Chintu:~$ gzip ex1.html FeroozKhanKM@Chintu:~$ gzip -a ex1.html gzip: option --ascii ignored on this system gzip: ex1.html: No such file or directory FeroozKhanKM@Chintu:~$ gunzip ex1.html FeroozKhanKM@Chintu:~$ gzip -a ex1.html gzip: option --ascii ignored on this system FeroozKhanKM@Chintu:~$ gzip -d ex1.html FeroozKhanKM@Chintu:~$ gzip -f ex1.html FeroozKhanKM@Chintu:~$

TO DISPLAY THE CONTENTS OF THE FILE


//////////////MORE//////////// FeroozKhanKM@Chintu:~$ more fer. Sample Document Commands FeroozKhanKM@Chintu:~$more -c fer.c Sample Document Commands

FeroozKhanKM@Chintu:~$ more -1 fer.c Sample Document --More--(61%)

///////////////////HEAD///////////////////

FeroozKhanKM@Chintu:~$ head fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ head 1 fer.c head: cannot open `1' for reading: No such file or directory ==> fer.c <== Sample Document Commands

FeroozKhanKM@Chintu:~$ head -1 fer.c Sample Document

//////////////////////TAIL/////////////////// FeroozKhanKM@Chintu:~$ tail 1 fer.c tail: cannot open `1' for reading: No such file or directory ==> fer.c <== Sample Document Commands FeroozKhanKM@Chintu:~$ tail -1 fer.c Commands FeroozKhanKM@Chintu:~$ tail fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ tail -l fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ tail -b fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ tail -c 1 fer.c

//////////////////////grep////////////////

FeroozKhanKM@Chintu:~$ grep mm fer.c Commands FeroozKhanKM@Chintu:~$ grep -e mm fer.c Commands FeroozKhanKM@Chintu:~$ grep -A 1 am fer.c Sample Document

FeroozKhanKM@Chintu:~$ grep -A 2 am fer.c Sample Document Commands `

///////////////lpr//////////////// FeroozKhanKM@Chintu:~$ lpr -P root fer.c lpr: The printer or class was not found. FeroozKhanKM@Chintu:~$ lpr -#10 fer.c lpr: Error - no default destination available. student@localhost ~+$ lpr -P root -#10 fer.c lpr: The printer or class was not found. FeroozKhanKM@Chintu:~$ lpr -T root fer.c lpr: Error - no default destination available.

//////////////PRINT///////////// FeroozKhanKM@Chintu:~$ pr fer.c 2013-12-23 18:34 fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ pr -d fer.c 2013-12-23 18:34 fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ pr -F fer.c 2013-12-23 18:34 fer.c Sample Document Commands FeroozKhanKM@Chintu:~$ pr -m fer.c fer1.c 2013-12-23 19:19 Page 1 Sample Document Helooo World!!!!!!!!! Commands

Page 1

Page 1

Page 1

////////////////////////////////CMP///////////// FeroozKhanKM@Chintu:~$ cmp fer.c fer1.c fer.c fer1.c differ: byte 2, line 1 FeroozKhanKM@Chintu:~$ cmp -c fer.c fer1.c fer.c fer1.c differ: byte 2, line 1 is 141 a 163 s FeroozKhanKM@Chintu:~$ cmp -i 3 fer.c fer1.c fer.c fer1.c differ: byte 1, line 1 FeroozKhanKM@Chintu:~$ cmp -l fer.c fer1.c 2 141 163

3 155 144 4 160 141 5 154 155 6 145 160 7 40 154 8 104 145 9 157 40 10 143 104 11 165 157 12 155 143 13 145 165 14 156 155 15 164 145 16 12 156 17 12 164 18 103 12 19 157 12 20 155 103 21 155 157 22 141 155 23 156 155 24 144 141 25 163 156 26 12 144 cmp: EOF on fer.c

///////////DIFF////////////// FeroozKhanKM@Chintu:~$ diff f1 f2 1,19c1 < hey haii < this is our first lab <helloo <a <b <C

<d --> haiii

FeroozKhanKM@Chintu:~$ comp fer.c fer1.c bash: comp: command not found FeroozKhanKM@Chintu:~$ comm fer.c fer1.c Helooo World!!!!!!!!! Sample Document Commands FeroozKhanKM@Chintu:~$ comm -1 fer.c fer1.c Helooo World!!!!!!!!! FeroozKhanKM@Chintu:~$ comm -2 fer.c fer1.c Sample Document Commands FeroozKhanKM@Chintu:~$ comm -3 fer.c fer1.c Helooo World!!!!!!!!! Sample Document Commands

///////////////DIRECTORIES/////////////////////
TO MAKE A DIRECTORY FeroozKhanKM@Chintu:~$ mkdir Khan FeroozKhanKM@Chintu:~$ mkdir -p khan

FeroozKhanKM@Chintu:~$ mkdir -v khan mkdir: cannot create directory `khan': File exists FeroozKhanKM@Chintu:~$ mkdir -z khan mkdir: invalid option -- z Try `mkdir --help' for more information. FeroozKhanKM@Chintu:~$ mkdir -Z khan Sorry, cannot set default context to khan. FeroozKhanKM@Chintu:~$

TO CHANGE DIRECTORY: FeroozKhanKM@Chintu:~$ cd Khan *student@localhost Khan+$ cd .. FeroozKhanKM@Chintu:~$ cd /home/student/Khan *student@localhost Khan+$ cd .. FeroozKhanKM@Chintu:~$

TO REMOVE A DIRECTORY FeroozKhanKM@Chintu:~$ rmdir Khan FeroozKhanKM@Chintu:~$ rmdir --ignore-fail-on-non-empty Khan

rmdir: Khan: No such file or directory FeroozKhanKM@Chintu:~$ rmdir aaa rmdir: aaa: No such file or directory FeroozKhanKM@Chintu:~$ cd khan *student@localhost khan+$ rmdir aaa *student@localhost khan+$ d.. bash: d..: command not found *student@localhost khan+$ cd .. FeroozKhanKM@Chintu:~$ cd khan *student@localhost khan+$ rmdir -p aaa *student@localhost khan+$ FeroozKhanKM@Chintu:~$ rmdir -v khan rmdir: removing directory, khan FeroozKhanKM@Chintu:~$

TO PRINT THE WORKING DIRECTORY:

FeroozKhanKM@Chintu:~$ pwd /home/student

///////////EDITING AND FORMATTING UTILITIES//////////


Sed command: FeroozKhanKM@Chintu:~$ sed fer.c sed: -e expression #1, char 1: unknown command: `f'

FeroozKhanKM@Chintu:~$ sed Usage: sed *OPTION+... ,script-only-if-no-other-script- *input-file+... -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression add the script to the commands to be executed -f script-file, --file=script-filw add the contents of script-file to the commands to be executed -i*SUFFIX+, --in-place*=SUFFIX+ edit files in place (makes backup if extension supplied) -c, --copy use copy instead of rename when shuffling files in -i mode (avoids change of input file ownership) -l N, --line-lengt=N specify the desired line-wrap length for the `l' command --posix disable all GNU extensions.

-r, --regexp-extended use extended regular expressions in the script. -s, --separate consider files as separate rather than as a single continuous long stream. -u, --unbuffer load minimal amounts of data from the input files and flush the output buffers more --help display this help and exit --version output version information and exit

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read. E-mail bug reports to: bonzini@gnu.org . Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. FeroozKhanKM@Chintu:~$ sed -n fer.c sed: -e expression #1, char 1: unknown command: `f'

Emacs command: FeroozKhanKM@Chintu:~$ emacs fer.c FeroozKhanKM@Chintu:~$ emacs 1 fer.c FeroozKhanKM@Chintu:~$ emacs -q fer.c

Fmt command:

FeroozKhanKM@Chintu:~$ fmt fer.c Sample Document

Commands FeroozKhanKM@Chintu:~$ fmt -c fer.c Sample Document

Commands FeroozKhanKM@Chintu:~$ fmt -s fer.c Sample Document

Commands FeroozKhanKM@Chintu:~$ fmt -w 10 fer.c SampleDocument

Commands

fold command: FeroozKhanKM@Chintu:~$ fold fer.c Sample Document

Commands FeroozKhanKM@Chintu:~$ fold -b fer.c Sample Document

Commands FeroozKhanKM@Chintu:~$ fold -w 5 fer.c Sampl e Doc ument

Comma nds

vi command: FeroozKhanKM@Chintu:~$ vi fer.c

FeroozKhanKM@Chintu:~$ vi -s fer.c

FeroozKhanKM@Chintu:~$ vi -L Swap files found: In current directory: -- none -In directory ~/tmp: -- none -In directory /var/tmp: -- none -In directory /tmp: -- none --

FeroozKhanKM@Chintu:~$ vi -R fer.c FeroozKhanKM@Chintu:~$

Scp command:

FeroozKhanKM@Chintu:~$ scp -1 fer.c fer1.c FeroozKhanKM@Chintu:~$ scp -1 fer.c fer1.c FeroozKhanKM@Chintu:~$ scp -2 fer.c fer1.c FeroozKhanKM@Chintu:~$ scp -2 fer.c fer1.c FeroozKhanKM@Chintu:~$ scp -4 fer.c fer1.c FeroozKhanKM@Chintu:~$ scp -4 fer.c fer1.c FeroozKhanKM@Chintu:~$

Tee command: FeroozKhanKM@Chintu:~$ tee *6++ Stopped tee FeroozKhanKM@Chintu:~$ tee fer.c *7++ Stopped tee fer.c FeroozKhanKM@Chintu:~$ tee fer.c assdsas assdsas ffgfg ffgfg *8++ Stopped tee fer.c FeroozKhanKM@Chintu:~$ tee -a fer.c My name is Khan My name is Khan *9++ Stopped tee -a fer.c

FeroozKhanKM@Chintu:~$ tee -i fer.c sadasd sadasd sad sad sa sa *10++ Stopped tee -i fer.c

kill command: FeroozKhanKM@Chintu:~$ kill kill: usage: kill *-s sigspec | -n signum | -sigspec+ pid | jobspec ... or kill -l *sigspec+ FeroozKhanKM@Chintu:~$ kill -n bash: kill: -n: option requires an argument FeroozKhanKM@Chintu:~$ kill -n 1 kill: usage: kill *-s sigspec | -n signum | -sigspec+ pid | jobspec ... or kill -l *sigspec+ FeroozKhanKM@Chintu:~$ kill -p bash: kill: p: invalid signal specification FeroozKhanKM@Chintu:~$ kill -1 kill: usage: kill *-s sigspec | -n signum | -sigspec+ pid | jobspec ... or kill -l *sigspec+

SHELL SCRIPT
1) FACTORIAL OF A NUMBER
#!/bin/sh fact() { a=1 res=1 while [ $a -le $1 ] do res=`expr $res \* $a` a=`expr $a + 1` done echo "\aResult== $res" }

echo "Factorial of a number" echo "Enter a no: " read num fact $num

Output
FeroozKhanKM@Chintu:~$ vi factorial.sh FeroozKhanKM@Chintu:~$ sh factorial.sh

Factorial of a number Enter a no: 5 Result== 120

1) FIBONACCI SERIES
#!/bin/sh first=0 second=1 echo "ENTER HOW MANY NUMBERS" read num echo "FIBONACCI SERIES" echo "$first" echo "$second" count=2 while [ $count -le $num ] do num3=`expr $first + $second` echo "$num3" first=$second second=$num3 count=`expr $count + 1` done ~

OUTPUT:
FeroozKhanKM@Chintu:~$ vi fibo.sh FeroozKhanKM@Chintu:~$ sh fibo.sh ENTER HOW MANY NUMBERS 10 FIBONACCI SERIES 0 1 1 2 3 5 8 13 21 34 55

2) PRIMENO:
#!/bin/sh echo "Enter the starting range" read start echo "Enter the end range" read end i=$start j=2 while [ $i -le $end ] do { flag=0 temp=0 temp=`expr $i / 2` while [ $j -le $temp ] do { temp1=0 temp1=`expr $i % $j` if [ $temp1 -eq 0 ] then flag=1 fi j=`expr $j + 1` } done if [ $flag -eq 0 ] then echo "$i" fi j=2 i=`expr $i + 1` } done

Output
FeroozKhanKM@Chintu:~$ sh prime.sh Enter the starting range 1 Enter the end range 10 2 3 5 7

3) ARMSTRONG:
#!/bin/sh echo "Enter a num:" read num n=$num sum=0 while [ $n -ne 0 ] do dig=`expr $n % 10` temp=`expr $dig \* $dig \* $dig` sum=`expr $sum + $temp` n=`expr $n / 10` done if [ $sum -eq $num ] then echo "$num is an armstrong no" else echo "Not an armstrong no" fi

Output
FeroozKhanKM@Chintu:~$ sh armstrong.sh Enter a num: 153 153 is an armstrong no Enter a num: 152 Not an armstrong no

4) LARGEST OF THREE NOS


#!/bin/sh great() { if [ $1 -gt $2 ] then if [ $1 -gt $3 ] then echo "$1 is greater" fi elif [ $2 -gt $1 ] then if [ $2 -gt $3 ] then echo "$2 is greater" fi else echo "$3 is greater" fi }

echo "Enter first no:" read a echo "Enter second no:" read b echo "Enter third no:" read c great $a $b $c

Output
FeroozKhanKM@Chintu:~$ sh maxof3.sh Enter first no: 5

Enter second no: 25 Enter third no: 2 25 is greater

5) GCD:
#!bin/sh echo "Enter 2 nos" read a read b a[0]=$a a[1]=$b if [ $a -gt $b ] then min=$b else min=$a fi while [ $min -ne 0 ] do temp=`expr $a % $min` temp1=`expr $b % $min` if [ $temp -eq 0 ] then if [ $temp1 -eq 0 ] then gcd=$min break fi fi min=`expr $min - 1` done echo "GCD==$gcd"

Output
FeroozKhanKM@Chintu:~$ sh gcd.sh Enter 2 nos 4 5 GCD==20

6) LCM:
#!bin/sh echo "Enter 2 nos" read a read b a[0]=$a a[1]=$b if [ $a -gt $b ] then min=$b else min=$a fi while [ $min -ne 0 ] do temp=`expr $a % $min` temp1=`expr $b % $min` if [ $temp -eq 0 ] then if [ $temp1 -eq 0 ] then gcd=$min break fi fi min=`expr $min - 1` done

num=`expr $a \* $b` lcm=`expr $num / $gcd` echo "LCM== $lcm"

Output
FeroozKhanKM@Chintu:~$ lcm.sh
Enter 2 nos 4 2 LCM== 4

7) VOWELS:
#!/bin/sh echo "Enter a string:" read str st=$str l=$(echo $str | wc -m) echo $l size1=0 size=`expr $l - 1` size2=`expr $l - 1` i=0 while [ $size -ge 0 ] do b[$i]=${str:$size:1} i=`expr $i + 1` size=`expr $size - 1` done vow=0 while [ $size1 -le $size2 ] do ba=${b[$size1]} case $ba in 'a') echo "${b[$size1]}"

vow=`expr $vow + 1` ;;

'e') echo "${b[$size1]}" vow=`expr $vow + 1` ;; 'i') echo "${b[$size1]}" vow=`expr $vow + 1` ;; 'o') echo "${b[$size1]}" vow=`expr $vow + 1` ;; 'u') echo "${b[$size1]}" vow=`expr $vow + 1` ;; esac size1=`expr $size1 + 1`

done echo "No of vowels== $vow"

Output
FeroozKhanKM@Chintu:~$ sh vowels.sh Enter a string ferooz e o o No of vowels== 3

8) PALINDROME:
#!/bin/sh echo "Enter a string:" read str st=$str l=$(echo $str | wc -m) echo $l size1=0 size=`expr $l - 1` size2=`expr $l - 1` i=0 while [ $size -ge 0 ] do b[$i]=${str:$size:1} i=`expr $i + 1` size=`expr $size - 1` done echo ${b[*]} s=1 v=0 while [ $size1 -le $l ] do c=${st:$size1:1} x=$c y=${b[$s]} echo "$x $y" if [ $x = $y ] then flag=0 else flag=1 break fi size1=`expr $size1 + 1` s=`expr $s + 1` done if [ $flag -eq 0 ] then echo "Palin:" else

echo " NOt palin" fi

Output
FeroozKhanKM@Chintu:~$ sh palin.sh Enter a string: ferooz ferooz Not palin Enter a string: madam madam Palin:

10) STUUDENT MARK ANALYSIS


#!/bin/sh echo " ENTER THE NO OF SUBJECTS " read n i=0 while [ $i -lt $n ] do read a[$i] i=`expr $i + 1` done echo " THE MARKS ENTERED ARE " j=0 while [ $j -lt $n ] do echo "${a[$j]}" j=`expr $j + 1` done k=0 total=0 tot=0 while [ $k -lt $n ] do if [ ${a[$k]} -ge 91 ] then ch='s' elif [ ${a[$k]} -ge 81 -a ${a[$k]} -le 90 ] then

ch='a' elif [ ${a[$k]} -ge 71 -a ${a[$k]} -le 80 ] then ch='b' elif [ ${a[$k]} -ge 71 -a ${a[$k]} -le 80 ] then ch='c' elif [ ${a[$k]} -ge 61 -a ${a[$k]} -le 70 ] then ch='d' else [ ${a[$k]} -ge 51 -a ${a[$k]} -le 60 ] ch='e' fi total=`expr $total \+ ${a[$k]}` echo " subject `expr $k + 1` grade $ch " case $ch in 's') tot=`expr $tot + 30` ;; 'a') tot=`expr $tot + 27` ;; 'b') tot=`expr $tot + 24` ;; 'c') tot=`expr $tot + 21` ;; 'd') tot=`expr $tot + 18` ;; 'e') tot=`expr $tot + 15` ;; esac k=`expr $k + 1` done echo " THE TOTAL MARKS OBTAINED IS $tot " grade=`expr $n \* 3` m=`expr $tot \/ $grade` echo " THE GPA IS $m "

OUTPUT:
FeroozKhanKM@Chintu:~$ vi student1.sh FeroozKhanKM@Chintu:~$ sh student1.sh

ENTER THE NO OF SUBJECTS 3 78 78 78 THE MARKS ENTERED ARE 78 78 78 subject 1 grade b subject 2 grade b subject 3 grade b THE TOTAL MARKS OBTAINED IS 72 THE GPA IS 8

11) QUADRATIC EQUATION


#!/bin/sh echo " ENTER THE COEFFICIENT OF X^2" read a echo " ENTER THE COEFFICIENT OF X" read b echo " ENTER THE CONSTANT " read c sum=`expr $b \* $b` sum1=`expr 4 \* $a \* $c` res=`expr $sum \- $sum1` if [ $sum -gt $sum1 ] then num=`expr $sum \- $sum1` sq=$(echo "scale=2;sqrt($num)" | bc) echo " $sq" sam=-1 bb=`expr $sam \* $b` x1=`expr $bb \- $sq` x2=`expr $bb \+ $sq` sam1=2 de=`expr $sam1 \* $a` r1=`expr $x1 \/ $de` r2=`expr $x2 \/ $de` echo " THE TWO ROOTS ARE $r1 $r2 " elif [ $res -eq 0 ] then sam=-1 bb=`expr $sam \* $b` sam1=2

de=`expr $sam1 \* $a` x3=`expr $bb \/ $de` echo " THE ROOTS ARE $x3 $x3" else echo "THE ROOTS ARE IMAGINARY " fi

OUTPUT:
FeroozKhanKM@Chintu:~$ sh qua.sh ENTER THE COEFFICIENT OF X^2 1 ENTER THE COEFFICIENT OF X 5 ENTER THE CONSTANT 6 1 THE TWO ROOTS ARE -3 -2 FeroozKhanKM@Chintu:~$ sh qua.sh ENTER THE COEFFICIENT OF X^2 1 ENTER THE COEFFICIENT OF X 4 ENTER THE CONSTANT 4 THE ROOTS ARE -2 -2 FeroozKhanKM@Chintu:~$ sh qua.sh ENTER THE COEFFICIENT OF X^2 1 ENTER THE COEFFICIENT OF X 5 ENTER THE CONSTANT 9 THE ROOTS ARE IMAGINARY

12) PERMUTATION
list=`echo {A..B}` c1=0 c2=0 for c1 in $list do for c2 in $list do echo $c1$c2 done done

OUTPUT:
FeroozKhanKM@Chintu:~$ sh permutation.sh AA AB BA BB

FILE COMMANDS GREP COMMAND:#include<iostream> #include<string.h> #include<fstream> #include<conio.h> using namespace std; main() , ifstream fptr,fptr1; char word*20+,c; cout<<"Enter the pattern to find..."; cin>>word; int i=0,line=1,found=0,j; int arr*20+; int ln=0;

fptr.open("a.txt");

while(!fptr.eof()) , c = fptr.get();

if( word*i+ == c ) i++; else i=0; if( c == '\n' ) line++;

if( i == strlen(word) )

, if(arr*ln+ != line) arr*ln+++=line; found++; -

fptr.close(); cout<<"\nNo of occurences=="<<found; cout<<"\nHIii"; cout<<"\nLine NOS\n"; for(j=0;j<ln;j++) cout<<arr*j+;

cout<<"\n"; //fptr.close(); fptr1.open("a.txt"); c=1; int n=1; i=0; char str*100+; while(!fptr1.eof()) , // cout<<"jii "; if(n == arr*i+) , cout<<"\n"; fptr1.getline(str,100); cout<<str; n++;i++; else ,

// cout<<"else"; fptr1.getline(str,100); n++; getch(); -

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/File Commands$ ./a.out Enter the pattern to find...aaa

No of occurences==5 Line NOS 2468 aaa aaa ccc sss hhh cvv ddd sss aaa ccc ddd aaa bbb aaa rrr

SED COMMAND:#include<iostream> #include<conio.h> #include<fstream> #include<string.h> using namespace std; main() , ifstream fptr,fptr1,fptr2; ofstream temp; char word*100+; int i=0,line=1,found=0,j; int arr*20+; int ln=0; char c,str*100+; int flag=0;

cout<<"\n Contents \n"; fptr1.open("a.txt"); while(!fptr1.eof()) , fptr1>>str; cout<<str; fptr1.get(c); if(c=='\n') cout<<"\n"; else cout<<" "; -

cout<<"\n\nEnter the pattern to find..."; cin>>word;

fptr.open("a.txt");

while(!fptr.eof()) , fptr>>str; fptr.get(c); if(strcmp(str,word)==0) , if(flag==0) , found++;flag=2;arr*ln+++=line; else , found++; -

if(c=='\n') , line++; flag=0; -

cout<<"\nNo of occurences=="<<found; cout<<"\nLine NOS\n"; for(j=0;j<ln;j++) cout<<arr*j+<<" ";

cout<<"\n";

cout<<"\nLines until first occurence of word\n"; ifstream fptr4; int n=1; i=0; fptr4.open("a.txt"); while(!fptr4.eof()) , if(n==arr*i+) , fptr4>>str; cout<<str; c=fptr4.get(); cout<<c; if(strcmp(word,str)==0) break; else , fptr4.getline(str,sizeof(str)); cout<<str; cout<<"\n"; n++; -

cout<<"\n\n\nLines after first occurence of word\n"; ifstream fptr5; n=1; flag=0; i=0; fptr5.open("a.txt"); while(!fptr5.eof()) , if(n==arr*i+) , fptr5>>str; if(strcmp(word,str)==0)

flag=1; if(flag) cout<<str; c=fptr5.get(); cout<<c;

else , fptr5.getline(str,sizeof(str)); n++; -

cout<<"\n\n\nLines between words\n"; ifstream fptr6; n=1; flag=0; i=0; fptr6.open("a.txt"); while(!fptr6.eof()) , if(n==arr*i+) , fptr6>>str; if(strcmp(word,str)==0) flag++; if(flag==1 || flag==2) cout<<str; c=fptr6.get(); cout<<c; if(flag==2) break; else , fptr6.getline(str,sizeof(str));

n++; fptr6.close(); cout<<"\n\nAfter deleting the lines that contain pattern\n"; ofstream temp3; ifstream fptr7; temp3.open("temp.txt"); fptr7.open("a.txt"); n=1; i=0; while(!fptr7.eof()) , if(n==arr*i+) , fptr7.getline(str,sizeof(str)); n++;i++; else , fptr7.getline(str,sizeof(str)); temp3<<str; temp3<<"\n"; n++; fptr7.close(); temp3.close(); ifstream temp4; ofstream temp5; temp4.open("temp.txt"); temp5.open("a.txt"); while(!temp4.eof()) , c=temp4.get(); temp5<<c; -

temp5.close(); ifstream fptr8; fptr8.open("a.txt"); while(!fptr8.eof()) , c=fptr8.get(); cout<<c; -

getch(); -

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/File Commands$ g++ seddd.cpp FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/File Commands$ ./a.out

Contents Hi My name is khan aaa aaa ccc sss hhh ggg ttt jjj cvv ddd sss aaa ttt ttt eee sss ccc ddd aaa bbb eee rrr ttt aaa rrr rrr

Enter the pattern to find...aaa

No of occurences==5 Line NOS 2468

Lines until first occurence of word Hi My name is khan aaa

Lines after first occurence of word aaa aaa ccc sss hhh ggg ttt jjj cvv ddd sss aaa ttt ttt eee sss ccc ddd aaa bbb eee rrr ttt aaa rrr rrr

Lines between words aaa aaa

After deleting the lines that contain pattern Hi My name is khan ggg ttt jjj ttt ttt eee sss eee rrr ttt

AWK COMMAND:#include<iostream> #include<conio.h> #include<fstream> #include<string.h> using namespace std; main() , ifstream fptr,fptr1,fptr2; ofstream temp; char word*100+; int i=0,line=1,found=0,j; int arr*20+; int ln=0; char c,str*100+; int flag=0;

cout<<"\n Contents \n"; fptr1.open("a.txt"); while(!fptr1.eof()) , fptr1>>str; cout<<str; fptr1.get(c); if(c=='\n') cout<<"\n"; else cout<<" "; -

cout<<"\n\nEnter the pattern to find..."; cin>>word;

fptr.open("a.txt");

while(!fptr.eof()) , fptr>>str; fptr.get(c); if(strcmp(str,word)==0) , if(flag==0) , found++;flag=2;arr*ln+++=line; else , found++; -

if(c=='\n') , line++; flag=0; -

cout<<"\nNo of occurences=="<<found; cout<<"\nLine NOS\n"; for(j=0;j<ln;j++) cout<<arr*j+<<" "; cout<<"\n"; char b*10+;

cout<<"enter the pattern that you want to replace with.."; cin>>b; temp.open("temp.txt"); fptr2.open("a.txt"); int n=1; i=0; while(!fptr2.eof()) , if(n==arr*i+) , fptr2>>str; // cout<<str; if(strcmp(word,str)==0) temp<<b; else temp<<str; fptr2.get(c); if(c==32) , temp<<c; else , n++;i++; temp<<c; -

else , fptr2.getline(str,sizeof(str)); temp<<str; temp<<"\n"; n++; -

temp.close(); fptr.close(); fptr1.close(); fptr2.close(); ifstream temp1; ofstream temp2; temp1.open("temp.txt"); temp2.open("a.txt"); while(!temp1.eof()) , c=temp1.get(); temp2<<c; -

temp2.close(); cout<<"\ncontents after replacing\n";

ifstream fptr3; fptr3.open("a.txt"); while(!fptr3.eof()) , c=fptr3.get(); cout<<c; -

cout<<"\nLines until first occurence of word\n"; ifstream fptr4; n=1; i=0; fptr4.open("a.txt"); while(!fptr4.eof()) , if(n==arr*i+) , fptr4>>str; cout<<str;

c=fptr4.get(); cout<<c; if(strcmp(b,str)==0) break; else , fptr4.getline(str,sizeof(str)); cout<<str; cout<<"\n"; n++; -

cout<<"\n\n\nLines after first occurence of word\n"; ifstream fptr5; n=1; flag=0; i=0; fptr5.open("a.txt"); while(!fptr5.eof()) , if(n==arr*i+) , fptr5>>str; if(strcmp(b,str)==0) flag=1; if(flag) cout<<str; c=fptr5.get(); cout<<c;

else , fptr5.getline(str,sizeof(str)); n++;

cout<<"\n\n\nLines between words\n"; ifstream fptr6; n=1; flag=0; i=0; fptr6.open("a.txt"); while(!fptr6.eof()) , if(n==arr*i+) , fptr6>>str; if(strcmp(b,str)==0) flag++; if(flag==1 || flag==2) cout<<str; c=fptr6.get(); cout<<c; if(flag==2) break; else , fptr6.getline(str,sizeof(str)); n++; fptr6.close(); cout<<"\n\nAfter deleting the lines that contain pattern\n"; ofstream temp3; ifstream fptr7; temp3.open("temp.txt"); fptr7.open("a.txt"); n=1; i=0;

while(!fptr7.eof()) , if(n==arr*i+) , fptr7.getline(str,sizeof(str)); n++;i++; else , fptr7.getline(str,sizeof(str)); temp3<<str; temp3<<"\n"; n++; fptr7.close(); temp3.close(); ifstream temp4; ofstream temp5; temp4.open("temp.txt"); temp5.open("a.txt"); while(!temp4.eof()) , c=temp4.get(); temp5<<c; -

temp5.close(); ifstream fptr8; fptr8.open("a.txt"); while(!fptr8.eof()) , c=fptr8.get(); cout<<c; -

int l; char s*100+; cout<<"\nenter the line no..";

cin>>l; cout<<"enter the string.."; cin>>s; n=1; i=0; ifstream ptr; ofstream temp6; //cout<<"jii"; ptr.open("a.txt"); temp6.open("temp.txt"); while(!ptr.eof()) , //cout<<"jiji"; if(n==l) , temp6<<s; temp6<<"\n"; n++; else , ptr.getline(str,sizeof(str)); temp6<<str; temp6<<"\n"; n++; temp6.close(); ifstream temp7; ofstream temp8; temp7.open("temp.txt"); temp8.open("a.txt"); while(!temp7.eof()) , c=temp7.get(); temp8<<c; -

temp8.close();

ifstream fptr9; fptr9.open("a.txt"); while(!fptr9.eof()) , c=fptr9.get(); cout<<c; getch(); -

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/File Commands$ g++ awk.cpp FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/File Commands$ ./a.out

Contents Hi My name is khan ggg ttt jjj ttt ttt eee sss eee rrr ttt

Enter the pattern to find...ttt No of occurences==4 Line NOS 234 enter the pattern that you want to replace with..abcd

contents after replacing Hi My name is khan ggg abcd jjj abcd abcd eee sss

eee rrr abcd

Lines until first occurence of word Hi My name is khan ggg abcd

Lines after first occurence of word abcd jjj abcd abcd eee sss eee rrr abcd Lines between words abcd jjj abcd

After deleting the lines that contain pattern Hi My name is khan

FILE SYSTEM CALLS


PROGRAM :
#include<stdio.h> #include<fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include<stdlib.h>

main() {

int ch; char name[50]; int fd,nd,np;//file descriptor char buffer[100]; char message[]="My name is Khan"; struct stat s,s1; do{ printf("1.Create\n2.Open\n3.lseek\n4.dup\n5.dup2\n6.link & unlink\n7.fstat\n8.chmod\n9.ioctl");

printf("Enter ur choice..");

scanf("%d",&ch);

switch(ch) { case 1: //CREATION printf("Enter ur file name..");

scanf("%s",name); fd = creat(name,S_IREAD | S_IWRITE); if (fd == -1) printf("Error in opening datafile.dat\n"); else { printf("%s opened for read/write access\n",name); printf("%s is currently empty\n",name); }

close(fd); break;

case 2: //OPENING OF FILE printf("Enter the name of the file.."); scanf("%s",name); fd = open(name,O_RDWR | O_EXCL); if (fd != -1) { printf("%s opened for read/write access\n",name); write(fd, message, sizeof(message)); close (fd); fd = open(name, S_IREAD | S_IWRITE); if (read(fd, buffer, sizeof(message)) == sizeof(message)) printf("\"%s\" was written to %s\n", buffer,name); else printf("*** error reading %s ***\n",name); close (fd); }

else printf("*** %s does not exists ***\n",name); break;

case 3: //lseek

printf("Enter the name of the file.."); scanf("%s",name);

fd = open(name, O_RDONLY); if ( fd != -1) { lseek(fd,2,SEEK_CUR); if (read(fd, buffer, sizeof(buffer))) printf("\n%s", buffer); else printf("Error Reading");

} else printf("File does not exists\n");

close(fd); break;

case 4: //dup

printf("Enter the name of the file.."); scanf("%s",name);

fd = open(name,O_RDONLY ); if (fd == -1) { perror("Error"); // exit (1); } close(1); dup(fd); //close(fd); printf("Hello, world!\n"); break;

case 5://dup2 printf("Enter the name of the file.."); scanf("%s",name); fd = open(name,O_RDWR);

printf(" New file discriptor is created\n");

dup2(fd,1);

//

printf(" New file discriptor is created\n");

printf("Written to old file discriptor"); close(fd); break;

case 6: //link //Linking file1.txt to file2.txt if(link("fer.txt","fer1.txt")==0) printf("Link created.\n"); if((unlink("fer1.txt"))==0) printf("Unlink executed.\n");

break;

case 7: stat("fer.txt",&s); printf("Enter the name of the file.."); scanf("%s",name); fd = open(name,O_RDWR); //Using fstat with file descriptor fstat(fd,&s1); printf("File size :\t %d bytes\n",s1.st_size); printf("No of links:\t %d \n",s1.st_nlink); printf("File number:\t %d\n",s1.st_ino); printf("File permission:\t"); printf((s1.st_mode & S_IRUSR)?"read/":"-/"); printf((s1.st_mode & S_IWUSR)?"write/":"-/"); printf((s1.st_mode & S_IXUSR)?"exec":"-"); break;

case 8: //chmod printf("\nchanging permission:\n");

chmod("fer.txt",0400); np=access("fer.txt",R_OK); if(np==0) printf("Readable.\n"); else printf("Not Readable.\n"); //close(np); np=access("fer.txt",W_OK); if(np==0) printf("Writable.\n"); else printf("Not writable.\n"); chmod("fer.txt",0700);

break;

case 9://ioctl np=ioctl("temp.txt",1); if(np==0) printf("Device control works.\n"); else printf("Error occured in control device.\n");

} }while(ch<=9);

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/System Calls$ ./a.out 1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..1 Enter ur file name..fer.txt fer.txt opened for read/write access fer.txt is currently empty 1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..2 Enter the name of the file..fer.txt

fer.txt opened for read/write access "My name is Khan" was written to fer.txt 1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..3 Enter the name of the file..fer.txt

name is Khan1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..6 Link created. Unlink executed. 1.Create 2.Open 3.lseek 4.dup 5.dup2

6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..8 changing permission: Readable. Not writable. 1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..7 Enter the name of the file..fer.txt File size : No of links: File number: 16 bytes 1 11691

File permission: read/write/exec1.Create 2.Open 3.lseek 4.dup 5.dup2 6.link & unlink 7.fstat 8.chmod 9.ioctlEnter ur choice..10

PROCESS RELATED SYSTEM CALLS


#include<stdio.h> #include<sys/wait.h> #include<stdlib.h> #include<unistd.h>

main() { int pid; int status , chpid; int childpid;

printf("\nGroup id:=%d\n",getgid()); printf("\nUser id:=%d\n",getuid()); printf("\nPID:=%d\n",getpid());

pid = fork();

wait(0); if(pid==-1) { perror("Fork Failed"); exit(EXIT_FAILURE); }

else if(pid==0)

printf("\nChild process with PID %d is running \n",getpid()); printf("\nPID of my parent is %d \n",getppid()); childpid=fork(); wait(0);

if(childpid==-1) { perror("\nFork Failed for inner child"); exit(EXIT_FAILURE); } else if(childpid==0) {

printf("\nInner child parent = %d \n Inner child process = %d",getppid(),getpid()); //printf("\nInner child process = %d",getpid());

printf("\nDate is :\n "); printf("--------------"); execl("/bin/date","date",NULL); printf("\n--------------\n"); printf("Killing Inner Child\n"); kill(childpid,0); }

else {

printf("\nInner parent waiting"); wait(0); }

// _exit(EXIT_SUCCESS); }

else { printf("\nparent process with PID %d is running \n",getpid()); chpid=wait(&status); printf("\nchild process with PID %d terminated with termination code %d",pid,status); }

printf("\nProcess with PID %d terminated\n",getpid()); }

Output
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/System Calls$ ./a.out

Group id:=1000

User id:=1000

PID:=9792

Child process with PID 9793 is running

PID of my parent is 9792

Inner child parent = 9793 Inner child process = 9794 Date is : Sun Apr 6 08:05:23 IST 2014

Inner parent waiting Process with PID 9793 terminated

parent process with PID 9792 is running

child process with PID 9793 terminated with termination code -1216486784 Process with PID 9792 terminated

INTERPROCESS COMMUNICATION
Using Pipes
#include<stdio.h> #include<unistd.h> #include<string.h>

main() { int p1[2],p2[2],p3[2],p4[2],p5[2]; int i=0,j=0; char str[20]; char split_str1[20]; char split_str2[20];

printf("\n Process 1: \n "); printf("Enter a string.."); scanf("%s",str);

pipe(p1); pipe(p2);

printf("ID of the parent process %d \n\n",getpid());

write(p1[1],str,sizeof(str)); write(p2[1],str,sizeof(str));

int child1 = fork();

wait(0); // printf("\nCreating new child"); if(child1 == 0) { printf("\nInside child 1"); printf("\nID of this child is %d:",getpid()); printf("\nparent of tis child is %d:",getppid());

printf("\n\nProcess 2:\n It splits the string");

read(p1[0],str,sizeof(str)); int length = strlen(str);

for(i=0;i<length/2;i++) split_str1[i]=str[i]; for(i=length/2;i<=length;i++) split_str2[j++]=str[i];

pipe(p3); pipe(p4);

write(p3[1],split_str1,sizeof(split_str1)); write(p4[1],split_str2,sizeof(split_str2));

int child2 = fork(); wait(0); // printf("Creating New child"); if(child2 == 0)

{ printf("\nInside child 2"); printf("\nID of this child is %d:",getpid()); printf("\nparent of tis child is %d:",getppid());

read(p3[0],split_str1,sizeof(split_str1)); // read(p4[0],split_str1,sizeof(split_str1));

printf("\n\nProcess 4:\n Sub string : %s\n",split_str1); printf("No of charac : %d",strlen(split_str1)); } else {

int child3 = fork(); // printf("\nCreating New child");

if(child3 == 0) { printf("\nInside child 3"); printf("\nID of this child is %d:",getpid()); printf("\nparent of tis child is %d:",getppid());

read(p4[0],split_str2,sizeof(split_str2));

printf("\n\nProcess 5:\n Sub string : %s\n",split_str2); printf("No of charac : %d",strlen(split_str2)); } else

wait(0);

} else { wait(0); char str1[20],str2[20]; int child4=fork();

if(child4 == 0) { printf("\nInside child 4"); printf("\nID of this child is %d:",getpid()); printf("\nparent of tis child is %d:",getppid());

read(p2[0],str,sizeof(str)); int l=0; for(i=strlen(str)-1;i>=0;i--) { str2[l++]=str[i]; } int k=0; for(i=0;i<strlen(str);i++) { if(str[i]==str2[i]) k++; else

continue; } if(k==strlen(str)) printf("\t PROCESS 3: the given string is palindrome\n"); else printf("\t PROCESS 3: the given string is not palindrome\n");

Output
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ ./a.out

Process 1: Enter a string..madam ID of the parent process 9824

Inside child 1 ID of this child is 9825: parent of tis child is 9824:

Process 2: It splits the string

Inside child 2 ID of this child is 9826: parent of tis child is 9825:

Process 4: Sub string : ma No of charac : 2 It splits the string Inside child 3 ID of this child is 9827: parent of tis child is 9825:

Process 5: Sub string : dam No of charac : 3 It splits the stringFeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ Inside child 4 ID of this child is 9828: parent of tis child is 1: PROCESS 3: the given string is palindrome

Using Shared Memory


Client:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> #include <stdlib.h> #define MAXSIZE 27

void error(char *s)

{ perror(s); exit(1); }

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

key = 6789; char ch;

if ((shmid = shmget(key, MAXSIZE, 0666)) < 0) error("Error in SHMID");

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) error("Error in SHM");

do { for (s = shm; *s != '\0'; s++) putchar(*s); putchar('\n'); //sleep(1000); printf("Do you want to recieve:"); scanf(" %c",&ch); }while(ch=='y');

*shm = '*';

exit(0); }

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

#define MAXSIZE

27

void error(char *s) { perror(s); exit(1); }

int main() { char c; int shmid; key_t key; char *shm, s[100]; char *str;

key = 6789;

shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666);

if ((shmid) < 0) error("Error in SHMID");

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) error("Error in SHM");

char ch;

printf("%d",shm); while(1) { str = shm; printf("\nEnter a string..."); scanf("%s",s);

int i=0; while(s[i]!='\0') { str[i]=s[i]; i++; } // printf("hii");

str[i]='\0'; // scanf("%c",ch); }

while (*shm != '*') sleep(1);

exit(0); }

Output:
Client:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ ./a.out fer Do you want to recieve:y fer Do you want to recieve:y ferooz Do you want to recieve:y khan Do you want to recieve:y world Do you want to recieve:y HEllowrold

Server:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ ./a.out -1217462272

Enter a string...ferooz

Enter a string...khan

Enter a string...heloooo world

Enter a string... Enter a string...HEllowrold

Using Message Queue


#include<stdio.h> #include<sys/ipc.h> #include<sys/msg.h> #include<string.h> main() { int pid,len,qid;

struct { long mtype; char mtext[10]; }msg[10];

printf("\n Interprocess Communication");

qid = msgget((key_t)13,IPC_CREAT| 0666); printf("\n Message queue is created \n");

if(qid == -1) { printf("Message queue is not created"); //exit(1); } int n; printf("\nthe value of qid is %d\n\n",qid);

printf("Enter no of inputs.."); scanf("%d",&n);

char str[100];int i; for( i=0;i<n;i++) { scanf("%s",str); strcpy(msg[i].mtext,str); msg[i].mtype=1; }

pid=fork();

printf("\nthe value of pid is %d\n",pid);

if(pid==0) {

for( i=0;i<n;i++) msgsnd(qid,&msg[i],strlen(msg[i].mtext),IPC_NOWAIT); printf("\nMessages are sent"); }

else if(pid) { wait(0); for( i=0;i<n;i++) { msgrcv(qid,&msg[i],strlen(msg[i].mtext),0,IPC_NOWAIT | MSG_NOERROR); printf("\n Message %d : %s",i+1,msg[i].mtext); } printf("\nMessages are received\n"); }

if(pid==-1) { printf("\n Error in creating a child\n"); //exit(1); }

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ cc msgpass.c FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab$ ./a.out

Interprocess Communication Message queue is created

the value of qid is 0

Enter no of inputs..5 fer khan chintu helloo world

the value of pid is 9971

the value of pid is 0

Messages are sent Message 1 : fer Message 2 : khan Message 3 : chintu Message 4 : helloo Message 5 : world Messages are received

SYNCHRONIZATION
PRODUCER-CONSUMER PROBLEM
#include<stdio.h> #include<pthread.h> #include<semaphore.h> int buf[5],f,r; sem_t mutex,full,empty; void *produce(void *arg) { int i; for(i=0;i<10;i++) { sem_wait(&empty); sem_wait(&mutex); printf("produced item is %d\n",i); buf[(++r)%5]=i; sleep(1); sem_post(&mutex); sem_post(&full); printf("No of items in stack : %u\n",full); } } void *consume(void *arg) { int item,i;

for(i=0;i<10;i++) { sem_wait(&full); printf("No of items in stack : %u\n",full); sem_wait(&mutex); item=buf[(++f)%5]; printf("consumed item is %d\n",item); sleep(1); sem_post(&mutex); sem_post(&empty); } } main() { pthread_t tid1,tid2; sem_init(&mutex,0,1); sem_init(&full,0,1); sem_init(&empty,0,5); pthread_create(&tid1,NULL,produce,NULL); pthread_create(&tid2,NULL,consume,NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); }

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/Syncho$ ./a.out No of items in stack : 0 consumed item is 0 produced item is 0

No of items in stack : 1 produced item is 1 No of items in stack : 0 No of items in stack : 1 produced item is 2 No of items in stack : 2 produced item is 3 No of items in stack : 3 produced item is 4 No of items in stack : 4 produced item is 5 No of items in stack : 5 consumed item is 1 No of items in stack : 4 consumed item is 2 No of items in stack : 3 consumed item is 3 No of items in stack : 2 consumed item is 4 No of items in stack : 1 consumed item is 5 No of items in stack : 0 consumed item is 1 produced item is 6 No of items in stack : 1 produced item is 7 No of items in stack : 0 No of items in stack : 1 produced item is 8

DINING-PHILOSOPHERS PROBLEM
#include <unistd.h> #include <stdio.h> #include <pthread.h> #include <signal.h> #include <semaphore.h>

sem_t chopstick[5]; int philosopher(int n); int main() { int i, status; pthread_t phil[5]; /* to store thread id*/

printf("The Dining-Philosophers Problemn\n"); for(i = 0; i < 5; i++) sem_init(&chopstick[i], 0, 1); for(i = 0; i < 5; i++) if(pthread_create(&phil[i], 0, (void*(*)(void*))philosopher,(void *)i)) return -1; /*main waiting for threads */ for(i = 0; i < 5; i++) { pthread_join(phil[i], (void **)&status); if(status == i) printf("Philosopher %d went to heaven\n", i); else printf("Philosopher %d went to hell\n", i);

} for(i = 0; i < 5; i++) sem_destroy(&chopstick[i]);

return 0; }

int philosopher(int n) { int i, j, first, second; printf("Philosopher %d was born!\n", n); first = (n < 4)? n : 0; /* left for phil 0 .. 3, right for phil 4 */ second = (n < 4)? n + 1 : 4; /* right for phil 0 .. 3, left for phil 4 */

for(i = 0; i < 5; i++) { printf("Philosopher %d is thinking \n", n); sem_wait(&chopstick[first]); /* get first chopstick */ sem_wait(&chopstick[second]); /* get second chopstick */ sleep(1); printf("Philosopher %d is eating\n", n); sem_post(&chopstick[first]); /* release first chopstick */ sem_post(&chopstick[second]); /* release second chopstick */ sleep(1); }

pthread_exit((void *)n); }

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/Syncho$ ./a.out The Dining-Philosophers Problemn Philosopher 4 was born! Philosopher 4 is thinking Philosopher 3 was born! Philosopher 3 is thinking Philosopher 2 was born! Philosopher 2 is thinking Philosopher 1 was born! Philosopher 1 is thinking Philosopher 0 was born! Philosopher 0 is thinking Philosopher 4 is eating Philosopher 3 is eating Philosopher 4 is thinking Philosopher 3 is thinking Philosopher 2 is eating Philosopher 2 is thinking Philosopher 3 is eating Philosopher 1 is eating Philosopher 3 is thinking Philosopher 2 is eating Philosopher 1 is thinking Philosopher 0 is eating Philosopher 3 is eating Philosopher 2 is thinking Philosopher 1 is eating

Philosopher 0 is thinking Philosopher 4 is eating Philosopher 2 is eating Philosopher 3 is thinking Philosopher 1 is thinking Philosopher 4 is thinking Philosopher 2 is thinking Philosopher 3 is eating Philosopher 1 is eating Philosopher 2 is eating Philosopher 3 is thinking Philosopher 1 is thinking Philosopher 0 is eating Philosopher 2 is thinking Philosopher 3 is eating Philosopher 1 is eating Philosopher 0 is thinking Philosopher 4 is eating Philosopher 1 is thinking Philosopher 2 is eating Philosopher 4 is thinking Philosopher 1 is eating Philosopher 0 is eating Philosopher 4 is eating Philosopher 0 is thinking Philosopher 4 is thinking Philosopher 0 is eating Philosopher 4 is eating Philosopher 0 is thinking

Philosopher 0 is eating Philosopher 0 went to heaven Philosopher 1 went to heaven Philosopher 2 went to heaven Philosopher 3 went to heaven Philosopher 4 went to heaven

READER-WRITER PROBLEM:
#include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<semaphore.h> void * reader(void *) ; void *writer (void *) ; sem_t x,y,z,wsem,rsem ; int readcount=0 ; int writecount=0 ; int data=0;

main() { int a=1,b=1;

sem_init(&wsem,0,1) ; sem_init(&x,0,1) ; sem_init(&rsem,0,1) ;

sem_init(&y,0,1) ; sem_init(&z,0,1) ;

pthread_t r,w,r1,w1,r2,r3,w2,w3 ; pthread_create(&r,NULL,reader,(void *)a); a++; pthread_create(&r1,NULL,reader,(void *)a); a++; pthread_create(&w,NULL,writer,(void *)b); b++; pthread_create(&w1,NULL,writer,(void *)b); b++; pthread_create(&r2,NULL,reader,(void *)a); a++; pthread_create(&w2,NULL,writer,(void *)b); b++; pthread_create(&r3,NULL,reader,(void *)a); a++; pthread_create(&w3,NULL,writer,(void *)b); pthread_join(r,NULL); pthread_join(r1,NULL); pthread_join(w,NULL); pthread_join(w1,NULL); pthread_join(r2,NULL); pthread_join(w2,NULL) ; pthread_join(r3,NULL); pthread_join(w3,NULL); printf("main terminated\n"); }

void * reader(void * arg) { int c=(int)arg ; printf("\nreader %d is created",c); sleep(1); sem_wait(&rsem); sem_wait(&x) ; readcount++; if(readcount==1) sem_wait(&wsem) ; sem_post(&x) ; sem_post(&rsem); sleep(1) ;

/*Critcal Section */

printf("\n\nreader %d is reading\n ",c); printf("Data read == %d\n",data); sleep(1) ; printf("\nreader%d finished reading\n",c);

/* critical section completd */ sem_wait(&x) ; readcount-- ; if(readcount==0) sem_post(&wsem) ; sem_post(&x) ; }

void * writer(void * arg) { int c=(int)arg ; printf("\nwriter %d is created",c); sleep(1); sem_wait(&y) ; writecount++ ; if(writecount==0) sem_wait(&rsem) ; sem_post(&y) ; sem_wait(&wsem) ; data++; printf("\nwriter %d is writing\n",c) ; printf("\ndata written == %d",data); sleep(1); printf("\nwriter%d finished writing\n",c); sem_post(&wsem) ; sem_wait(&y) ; writecount-- ; if(writecount==0) sem_post(&rsem) ; sem_post(&y) ; }

Output:
FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/Syncho$ ./a.out

reader 4 is created writer 4 is created writer 3 is created reader 3 is created writer 2 is created writer 1 is created reader 2 is created reader 1 is created writer 4 is writing

data written == 1 writer4 finished writing

writer 3 is writing

data written == 2 writer3 finished writing

reader 4 is reading Data read == 2

reader 3 is reading Data read == 2

reader 2 is reading Data read == 2

reader 1 is reading Data read == 2

reader3 finished reading

reader4 finished reading

reader2 finished reading

reader1 finished reading

writer 2 is writing

data written == 3 writer2 finished writing

writer 1 is writing

data written == 4 writer1 finished writing main terminated FeroozKhanKM@Chintu:~/Studies/4th Sem/OS Lab/Syncho$

You might also like