D:\JavaFrameworks\InsuranceFramework\src\java\unix_code\Unix_PIPES.properties |
1 PIPES 2 3 A pipe is a connection of the standard output of one command to the standard input of another command. 4 5 A pipe has the same effect as redirecting the standard output to a file, then using that file as the standard input to another command. 6 7 A pipe is essential a queue of bytes between two processes. The pipe is accessed by a file descriptor, like an ordinary file. 8 9 One process writes into a the pipe and the other process reads from the pipe. The size of the pipe (typically 4096 bytes) is fixed by the system. 10 11 The piping process is done as follows: 12 1) A temporary file (pipe file) is created and the first command stores its output into the file (pipe file). 13 2) The following command uses the file (pipe file) as its input. 14 3) The temporary file (pipe file) is deleted after the last command is executed. 15 16 The symbol for a pipe is a vertical bar (|) 17 18 command_one [arguments] | command_two [arguments] 19 20 $ ls al | pg 21 $ ls -al | sort -r 22 $ ls | sort -r | pg 23 $ who | sort -r | pg 24 $ ls | grep ".c" | sort -r | pg 25 26 NOTE: 27 sort command will return the entire line that contains the pattern. 28 29 FILTER 30 31 A filter is a command that processes an input stream of data to produce an output stream of data. 32 A filter is a command that takes input from the standard input, performs some operations on the input and produces from it a different output. 33 34 35 ========================================================================= 36 ========================================================================= 37 38 FILTER 39 40 INPUT ======> ++++++>> OUTPUT 41 42 $ ls al | sort | pg 43 $ who | grep ".c" | wc -l 44 $ cat *.c | grep "int" | sort -r | pg 45 46 sort, grep, pg, wc are all filter commands while who, cat and ls are not since they can not take an input from the standard input. 47 48 TEE 49 50 The tee command enables you to use the output of one command in two way as follows: 51 1) Saved into a file 52 2) An input for another command 53 54 INPUT TEE OUTPUT 55 56 ========================================================================= 57 ========================================================================= 58 59 FILE 60 61 62 $ ls al | grep my_pattern | tee save_file | wc 63 $ cat save_file 64 $ who | grep "unoureld" | tee my_file | wc 65 66 List the directory using al options, directed to command grep to search for my_pattern and at the same time save the input to save_file and send a copy to the word count command. The last command (wc) counts the lines , word and the character within save_file 67 68 SPECIAL FILES 69 Unix treats devices as files. There are special files in Unix that represent different mechanisms or take care of special processes (device drivers). 70 All Unix systems support four types of files: 71 72 1) Ordinary files - text files 73 2) Directories 74 3) Block files 75 4) Character files 76 77 System V supports other files named pipes (FIFO files), while Berkeley Unix supports two other file types named LINK and SOCKETS. 78 These types of files (including pipes and link and sockets) represent how the operating system (Kernel) handles the devices or different type of file formats. 79 Let us look at the dev directory by changing to /dev directory 80 81 $ cd /dev # change to device directory 82 $ ls -al | pg # ls all file with type of file 83 84 Look at the first character in the file list which will tell the type of file it is. 85 86 drwxr-xr-x 13 root sys 2816 mar 11 09:49 ./ 87 crw--w--w- 2 root sys 64 jul 29 1991 console 88 89 The type of files can fall in one of the following types: 90 91 b block special file 92 c character special file 93 d directory file 94 f ordinary file 95 p FIFO (named pipe system V only) 96 l symbolic link (Berkeley only) 97 s sockets (Berkeley only) 98 99 100 ORDINARY FILE: 101 It stores user data such as input data or programs (Pascal, C, FORTRAN) without text. 102 103 104 DIRECTORY: 105 Directory is a file that contains i-nodes for all the files that exist in the directories. It basically keeps track of the list of i-nodes and all the files it contains. 106 It also has 2 entries ( "." ".." ) 107 "." is the link to the directory itself 108 ".." is the link to the parent directory. 109 110 rm a file is deleting the i-node for the directory file and telling the Operating System so it can free the file space on the disk. 111 112 113 PIPE (FIFO): 114 System V has pipe or what is known as FIFO special file (first in first out). These files are used to pipe commands where the output of one command is the input for the next command. Pipes file are temporary and deleted once the command is finished. 115 Fifo files give the system the freedom of having one process that finishes its job and sends its output into a fifo file without having to wait idle for another process to receive it. It is like a dump tank where one process dumps and the other process takes from the dump without any time restrictions. 116 117 118 SOCKETS: 119 The Berkeley Unix provides an equivalent file to pipes which are named Sockets. 120 UNIX treats devices as files. Sockets are special files that allow processes that are not running at the same time and are not the children of the same ancestor to exchange information. 121 122 123 LINKS: 124 A link is a pointer to a file, it is an address to a file on the disk. The pointer or its link is a way of getting to files on the system disk. Links can be for both files and directories. 125 126 $ ln link_x.c /usr/unoureld/c_prog/x.c 127 128 This would make the file link_x.c points to the same file on the disk space of the file x.c but in a different directory. 129 130 $ vi ............. 131 132 ..................... 133 134 135 BLOCK FILES: 136 Block files are used when the rate of reading/writing is one block (512 or 1024 bytes) at one time. The files are used in disk reading and swapping or buffering of file. 137 138 139 CHARACTER FILES: 140 Character files are used when the rate of reading/writing is one character at the time. The printer, console, modem are examples of character files (drivers). 141 142 143 ROW BLOCK DEVICES: 144 This the case of data is transferred without ant buffering and read/write is done directly on the devices. The tar command is used to read/write to the system tape using row block device files. 145 146 THE UNIX SHELL 147 148 The shell is the UNIX system's command interpreter. It is a program that reads the lines you enter at a terminal and performs various operations depending on what you type in. 149 150 Everyone on a UNIX system has his/her own copy of the shell program, so a user can do the things without bothering or being bothered by others. 151 There are three different features of UNIX shell, each with its own flavors. This is actual an added flexibility to the system. A user can switch from one shell to another at his convenience (if system has it). 152 The three popular shells are: 153 154 . Bourne shell sh invented by Stephen Bourne 155 . C shell csh done by Berkeley University & written in C. 156 . Korn shell ksh invented by David Korn 157 158 Bourne shell is the shell for AT&T UNIX system. 159 Korn shell is compatible with Bourne shell 160 C shell is not compatible with either one. 161 162 163 The Shell Provides the Following: 164 165 1) command execution 166 2) file manipulation 167 3) environment control; 168 4) utilities & library services 169 5) piping of commands filter 170 6) input/output redirection 171 7) Interpreted programming language (Shell Programming). 172 173 ========================================================================= 174 ========================================================================= 175 176 BACKGROUND 177 178 All the commands that are executed so far were running in the FOREGROUND. When you rum a command in the foreground, the shell waits for it to finish before the shell gives you another prompt to allows you to continue. 179 A BACKGROUND command is a command that is executed by the shell but you do not have wait for the command to finish. 180 The command is executed by the shell at low priority and the command does not tie the system up by the shell. This is very useful if the command or the program will run for a long time and does not need supervision. The terminal is free so you can use it for other work. 181 To run a command is the BACKGROUND, you type an ampersand (&) just before the return that ends the command line. 182 183 $ ls al | lp& 184 185 This example run ls al command and send to print in the background. 186 The system will prompt you with a process identification number (pid) 187 188 $ ls al | lp& 189 1234 190 191 To kill or stop a background process you must use the kill command to kill the process. 192 193 $ kill - 9 1234 # -9 to kill for good 194 195 The option "-9" is used to signal that the process is to totally eliminated. 196 197 NOTE: 198 CTLR C will not stop the background command. 199 200 201 202 If you do not know the precess id, you use the ps command to get the pid of all the running processes. 203 204 $ ps 205 pid tty time command 206 1234 tty03 0:05 sh 207 1245 tty03 0:40 dircomp 208 1524 tty03 0:55 ps 209 210