Unix

India CompuMasterLtd,.

INTRODUCTION TO UNIX & ACCESSING THE UNIX SYSTEM
Introduction to the UNIX Operating System What is an operating system? What is a UNIX system? Starting a UNIX session - Logging In Basic UNIX Commands Introduction to the UNIX Operating System UNIX is the most popular operating system on multi-user environments. It has evolved over the past 25 years from its conceptual stage into a powerful and effective operating system. Ken Thompson of Bell Laboratories originally designed the UNIX operating system in 1969. This was evolved from one of the early time-sharing operating systems called MULTICS in response to the programming requirements of that time. UNIX was first loaded onto the PDP-7, a minicomputer from DEC with less processing power. When the DEC PDP-11, a faster machine was introduced, the entire project was shifted to this machine. UNIX was originally written in assembly language. However, with the development of the ‘C’ programming language in 1971 by Dennis Ritchie, the core of the UNIX system was recorded in ‘C’ in 1973, as were any further developments to the system. This was the first instance when an entire operating system, or at least a major part, had been coded in a high-level language. The portability that resulted, is one of the major reasons for its popularity. However, some of the lower-level functions of the core in the UNIX system are still in assembly language. What is an operating system? An operating system (OS) is a software program that acts as an interface between a user and a computer. The purpose of an operating system is to provide an environment in which a user can execute programs. An operating system is thus an important part of every computer system. Operating systems are of two types: q Single-user operating systems Example: DOS, Windows-95. q Multi-user operating systems Example: UNIX, Zenix. Single-user operating system A personal computer is a single-user system. A single person can use a PC, at a time.

Unix

India CompuMasterLtd,.

Generally, the operating system used on single-user systems is MS-DOS. Multi-user operating system An operating system that can cater to the needs of a number of users at a time, is known as a multi-user operating system. This can be done by using the technique known as time-sharing. The operating system divides the total time into a number of time-slices and schedules the tasks one after another in a given priority. The switching is done so fast that the user has the illusion that the total computer resources are available to him. What is a UNIX system? A typical UNIX-based computer system includes a number of hardware and software components. Hardware A system unit that has the system’s central processing unit and one or more disk drives for mass storage including a backup device (floppy disk, magnetic tape drive). A user terminal from where users interact with the system, connected through cables. A console from which system operation is controlled. It may also be used as an ordinary terminal. Communication lines connecting the system to other UNIX-based systems or mainframe computers. Software Like all operating systems, UNIX is also a software operating system. UNIX functions on a computer system in conceptual layers. The UNIX system has built-in methods to keep the activities of each user separate. Every user of the UNIX system has his or her distinct identification, which consists of a user name and password. This distinct user identification is called the user’s account. All systems have a user who looks after the administrative duties of the system, and has all the privileges and is called the superuser or administrator. The user name of the superuser is root. The superuser is responsible for creating various users and maintaining accounts of the users. To use the system, the user has to log into the system. The procedure of entering the system is called logging in, logging on or signing onto the system. A user’s interaction with the system between login and logout is termed as a session. Starting a UNIX session - Logging In A user of a UNIX based system works at a user terminal. After the boot procedure is completed, the following messages appear at each user’s terminal. login: Here, the user has to type in the login name/user account and press the <Enter>

Unix

India CompuMasterLtd,.

key after which the system asks for the password. The password is used as a means of security and is not echoed (displayed) when typed. The password must consist of at least 6 characters. These characters may be a combination of letters, numbers or special symbols. Normally, only the first eight characters of the password are significant. However, this length can be set and is decided by the superuser. The password can be changed only by the user after successful login or the superuser and hence, provides effective security to the system. If the login name or the password is invalid or typed with an error, the "login incorrect" message is displayed and another login prompt appears. The login procedure has to be repeated till successful. If the user is unable to login correctly after a fixed number of attempts, the system locks out the account. Also, if the user is unable to enter the password within a certain amount of time of entering the user-name, the system disconnects by giving the message login time-out. These features help prevent unauthorized access to the system. So, to log onto the system the user has to identify to the system that he is an authorized user of the system. The login name or the user-id is used for identifying the user to the system and in turn, to the other users. When a valid user name and password are entered at the terminal, the $ symbol is displayed on the screen. This is the UNIX prompt. When a user logs in, he is taken directly into his HOME directory created at the time of creation of users. This indicates that the system is now ready to accept commands from the user and execute them. By default, the user has all the permissions in his home directory. However, to access the other areas of the system, users must have the appropriate permissions to access other users’ directories. System Messages After a successful login, the user may receive any or all of the following system messages. q Last successful login and last unsuccessful attempt to login q Copyright notices q Message of the Day q Mail Notification After successful login, the system displays messages regarding the last time the user had successfully logged-in and the last time the user had unsuccessfully tried to login. And finally, it displays the Shell prompt indicating that the system is ready to accept commands. The $ is therefore the command prompt. Cautions about logon The UNIX system unlike the DOS system distinguishes between upper case and lower case characters. Since user name and password can only be lower case letters, it is necessary to type user name and password in lower case, or else the system may not recognize the user.

Unix

India CompuMasterLtd,.

Changing your password The passwd command changes the password. Whether or not a password is required for account access is really a matter of system administration policy. The password helps in preventing unauthorized users from accessing a user’s account. It is a good practice to change the password from time to time. The new password must differ at three positions at least. When a user tries to change the password the passwd program prompts the user for the old password. This prevents unauthorized users from changing another user’s account password. If the password is being set for the first time, the user is not prompted for the old password. The program prompts the user to re-enter the password in order to make sure that the user has actually remembered the password. If the user is unable to enter the old password or if the password re-entered for second time does not match, the program terminates with a ‘sorry’ message. A password must contain at least two alphabetic characters and at least one numeric or special character. The password must be different from the user’s login ID. There are a number of situations under which the user cannot change the password. A few conditions are listed below. q The new password does not follow the rules for password. q The minimum time between password changes has not elapsed. q The superuser has not conferred upon the user the privilege to change his/her password. Ending a UNIX session - Logging Out Logging out of the system ends a session. When a user logs out of the system, resources that were being used can be allocated to the other users on the system. Logging off ensures that the commands are stopped and files that you were using are closed property. By logging out of the system, the user avoids the user account from being misused by some other user. To logout of the system, the user has to type exit at the Shell prompt or press ^D. Basic UNIX Commands UNIX commands can be entered after the $ prompt appears on the VDU. All UNIX commands should be entered in lower case. Command: date Function: The date command tells the current date and time. q Example: $ date

Unix

India CompuMasterLtd,.

Thu Apr 1 09:34:50 EST 1999 $ Command: who Function: This command gives details of all users who have logged in to the Unix system currently. q Example: $ who vijay ttyp2 Apr 1 11:56 rag ttyp1 Apr 1 11:53 $ who am i rag ttyp1 Apr 1 11:53 Command: man Function: This command provides help about the specified command. q Example: $ man who #This command gives complete details about the #usage of the who command. Directory Commands Command: pwd (print working directory) Function: Displays the full path name for the current working directory. q Example: $ pwd <RET> /usr/bin $ #Here, /usr/bin is the directory in which the #user is currently working. Command: cd (change directory) Function: Changes the working directory. q Example: #Assume that user Raghu has logged in and then #gives the following command: $ cd/user <RET> $ pwd <RET>

Unix

India CompuMasterLtd,.

/user $ #Here, the full path name of the directory which #the user wants to make current, has been #specified. Relative path names can also be used to change to a parent directory. For example, Raghu can enter the following command after logging in, to change to the parent directory of his HOME directory: q $ pwd <RET> /user/raghu $ cd.. <RET> $ pwd <RET> /user $ #Here, .. refers to the parent directory of #the current working directory. Note: The cd command without any path name always takes the user back to the user’s HOME directory. Command: head Function: Displays the initial part of a text file. q Example: #Assume that the user needs to display the first #four lines of a file called wordlist. $ head -4 wordlist Command: tail Function: It is a complement of the head command that displays the last part of a text file. Example: #Assume that it is needed to display text from the #10th line from the beginning of a file called wordlist $ tail +10 wordlist Command: mkdir (make directory) Function: Creates sub-directories.

cob_prog. the directory to be removed. However. The full path name may also be specified with rmdir. owner of the file. q Example: $ rmdir cob_prog <RET> $ #Here. #The directory cob_prog should: #be empty (not contain files or sub-directories) #not be the current working directory. Full path-names can be specified when creating a directory. q ls –d: Lists a directory file instead of its contents. file size. number of links. q ls –l: Lists files in the long format. modification date and time and filename. Command: rmdir (remove directory) Function: Removes empty directories. q ls –r: Lists the files and directories in reverse order. The files are displayed along with their mode. is #the directory under the current #working directory. q Example: #Assume that in the sub directory . (The command l is equivalent to ls -l). Example: #Assume that Raghu has just logged in and #gives the following command $ mkdir prog_files <RET> $ The sub-directory prog_files is created under the current directory. Command : ls (lists the contents of a directory) Function: Lists names of files and sub-directories of a directory. q ls –u: Lists files in order of last access time. q ls –t: Lists in order of last modification time. the current working directory does not change to the new directory. q ls –a: Lists all entries including the hidden files..Unix q India CompuMasterLtd. There are many options available with this command.

1 raghu group 108 Dec 8 17:39 ex2 -rw-r--r-.1 raghu group 205 Dec 8 17:57 greater1 -rw-r--r-.1 raghu group 115 Dec 8 18:00 comp -rw-r--r-.1 raghu group 124 Feb 22 17:53 evodd -rw-r--r-.1 raghu group 14 Jan 12 12:57 ca drwxr-xr-x 2 raghu group 512 Apr 1 12:36 caa -rw-r--r-.1 raghu group 40 Dec 8 17:37 ex1 -rw-r--r-.1 raghu group 99 Jan 12 12:54 chco -rw-r--r-. The directory name is optional if the names of files and directories under the current working directory are to be listed out.1 raghu group 100 Dec 8 17:35 add -rw-r--r-.1 raghu group 139 Dec 9 14:17 fibno -rw-r--r-.1 raghu group 109 Dec 8 17:41 mul $ .1 raghu group 149 Dec 8 17:51 greater -rw-r--r-.1 raghu group 69 Dec 9 14:29 ex5 -rw-r--r-.1 raghu group 61 Feb 22 18:28 forno -rw-r--r-.1 raghu group 63 Feb 22 17:44 100no -rw-r--r-. #/user/raghu. q Example: $ ls -l total 32 -rw-r--r-. $ ls /user/raghu <RET> data1 data2 prog_files $ #Here. raghu has 2 files #called prog_files.1 raghu group 74 Feb 22 17:53 eod -rw-r--r-. the names are printed in stored order.Unix India CompuMasterLtd..

The cat command can also display more than one file as shown in the following example: q Example: $ cat data1 data2 <RET> A sample file Another sample file #(contents of data2) $ Command: cp (copy one file to another) Function: Duplicates files. the command assumes that data1 is #in the current working directory.. MORE UNIX COMMANDS File Commands More on Relative Path-Name in File Commands Pipes and filters File Commands Command: cat (concatenate files) Function: Displays the contents of one or more files.Unix India CompuMasterLtd. q Example: $ cat data1 <RET> A sample file $ #Here. Full path-names can also be specified to display a file in another directory. q Example: $ pwd <RET> /user/raghu $ ls <RET> data1 data2 .

Files can also be copied to and from another sub-directory.. the file data1 is copied to a new file called #data3. prog_files $ cp data1 data3 <RET> data1 data2 data3 prog_files $ #Here. Command: mv Function: Changes the names of a file or directory/moves files from one directory to another.Unix India CompuMasterLtd. the files to be removed are assumed to be #in the current working directory. its contents will be #destroyed and replaced by the contents of data1. q Example: $ rm data1 <RET> $ ls <RET> data2 data3 prog_files $ #Here. q Example: $ mv data3 new file<RET> $ ls <RET> data2 newfile prog_files $ . If data3 already exists. Command: rm (remove files) Function: Removes files.

In case the root directory is the current working directory. Any modification made in the additional file or in the original file is reflected in both of them. For example: q /user #Refers to the directory user under the root #directory. Example: #Renaming a directory $ mv/user/raghu/prog_files/ user/raghu/programs <RET> $ In UNIX. q Example: $ ln add addition $ The advantage of the ln command is that several users can have access to common data files. More on Relative Path-Name in File Commands In any command. a file can also be moved to another directory (not copied) as shown below: q Example: $ mv data3/user/raghu/programs/data3 <RET> Command: ln Function: Establishes an additional file name for the same ordinary file. a directory can be referred to in one of two ways: By it’s full path-name.. <RET> $ pwd <RET> / $ cd user <RET> $ pwd <RET> .Unix India CompuMasterLtd. then its sub-directory-user can be referred to simply by its name. The usage of the above is illustrated below: q $ pwd <RET> / $ cd/user <RET> /user $ cd..

) refers to the current working #directory. -i option like –r must immediately #follow rm.e. the –r and –i options #have to be used in combination. this command copies the file abc #from the directory /user/raghu to the current #working directory i. Another option provided with the rm command confirms the removal of any file before actually removing it. q Example: $ rm –r olddir <RET> $ #Here. as follows: . The full path name or relative path name for a directory may be specified with the rm-r command. UNIX provides an option with the rm command to remove a directory without having to go through the rigmarole of removing each file and subdirectory. under the #same name. The command to do so is shown below. the dot (.Unix India CompuMasterLtd. file1 is a file under the current working #directory.. For confirmation of removal of a #directory interactively. The rm –r command With the rmdir command. #The –r option must immediately follow rm #(separated by one or more spaces). /user/raghu #could have been used but using the dot saves on #typing. The directory to be removed #cannot be the current working directory. /user/raghu. So. Consider the following example: q $ pwd <RET> /user/raghu $ cp/user/raghu/abc <RET> $ #Here. UNIX allows only an empty directory to be removed.) is used to refer to the current working directory. q Example: $ rm -i file1 <RET> file? #Here. In place of the dot. /user $ A single dot (. olddir is assumed to be under the current #working directory.

commands were created to perform single tasks only. $ rm – ri olddir <RET> olddir? #Here. the files under the directory are #displayed one by one for individual confirmation #of removal. q Example: $ who | wc -l 3 $ Here. The following are some of the filters: . Filters also take input from a file. the output of the command who is taken as the input for the wc -l command and the result of that is displayed. Redirection provides an answer to this problem. If we want to perform multiple tasks in one command. Filters A filter takes input from the standard input. which are redundant and occupy disk space. Pipes A pipe is a mechanism. the user is asked to confirm the removal #of the directory. The important advantages of pipes are that they prevent users from making temporary files using I/O redirection. to replace the existing characters with some other characters etc. which takes the output of a command as its input for the next command. to arrange the contents of a file in a sorted order. it is not possible. We can extract specific columns of a file and can merge two or more files together using filters. processes it and then sends the output to the standard output.. Filters are used to extract the lines that contain a specific pattern. Filters are also used to store the intermediate results of a long pipe.Unix India CompuMasterLtd. But it creates a lot of temporary files. Once this confirmation is #given. $ rm –ri olddir <RET> olddir?y accounts?y letters?y $ Pipes and filters In Unix. Pipes and filters are used to overcome this obstacle.

-n. sort filter The sort filter arranges the input taken from the standard input in alphabetical order.Unix India CompuMasterLtd. q Example: $ sort -r jaya mala rita <press Ctrl +d to see the sorted output> rita mala jaya $ -f option ignores the case distinction and arranges the output in alphabetical order. q Example: $ sort anitha zarina yamuna <press Ctrl +d to see the sorted output> anitha yamuna zarina $ -r option displays input taken from the keyboard in the reverse alphabetical order. This filter comes with various options like -r. -f. -b. -t etc.. q Example: $ sort -f Vanita vimala girija Gomati .

Then.Unix India CompuMasterLtd. q Example: #Let us assume that we have a file called names #to contain the following: $ cat names george mathew thomas gideon kumar anand slyvia mary peter $ #Let us assume that we want to sort on #the middle name. $ sort +2 -3 names #gideon kumar anand #slyvia mary peter #george mathew thomas $ grep filter This command is used to search for a particular pattern from a file or from the standard input and display those lines on the standard output. Then. The various options available with this command are: . -pos2 option is used to sort on any one field. <press Ctrl +d to see the sorted output> girija Gomati Vanita Vimala $ #Note: The ASCII values for A to Z are less #than that of a to z.. +pos1. grep stands for "global search for regular expression". $ sorts +1 -2 names #gideon kumar anand #slyvia mary peter #george mathew thomas $ #Let us assume that we want to sort on the last #name.

q -v: Displays only those lines which do not match the pattern specified. #"Nigeria". These multiple patterns should be separated by ‘|’ symbol. -n: Displays those lines which match the pattern specified along with the line number at the beginning of the line.Unix q India CompuMasterLtd.. -c: Displays only the count of those lines which match the pattern specified. Suppose #we want to search for the lines which have "India". egrep means "extended global search for regular expression". -i: Displays those lines which match the pattern specified ignoring the case distinction. "Argentina" then $ egrep "India|Nigeria|Argentina" data . Example: #Let us assume that we have a file called data #whose contents are as follows: $ cat data India 6890 Asia China 8765 Asia France 3243 Europe Nigeria 3212 Africa Argentina 1234 South America Mexico 4563 North America $ #If we want to extract those lines that have #"Asia". q Example: #Let us consider the same above example data. then $ grep "Asia" data India 6890 Asia China 8765 Asia #If we want the above example to ignore case #distinction then $ grep -i "asia" data India 6890 Asia China 8765 Asia $ egrep command This is an extension of the grep command. Multiple patterns can be searched for in a file using a single command line.

The difference between the pg filter and the more filter is that viewing a screenful of the latter can be done by pressing the space bar. The fgrep command when used with -x option is used to extract those lines that match the string exactly.. this is only possible by this command. This command is used to extract only fixed strings without the use of any regular expression. q Example: $ who | more cut command .Unix India CompuMasterLtd. q Example: $ ls -l | pg more filter It functions in the same way as the pg filter. q Example: #Let us suppose that we have a file called old whose #contents are as follows: $ cat old this is a test to extract the exact string. while that of the former is done by pressing enter. $ #Suppose we specify the string to be searched for as #"this is". India 6890 Asia Nigeria 3212 Africa Argentina 1234 South America $ fgrep command The fgrep command stands for "fixed grep". then $ fgrep "this is" old this is a test this is only possible $ pg filter This displays the output of a command on the screen page by page.

. One particular field from any file or from the output of any command can be extracted and displayed using the cut command. q Example: #Let us consider the following file called mast: $ cat mast b001 : kane and abel : j archer b001 : kane abd abel : j archer boo3 : the naked face : s sheldon $ cut -d ":" -f2 mast kane and abel kane and abel the naked face $ cat mast | cut -d ":" -f3 j archer j archer s sheldon paste command This command merges the contents of two files into a single file. q Example: #Let us suppose that we have two files. One particular character can also be extracted using the -c option of this command. $ cat first george victoria slyvia $ cat second mathew thomas peter $ paste first second . first and #second.Unix India CompuMasterLtd. It reads a line from each file in the file list specified and combines them into a single line.

george mathew victoria thomas slyvia peter $ tr command This command is used to translate characters.Unix India CompuMasterLtd.. $ cat bag | tr "[a-z]" "[A-Z]" THE BAG CONTAINS AN ASSORTMENT OF THINGS. q Example: #Let us assume that we have file called bag. $ cat bag The bag contains an assortment of things. $ THE STRUCTURE OF UNIX The Kernel Functions of an operating system Features of UNIX Portability Modularity File structure and Security I/O Redirection and Piping File Management . This command when used with -s option squeezes multiple spaces into a single space.

the Kernel is read into memory. It stays in memory while the system is running.000 lines of Assembly code. Communication Ports etc. When the system is booted. Disk Management Data and programs that the user generates while working on the system are stored in magnetic form on the disks. The OS maintains a record of the occupancy in a separate location on the disk. 200 lines for hardware efficiency and 800 lines of code for certain operations are available which was not possible in ‘C’ language.. Just like programs. out of which. connected to the system are called as Peripherals. The Kernel programs consist of 10. Therefore. Peripheral Management The various devices such as Monitors. which is called the File System. Printers.000 lines of ‘C’ code and 1. The function and services of the Kernel are: q File Management and Security Management q Input/Output Services q Process Scheduling and Management q Memory Management q System Accounting q Interrupt and Error Handling q Date and Time Services Functions of an operating system Memory Management Every program that is executed by the user initiates a process. The components are: q q q The Kernel The Shell Commands and Utilities Applications q The Kernel The Kernel is the heart of the UNIX operating system. All the other components call on the services of the Kernel.Unix India CompuMasterLtd. Every process that is initiated needs memory for execution. it becomes necessary to maintain a record of the disk space occupancy by the various files on the disk.. Each OS has its own way of performing these tasks. the peripherals also needs . The operating system allocates and deallocates memory for the various processes running on the system as per their requirements and priorities.

you enter its name just as you would a command’s name. The peripherals have to be initiated before there are actually used by the system.Unix India CompuMasterLtd. UNIX commands and utilities cover a wide range of operations. applications are just programs. As far as the Kernel is concerned. An application is executed in a Shell. The Shell begins to operate as soon as the user logs onto the system. database managers. protecting the system resources from incorrect accesses. spreadsheets. Commands and Utilities Commands are programs that performs specific operating–system tasks. one at a time. The Shell The Shell is the component that interacts directly with the user. The Shell thus forms a layer above the Kernel and hides it from the user. as are Shells and commands. It takes commands from the user and carries them out. There are several types of Shells available to UNIX users. The Bourne Shell and the C Shell are available on the most prominent UNIX systems. from editing. the Shell accepts keystrokes and essentially arranges for the Kernel to run a command. copying. some memory on the system for their functioning. This is done by generating appropriate interrupts. Time-sharing OS q Multi-tasking OS q Portability q Modularity q System security q File Structure and Security q I/O Redirection and Piping q Device independence .arguments or parameters to the Kernel.. The operating system takes care of the proper functioning of the peripherals attached to the system as per the user's requests. and erasing files to sending and receiving electronic mail messages to managing the progress of a large software development project. in turn. The Shell thus forms a layer of abstraction over the Kernel. Application An applications program is software that performs a specific type of task. It is a command interpreter. loads programs and commands and ensures that the commands have access to the parameters and other information. and CAD/CAM programs are all examples of applications. Although the Kernel allocates resources. To start an application. Word processors. It interacts with the Kernel by passing commands and the other information. Its major features are: q Multi-user. The commands are invoked by name through the Shell. The Kernel. Features of UNIX UNIX has a number of features and capabilities.

C or BASIC can be easily transported to a new system by recompiling the source codes. and the next user’s request from the job queue is executed. Around 90% of UNIX is written in machine-independent ‘C’ language and only the remaining small part of it is written in machine-dependent assembly language. which allows several users to work on the computer system at the same time. The users can work on the system through multiple terminals connected to a host computer.Unix q India CompuMasterLtd. without making major changes to the software. Porting the Data UNIX utilities are available for transferring data from one system to another.. The users’ programs are queued up and executed for a fixed amount of time. it is hardware independent and can be easily installed on machines with varying architecture. The first process gets its chance again after all the other programs are executed once. after which its status is saved. To port the UNIX OS to another machine. UNIX gets its portability not because of some special design but because of the language used to develop the system. The UNIX system comes with a large number of utility programs. while the rest of the system is obtained by compiling the higher level ‘C’ code. . Any module can be mounted or unmounted from the system as when the need arises. UNIX uses the time-sharing method. Multi-Tasking Operating System UNIX. such as COBOL. To provide a multi-user environment.e. Modularity One of the unique features of the UNIX system is its modular design. This can be done by placing some tasks in the background while the user continues to work in the foreground. This rule may be violated due to priority level differences. Communication Multi-User Operating System UNIX is a multi-user operating system. FORTRAN. as a multi-tasking operating system allows a user to perform a number of tasks at the same time.. Portability has three aspects: Porting the Operating System UNIX OS is portable i. Porting an Application Application programs written in any high level language. which are designed to independently perform a specific task. The programs are called modules. only the machine-specific part of the Kernel has to be rewritten in the assembly language of the new machine. The user cannot control the execution of the tasks in the background. Portability Portability refers to the ability of software that operates on one machine to operate as efficiently on any another machine.

Its file structure is like an upside down tree with the roots at the top. The system maintains a list of users who are allowed to access the system. This structure also allows implementing the File security system. Without the necessary privileges.e. New users have to be added to the list before they can have an access to the system. Modular Structure of UNIX System System Security UNIX being a multi-user operating system. It allows the files to be organized into various directories for easy access. the administration of the entire system. The UNIX file structure allows the files to grow dynamically. File structure and Security UNIX has a hierarchical file structure.Unix India CompuMasterLtd. write and execute permissions . The concept of superuser helps in maintaining and administering the system i.. It is the system administrator who creates user accounts and assigns them privileges. Read. File Security system UNIX provides security at file and directory level. The system is thus not open to all the users at all levels. It also keeps track of what files and resources each user is authorized to use. the user cannot access the system’s resources.. Users are assigned a user name and password. it becomes necessary to offer protection to the user’s information from illegal accesses.

Communication UNIX has three levels of communication: q Communication between terminals connected to the same computer q Communication between two computers at the same site which may not use either the same hardware or software. are given to each file at three levels. OTHERS: The other members are the ones who are neither the owners nor group members of a file. UNIX has a facility called Piping. USER: The user is the owner of the file. File Management UNIX directory structure root . q Communication between two computers at remote locations through public and international data communication networks. this allows input/output independent of the hardware device actually accessed. The members of a group may be members of a programming team who share the same data for testing their programs. through local area networks. and hence. Furthermore. Write and Execute Privileges can be assigned to the owner. The owner of the file is the one who has created the file or the one to whom the ownership has been transferred by the creator of the file. the group and others. The special files through which the devices are accessed are called device drivers. the devices can be considered as files. By writing to and reading from these files all input/output activity can be performed.Unix India CompuMasterLtd. UNIX being hardware independent provides a redirection facility where input-output can be done to or from the files and other devices like printers. Read. Device Independence The UNIX system considers all devices connected to it as files. I/O Redirection and Piping UNIX commands are designed in such a way that they take input from a conceptual file called the Standard Input and send their output to another conceptual file called the Standard Output. Once a device is associated to the special file. GROUP: The members of a group have the same group-id but different user-id’s. Pipes allow data to be processed by a sequence of commands and preclude the use of temporary files.. by which output of one-command acts as the input of another command. speed up the operation.

. which is more popular. In a screen editor. A screen editor is used for this purpose. either by you or by the system. then you add or change the text to your linking. etc dev bin lib tmp usr where etc directory comprises of all system calls and commands bin directory comprises of all other commands dev directory comprises of all device files entry lib directory comprises of all library call and functions tmp directory is used by the Kernel for dumping any data which is not processed. The cursor is the little blinking line or box that shows you where the next character will be printed. vi was developed by the University of California at Berkeley and is also supplied with the Berkeley distribution of the UNIX system. usr directory has all the files related to users vi EDITOR Entering vi Basic vi commands Using vi Deleting and changing text Control commands Editing is one of the major things done while working on a computer. Entering vi . A screen editor allows you to see the portions of your file on the terminal’s screen and to modify characters and lines by simply typing at the current cursor position. you move the cursor around your file until you find the part that you want to modify. The screen editor supplied with UNIX system is vi.Unix India CompuMasterLtd.

meaning the characters you type in are placed before the current character position. the space bar can also be used. After the Esc is pressed. requiring an Esc to finish inserting. Adding Text For adding text. You can precede these keys with numbers. When you type in two capital Z’s. The second way of adding text is with the i command. you know that you are no longer in append mode. all the special screen-editing features go away. everything you type is appended to the text after the character the cursor was positioned over. i also puts you insert mode. Deleting text Now. but the easiest is with the ZZ command. you can run vi just like any other UNIX command. Summary of the basic vi commands Command H Operation Move cursor left . but it inserts instead of appending. the next thing to learn is how to delete text. k and l. moving the rest of the line left into the void created by the deleted character. To delete a line. When you press the Esc key. position the cursor over a character and type in ‘a’. that you can add text to a file. number of lines. there are two commands that delete text in vi: x and dd. Saving the file There are several ways to write a file in vi. Now. Basically. the cursor moves back to the last character you entered. it will print out the file name. This put you in a special mode of operation called append mode. which allow you to move more than one column or line at a time. vi will beep. Instead of the l key. In general.. x deletes the character at the current position. When you start vi. Moving around The basic screen motion commands are h. up and right respectively. Now. Once you’re back in the Shell. it will beep. The motions of h. To delete one character. you use the x command. we use the dd command. j. and number of characters at the bottom of your screen. If you try to move quickly to the beginning or end of the file. k. This way. when vi doesn’t like one of your commands. vi will automatically write the file and quit. just as with the a command.Unix India CompuMasterLtd. the cursor moves back to the last character inserted. putting you back in the Shell. l are left. i works like the a command. The space bar has exactly the same effect on the cursor as the l key. down. j.

Suppose you . When you type in a /. vi also supplies you with commands for scrolling several lines at a time. the text on the message line usually gets clobbered. After scroll. Some times the lines at the bottom will scroll down off the screen as more lines are listed at the top.Unix India CompuMasterLtd. J K L A I X DD P ZZ Move cursor down Move cursor up Move cursor right Append after cursor Insert Delete character at current position Delete Paste Write file and quit Using vi Scrolling Screen editors use scrolling to edit large files. so you can see the string you’re searching for. vi scrolls the screen up a line. respectively. Only the first 23 lines are displayed by vi when you start it on a large file. finding the next occurrence of a string. vi puts these characters on the message line. when you try to move past the bottom of the screen. As you can see. You also have the commands ^f and ^s that scrolls forward and back one full screen. As you work with the file and move about in it. vi puts a / on the message line at the bottom of the screen. As you type in the characters you want to search for. The ^d and ^u scroll half a screen down or up if there are enough lines in the file to do so.. the top lines will sometimes scroll up off the screen as more lines are displayed at the bottom. The / command searches forward or down through a file.

The dd is a special case of the d command that deletes the current line. Words The objects called words that are simply letters and numbers separated by blanks. which moves the cursor to the specified line number. which writes the file without quitting vi. These are controlling commands that have to do with external file manipulation and some special function of vi. Deleting and changing text vi provides you with several ways to delete and change text.Unix India CompuMasterLtd.. The file manipulation commands include: q. then it would search upwards for the previous string. :w. If you used the ? Command instead of /. which quits vi. One method of deleting the text is with the d command. q The w command advances the cursor to the beginning of the next word. q The b command moves the cursor back to the beginning of the current word. deleting them. The control commands that perform special functions include: number. q If the cursor is already at the beginning of the word. which causes the ed command to be executed. . vi allows you to move from word to word. q The e command will advance the cursor to the end of the current word. and changing them with simple commands. It puts you in input mode so you can type in your changes. want to search for the same string in the next occurrences. Control commands There are certain commands in vi that are preceded by a colon (:). you can use / command without typing the string again. The c command changes whatever lies in specified motion. the b command moves the cursor backward a word. The w command moves the cursor to the next word. and the e command moves the cursor to the end of a word. The d command is always followed by another character that specifies what will be deleted. If the cursor is already at the end of a word. it will be moved back to the beginning of the previous word. q The return key is used to end input for editing commands that take a variable length argument. discarding all changes. a subsequent e command advances the cursor to the end of the next word. scrolling if necessary and the ed -command. and ql: which quits vi without writing. or punctuation marks can be used in vi. tabs.

The language has features borrowed from C. What makes Shell programs powerful is that an external command can be used as a control command for any of the Shell’s constructs.Unix India CompuMasterLtd. make it an externally useful programming language. conditions and loops. The scope for Shell programming often goes beyond the limits of conventional languages. with its own variables. though most of its constructs are compact and simpler to use than those in C. These internal commands can be strung together as a language. The Shell also has rudimentary programming features. System variables While you are free to set up your own variables. q $ set HOME = /usr/icm IFS = MAIL = /usr/spool/mail/icm PATH = /bin: /usr/bin PS1 = $ PS2 = > PWD = /usr/kk/icm SHELL = /bin/sh TERM = ansi . To have a complete list of variables you can user the set statement. Programming WITH THE SHELL System variables Environmental variables The MAIL variable The variables PS1 and PS2: The SHELL variable The TERM variable The Shell not only plays an important role in command interpretation but also in more things than that.. there are a number of variables that are separately set by the system. some during the booting sequence. In addition to the external commands. These variables are called system variables. you can use the entire set of the Shell’s internal commands inside a Shell program. Shell programs run slower than those written in high-level languages. coupled with the use of UNIX commands and other programs. and some after logging in. However. which.

every user has the directories /bin and /usr/bin included in his PATH variable. it is preferable to use lowercase names only. You can confirm the contents of this variable by taking its octal dump. and is controlled directly by the variable HOME.. This sequence makes the commonly used UNIX commands universally available to every user.). By default. such built-in variable names are defined in uppercase. but the blank line following it only suggests that the newline character is part of the string. UNIX normally places you in a directory named after your login name. and finally. tab and newline characters. All these characters are invisible. q $ echo "$IFS" | od –bc . You can switch from any directory to your home directory by using the evaluated variable $HOME as an argument to the cd command. the search will begin from /bin. then for the purpose of distinguishing them from the ones built in. through /usr/bin. The PATH variable: The PATH variable contains a list of all full path names of directories that are to be searched for any executable program.Unix India CompuMasterLtd. Environmental variables Besides user-defined variables. This directory is called the home or login directory. The string normally consists of the space. q $ pwd /usr/bin $ cd $HOME $ pwd /usr/icm $ The IFS variable: The IFS variable contains a string of characters. If you use variables of your own. which are used as word separators in the command line. In this case. to the current directory indicate by a dot (. The HOME variable: When you log in. the Shell has special variables called environmental variables. q $ echo $PATH /bin:/usr/bin: $ This shows a list of directories that have to be scanned by the Shell while hunting for a command. $ By convention.

respectively. then the Shell informs the user of this with the familiar message "You have mail". 0000000 \t \n \n 040 011 012 012 0000004 $ The space character is represented by the ASCII octal value 040. while \t and \n universally represent the tab and newline characters. the system administrator uses the # as the prompt while working in the root directory or as a super user.lst $ The MAIL variable: MAIL determines where any mail addressed to the user is to be stored. this is the most popular Shell in the UNIX world. You can change the primary prompt string to C> if you find the MS-DOS environment more reassuring. stored in PS1 and PS2. and you can select the one you like the most. Normally. PS1 and PS2 are set to the characters $ and >. Today. for instance and use this call character as the word delimiter.Unix India CompuMasterLtd. You will see when using the Shell loop structures how a multi-line command makes the shell respond with a>. respectively. . q $ IFS = # $ cat #emp. q $ ps1 = "c>" C> To reset the variable to the familiar $. q C> ps1 = "$" $ Although $ is the most commonly used primary prompt string. Besides the Bourne Shell. The Shell searches this directory every time a user logs in.. This is the secondary prompt string indicated by PS2. If any information is found here. The PS1 variable contains the system prompt ($). there are a host of Shells that accompany any UNIX system. You can reset the value of this variable to a #. The variables PS1 and PS2: The Bourne shell has two prompts. All incoming mail will be stored in the directory defined by the variable. The SHELL variable: SHELL determines the type of the Shell that a user sees on logging in. simply make the reassignment at the C> prompt.

which is executed by the Shell when a user logs in.bat file in DOS. and display will be faulty. . The Script Executed During Login Time – the .profile in UNIX is similar to the Autoexec. because of some inherent advantages. This is really a Shell script. The C shell is known by the program csh.. The file is created when the system administrator adds a user to the system. which are terminal-dependent. That is why every time you make changes to it. If TERM is not set correctly. The HOME variable here.profile in your login directory. you should logout and login again. The TERM variable: TERM indicates the terminal type being used.profile HOME = /usr/icm/progs/ PATH = /bin:/usr/bin:.Unix India CompuMasterLtd. One of them is vi editor.:/usr/icm/progs MAIL = /usr/mail/icm #mailbox location IFS = PS1 = $ PS2 = > echo "Today’s date is ‘date’" cd echo "You are now in the HOME directory" Some of the system variables have been assigned in this script. you will switch to this directory. There are some utilities. Profile The . so that when you cd without arguments.profile alters the operating environment of a user. q $ cat . and they require knowing the type of terminal being used. The . which remains in effect through the login session. The ls–a command locates the . is set to the progs sub-directory. the C and Korn Shells have also carved out a niche for themselves. vi won’t work. and the Korn shell by ksh. which makes use of a control file in a sub-directory /usr/lib/terminfo. The file is routinely executed during login time by the Shell in a special manner.

and returns either a true or false exit status..test: When if is used to evaluate an expression. This is not always required. For this purpose. Mark the keywords then and fi.test test – file tests test . test evaluates the condition placed on its right. then the sequence of commands following it is executed. This return value is used by if taking decisions. which must necessarily accompany every if conditional. test uses certain operators to evaluate the condition. the test statement is invariably used as its control command.string comparison The case…esac construct The if condition Like the constructs for looping and decision-making. Operator -eq -ne -gt Meaning Equal to -ne Not equal to Greater than . and the simplest form of the if statement thus condenses to: if <condition> then <execute command(s)> fi if’s companion . The complete set of operators is shown in the following table. CONDITIONS AND LOOPS The if condition if’s companion . If the condition is fulfilled. if <condition> then <execute command(s)> else <execute command(s)> fi if evaluates a condition which accompanies its command line. the UNIX Shell also offers constructs that can be used in shell scripts. The construct also provides for an alternate action using the optional keyword else.Unix India CompuMasterLtd.

write or executable permissions.. As abridged list of the possible file related tests is shown in the following table. You can test whether a file has the necessary read.string comparison The third type of usage of the test statement is in testing and comparing strings. the user enters some non-whitespace characters when the script pauses twice. -ge -lt -le test – file tests Greater than or equal to Less than Less than or equal to test can be used to test the various file attributes. Test -f <file> -r <file> -w <file> -x <file> -d <file> -s <file> Exit Status True if <file> exists and is a regular file True if <file> exists and is readable True if <file> exists and is writable True if <file> exists and is executable True if <file> exists and is directory True if <file> exists and has a size greater than zero test . It doesn’t have its parallel in most languages. This construct is used in Shell scripts to perform a specific set of instructions depending on the value of a variable and is often used in .e. though test uses a different syntax. The case…esac construct The case statement is the second conditional statement offered by the Shell. There is nothing unusual in these tests which you can’t find elsewhere. Test -n stg -z stg s1 = s2 s1 != s2 stg Exit Status True if string stg is not a null string True if string stg is a null string True if string s1 = s2 True if string s1 is not equal to s2 True if string stg is assigned and not null The test output is true only if both the variables are non-null strings i. The following table lists the string handling results.Unix India CompuMasterLtd.

5) Quit to UNIX" 7. 4. It matches the expression first of pattern1.. q <pattern2> <command> : : . 1. it then falls through and matches pattern2 and so on.> q esac The keywords here are the in and esac.. <………. place of the if construct..sh echo "MENU" 1) List of Files 2) Process of User 1. The general syntax of case statement is as follows: case <expression> in <pattern1> <command> : : .. 4) Users of System 5. q Example: $ cat menu. A pair of semi-colons terminates each command list. and the symbols . The construct also uses the ) to delimit the pattern from the action. and if successful.Unix India CompuMasterLtd. are used as option terminators. 2. and the entire construct is closed with an esac. If it doesn’t. echo "enter your option: \c" read choice echo case "$choice" in . executes the commands associated with it. 6. 3) Today’s Date 3.

12. q 1) ls –l . The first two are basically complementary to each other. The while statement should be quite familiar to most programmers. 11. 8. case becomes even more powerful in its handling of the Shell wild cards for matching patterns. CONDITIONS AND LOOPS1 The while Statement The break and continue commands The until Statement The set and shift Statements The shift command The while Statement The Shell features three types of loops – while. The general syntax of this command is as follows: while <condition> do <execute commands> done The keywords here are do and done. Like in the if statement. The set of instructions enclosed by do and done are to be performed as long as the condition remains true. 3) date . Programmers frequently encounter a logic. 5) exit esac $ case can also match more than one pattern with each option. 10.Unix India CompuMasterLtd. 4) who . It repeatedly performs a set of instructions till the control command returns a true exit status. which prompts the user for a response. this condition is actually the return value of a UNIX command or program... This means . until and for. 2) ps –f . 9... case and the for loop are the only Shell control flow statements that accept the same set of metacharacters as used in matching filenames..

The break statement causes control to break out of the loop and the continue statement suspends execution of all statements following it. It performs the loop. Thus. and file tests. that you can use the test command here also. done Here. numeric and string comparisons. There is no next statement here neither can a step be specified. with each string separated from the other by white space. The break and continue commands are used with for. and switches control to the top of the loop for the next iteration. The additional keywords are variable and list. it doesn’t test a condition but uses a list instead.Unix India CompuMasterLtd. as many times . while and until commands. The continue statement continues if the condition is true till the loop terminates while the break statement comes out of the loop when it occurs. the while command is replaced by the until statement. The list consists of a series of character strings. The syntax of this condition is as follows: for <variable> in <list> do <command> <command> done The loop body is identical in structure to the while and until loops. The break and continue commands break and continue are also commands that are used in C. Each item in the list is assigned to the variable in turn.. $ until false do …… ……. while true has now become until false Looping with for The for loop is different in structure from one used in other programming languages. and the loop body is executed. with its associated expressions. Unlike while and until. The until Statement The until statement complements the while construct in the sense that the loop body here is executed repeatedly as long as the condition remain false.

as there are words in the list. It also sets the other parameters $# and $*.e. In this way. Opening and closing a file involves disk I/O. The shift command The Shell creates up to only 9 positional parameters. you can access more than nine positional parameters in a script. i..Unix India CompuMasterLtd. This feature is especially useful for picking up individual fields from the output of a program. Output redirection The singular statement for adding a record to the file uses the >> symbol. The set and shift Statements The set statement makes it possible to convert its arguments into positional parameters. When called once. $1 to $9. . I/O REDIRECTION AND SYSTEM PROCESSES Input redirection Output redirection System Processes Input redirection Taking input from a file other than the user’s terminal keyboard is termed as input redirection. $2 becomes $1. So. performance becomes slow. $1 is lost every time shift is invoked. if there are more than nine arguments the shift command can be used. shift transfers the contents of positional parameters to its immediate lower number. This goes on as many times as the statement is called. The important thing to remember is that the contents of the left most parameter. i.. when large data files are processed in this way. And. resulting in significant overheads. 2345 to $2 and 6213 to $3. the < (less than symbol) implies redirection from the named file. $3 becomes $2. a ‘<’ symbol is used.e. and so on. A simple use of this statement can be: q $ set 9876 2345 6213 $ This assigns the value 9876 to the positional parameters $1.. The syntax is $ <program> < <file name> <RET> Here. For this.

Then we will have two programs running. Initiating a background process is easy with the UNIX system. If a program that we are running seems to be taking a long time to finish and we would like to begin work on another task. and is closed finally when the loop iteration ends. The file is opened only once. the system can execute only one program or process at any one instant. you may wish to redirect the output of your . which means that it can schedule the execution of more than one program at the same time. Thus. including foreground processes. background processes don’t have to have finished executing before you can begin another program. In a moment we will see how to use this number to inquire about the status of a background process or even to terminate a background process before it finishes of its own accord. but all individual statements inside the loop also get redirected. or PID. since the background process is still executing while the foreground is running. UNIX is a multitasking operating system. Though they are internal commands. the Shell accords them the same status as the external commands.Unix India CompuMasterLtd. All the Shell’s conditionals and loops can be redirected. From a practical point of view. All these constructs are redirected by using the redirection symbols after the final keyword. The PID serves to identify our background process uniquely. your Shell prompt returns immediately to signal the fact that we may have invoked another program in the foreground. the Kernel program limits the total number of processes (foreground plus background) to a reasonable value (usually some 20 to 50 per user and 100 to 250 or so system-wide) for our particular UNIX implementation. The main advantage of running programs in the background is that unlike foreground processes (when you work on a task. we may schedule our program to run in what is known as Background Processing. have PIDs. This speeds up execution time. After beginning a process in the background. but it switches between processes so quickly. on our terminal and immediately prompt for our next command. For example: done > $filename Here. the slower your system runs overall. Simply type an ampersand character (&) at the end of the command line invoking the process that we wish to execute in the background. System Processes Running a Process in the Background: As we already know. We mention PIDs here because the & command happens to display the PID of the background process that we have invoked.. that most of the time all programs seem to be running at the same time. usually within one thousandth of the second. In fact. It is important to note that although you may start more than one background process. the more processes you start in the background. The Shell will respond by printing an identifying number known as the process identification number. which can be executed only with your intervention). But this can be avoided by some simple means. It’s important to note that all processes. at the beginning of the iteration. One inconvenience of background processing is that output will appear on your terminal screen intermixed with any output from foreground and other background processes. to avoid a confusion display. the > (greater than symbol) implies redirection of output to the named file.

input destined for the background process might be read by the foreground process and vice versa. a task like determining the disk usage for your entire file system. so that in our example. Normally. background processes to a disk file. which also reads input form the keyboard would then conflict with the input request of the background process. Because the Shell protects such tasks from terminating in response to an interrupt signal.Unix India CompuMasterLtd. q $ du />du.all& and make a note of the PID number that appears after you press <return>. let’s select a task that generally requires a minute or so to complete. be sure not to modify that file until the background process has finished executing. Further more. this permission is rw-rw-rw (octal value 666) for regular files. for example. if the background process reads its input from a disk file. UNIX SECURITY FEATURES File Permission mask Types of Files File Access Permissions Determining the File Access Permissions Changing File Access Permissions File Permission mask All the files inherit a system-defined permission when they are created.. we’ll redirect the output of the du command so that the results are placed in a disk file and du’s output won’t be sent to your terminal screen. the next 802 and so on. the very next process that is started in the system would have PID 801. You may have used the generally available du command to accomplish this. The / argument tells du to begin examining the file system at the root directory. To practice running a process in the background. We must not initiate a background process that reads input from the keyboard because any foreground process including your Shell. Once du is placed in the background. In addition. you may continue with foreground tasks without the output from the background task distributing the display on your terminal screen. That is. we may go ahead and run foreground processes. we cannot terminate a background process with an interrupt character.all& 800 $ The process numbers are unique and are assigned sequentially by the Kernel. enter du />du. To invoke du in the background. and rwxrwxrwx (octal . With du in the background.

executable files etc. value 777) for directories. Normally. the system administrator takes care of this security feature. you can only remove them. Types of Files As you already know in UNIX. but you should also have knowledge of it. which is what a directory gets by default. The system default is usually transformed by making a subtraction from it. So.. program files. object files. Examples of these files are data files. The user can make changes to such kind of files. every thing is treated as a file. 2 and 4 remove the executable write and read permissions respectively. The user mask thus changes this to 755 (-rwxr-xr-x). . After subtracting the mask from the system default. in case the settings made by him is not adequate. This is set by the user and is represented by the umask statement of the Shell.022 = 644. The user cannot modify this directory file. which mean that everyone gets all the permissions. When you subtract the mask 022 from it. you can always use chmod to turn on permissions of files individually. This represents the default permissions that you normally see when you create a regular file (-rw-r--r--). there are three types of files: q Ordinary files: All the files created by the user come under this category.Unix India CompuMasterLtd. It is called a mask because each digit is subtracted from the system default to remove permission. so that other users don’t automatically have permissions. the expression yields 666 . The system default directories is 777. q Directory files: For each directory there is a file by the same name as the directory. as and when required. it leaves the permissions for the owner unchanged. In UNIX. which contains information about files under that directory. However. One important thing to remember while using this mask is that you can’t turn on permissions not specified in the system-wide default settings. apart from user’s program files there are also special files such as files that contain information about directory contents or files that store information about various input-output devices connected to the system. To change the umask setting. Thus. you have to use umask with an argument. and lowers the other permissions by 2. 1. A value 000 means that the default permissions will be used while creating files. To find out the default value of this mask. you need to enter umask without any arguments: q $ umask 022 $ The output shows a set of three octal numbers. You should make sure that the system default is properly masked. which you feel they shouldn’t. q $ umask 777 $ This means that all files are to be created with no permissions on.

an ordinary file is not given execute permission when it is created. The owner is the system user who created the file. There are three classes of system users. the owner is usually the system user who created the file. Write permission is necessary in . may use certain privileged programs to write on the directory. These three classes of users and modes of access give rise to the nine different kinds of access permission allowed within the UNIX file system. System users with write permission may write to the file and change its contents by using an editor. for the group owner. and for the "other" user category may be the same or different. The UNIX file system allows each user class to access the file independendently of the other classes. most of the system files are special files. The meaning of these same access modes is different for a directory file. the access rights for the file owner. The system user with write permission. First. Group: (denoted by g). that is.. The group is one or more users who may access the file as a group. The system user with read permission may read (by listing) the contents of the directory using the ls command. Other: (denoted by o). System users with execute permission for ordinary files may execute the file as command. The user cannot alter these special files.Unix q India CompuMasterLtd. The owner has the full control over restricting or permitting access to the file at any time. Generally. every file has an owner. A Shell script is a file that contains a list of one or more commands that can be executed by the Shell. for user). The "other" category refers to any other user of the system. q Owner: (denoted by u. File Access Permissions In UNIX. there are three different classes of file users and three modes of file access. Special files: In UNIX. Executing a file only makes sense if the file is actually a program (command) or a Shell script. on the other hand. File accessing permissions and their meaning Access Mode Ordinary File Directory File Read Allows examination of Allows listing of the files within file contents the directory Write Allows changing Allows creation of new files and of file contents removal of old ones Execute Allows executing file as Allows searching of a directory command System users with read permission may read (examine) the contents of an ordinary file by using cat command.

1 icmdoc docum 40 Nov 1 13:23 letter .rwxrwxrwx This directory is completely accessible by all system users. other users cannot search it or read or write to any files contained in it.rwx-----This directory is only accessible by its owner. a file that the owner wishes to keep private from all other users has this set of permissions. and execute) to give nine possible sets of permissions as shown here. For instance. but it can’t be written to (changed) or executed by any one.Unix India CompuMasterLtd. and the file will be read into memory by the Shell and run as a command program. Generally. Some commonly seen permission patterns for directory files and their meanings are: . ls and the like. The absence of permission is indicated by a dash (-) in the same place. or special – and what set of access permissions it has. order to create files or to remove files. and others) may be combined with the three types of access (read.. some common permission patterns for ordinary files and their meanings are: r—r—r-This ordinary file can be read (or examined) by all three user classes. This file is write protected and read protected. The format is generally similar to the following: q $ls –l letter -rw-rw—rw. groups. A file with this set of permissions is known as read only or write protected. Generally. public directories that must be readable and writeable by all the users would have this set of permissions. . In general. In general. by using the long listing option (-l) with the ls (directory listing) command. You can easily determine what type a file is – whether ordinary. as well as other information about the file. any user can type the name of the file after the Shell prompt. executable programs (commands) such as cat. write. --x--x--x This ordinary file can be executed like any command program file by all system user classes. r w s Owner r w s Group r w s Other The presence of permission is indicated by the appropriate letter being in its correct location. directory. Determining the File Access Permissions The three classes of file users (owners. rw------This ordinary file is readable and writeable only by the file owner. That is. that are installed in publicly accessible directories are given this set of permissions. users on a multiuser time-sharing system would establish this pattern of permissions for their home directory workspace.

we have requested the long directory listing for the ordinary file called letter. we see that the file’s size is 40 characters. the ones that are important for you to know are the dash (-). Next. group owner. There is only one restriction in the use of the long listing option. Here is an explanation of what the various segments or fields in the long listing mean: -rw-rw-rw. =: Assign the indicated permissions. $ Here. which indicates a directory file. we’ll lead you through the details one by one: $ chmod[who] op-code permission …file… The who argument tells chmod the user class and may be any of the following: q u: User (individual file owner) g: Group file owner o: Users classified as "other" a: All system users (file owner. The date and time "Nov 1 13:23" show when the file was last modified. the file name is listed. -: Remove the indicated permissions from the existing permissions. Changing File Access Permissions The chmod command (for "change mode") allows you to alter the permission modes of one or more files or directories.. The file name is of course. The user requesting a long directory listing must have execute (search) permission in addition to read permission for the directory that contained the file. which means that this file only has one name associated with it. Because the command line format for chmod is somewhat more complicated than the format for other commands you have seen so far. Because the long listing is especially helpful for working with files. Next. and d. the access permissions are all enabled. While there are actually four types of files that may be indicated in the first field. "letter". which indicates an ordinary file. and the "other" category). the "1" indicates that there is only one link for this file from the directory. In the next slot. The op-code argument represents the operation to be performed by chmod: q +: Add specified permissions to the existing permissions.Unix India CompuMasterLtd. it is important to know how to read it. The permission argument uses the same abbreviations as you saw earlier in the discussion of types of file access: . Last of all. we see that the dash (-) in the file type field indicates that the file is an ordinary file. The word "icmdoc" indicates that the file owner has the user name "icmdoc": the group that has the access to this file is referred to as "docum".1 icmdoc docum 40 Nov 1 13:23 letter permissions links owner group size date time of filename last modification Reading from left to right.

.. --verbose output a diagnostic for every file processed. --quiet suppress most error messages... --changes be verbose whenever change occurs -f.w: Write permission . --changes like verbose. or chmod [OPTION].. --verbose output a diagnostic for every file processed. --changes like verbose.. --quiet suppress most error messages -v. chgrp [OPTION].r: Read permission .. but report only when a change is made.. chmod: Changes the access permissions of the file chmod [OPTION]. Options: -c.. --recursive change files and directories recursively. --silent..[GROUP] FILE... --verbose explain what is being done -R. but report only when a change is made. OCTAL_MODE FILE... --silent. or chown [OPTION]. GROUP FILE. MODE[.... chown: Changes the user and group ownership of files chown [OPTION]. --recursive change files and directories recursively . FILE. -f. . -v. Options: -c. Options: -c. -v..Unix q India CompuMasterLtd.MODE].[GROUP]] FILE. --recursive change files and directories recursively.. -R. --quiet suppress most error messages.. OWNER[. --silent.. ..x: Execute permission Some commands to change the file access permissions: chgrp: Changes the group ownership of files.. -f. -R.

-r: Print the operating system release. -n To change node name. grpck: Group file checker. login name. q The check includes validation of fields. -a Print all above information. login directory and program names specified.. q Verification includes check of the number of fields. group-id. q Executes new Shell changing real user-id. q The default password file is /etc/passwd. uname: Change or print current system name. pwck [file] q Scans the password file and notes any inconsistencies. uname [ -snrvma] Options: -s: Print the system name (default). umask: Set file creation mask. pwck: Password file checker. -v: Print the operating system version. q An entry is made in /usr/admn/sulog. su[ . -n: Print the node name. -s To change system name.Unix India CompuMasterLtd. MORE COMMANDS IN UNIX su: Become a User or another User. . group name. existence of login names in /etc/passwd file.] [name] [arg…]] q Become a superuser without logging off. user-id. -m: Machine hardware name. umask [000] q Sets user file creation mode. group-id. grpck [file] q Verifies all entries in the group file.

Physical block size: . An entry can be made in the user’s . blocks: Physical disk blocks. B: Block type (example: disk. q -N option makes mkfs run faster. The value of each specified digit is subtracted. terminal). blocks/cyl: Physical blocks per cylinder (by default 400). C: Character type (example: printer. mkfs special blocks [:inodes] [gap blocks/cyl] Arguments: special: Special file on which file system is to be constructed. If an argument is omitted. q q q Octal number determines to read/write/execute permissions for owner.profile file. q The major device numbers are specific to each system (can be obtained using the ‘gendev’ command). P: FIFO file. q Makes a directory entry and corresponding I-node. Minor: Minor device number. group and others.Unix q India CompuMasterLtd. mknod: Build a special file. then mkfs will automatically calculate the correct number of logical blocks and I-nodes. q If the argument % is specified. tape). The logical block size is usually different from the physical block size (by default 2048 bytes). Major: Major device type. q Only superusers are allowed to use the first form. Arguments: Name: Name of special file. gap: Rotational gap.. then the current value will be displayed. inodes: Numbers of I-nodes. Logical block size: The size of the chunks the UNIX system Kernel uses to read or write files. Mkfs: Construct a file system. q Only the superuser can use it.

The value of supermax is 512 bytes. q If the file system is not specified. q Only superusers are allowed. Example: .. umount: Unmount file system. then it reads from the file /etc/ checklist. Options: -y: Yes response to all questions. q If the mount command does not work on a file system. q On the damaged file system first run fsck. -f: Fast check. then mount it. q If the file system is damaged. then mounting is not possible. q If pwd is the mounted directory. -t: To specify the scratch file. q Unmounts the file system. -sx: Construct new free list ignoring the actual one. q It can be used to check the validity of the file system. q Prints the table if invoked with arguments. then mount is not allowed. q The initial / points to the new root.Unix India CompuMasterLtd. -q: Quiet fsck. mount [special_file directory [-r] ] q Directory should exist. -d: Check directories for bad blocks. chroot new root command q The given command gets executed relative to the new root. q Redirection is relative to the original root. q Only superusers are allowed to use this system. The size of the smallest chunk that the disk controller can read or write to. q Only a superuser is allowed. fsck: File system consistency check and repair. Mount: Mount a file system. chroot: Change root directory for a command. first run fsck on that file system. q If invoked with arguments. -n: No response to all questions. an entry is added in the table of mounted devices.

'newgrp' changes the group identification back.. real time and user time spent in execution of that command. q If '-' is present as the first argument. then executes the command with the modified environment. To ensure file system integrity. sync must be called before the system is stopped. time command After the command is complete. sync: Update the super block. q Increment range: 1-19. env: Display or set environment for command execution. time prints the system time. newgrp: Log into a new group. q The .. q Current directory remains unchanged. q Superuser may use a negative increment to run a command with priority. q command is executed exactly with the environment specified by arguments time: Time a command. find: Find files find path_name_list expression q Recursively searches the directory hierarchy for each path name. q Higher than normal. newgrp [-] [group] q Changes user's group identification. the environment changes. q Modifies environment according to its arguments.Unix India CompuMasterLtd. chroot / newroot is > x q Only the superuser can use it. env [-] [name = value] ……[command args] q Without arguments. nice [-increment] command q Runs a command with a lower CPU scheduling priority which is good for others and hence the name 'nice'. nice: Run a command at low priority. q Access permissions to files. sync q q Flushes the internal buffers to the disk. q Without arguments. q The user should be a member of the called new group.flag causes the inherited environment to be ignored completely i. . env prints current environment.e.

killall: Kill all active processes. System time The amount of time used directly by the UNIX system in the service of the command. od: Octal dump. q -k: The SIGKILL signal sent to each process. q Outputs the process ids of the processes that are using files as specified. they can be unmounted. od [-bcdosx] [file] [[+]offset [. fuser: Identify a process using a file or file structure.Unix q India CompuMasterLtd. kill [-signo] PID q Sends terminate signal to the specified process. q Only the superuser can terminate another user's process. q By default signal 9 is sent. -d: Interpret words in unsigned decimal. killall [signal] q Kills all active processes. the superuser mode is required. Times are printed on standard error.. -o: Interpret words in octal. Real time Total elapsed time from the beginning of the command execution until it ends. fuser[-ku] files[-] [[-ku]files] Options: q -u: The user login name follows the process id. -c: Interpret bytes in ASCII.] [b]] Options: -b: Interpret bytes in octal. q To kill other users’ processes. q Process number can be found using 'ps'. q Make mounted file systems unbusied and hence. User time The amount of time that the program spends executing it’s own code. kill: Terminates a process. q "kill –9…" is a sure kill. .

df [OPTION] [PATH]. [PATH]. not 512 despite POSIXLY_CORRECT. -l. -x. . --inodes List inode information instead of block usage. offset argument must be preceded by +. --no -sync Do not invoke sync before getting usage info. ps: It gives the snapshot of the current process. the offset is interpreted in blocks of 512 bytes. q Dumping continues until end-of-file. dd: Convert a file while copying it. -t. Options: -a. not just directories. --count –links Count sizes many times if hard linked. -k.. --print -type Print filesystems type.. --kilobytes Use 1024 blocks. --all Write counts for all files. is appended.. df: Summarize disk free space. the offset is interpreted in decimal. --total Produce a grand tota -k.. q By default. q If b is appended. -x: Interpret words in hex. -i. q If . --kilobytes Use 1024 blocks. q Offset argument specifies the offset in the file where dumping is to commence (by default octal).Unix India CompuMasterLtd. q If the file is omitted. du [OPTION]. --exclude -type = TYPE Limit the listing to not TYPE filesystems type -v (ignored). --type = TYPE Limit the listing to TYPE filesystems type. --bytes Print size in bytes. --sync Invoke sync before getting usage info (default). -T. Options: -a. -s: Interpret 16-bit words in signed decimal. output with -o option. --all Include file systems having 0 blocks. --portability Use the POSIX output format. -c. du: summarize disk usage. -P.. -b.. not 512 despite POSIXLY_CORRECT..

bc: An arbitrary precision calculator language. --bytes = N Output the last N bytes. [FILE]. -n.Unix India CompuMasterLtd.. With no FILE. [FILE]. --dereference Dereference all symbolic links. -q.. --summarize Display only a total for each argument. --verbose Always print headers giving file names. --one -file -system Skip directories on different filesystems. read standard input. Options: -c.. or when FILE is -. Options: -c. --silent Never output headers giving file names. precede each with a header giving the file name.. --lines = NUMBER Print first NUMBER lines instead of first 10. -S. --lines = N Output the last N lines. -v. --follow Output appended data as the file grows. With more than one FILE. With more than one FILE. For example. it displays the current month calendar as follows: June 1998 Su Mo Tu We Th Fr Sa 1234567 . --quiet. -x.. head: Output the first part of the file. read standard input. --silent Never print headers giving file names.. --separate -dirs Do not include size of sub-directories. --dereference -args Dereference PATHs when symbolic link. Print first 10 lines of each FILE to standard output. --bytes = SIZE Print first SIZE bytes. tail [OPTION]. -q. -f. With no FILE.. head [OPTION]. Print last 10 lines of each FILE to standard output... -D. when the user enters the cal command. echo: Display a line of text. -s. -L. tail: Output the last part of the file. --quiet. or when FILE is -. cal: Displays a calendar. instead of last 10. precede each with a header giving the file name. -n.

id: Display the current user and groupID name. --real: Print the real ID instead of effective ID. -f namefile: Read the names of the files to be examined from namefile one per line) before the argument list. -m: List specifying an alternate list of files containing magic numbers. -n. 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 file: Determine file type. --name: Print a name instead of a number. --group: Print only the groupID. This is usually used in conjunction with -m to debug a new magic file before installing it. -r: Remove the file upon completion of spooling or upon completion of printing (with the -s option). for –ugG. to test the standard input. Usually files are copied to the spool directory. for –ugG. or the value of the environment variable PRINTER is used. id [OPTION]. -u. or a colon-separated list of files. for compatibility with other versions. -h: Suppress the printing of the burst page. This can be a single file. Options: -v: Print the version of the program and exit. --user: Print only the user ID. [USERNAME] Print information for USERNAME. -z: Try to look inside compressed files. -G.. -s: Use symbolic links.Unix India CompuMasterLtd. Either namefile or at least one filename argument must be present. use "-" as a filename argument.. Options: -a: Ignore. -r. -c: Cause a checking printout of the parsed form of the magic file. or the current user. . The -s option will use symlink (2) to link data files rather than trying to copy them so large files can be printed. Options: -P: Force output to a specific printer. the default printer is used (site dependent). Normally.. -g. logname [option] lpr: offline print. -m: Send mail upon completion. logname: Print user's login name. --groups: Print only the supplementary groups.

Unix India CompuMasterLtd. -t: Do not print the 5-line header and the 5-line trailer that are normally on each page. -d: Double space the output.. -l: Load specifies that no new jobs (commands) should be started if there are others jobs running and the load average is at least load (a floating-point number). the other dependencies of these targets can be processed all the same. dir to search for included makefiles. pr: Convert text files for printing. -k: Continue as much as possible after an error. write user [@host] [ttyname] . removes a previous load limit. wall: Write a message to users. -o file: Do not remake the file even if it is older than its dependencies. -h header: Replace the filename in the header with the string header.g. target . -I dir: Specifies a directory. -d: Print debugging information in addition to normal processing. and do not fill out the bottoms of pages (with blank lines or formfeeds). The default is 72.. and those that depend on it. make: To maintain group of programs. and do not remake anything on account of changes in file. cannot be remade. -f: Use a formfeed instead f newline to separate output pages.. Options: -C dir: Change to directory dir before reading the makefiles or doing anything else. -F. -v: Print unprintable characters in octal backslash notation. then execute as usual or as otherwise specified. -b: Balance columns on the last page. print other unprintable characters in octal backslash notation. While the target that failed. With no argument. Wall [file] write: Send a message to another user. --version: Print version information on standard output then exit. -f: File Use file as a makefile. -c: Print control characters using hat notation (e. -e: Gives variables taken from the environment precedence over variables from makefiles. This means the files should not be modified or removed until they have been printed. ‘^G’). -w page –width: Set the page width to page-width columns. -p: Print the data base (rules and variable values) that results from reading the makefiles.. make [ -f makefile ] [ option ] . -I: Ignore all errors in commands executed to remake files.. Essentially the file is treated as very old and its rules are ignored. Options: -a: Print columns across rather than down.

tty: Print the file name of the terminal connected to standard input.. When you have finished typing your message. Each line that you type will get displayed on tim’s terminal. --quiet Print nothing. write allows you to send a message to any other logged-in user. making it inconvenient for you to talk directly with that person. The user will then have the option to send you a reply by initiating a write command from his or her own terminal. If tim is not currently logged in. As a matter of convention. The tty number designates the terminal that the message is to be sent to. The message that you send will appear on that user’s screen. sleep: Delay for a specified amount of time. only return an exit status. This message can contain as many lines as you like. This will terminate the write and display the line <EOT> on tim’s terminal to tell him you have finished. As an example. the system will wait for you to type your message. Print the file name of the terminal connected to standard input. the write command can be used as an alternative to calling that person on the telephone.. This later information is needed when there is more than one logger-in user with the same user-id. or even in another planet location. The syntax of the write command is write user tty where user is the user-id of the logged-in user and tty is an optional tty number. the command write tim tells the UNIX system that you wish to start a conversation with the user tim. With this technique.. UNIX in the Office "Talking-to" other users: If you want to send a message to a user who is logged in on another terminal which is located in another office on the floor. -s. --silent..Unix India CompuMasterLtd. m for minutes. NUMBER [SUFFIX] Pause for NUMBER seconds. sleep [OPTION]. .. h for hours or d for days. enter CTRL-D as the first and only character on the line. tty [OPTION]. SUFFIX may be s to keep seconds. then the following will occur: $ write tim tim is not logged on $ After initiating the write. two users can effectively have a "conversation" through their terminals. most UNIX users typically end their message lines with the characters -o to tell the other user that their message line is finished and that they are awaiting a reply.

the mail program will then wait for you to type your message to be sent to the user..Unix India CompuMasterLtd. is that the latter requires that the person be logged in at the time that the message is sent. The character -oo are often used to signal the end of the conversation. memos. The format of the command to send mail to a user is simple: mail user where the user is the user-id of the person you want to send the mail to. UNIX treats everything as a file and accesses directories and . it can be done by using the command mesg. to inhibit incoming messages while you are editing a file. Under the UNIX system. after your edits are complete. the Shell automatically checks to see if you have received any new mail. the mail command handles the sending and receiving of electronic mail. without the use of paper. Once this command line has been typed. the former specifies that you don’t want to receive any messages. Periodically. that is. or any types of documents to other users electronically. Then. the latter specifies that you do. This command takes a single argument n or y. If you have. The main difference between sending a message to someone using the electronic mail facility and the write command. So. you can tell the system that you’re willing to again receive messages by typing the command mesg y Electronic mail Electronic mail are the most common words used these days. You have mail FILE SYTEM INTERNALS Partitions and file systems The four components of a file system Disk blocks and i-nodes The Structure of the I-node As it is already known. the mail is automatically kept by the system until the user issues the necessary command to read his or her mail. Inhibiting messages with the mesg command If you don’t want to receive any messages while you are doing some important work and don’t want to be disturbed. then you will get the following message displayed at your terminal. With electronic mail. Electronic mail gives you the ability to send messages. type the command mesg n before you enter the editor.

The disk partition should be made for it. Normally. This chapter thus contains material mainly useful to him. Partitions and file systems Like other operating systems.. and has a lot of advantages: q If the entire operating system along with the user files and directories is in one big superstructure. as this knowledge is necessary for the maintenance of a healthy and correct file system. q Backups can be made easy.Unix India CompuMasterLtd. If the system has an adequate number of partitions. After the disk has been formatted the next step is to divide it into several slices called partitions. This can be prevented by disk partition. devices with the same commands. there may be conflicts that may arise between the various data areas. However. the administrator can’t afford to remain ignorant of the file system internals. Formatting creates address information on the platters of disk and also isolates bad sectors. the job of system administration becomes a lot easier. other areas are effectively shielded from this evil influence. Knowledge of file system is essential for the system administrator in order to be able to fix inconsistencies that tend to crop up. Each partition can contain a file system as shown: disk drive . and if there is corruption in one area. then each partition can be backed up separately in a single volume of tape. q By all these. the formatting operation lies outside the operating system’s domain and is carried out by special utilities supplied by the vendor. q If the disk is partitioned. a formatted hard disk (also known as fixed disk) must be made available before UNIX can be installed. if the disk is partitioned.

Disk drive. partitions. and a filesystem The following figure shows the filesystem in more detail: directory blocks and data blocks file system in more detail f1 First data block f2 second data block .Unix India CompuMasterLtd..

Since this is a vital activity.This is the partition that no user is allowed to access but is used by the Kernel to control the movement process. should be known to write something into a block. q The /usr partition . Even though the blocks are numbered consecutively. these are the four components. Disk blocks and i-nodes Every hard disk or floppy diskette is organized into blocks (or sectors) where all data resides. This area includes the system’s root directory. q The swap partition . which breaks up each partition into number of distinct components. but the vast majority of them are set aside for the users to store all their data and programs.Unix India CompuMasterLtd.This is the area where a lot of heterogeneous material can be found. When the file system gets corrupted. f3 third data block Most UNIX systems usually will have at least the following three partitions: q The root partition . The address. The creation of a file system for each partition is done with the mkfs command. The super block: It contains global information about the file system. which every file system will have: The boot block: This block contains a small boot program. Some of these blocks are kept reserved for use by the Kernel. All attributes of a file and directory are stored in this area except the name of the file or directory itself. /dev. The I-node blocks: This region contains a table for every file of the system. which should be unique. When these swapped processes are ready to run. Additionally.This is the partition that is used for the bringing up and keeping the system in single user mode.. the Kernel has to move process out of memory to a special area of the disk called the swap area. because if fsck throws out frightening messages and prompts him for approval of the actions to be taken. they are loaded back to memory. The data blocks: All data and programs created by the users reside in this area. it also maintains a free list of I-nodes and data blocks. When the system memory is heavily loaded. and /etc directories and are generally sufficient to keep the system going. The four components of a file system The knowledge of how the Kernel and the components of the file system work in tandem to organize the allocation space for files is necessary. The entire file system is organized in a sequence of blocks. one may not find the data . mainly for the system administrator. which can be immediately allocated by the Kernel when creating a file. To briefly recapitulate. all numbered from zero to a number determined at the time of its creation. He should have an adequate knowledge of the system internals. every system has a swap partition. he has to run the fsck command to correct the distortions that take place. he must make quick and correct decisions. The system administrator uses this mode for performing his maintenance tasks. the /bin.

by a file. write and execute) for both owner. This value for any file can be found out with –i option of ls: q $ ls –il icmcomp 9087 -rw-r--r-. All I-nodes are collectively stored in I-node blocks. Note that this number is not stored in the I-node. The I-node also records when a file was last read. Finally. Because the blocks of a file are scattered throughout the disk. arranged contiguously in another dedicated area of the file system and preceding the data blocks. directory or device). These are called data blocks. UNIX has a complicated but elaborate scheme for maintaining all the disk block addresses of a file. if more bytes are added to it. ten contiguous blocks. A unique number. if a file created occupies say. no doubt. q Size in bytes. For instance. wherever they may be. linked lists of addresses of all the blocks used. Later. which simply references the position of the I-node in the list. of file arranged in contiguous blocks. A disk block contains 16 64-byte I-node. which require a further three blocks. This. since they are arranged contiguously in the disk. q A linked list of disk block addresses. and last changes of the I-node. The Structure of the I-node An I-node for each file contains: q File type (regular. and contain only data and not the file attribute. group owner and others. The user may not be lucky enough to find sufficient free space immediately after those ten blocks. leads to disk fragmentation.1 kiran 51813 jan 13 1998 11:30 icmcomp . q Permissions (read. q Time of last modification and last access of file. it simply provides another name for a file. which maintains among other things. For every file. sometimes called an I-node number. this fragmentation also allows files to be enlarged or reduced at will and keeps wastage to a minimum. and not just the starting and ending block addresses. which may be occupied by another file. if the user has forgotten what a link is. However.Unix India CompuMasterLtd. q Owner and group owner. This table should also contain practically all the file attributes except the file name. written or executed (accessed) or modified or had any of the I-node parameters changed. there has to be a table. identifies every I-node. q Number of links. but its table is easily located. The remaining data then has to be written to the next free blocks that are available. it is obvious that the addresses of all its blocks have to be stored.. The table is called an I-node. normally 64 bytes long (if the filename is restricted to 14 characters) and maintained individually for each file. and consequently increases the overheads of read/write operations. This situation immediately leads to two requirements: A separate (also the largest) portion of the file system has to earmark for all data contained in files.

there is also another block.e. and the remaining entries are flushed out with zeroes. The first ten addresses are simply enough. so one logical block contains two physical blocks. Apart from these direct data blocks. the eleventh entry doesn’t store the address of the eleventh block of the . Each block has one unique address. The complication begins when files exceed ten blocks. LOGICAL AND PHYSICAL BLOCKS The block addressing scheme Addressing a Directory The Boot Block The Super Block Role of super block in file creation The immediate block after the I-node block is the data block. the first three entries in the table contain the disk block numbers. there are also indirect blocks which contain the addresses of the direct blocks. the physical block. However. if you use only 3 bytes for writing. A physical block is 512 bytes long. Apart from a logical block.. The block addressing scheme Even though the I-node is only 64 bytes long. which is often called a logical block. If a file is only three blocks long. which read and write one character at a time. In that case. Unlike terminals and printers (character devices).Unix India CompuMasterLtd. the remaining 1021 bytes will be wasted. floppy diskettes and tapes (block devices) handle data in chunks or blocks. No other file in the same file system can have this number unless the file is removed. The standard system V block size is 1024 bytes. hard disks. The I-node maintains list of these indirect block addresses. reserving space for ten addresses in the I-node table doesn’t mean that ten disk blocks are automatically allocated. These blocks may occur either in a contiguous or fragmented manner in the disk. These blocks contain all data contained in files and directories. it is sufficient to keep track of the addresses of even very large files. It will be interesting to see just how these thirteen addresses suffice to locate the data. They contain the disk addresses of the first ten blocks of the file. $ The file icmcomp has the I-node number 9087. the Kernel will allocate this number to a new file. There are thirteen entries (or addresses) in the I-node table containing the address of upto thirteen disk blocks. Unlike the first ten entries of the address table. a large amount of wastage will be there i. For this.

and not simply the number storing only the data. Hence. so you can now have up to 256 double indirect blocks. Combine it with the -l and -i (I-node) options. but appropriates space from the data block area. i. this entry comes into play when the file size exceeds 266 blocks. which in turn contains the addresses of upto 266 single indirect blocks. finally becomes 16. counts the number of bytes read. A double indirect block itself is thus able to access 65. Instead. The maximum size a UNIX file system can support. you should remember that the block sizes shown by the df.536 + 266) blocks. the thirteenth and final entry of the address table points to what is called a triple indirect block. When you read a file larger than ten blocks (i. The location information of a file.e. This also consumes one disk block for storing the 256 addresses. as shown in the output. The file indeed uses 96 physical blocks to store its data. an additional logical block. find and ls commands actually refer to the number of blocks allocated for the file.018 (256 * 256 * 256 + 65802) blocks. and you can see as many file attributes as is possible with this command. This is the way it should be. since the file uses a partial block. because there is no other way for the Kernel to know that the end of file has been reached. requires indirect block addressing). Each of these single indirect blocks in turn contains 256 addresses. This block is known as a single indirect block. Dividing 48527 by 1024. Actually.843. which raises the file "consumption" size to 96 + 2 = 98 blocks. The maximum size of a file now gets enlarged to 65. file. the services of a single indirect block are required to support this file size.e. since the file size exceeds 10KB. and then rounding off to the next higher integer. it contains the addresses of upto a further 256 data blocks (for a 32 bit machine and having a logical block size of 1024 bytes). Using an additional entry in the I-node table now increases the maximum file size to 266 (10 + 256) blocks. each being able to address 256 single indirect blocks. two physical blocks. it has the address of another block that is neither an I-node block nor a data block.802 (65..536 (256 * 256) disk block addresses and used for file sizes from 267KB to 64MB. Finally. it instructs the disk driver to move the heads to the respective blocks. Each of 256 addresses of the block further points to a double indirect block. It points not to a single. Predictably.Unix India CompuMasterLtd. The single indirect block is now the "second address" for the file. So. matches it with the file size and reads till the two numbers match. It then reads the indirect block and all it’s associated direct block entries. But.e.1 kumar group 48527 mar12 14:21 icm1 $ The second column shows the file size in 512 byte blocks. but to a double indirect block. using this triple indirect block. the Kernel goes to the corresponding I-node table. q $ ls –lis icm1 305 98 -rw-r--r-. notes the file size (that is also in the table) and the direct block addresses. Now that the eleventh entry of the address table points to (i. ls uses the -s option to list each file in physical blocks. represented by the array of thirteen pointers. du. applies . This should be 48 logical blocks or 96 physical blocks. This implies a phenomenal 17 gigabytes.contains the addresses of) another disk block.

but eventually it will load the Kernel into memory. which is loaded into memory when the system is booted. only to non-special files. the Kernel does not have to scan the disk. they are usually never required. it looks up the list instead. it searches the I-node blocks and locates the I-node for this directory. the address of the data block that contains the directory file. Since the first ten entries in the I-node address table point to ten disk blocks. A directory is a 16-byte structure. with two bytes reserved for the I-node number and 14 bytes for file name.Unix India CompuMasterLtd. as the list is always updated. A directory file which contains 640 file entries. The first block is known as the boot block. . and then accesses the file using the disk block addresses. The Kernel now reads the disk address entries. When a file is opened. Therefore. which may only slow down file access. and . you should not let a single directory have more than 62 files. It is not easily displayed either. this area does create problems when I-node structures become inconsistent necessitating repair. The UNIX system also maintains an I-node table in memory for the file. What happens when you issue the command cat icm? The file is displayed on the standard output. directories. then the directory structure can house 64 (1024/16) files. and hence quite reliable. The efficiency of the system is greatly increased. but only after the Kernel has taken the following steps: The Kernel must first know the I-node for the current directory that is always maintained in memory. and ordinary users can afford to remain ignorant about them. Using this number. It fetches from this I-node. for efficient organization of files. which is being used by it. Note that you know the addressing mechanism used by the Kernel for accessing files and directories. its I-node is copied from the hard disk to the system’s own I-node table. and the startup operations commence. If a directory file is allowed to occupy an entire logical block. The file/UNIX is loaded. the Kernel looks for the file icm and its I-node number. the bootstrapping program is read in from the boot block of the root (main) file system. load another program from the disk. For other file systems this block is simply kept blank.. However. Addressing a Directory Most of the preceding discussion applies equally to directories. When a file is created.. having the number 0. This program may in turn. The Boot Block: Preceding the I-node blocks are only two blocks. However. Two of these are occupied by the. From the directory file. The system also maintains in the super block a partial list of free I-nodes that can be immediately allocated when files are newly created. This contains a small bootstrap program. It then goes back to the I-node blocks and locates the I-node for the icm. which spring into existence when a directory is created. This is an enormous amount of work.

This ensures that it is nearly as recent as its copy in memory.. q Last time of updating. . The Kernel then decrements the free I-node count that is also maintained in the super block when it finds the free I-node list empty. the super block maintains a working list of those free data blocks that are available for allocation. a complete list of free data blocks is kept in a separate area of the disk. Now. the disk super block needs to update in a periodic manner. The Super Block: Next comes the block known as the super block (numbered 1) that contains global file information about disk usage and availability of data blocks and I-nodes. This is mainly what it contains: q The size of the file system. and fills the super block array with fresh list. it immediately scans the I-node blocks in the disk. a copy of the super block is also kept in memory. unlike the I-node whose free status is determined by status of the file type field. because the information on the file system needs to be written to disk before the power is turned off. with one notable difference. and fills the super block array. From the "address book". Since the super block maintains a list of free I-node numbers.Unix India CompuMasterLtd. initialized and written back to disk. The Kernel first reads this area before allocating disk blocks and I-nodes for new files. From time to time. This is because a disk block itself doesn’t contain any mark to indicate whether it is free or not. as the system checks this information during the booting. The Kernel reads and writes the copy in memory when controlling allocation of I-nodes and data blocks. q A partial list of immediately usable I-nodes. it immediately reads this address book and not the disk blocks directly. q Number of free I-nodes available. Unlike the I-nodes. Role of super block in file creation: Let’s see the importance of the super block. The mechanism of allocation is similar to that for I-nodes. along with a number of other parameters. The machine is powered down by going through a formal routine. The disk copy of the I-node is then read into the memory. This complete list is initially created during the creation of the file system itself with the mkfs command. While one disk block is kept reserved for storing this information. When the Kernel finds the free block is list empty. there may be serious damage caused to the file system. q The length of disk block. never be newer than the corresponding copy in memory. therefore. If that is not done. the disk blocks have to be allocated. q The number of free data blocks available. q A partial list of immediately allocable free data blocks. therefore. be correct for the healthy operation of the system. Information recorded on super blocks should. Since the Kernel works with the memory copy of super block rather then the disk copy. the Kernel takes the next I-node number from the list and assigns it to the file. it updates the disk copy of the super block with the memory copy. The disk copy can.

depending on the number of physical disks that the machine may have. This organization of directories into multiple file systems is normally done at the time of installing the UNIX operating system. and which change frequently should have a file system of its own. After that is done. who uses a different login name to perform his duties. Multi-volume tapes are usually not supported by the UNIX backup commands. then its size should not be more than the capacity of the backup device. Partitioning a disk into several file systems has distinct advantages. it needs to be first formatted with the format command available in all systems. because some of his tools act and report on each file system separately. the mkfs (make file system) . it is quite common to have the /usr directory in a separate file system. Thus. the superblock and the boot block typically describes the structure of a single file system. Further. can use the commands featured in this section. you can also use a floppy diskette to create a file system on it. so make sure that the file system size is smaller than the capacity of the tape. there can be multiple file systems. Using a Floppy Diskette to Create a File System While you can have multiple file systems on fixed disks. as the data will normally be found clustered together. Only the system administrator.. CREATING A FILE SYSTEM The mkfs command The Hard Disk Engaging and Disengaging File Systems File system checking with fsck Process Termination Environment List Memory layout of a C program The mkfs command: The data blocks. It makes the system administrator’s task easier. sometimes. I-node. This reduces disk fragmentation a great deal. if a file system is to be backed up. To create a file system on a diskette. But. a single disk can also be partitioned into two or more file systems.Unix India CompuMasterLtd. It is just as well that we should choose the floppy diskette as the device for illustrating the various file systems commands since we access the floppy much more directly than we access the hard disk. Data which can be grouped into a category.

The character mode files of floppy devices are store in the /dev/dsk directory. You can override this default option: q $ mkfs /dev/rdsk/foq15dt 2400:250 The two parameters (2400 and 250) are separated by the (:). one fourth of the number of logical blocks (i. mkfs initializes the file system so that it has an empty root directory. These relate to the organization of the hard disk. and it is not possible to control the movement of a head individually. This knowledge is necessary when you create a file system on a disk with the mkfs command. There is a magnetic head for reading and writing each surface.e. Optionally. Each track is further broken up into sectors or blocks. you can also specify the number of I-nodes that you need to allocate on the disk. You can then visualize a cylinder comprising all tracks bearing the same number on each disk surface. using a maximum of 2400 blocks.m). The Hard Disk The system administrator should understand the significance of a couple of parameters related to the hard disk. Typically. Though diskettes are block devices (i. however.e. they read and write in blocks). A physically block normally stores 512 bytes of data. the figure that was specified. two surfaces). This specification indicates that there can be a maximum of 250 files on the diskette. which is usually. Note that there should be no white space on either side of this delimiter. which have numbers 0. all the blocks of tracks pass through the . Each surface is composed of a number of concentric tracks. mkfs optionally accepts two important parameters . one eighth of the number of physical blocks). 2 etc. the system assumes a default value. This figure. they have a character mode as well. there will be as many tracks bearing the same track number as there are surfaces. varies across systems.e. The disk is spinning constantly (normally 3600 r.. there will be as many as many cylinders in the disk as there are tracks on each usable surface.2 MB). When it is omitted. The diskette fills up whichever figure is attained first. they will require eight heads.the rational gap. Since a disk may have more than one platter (i. command can be used to create a file system on the device that follows the command. The disk heads move radial from track to track. The raw device has to be specified here because it erases the contents of the disk and creates a directory table instead. and a head is above a single track at any point of the time. So. The heads move in tandem. a 1. and the number of blocks/cylinder. and when the head is positioned above a particular track.2 MB floppy requires 1200000 / 12 = 2400 physical blocks.p. each of which has surfaces. 1. The other argument specifies the number of blocks to be used for the new file system. if there are eight usable surfaces. Every disk contains one or more platters. When used in this way: q $ mkfs /dev/rdsk/foq15dt 2400 it creates a file system on the raw floppy device (1.Unix India CompuMasterLtd. So.

and the number of blocks skipped is known as rotational gap.e. you have to mount (i. the root file system in the root partition) doesn’t even know of its existence. this takes some time. head in a very short time. a number of blocks of the track would have passed through the head already.e. The latter is actually the number of usable surfaces multiplied by the number of blocks per track. Now.. Moreover.. as a subdirectory of /usr/Kumar. the lower is the gap. You need to specify the absolute pathname of this directory as an argument to mount to indicate to the Kernel that it is this directory under which the file system is to be mounted: q $ mount /dev/dsk/foq15dt /usr/Kumar/safe $After this device is mounted. indicated by the directory /usr/Kumar/safe. The mount and umount commands: Once a file has been created. block number 2) is read. for all practical purposes. when a block of data (say. The attachment made between the root directory of the new file system and an empty directory (usually / mnt) in the main file system process is called mounting.Unix India CompuMasterLtd. and before the head starts reading again. By skipping blocks the possibility of the disk making full turn to make the next block available is greatly reduced. . so it is obvious that the layout scheme has to be different. the root directory of the file system created on the floppy loses its separate identity. When you use the mkfs command to create a file system. It now becomes the directory /usr/Kumar/safe. and made to appear as if it is part of the main file system. The higher the transfer rate. it is transferred to the buffer of the disk controller. it just sits in a stand-alone mode with an empty root directory: the main file system (i. The bypassing of sectors is known as interleaving. and is achieved with the mount command. you can neither "cd" to the root directory of the file system. if you want to have a sub-directory /usr/kumar/safe in the diskette. The main file system is thus logically extended with the incorporation of the secondary file system on the diskette. This simple numbering scheme of blocks will greatly increase the read/write times.. nor can you access its files. you first have to create this sub-directory in the main file system: q $ mkdir safe $ cd safe $ pwd /usr/Kumar/safe Once you have done that. Engaging and Disengaging File Systems. Remember that the objective is to have free access to the floppy so that you can consider it. engage) the secondary file system at this point. you may need to specify this gap as well as the number of blocks/cylinder. The point at which this linkage takes place is called the mount point. the disk has to make a complete turn before block number 3 comes up for reading. If the blocks are numbered sequentially. For instance.

implying that all the Shell script files have been copied onto the floppy diskette. and then issuing ls. try copying the script files to the subdirectory safe: q $ cp *. Many installations have the /usr/man and /usr/spool directories as separate file systems. However.sh safe $When you issue this command. These attached file systems are then made to look as one to the user. Try changing your directory to safe. Even though this directory has been created on the main system. q Two i-nodes can claim the same disk block. and then issue umount from there. inconsistencies tend to crop up in the information maintained at these places. There is one important rule that needs to be observed when you issue this command. You have thus managed to create a backup of your files on the floppy. However. To make sure. To verify that nothing has actually been copied to the directory safe in the main system. Make sure that no one is using the device. Some of the more common ones are listed below. that doesn’t prove yet that the sub-directory safe in the hard disk doesn’t also have these files. indicating that the files are being copied onto the floppy. You will see the floppy drive lighting up again. This is achieved with the umount command. For instance you can't "cd" to safe. After the attachment process is complete. so that consistency is maintained. Since the file system has been mounted at that point. These relate mainly to the in-memory copies of the superblock and i-nodes and their disk counterparts. these files won’t be copied there. the Kernel free its data blocks and returns them to the free list maintained in the in-memory copy of the superblock.. it is better to "cd" to the home directory. you must be positioned in a directory which is not part of the file system you are trying to umount. File system checking with fsck The file system is one of the most sensitive components of the UNIX system. and then issue the command. which takes only the device name as the argument: q $ /etc/umount /dev/dsk/foq15dt $ ls safe $There are no files in the directory. It is worth nothing that the sub-directory safe could be used as a mount point only because it was empty. It maintains all information pertaining to files at a number of places. and then empty the mount point. Remember to mount it again when you use it. It can then assign one of these disk blocks for use by another . You can use this directory just like any other in the main file system. the light on the floppy drive glows. you must first unmount the file system you just created on the diskette. These file systems are mounted at start up. across file systems. When a file is deleted. Now the floppy can be removed and stored in a safe place. with suitable links between them. where you can copy your files or even "cd" to it. Several things can happen to cause these mismatches. The device can now be considered to be mounted and you can treat it as a secondary hard disk. or power failure causes a system crash. the separate identity of the root directories of all these systems disappears.Unix India CompuMasterLtd. except that you can’t link files. if the system is not shut down properly.

But before updating the old I-node. It checks the i-node types. The file type may also be invalid. This can easily happen if the i-node of a removed file is written to disk just before the system crashes and the superblock is left unwritten to disk. Since each file in a directory uses 16 bytes. which is out of range. A block may neither be in the free list nor be used by an i-node. This file usually contains a list of all the active file systems.Unix India CompuMasterLtd. the file is then effectively not available in the file system. You now have an outdated superblock. not always the case. the size of the directory must normally be a multiple of 16. There can also be unreferenced directories or I-nodes with incorrect link counts. however. If the system crashes after the file is created. You can have a bad block number. This is. An i-node is neither in the free i-node list nor in any directory. If the power goes off the Kernel writes the data and i-node of the new file to disk. q Phase1: The first phase scrutinizes the file system’s i-node list. An allocated file doesn’t have at least one directory entry. You can have a totally garbled directory. when the file system is corrupted. It also detects variations between a file size and the number of blocks used by its i-node. The command takes the name of the file system as the argument. there will be two claimants of the same i-node. q q q q q file. There may be a mismatch between the number of blocks used by the i-node and the file size.. filenames may contain an embedded link. but before updating the directory entry. leading to directory misalignment.Check Free List When used with the arguments. The i-node may show a zero link count. the command checks all the file systems specified in the file /etc/checklist. An I-node must have as many directory entries as there are links and this situation may get disturbed. and the latest copy of the i-node not showing the disk block number in the list. but can be invoked without any argument as well. One of the most important functions of the system administrator is the maintenance of the integrity of the file system. along with questions. He should make use of the services of fsck command to achieve this task.Check Blocks and Sizes ** Phase 2. their format and the block numbers for bad and duplicate blocks. and yet otherwise have a valid format.Check Reference Counts ** Phase 5.Check Connectivity ** Phase 4. The output that you see above is normal when the file system is consistent. not having the block in the free list. which are out of range. which you have to answer correctly. q fsck ** Phase 1. you will see plenty of messages on the system console. or there may be i-node numbers in a directory. It declares a block "BAD" if the block number is out of range and "DUP" if it ] . The i-node format could be grabbled. However.Check Pathnames ** Phase 3. Moreover.

which replaces the erroneous free block list with a newly computed one. starting from root. ******** BOOT UNIX (NO SYSC!) ********** This means that if the administrator uses sync or /etc/shutdown to write the incorrect superblock disk. Sometimes. simply specifies the maximum size currently set: q $ ulimit 2097151 $ This indicates that the maximum file size is restricted to 2097151 physical blocks. and when invoked without an argument. the default ulimit is set inside the Kernel. Unreferenced directories are reported as error messages. Apart from i-node. Normally.. it prompts the administrator before repairing any damage that it has detected. fsck checks the link count as stored in the I-node with the directory entries from the information generated in the second and third phases. with the user’s approval. Phase 2: This phase checks all the directory entries. When fsck runs in rectification mode. A salvage operation may be carried out. It is Shell built-in. fsck also checks whether all directory file sizes are exact multiples of 16. which should have been in this list but are not. Phase 5: Finally. When used with options. The ulimit statement of the Shell sets the maximum size of files. fsck’s free block count is compared with the figure maintained in the superblock. If bad or duplicate blocks were detected in Phase 1 or 1B. . but once it detects a single duplicate block. he should the immediately press the reset button and reboot the system.Unix India CompuMasterLtd. and this is checked in the third phase. as well as for unused blocks. then fsck prompts for their removal in this phase. Phase 3: Proper connectivity requires that each I-node has at least one directory entry. for "OUT OF RANGE" i-node numbers. the fsck rebuilds the file system. it rescans the disk for more duplicates. It may flash the following message. Improper link counts may now be fixed. q q q q q q is claimed by another i-node. The free block list is checked for bad and duplicate blocks. it automatically proceeds with the repair of every damaged file. the entire good work dome by fsck would be lost. the in-memory copies of the superblock and other tables that may contain old and inaccurate information. This is required when the root file system contains a serious problem. the information on the disk being more recent than the memory copy. Phase 1B: This phase is normally not seen in fsck output. fsck operates by displaying the error message that is encountered in each phase. Phase 4: Here. Phase 6: The last salvage operation in Phase 5 leads to this phase. Setting maximum file size with ulimit There should be a restriction on maximum size that a user can be permitted to create. An ordinary user can also issue this command with an argument to reduce the default value.

This function creates a new directory entry. If the newpath already exists. rename function A file or directory is renamed with the rename function. Returns 0 if OK. Note: To unlink a file we must have write and execute permissions in the directory containing the directory entry. const char *newname).h> int rename(const char *pathname). Syntax: #include <stdio. This function removes the directory entry and decrements the link count of the file referenced by pathname. If there are other links to the file. -1 on error. newpath. Returns 0 if OK. remove is identical to rmdir. -1 on error. For a file. Syntax: #include <unistd. remove function A file or directory can be unlinked using the remove function. link function The link function is used to create a link to an existing file. that references the existing file existing path.Unix India CompuMasterLtd. Only when the link count reaches 0 can the contents of the file be deleted. .h> int link(const char *existingpath. Syntax: #include <unistd. Returns 0 if OK. an error is returned. remove is identical to unlink. since it is the directory entry that we may be removing. unlink function This function removes an existing directory entry..h> int unlink(const char *pathname). const char *newpath). For a directory. Syntax: #include <stdio.h> int rename(const char *oldname. -1 on error. the data in the file is still accessible through other links. The file is not changed if an error occurs.

Most UNIX Shells provide a way to examine the exit status of a process. the exit status is undefined.. char *argv[]).Unix India CompuMasterLtd. main does a return without a return value. #include <unistd.h> void _exit(int status). Command-Line Arguments When a program is executed. usually cc. the exit function is called. argc is the number of command line arguments and argv is an array of pointers to the arguments. If either of these functions is called without an exit status. #include <stdlib. This is set up by the link editor when it is invoked by the C compiler. which performs certain cleanup processing and then returns to the Kernel. that if the main function returns. and exit. When a C program is started by the Kernel. This startup routine takes values from the Kernel and sets things up so that the main function is called. Normal termination: a)return from main b)calling exit c)calling _exit 2. -1 on error. exit and _exit functions Two functions terminate a program normally: _exit which returns to the Kernel immediately. The executable program file specifies this startup routine as the starting address for the program. a special startup routine is called before the main function is called. Both the exit and _exit functions expect a single integer argument.h> void exit(int status). the process that does the execution can pass . which is called the exit status. Process Termination There are five ways for a process to terminate: 1. Returns 0 if OK. Abnormal termination: a)calling abort b)terminated by a signal The startup routine is written such. ENVIRONMENT OF A UNIX PROCESS main function A C program starts execution with a function called main. or main ‘falls off the end’ (an implicit return). The prototype for the main function is int main(int argc.

For example. argv[i]). #include <stdio. i < argc./echoarg arg1 TEST foo argv[0]: . i++) printf("argv[%d]: %s\n". i. then. it could look like this: . extern char **environ. exit(0).h> #include <stdlib. The address of the array of pointers is contained in the global variable environ. with each pointer containing the address of a null-terminated C string. Like the argument list. $ . /* echo all command line arguments */ for (i = 0. } If the above program is compiled and the executable named as echoarg.h> int main(int argc. q Example: The following program echoes all command line arguments to standard output. command-line arguments to the new program.Unix India CompuMasterLtd. char *argv[]) { int i.. the environment list is an array of character pointers./echoarg argv[1]: arg1 argv[2]: TEST argv[3]: foo Environment List Each program is also passed an environment list. if the environment consisted of five strings.

. Data in this segment is initialized by the Kernel to arithmetic 0 or null pointers before the program starts executing. along with information that is saved each time a function is called. q Heap: Dynamic memory allocation usually takes place on the heap. Each time a function is called. the null bytes have been shown explicitly at the end of each string. The newly called function then allocates room on the stack for its automatic and temporary variables. q Uninitialized data segment: This segment is often called the "bss" segment. q Stack: This is where automatic variables are stored. and certain information about the caller’s environment is saved on the stack.Unix India CompuMasterLtd. Environment consisting of five C character strings In the above figure. the address of where to return to. q Initialized data segment: This is usually called the data segment and it contains variables that are specifically initialized in the program. Memory layout of a C program A C program in general comprises the following pieces: q Text segment: These are the machine instructions that are executed by the CPU. The following figure shows the typical arrangement of these segments: .

Unix India CompuMasterLtd. void *realloc(void *ptr.. size_t newsize). size_t size). q #include <stdlib. void *calloc(size_t nobj. All three return a non-null pointer if OK. void free(void *ptr). q calloc: Allocates space for a specified number of objects of a specified size. The space is initialized to all 0 bits. NULL on error. The initial value of memory is indeterminate. . q realloc: Changes the size of a previously allocated area.h> void *malloc(size_t size). Typical memory management Memory Allocation There are three functions in ANSI C for memory allocation: q malloc: Allocates a specified number of bytes of memory.

The object is normally a structure that contains all the information required by the standard I/O library to manage the stream.Unix India CompuMasterLtd. and Standard Error Character at a time I/O Line at a time I/O Direct I/O Formatted I/O Pipes A Unix pipe The standard I/O library handles details like buffer allocation and performing I/O in optimal-sized chunks. the file descriptor used for actual I/O. To reference the stream. the size of the buffer. FILE *fp). const char *type). its file pointer is passed as an argument to each standard I/O function. . stdout and stderr. FILE *freopen(const char *pathname. it may be considered as if a stream is associated with that file.h> header.. FILE *fdopen(int filedes. Standard Input. These three standard I/O streams are referenced through the predefined file pointers stdin. obviating the need to worry about using the correct block size. const char *type). and the like. STANDARD I/O LIBRARY Standard Input. and Standard Error Three streams are predefined and are automatically available to a process: q standard input standard output standard error These refer to the same files as the file descriptors STDIN_FILENO. the standard I/O function fopen returns a pointer to a FILE object. NULL on error. const char *type. an error flag. a count of the number of characters currently in the buffer. When a stream is opened. All three return file pointers if OK. These three file pointers are defined in the <stdio. When a file is opened or created with the standard I/O library. Standard Output. STDOUT_FILENO and STDERR_FILENO.h> FILE *fopen(const char *pathname. Opening a Stream The following three functions open a standard I/O stream: #include <stdio. a pointer to a buffer for the stream. Standard Output.

h> q int getc(FILE *fp). fopen opens a file. a or ab Append.h> q int fclose(FILE *fp). The difference between the first two functions is that getc can be implemented as a macro. freopen opens a specified file on a specified stream. closing the stream first. EOF on end of file or error. This means the following three things: q The argument to getc should not be an expression with side effects. open for writing at end of file. The syntax is: #include <stdio.. Returns 0 if OK. Type Description r or rb Open for reading. . EOF on error. a+ or a+b or ab+ Open or create for reading and writing at end of file. r+ or r+b or rb+ Open for reading and writing. The following are the different arguments for the type argument: An open stream can be closed by calling fclose. int fgetc(FILE *fp). it allows the address of fgetc to be passed as an argument to another function. All three return the next character if OK. Character at a time I/O Input functions: The following three functions are used to read one character at a time: #include <stdio. or create for writing. while fgetc cannot be implemented as a macro. int getchar(void). w+ or w+b or wb+ Truncate to 0 length or create for reading and writing. fdopen takes an existing file descriptor and associates a standard I/O stream with the descriptor. Since fgetc is guaranteed to be function. if it is already open.Unix q India CompuMasterLtd. the following three types of functions are used to read or write into the stream: q Character at a time I/O. Direct I/O. w or wb Truncate to 0 length or create for writing. Line at a time I/O. Reading and Writing a stream Once a stream is opened.

These three functions return the next character as an unsigned char converted to an int. exit(0). int feof(FILE *fp).h> char *fgets(char *buf. These functions return the same value whether an error occurs or an end of file is reached. To distinguish between the two. All three return c if OK. } Line at a time I/O Input functions: Line at a time input is provided by the following two functions: q #include <stdio. Each correspond to their respective input functions: #include <stdio. int putchar(int c). int n.Unix India CompuMasterLtd. 0 otherwise. Both return nonzero if condition is true. void clearerr(FILE *fp). #include <stdio. if (ferror(stdin)) printf("input error"). stdout) == EOF) printf("output error"). q char *fgets(char *buf). int fputc(int c..h> int putc(int c. FILE *fp). FILE *fp). The following example copies #standard input to standard output using getc and #putc. EOF on error q Example: #Usage of getc and putc.h> int ferror(FILE *fp). Output functions The following are the output functions. FILE *fp). ferror or feof should be called. .h> int main(void) { int c. Calls to fgetc take a longer time than those to getc. q #include <stdio. while ((c = getc(stdin)) != EOF) if (putc(c.

Input and Output functions: The following two functions are provided for binary I/O: #include <stdio. Output functions: Line at time output is provided by fputs and puts. #include <stdio. The following program #copies standard input to standard output using fgets #and fputs.. Both return buf if OK. stdout) == EOF) printf("output error"). 4. EOF on error. size_t nobj.Unix India CompuMasterLtd. while fgets reads from the specified stream. size_t fwrite(const void *ptr. FILE *fp). exit(0). FILE *fp). } Direct I/O These are also known as binary I/O. FILE *fp). stdin) != NULL) if (fputs(buf.h> int main(void) { char buf[MAXLINE]. Example: #Usage of fgets and fputs. int puts(const char *str). if (fwrite(&data[2]. sizeof(float). if (ferror(stdin)) printf("input error"). Both return number of objects read or written There are two common uses of these functions: q Reading or writing a binary array. size_t size. Both return a nonnegative value if OK.h> size_t fread(void *ptr. size_t nobj. size_t size. MAXLINE. float data[10]. q For example to write elements 2 through 5 of a floating point array.h> int fputs(const char *str. #include <stdio. NULL on end of file or error. while (fgets(buf. gets reads from standard input. fp) != 4) .

. To position a binary file using fseek a byte offset should be specified. The value returned by ftell for a binary file is this byte position. #include <stdio.h> int fgetpos(FILE *fp. Returns current file position indicator if OK. char name[NAMESIZE]. fpos_t *pos). void rewind(FILE *fp). fgetpos and fsetpos. struct { short count.Unix India CompuMasterLtd. size is specified as the size of the structure and nobj as 1. fp) != 1) printf("fwrite error"). -1L on error. q For example. and SEEK_END which means from the end of the file. Portable applications that need to move to non-UNIX systems should use fgetpos and fsetpos. if (fwrite(&item.. long offset. nonzero on error. The values of whence are SEEK_SET which means from the beginning of the file. Here. const fpos_t *pos). For a binary file. Returns 0 if OK. This datatype can be made as big as necessary to record a file’s position. int fsetpos(FILE *fp. a files position indicator is measured in bytes from the beginning of the file. int fseek(FILE * fp. These two functions introduce a new abstract datatype fpos_t. that records a file position. 1. int whence).h> q long ftell(FILE *fp). sizeof(item). and nobj as the number of elements. #include <stdio. Positioning a stream There are two ways to position a standard I/O stream: ftell and fseek. long total. q Reading or writing a structure. }item. These two functions assume that a file’s position can be stored in a long integer. Here size is specified as the size of each element of the array. printf("fwrite error"). SEEK_CUR which means from the current file position.

const char *format. #include <stdio. fprintf writes to the specified stream. There is always a need for one process to be able to inform another process of the occurrence of an event or to pass on data for processing. Returns number of characters stored in array. Both return 0 if OK. we have two forms of interprocess communication in this session. int sprintf(char *buf. …).h> int scanf(const char *format.also called named pipes). const char(format. the design of the UNIX operating system is such that no processes may remain in isolation. All three return number of input items assigned. and sprintf places the formatted characters in the array buf. int fprintf(FILE *fp. Both return number of characters output if OK. EOF if input error or end of file before any conversion. They are pipes (also called unnamed pipes) and FIFOs (First In. Formatted Input Formatted input is handled by the following three scanf functions: #include <stdio. This value can be used in a latter call to fsetpos to reposition the stream to that location. UNIX is a multi-user environment. This means that at any point in a time. INTERPROCESS COMMUNICATION As we already know. …). In fact. Formatted I/O Formatted Output Formatted output is handled by the three printf functions. cinst char *format. First Out . const char *format. negative value if output error. …). This gives rise to a need for processes to communicate with each other. nonzero on error. fgetpos stores the current value of the file’s position indicator in the object pointed to by pos.. . Here. …).h> int printf(const char *format. there will be a number of processes concurrently active. sprintf automatically appends a null byte at the end of the array. but this null byte is not included in the return value.Unix India CompuMasterLtd. …) int fscanf(FILE *fp. …). int sscanf(const char *buf. printf writes to standard output.

. which is physically located within the Kernel. The concept of Pipe As stated earlier. The system call opens a special file (called an unnamed pipe) twice . and Process B reads the file. UNIX has historically provided a means for the parent process and its child to communicate through the use of pipes. int pipe(fd).once for reading. then you will need to send this output to the grep utility. The Kernel takes care of opening the pipe.Unix India CompuMasterLtd. You would do this using a pipe: sort empdata | grep "a01" The process of piping output from one program as input to another can be visualized as shown below: The figure shows Process A talking to Process B. This file which acts as the medium of communication is a special kind of file. However. Process A writes its output into this file. It returns the two file descriptors through the integer array sent to it as a parameter.h> int fd[2]. This is because the pipe is going to be both read from and written into. the Kernel deletes the pipe. if you need to extract only a few lines of this output. a pipe is a means of transmitting the output of one program as input to another program. and the second is opened for writing. which would extract the required line and display them. An unnamed pipe is essentially a gateway between a parent process and its child through which they can exit on all UNIX platforms. But programs talk to each other through the medium of files. and once for writing. This implies that the pipe shown in the figure is a file. When the processes terminates. Pipes are simplest form of IPC and exit on all UNIX prompts.The pipe() System Call The pipe() call is used to create a pipe. Creating an unnamed pipe . An unnamed pipe is essentially a gateway between a parent and its child through which they can exchange data. Data in the pipe flows through the Kernel. The first one is opened for reading. Pipes You know that a new process is created using the fork() system call wherein a process spawns a child process. The syntax of this call is as follows: #include <unistd..

Now a pipe may be viewed as shown in the following figure: A Unix pipe Normally. data can flow only in one direction at a time. the parent closes the read end of the pipe and writes into the write end.that is.. The child closes the write end and reads from the read end. Data can now flow from parent to child. A point to be kept in mind about a pipe is that it is half-duplex in nature .Unix India CompuMasterLtd. For a pipe in the reverse direction. the process that calls pipe then calls fork. For a pipe from parent to child. and the child closes the read end. the parent closes the write end. The following figure shows a half duplex pipe after a fork call: . creating an IPC channel from the parent to the child or vice versa. either from parent to child or vice versa.

If action of the signal is either ignored or trapped. It returns a pointer to type FILE.. This function is very similar to the fopen() function. read will return a 0 to indicate an EOF. FILE *fp. type) char *cmdstring. the closing of the appropriate file descriptors and so on.the popen() and pclose() functions which create and delete pipes. write will return an error with errno set to EPIPE.Unix India CompuMasterLtd. Half duplex pipe after a fork The following rules must be kept in mind when one end of a pipe has been closed: q If a read is executed on a pipe whose write end has been closed. This is because of the implementation issue involved . cmdstring is an executable command.The popen() and pclose() Functions The pipe() system call is a rather complicated call to implement. *type.the pipe() call. the signal SIGPIPE is generated. The function differs from fopen() mainly in that the first parameter. The syntax of popen() and pclose() is: FILE *popen(cmdstring. the fork() call. Manipulating a Pipe . q If a write is executed on a pipe whose read end has been closed. type can be either r for read or w for write to indicate the direction of the pipe. . Consider the following lines of code. The standard library offers two front-end functions that take care of these details .

if ((fpin = popen("utol". Assume that you have to write a program called get_data that has to accept input in lower-case from the user. if (putchar(c) == EOF) { perror("utol"). A program called utol exists that converts string into lower-case. "r"). The command string. Example conversion Program: Case #include <stdio.through a fork() and exec() . The syntax of pclose() is: int pclose(fp) FILE *fp.h> main(){ int c. Since you can never be sure that the user is going to key his input in lower-case. FILE *fpin. } if (c == '\n') fflush(stdin). If the type is "w". an fgets() can be used to access this input. exit(1). Let us look at where you can use a pipe. } Example: Implementation of get_data using popen() #include <stdio. } exit(0).c will be executed . while ((c = getchar()) != EOF) { if ((c >= 'A') && (c <= 'Z')) c = 32. and the output of this command will be made available as input to the process that issues the call. you have to take care of this detail.h> main() { char line[BUFSIZE]. . Any executable file can be specified in place of this command string. fp = popen(ls *. then the command string must be some program that expects standard input. ls *. Since popen() returns a FILE pointer. "r")) == NULL) { perror("open").Unix India CompuMasterLtd.c..by popen().

the pipe device created is not visible to you . FIFO’s AND MESSAGE QUEUES. therefore acts as a filter between the standard input and the program get_data. The program utol. Limitations of Pipes: q They are half-duplex in nature . fpin) == NULL) break. BUFFSIZE. stdout). That is.SEMOPHORES FIFOs . } In get_data. When you use pipes. It is possible for unrelated processes to communicate using pipes. exit(1). and passes on this input to get_data. converts all upper-case characters to lower-case. setting its .Unix India CompuMasterLtd. In situations where unrelated processes need to communicate. } pclose(fpin). It takes input from the user. q They can only be used between directly related processes. exit(0).Named Pipes Unnamed pipes can only be used between processes with a common ancestor. A pipe is created by a process that then calls fork(). fflush(stdout).. however. and the pipe is used between parent and child. With FIFOs.Named Pipes Creating a FIFO Client-Server communication using a FIFO Message Queues Semaphores Shared memory FIFOs .data flows only in one direction at a time. you will be actually creating the special file required. the call to popen() causes the program utol to be executed.it is created in the Kernel and the Kernel takes care of creating it and deleting it when it is no longer needed. if (fgets(line. The output of utol is read in as input to get_data. FIFOs (also called named pipes) are used. the processes must have a parent-child relationship. } while (1) { fputs("input:".

Manipulating a FIFO Once a FIFO has been created using mknode(). it is removed from the pipe. Unlike. Path is the name of the file to be created. which are nothing but special files. the usual file I/O calls can be used to manipulate the file. do not support random access. and mode specifies the type of the file to be created. EINTR A signal interrupted the system call. are different from regular files. EEXIST The named file already exists. The syntax of this call is as follows: q int mknode(path. a value 0 is returned. The associated symbolic constants that errno is set to are tabulated below. otherwise -1 is returned and errno is set to indicate the error. it can be opened using open() and manipulated like any ordinary file . a pipe is simply used as a transition portion of data. We said that a process writes a data into this file. Once this data is read. Once the file has been created using the mknode(). which are usually used as a repository of data.Unix India CompuMasterLtd. Client-Server communication using a FIFO: One important use of FIFO’s is to send data between a client and server. mknode() is used to create special files. The permissions are set in the same way as inopen(). EFAULT Invalid path. ENOENT A directory named in the path does not exist.except that the lseek() function cannot be used as pipes. regular files. which is read by another process. Symbolic constants Interpretation EOT DIR A component of the path is not a directory. the symbolic constant S_IFIF0 must be used to specify that the file to be created is to be a FIFO. deleting it when it is no longer required etc. Creating a FIFO The mknod() System Call A FIFO is created using the mknod() system call. Another difference between the two system calls is that while open() creates the file and opens it in the desired mode. mknode() creates the file and does not open it. We conclude by examining how pipes. permissions. is used to create ordinary files. If there is a . but instead of specifying O_RDONLY/O_WRONLY/0_RDWR. The mknode() system call is similar to the open() system call in both function and format.. The difference is that while open(). int mode. both named and unnamed. If mknode() is successful. mode) char *path. The file you create will have a 'p' in the first column of ls-l output to indicate that it is a named pipe.

server contacted by several clients. each client can write its request to a well known FIFO i.Unix India CompuMasterLtd. the pathname of the FIFO is well known to all the clients that need to contact it.. The following figure describes this concept: Client sending requests to a server using a FIFO .e.

Every message on a queue has the following attributes: q Long integer type. identifies a particular queue of messages. A new message queue is created. The msgflag value is a combination of the constants shown in the below figure: . Data (if the length is greater than zero).Server communication using FIFO’s Message Queues The passing of messages between processes is possible in the UNIX system. and have an associated message queue identifier. In system V implementation of messages. Length of the data portion of the message (can be zero). This identifier which is called an msqid.h> int msgget(key_t key. all messages are stored in the Kernel. Client .h> #include <sys/msg. int msgflag).h> #include <sys/ipc. or an existing message queue is accessed with the msgget system call..Unix India CompuMasterLtd. q #include <sys/types.

Numeric 0400 0200 0040 0020 0004 0002 Symbolic MSG_R MSG_W MSG_R>>3 MSG_R>>3 MSG_R>>6 MSG_R>>6 IPC_CREAT IPC_EXCL Description Read by owner Write by owner Read by group Write by group Read by world Write by world The value returned by the msgget is a message queue identifier. then the valid semaphore values are zero and one. a process increments the semaphores value.. a processes needs to test its current value. FIFO and message queues. msqid. The example for this.Unix India CompuMasterLtd. The value of the variable at any point in time is the number of resource units available. the actual semaphore value must be stored in the Kernel. the processes must wait until the value is greater than zero. As a form of IPC. it will not be portable. To release a resource that is controlled by a semaphore. is if we write an application that uses messages that are 20000 bytes long. SEMAPHORES Semaphores Semaphores are a synchronization primitive. To obtain a resource that is controlled by a semaphore. they are not used for exchanging large amounts of data as are pipes. If some other process has been waiting for the semaphore value to become greater that zero. or -1 if an error occurred. Message Queue Limits There are certain limits in message queues. If the current value is zero. The magic number of 8192 bytes for a maximum message size is similar to the magic number of 4096 bytes for a maximum write to a pipe of FIFO. that other process can now obtain the semaphore. The system administrator can change some of these. say a file that is shared. by configuring a new Kernel. Since our use of semaphores is to provide resource synchronization between different processes. If we have one resource. This section provides a general overview of the semaphore system calls. . decrement the value by one. Consider a semaphore as an integer valued variable that is a resource counter. and if the current value is greater than zero. but are intended to let multiple processes synchronize their memory segments.

h> /* defines the ipc_perm structure */ struct semid_ds { struct ipc_perm sem_perm. Each value in the set is restricted to zero and one.a semaphore with a single value that can be either zero or one. /* ptr to first semaphore in set */ ushort sem_nsems. up to a system defined maximum value. break.Unix India CompuMasterLtd.h> #include <sys/ipc. } } What we have described as a semaphore is a single binary semaphore . /* time of last semop*/ time_t sem_ctime. q #include <sys/types.defined maximum. Instead.. /* time of last change */ . for( . ) { if (semaphore_value > 0) { semaphore_value--. the Kernel maintains the following structure of information. /* operation permission struct */ struct sem *sem_base. The system V implementation expands this in two directions. /* # of semaphore in set */ time_t sem_otime. For every set of semaphores in the system. each value in the set can assume any non-negative value. A semaphore is not a single value but a set of non-negative integer values. The number of non-negative integer values in the set can be from one to some system. .

one tests the value and waits for it to become zero. semid. and the other to increment the value . }. This is an example where multiple semaphore operations must be done automatically by the Kernel.h> #include <sys/sem.a single semaphore value that is either zero or one. /* # awaiting semval = 0 */ }. First. The client reads the data from the IPC channel.the operation would not work.h> #include <sys/ipc. /* # awaiting semval > cval */ ushort semzcnt. /* semaphore value.a pipe. File Locking with Semaphore We create a binary semaphore . /* pid of last operation */ ushort semncnt. then increment the value to one. or -1 if an error occurred. The server reads from the input file. Typically. but the data for every read is still copied by the Kernel from its block buffer to the caller’s buffer. the data is read by the Kernel into one of its internal block buffers and copied from there to the server’s buffer. FIFO or message queue. It should be noted that most UNIX systems detect sequential reading. wait for the semaphore value to become zero. If we took two system calls to do this . as is done by the server. Any of these forms of IPC require the data to be copied from the user’s buffer into the Kernel. int semflag). The sem structure is the internal data structure used by the Kernel to maintain the set of values for a given semaphore. q #include <sys/types. A semaphore is created.Unix India CompuMasterLtd.. and tries to keep one block ahead of the read requests. or an existing semaphore is accessed with the semget system call. This helps reduce the clock time required to copy a file. using one of the techniques described in this chapter . int nsems. The server writes the data in a message. The value returned by the semget is the semaphore identifier. again requiring the data be copied from . To lock the semaphore we call semop to do two operations automatically. Shared memory Consider the normal steps involved in the client-server file-copying program that we have been using for the example.h> int semget(key_t key. nonnegative */ short sempid. Every member of a semaphore set is described by the following structure: q Structsem { ushort semval.

read a file.an intercontext copy. and file descriptor 2 with the . other processes must wait for the read to finish before processing the data. lseek and close. the data is copied from the client’s buffer. The steps for client-server program now becomes: q The server gets accesses to a shared memory using a semaphore. these four copies are done between the Kernel and a user process . file descriptor 1 with the standard output. q A shared memory segment is created. to the output file. While most UNIX implementation try to speed up these copies as much as possible. the functions available for file I/O are those that open a file. By convention. the information has to go through the Kernel. There is. Additionally. The problem with these of IPC . the second argument to the write system call. of course. the Kernel’s IPC buffer to the client’s buffer. the Kernel returns a file descriptor to the process. write. The functions described here are unbuffered I/O.pipes. A file descriptor is a non-negative integer. q When the read is complete the server notifies the client. again using a semaphore. the second argument to the read system call. for example. FIFOs and message queues is that for two processes to exchange information. with the Kernel doing the actual write operation to the device at some later time. it is identified with the file descriptor that was returned by open or creat as an argument to either read or write. Most of the UNIX file I/O can be performed using only five functions: open. When an existing file is opened or a new file is created. points into shared memory. When the file is to be read or written.. UNIX Shells associate file descriptor 0 with the standard input of a process. Shared memory provides a way around this by letting two or more processes share a memory segment. write a file and so on. A total of four copies of the data is required. The address to read into. they can still be expensive. read. The term unbuffered refers to the fact that each read or write invokes a system call in the Kernel. If one process is reading into some shared memory. q The client writes the data from the shared memory segment to the output file. a problem involved in multiple processes sharing a piece of memory: the processes have to coordinate the use of the memory among themselves.Unix India CompuMasterLtd. This might involve just copying the data into a Kernel buffer and returning. File Descriptors To the Kernel. Finally. all open files are referred to by file descriptors. or an existing one is accessed with the shmget system call. q The server reads from the input file into the shared memory segment. FILE I/O In general.

. O_TRUNC If the file exists. The third argument is shown as …. O_WRONLY Open for writing only.h> int open(const char *pathname. which specifies that the number and types of the remaining arguments may vary. the third argument is used only when a new file is created. O_CREAT Create a file if it doesn’t exist. mode). mode_t mode */).Unix India CompuMasterLtd. close function .h> #include <fcntl.1 applications with the symbolic constants STDIN_FILENO. creat function A new file can also be created by this function. One or more of the following options can be included by OR’ing these: q O_READONLY Open for reading only.h>. The following constants are optional: O_APPEND Append to the end of file on each write. …/*. mode_t mode). int oflag. oflag: The options for this argument are present in <fcntl. Syntax: #include <sys/types.h> #include <sys/stat. and STDERR_FILENO. Note: One and only one of the above three constants must be specified. q Note: This function is equivalent to open(pathname O_WRONLY | O_CREAT | O_TRUNC.h> #include <fcntl. These are defined in the <unistd.h> int creat(const char *pathname. O_RDWR Open for reading and writing. -1 on error. Returns file descriptor opened for write-only if OK.h> #include <sys/stat.h> header. STDOUT_FILENO. Syntax: #include <sys/types. These numbers are replaced in POSIX. and if the file is successfully opened. q pathname: It is the name of the file to open or create. -1 on error. truncate its length to 0. For this function. open function A file is opened or created by calling the open function. Returns file descriptor if OK. standard error.

{ int fd. -1 on error. Returns new file offset if OK. q If whence is SEEK_SET.. the file’s offset is set to offset bytes from the beginning of the file. Syntax: #include <unistd. skval). If whence is SEEK_END. O_RDONLY). all open files are automatically closed by the Kernel. c). The interpretation of the offset depends on the value of the whence argument. Note: When a process terminates. Example: #include <fcntl.Unix India CompuMasterLtd.h> main(argc. -1 on error. An open file is closed. if (fd == -1) exit(). if (argc != 2) exit(). Syntax: #include <sys/types. skval = lseek(fd. lseek function An open file can be explicitly positioned by calling lseek. the file’s offset is set to its current value plus the offset.h> #include <unistd. char c. The offset can be positive or negative. } } q . &c. the file’s offset is set to the size of the file plus the offset. fd = open(argv[1]. char *argv[].h> off_t lseek(int filedes. 1). q If whence is SEEK_CUR. The offset can be positive or negative. 1023L. 1)) == 1) { printf("char %c\n". Returns 0 if OK. printf("new seek val %d\n". int whence).h> int close(int filedes). off_t offset. argv) int argc. skval. while ((skval == read(fd.

h> main() { int fd. Before a successful return. q Example: #include <fcntl. fd = open("/etc/passwd". q Example: #include <fcntl. -1 on error. O_RDONLY). the file’s offset is incremented by the number of bytes actually written. i++) buf[I] = ’a’. i < sizeof(buf). buf. write(fd. the offset is incremented by the number of bytes actually read.Unix India CompuMasterLtd. 20). For a regular file. i. O_WRONLY). the write starts at the file’s current offset. void *buff. read function Data is read from an open file with the read function. for (i = 0. After a successful write. Returns number of bytes read.h> main() { int fd. void *buff.h> ssize_t read(int filedes. lilbuf. Returns number of bytes written if OK. char buf[512]. size_t nbytes). } write function Data is written to an open file with the write function.h> ssize_t write(int filedes. } . fd = open("/etc/passwd". char lilbuf[20]. sizeof(buf)). 0 if end of file. size_t nbytes).. -1 on error The read operation starts at the file’s current offset. Syntax: #include <unistd. Syntax: #include <unistd. read(fd.

q To get/set file status flags (cmd = F_GETFL or F_SETFL).h> #include <fcntl. int cmd. Both return new file descriptor if OK. if ((val = fcntl(atoi(argv[1]). Return depends on cmd if OK. atoi(argv[1])). accmode = val & O_ACCMODE.h> #include "ourhdr. The new file descriptor returned by dup is guaranteed to be the lowest numbered available file descriptor. If filedes equals filedes2. 0)) < 0) err_sys("fcntl error for fd %d". dup and dup2 functions An existing file descriptor is duplicated by either of the following functions: Syntax: #include <unistd. char *argv[]) { int accmode. q To get/set file descriptor flags (cmd = F_GETFD or F_SETFD). F_GETFL.h" int main(int argc. then dup2 returns filedes2 without closing it.h> int dup(int filedes). val. if (argc != 2) err_quit("usage: a.h> #include <fcntl.Unix India CompuMasterLtd. F_SETLK. or F_SETLKW).. fcntl function The fcntl function can change the properties of a file that is already open. If filedes2 is already open. q Example: #include <sys/types.out <descriptor#>"). With dup2 the new value of the descriptor is specified in the filedes2 argument. it is first closed. …/* int arg */ ). int filedes2).h> #include <unistd. int dup2(int filedes. -1 on error. q To get/set record locks (cmd = F_GETLK. if (accmode == O_RDONLY) .h> int fcntl(int filedes. -1 on error. q To get/set asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN). The fcntl function is used for five different functions: q To duplicate an existing descriptor (cmd = F_DUPFD). Syntax: #include <sys/types.

else err_dump("unknown access mode").foo read write SYSTEM CALLS AND LIBRARY FUNCTIONS PROCESS CONTROL fork function All operating systems provide service points through which programs request services from the Kernel. synchronous writes"). exit(0). if (val & O_APPEND) printf(". if (val & O_NONBLOCK) printf(".Unix India CompuMasterLtd. printf("read only").foo write only. #if !defined(_POSIX_SOURCE) && defined(O_SYNC) if (val & O_SYNC) printf(". For example. All variants of UNIX provide a well defined.out 2 2>>temp. append").foo $ cat temp. . limited number of entry points directly into the Kernel called system calls. The technique used on UNIX systems is for each system call to have a function of the same name in the standard C library.foo write only $ a. nonblocking"). The user process calls this function. using the standard C calling sequence. the function may put one or more of the C arguments into general registers and then execute some machine instruction that generates a software interrupt in the Kernel.. else if (accmode == O_WRONLY) printf("write only").out 1 > temp. append $ a. #endif putchar(‘\n’). using whatever technique is required on the system. } #Input & Output $ a.out 5 5<>temp. This function then invokes the appropriate Kernel service.out 0 < /dev/tty read only $ a.

whereas the system calls usually cannot be replaced.h> #include <unistd. library functions can be replaced if desired. -1 on error. both system calls and library functions appear as normal C functions. Returns 0 in child. Syntax: #include <sys/types. Returns parent process ID of calling process. In this case the system call sbrk in the kernel allocates an additional chunk of space to the process and the library function malloc manages this space. which will probably use the sbrk system call. Both exist to provide services for application programmers. implements a particular type of allocation. This function is called once. Returns real group ID of calling process. Another difference between system calls and library functions is that system calls usually provide a minimal interface while library functions often provide more elaborate functionality. However. . The following functions return these identifiers: #include <sys/types. process ID of child in parent. PROCESS CONTROL Every process has a unique process ID. If we don’t like the way it allocates the memory. fork function The only way a new process is created by the UNIX Kernel is when an existing process calls the fork function. getgid(void). Returns process ID of calling process. there are other identifiers for every process.h> pid_t pid_t uid_t uid_t gid_t gid_t getpid(void). getuid(void). Process ID 0 is usually the scheduler process and is often known as the swapper. Returns effective user ID of calling process. getegid(void) Returns effective group ID of calling process. Note: Some functions such as the printf function may invoke the write system call to perform the output. Process ID 1 is usually the init process and is invoked by the Kernel at the end of the bootstrap procedure. In addition to the process ID. Returns real user ID of calling process. the memory allocation function malloc. In general.. although it does run with superuser privileges. It is a normal user process. a non-negative integer. but the functions strcpy and atoi (converts ASCII to Integer) do not involve the operating system at all.Unix India CompuMasterLtd. we can define our own malloc function.h> #include <unistd. It is part of the Kernel and is known as a system process. There are some special processes. For example. This is clearly illustrated in the above example of malloc. geteuid(void).h> pid_t fork(void). getppid(void). The new process created by fork is called the child process.

This checking feature and the . warnings are provided if the source files used in producing objfile cannot be found. An initial argument of -w permits overwriting locations in objfile. The only difference in the returns is that the return value in the child is 0 while the return value in the parent is the process ID of the new child... so there is no function that allows a process to obtain the process IDs of its children. It may be used to examine their object files and core files and to provide a controlled environment for their execution. all addresses refer to the executing program. The command-line options available are: -w While a process is running under sdb. The reason the child’s process ID is returned to the parent is because a process can have more than one child. so the child can always getppid to obtain the process ID of its parent. SYMBOLIC DEBUGGER symbolic debugger Addresses Commands Variable!value Miscellaneous commands Files sdb(CP) sdb -.] [[corefile|-]] Description The sdb command calls a symbolic debugger that can be used to debug C programs. or are newer than objfile. -W By default.symbolic debugger Syntax: sdb [-w] [-W] [[objfile [corefile]] [directory:directory:.. but returned twice.Unix India CompuMasterLtd. otherwise they refer to objfile or corefile. The reason fork returns 0 to the child is because a process can have only a single parent.

they are set to the first line in main(). If corefile exists. sdb will display the values of all the elements of a structure when it is requested to display a structure. Pointers may be dereferenced by using the form pointer[0]. and it is this value sdb displays. and the template used for the structure is that of the last structure referenced by sdb. but the file can still be examined and the program debugged. in which case the number is viewed as the address of the structure. Variables local to a procedure may be accessed using the form procedure:variable. sdb will interpret a structure as a set of variables. octal. Otherwise. and array elements as variable[number]. A particular instance of a variable on the stack may be referenced by using the form procedure:variable. The default for objfile is a.Unix India CompuMasterLtd. number is the occurrence of the specified procedure on the stack. It is useful to know that at any time there is a current line and current file. If it has not been compiled with a debug option specified.out. sdb does not truncate names. If no procedure is specified.. accompanying warnings may be disabled by the use of the -W flag. objfile is a COFF format executable program file which has been compiled with the -g or -Zi (debug) option.. Names of variables are written just as they are in C. or hexadecimal. A number may be used in place of a structure variable name. As with structures. It is also possible to specify a variable by its address. It displays only the address of the array itself or of the section specified by the user if subscripts are omitted. so that addresses may be input in decimal. sdb displays all the values of an array or of the section of an array if trailing subscripts are omitted. An unqualified structure variable may also be used with various commands.. The current line and file may be changed with the source file examination commands. not the addresses of individual elements. A "-" in place of corefile will force sdb to ignore any core image file. In place of number. Combinations of these forms may also be used. All forms of integer constants which are valid in C may be used. or as variable[number. counting the top. Thus... The core file need not be present.]. or most current.number may be used to indicate a range of values. the form number. the symbolic capabilities of sdb will be limited. the default for corefile is core. If no procedure name is given. Line numbers in the source program are referred to as filename:number or . All the variations mentioned in naming variables may be used. or subscripts may be omitted entirely if they are the last subscripts and the full range of values is desired.number. Elements of a multidimensional array may be referenced as variable[number][number]..number. It is also possible to refer to structure members as variable pointers to structure members as variable->member. then they are initially set to the line and file containing the source statement at which the process terminated. an asterisk (*) may be used to indicate all legitimate values for that subscript.. An entire structure does have an address. corefile is assumed to be a core image file produced after executing objfile. the procedure containing the current line is used by default. the procedure currently executing is used by default. Generally. An exception to this interpretation occurs when displaying variable addresses. as the first.

If no procedure or filename is given. f1) and (b2. Commands The commands for examining data in the program are: t Print a stack trace of the terminated or halted program. procedure:number. programs with separated text and data space) the two segments for a file may overlap. Addresses The address in a file associated with a written address is determined by a mapping associated with that file. the number is relative to the beginning of the file. the current file is used by default. e1 is set to the maximum file size. In either case. The initial setting of both mappings is suitable for normal a. variable\/clm . then. In some cases (for example. Each mapping is represented by two triples (b1. In order for sdb to be used on large files. all appropriate values are kept as signed 32-bit integers. e1. in this way the whole file can be examined with no address translation.out and core files..Unix India CompuMasterLtd. b1 is set to 0. and f1 is set to 0. e2. T Print the top line of the stack trace. the requested address is not legal. for that file. f2) and the file address corresponding to a written address is calculated as follows: b1 <= address < e1 then file address = address +f1 -b1 otherwise b2<=address<e2 then file address = address +f2 -b2 otherwise. If either file is not of the kind expected. the first line of the named procedure or file is used. If no number is given.

Unix

India CompuMasterLtd,.

Print the value of variable according to length l and format m. A numeric count c indicates that a region of memory, beginning at the address implied by variable, is to be displayed. The length specifiers are: b one byte h two bytes (half word) l four bytes (long word) Legal values for m are: c character d decimal u unsigned decimal o octal x hexadecimal f 32-bit single precision floating point g 64-bit double precision floating point s Assume variable is a string pointer and print the characters starting at the address pointed to by the variable.

Unix

India CompuMasterLtd,.

a Print characters starting at the variable's address. This format may not be used with register variables.

p Pointer to procedure. i Disassemble machine-language instruction with addresses printed numerically and symbolically. I Disassemble machine-language instruction with addresses just printed numerically. Length specifiers are only effective with the c, d, u, o, and x formats. Any of the specifiers, c, l, and m, may be omitted. If all are omitted, sdb chooses a length and a format suitable for the variable's type as declared in the program. If m is specified, then this format is used for displaying the variable. A length specifier determines the output length of the value to be displayed, sometimes resulting in truncation. A count specifier c tells sdb to display that many units of memory, beginning at the address of variable. The number of bytes in one such unit of memory is determined by the length specifier l, or if no length is given, by the size associated with the variable. If a count specifier is used for the s or a command, then that many characters are printed. Otherwise successive characters are printed until either a null byte is reached or 128 characters are printed. The last variable may be redisplayed with the command "./". The sh(C) metacharacters * and ? may be used within procedure and variable names, providing a limited form of pattern matching. If no procedure name is given, variables local to the current procedure and global variables are matched; if a procedure name is specified, then only variables local to that procedure are matched. To match only global variables, the form :pattern is used. linenumber?lm variable:?lm Print the value at the address from a.out or text space given by linenumber or variable (procedure name), according to the format lm. The default format is i. Variable=lm linenumber=lm

Unix

India CompuMasterLtd,.

number=lm Print the address of variable or linenumber, or the value of number, in the format specified by lm. If no format is given, then lx is used. The last variant of this command provides a convenient way to convert between decimal, octal, and hexadecimal. Variable!value Set variable to the given value. The value may be a number, a character constant, or a variable. The value must be well defined; expressions that produce more than one value, such as structures, are not allowed. Character constants are denoted ‘character’. Numbers are viewed as integers unless a decimal point or exponent is used. In this case, they are treated as having the type double. Registers are viewed as integers. The variable may be an expression that indicates more than one variable, such as an array or structure name. If the address of a variable is given, it is regarded as the address of a variable of type int. C conventions are used in any type conversions necessary to perform the indicated assignment. x Print the machine registers and the current machine-language instruction. X Print the current machine-language instruction. The commands for examining source files are: r e[procedure] e[filename] e[directory/] e [directory filename] The first two forms set the current file to the file containing procedure or to filename. The current line is set to the first line in the named procedure or file. Source files are assumed to be in directory (the default is the current working directory). The latter two forms change the value of directory. If no procedure, filename, or directory is given, the current procedure and file names are reported. /regular expression[/] Search forward from the current line for a line containing a string matching regular expression as in ed(C). The trailing \/ may be omitted. ?regular expression[?] Search backward from the current line for a line containing a string matching

The signal which caused the program to stop is reactivated with the C command and ignored with the c command. If count is given. If a line number is specified. The r command with no arguments re-uses the previous arguments to the program while the R command runs the program with no arguments. count[+-] Increment (+) or decrement (-) the current line by count lines. If count is given. a temporary breakpoint is placed at the line and execution is continued. z Print the current line followed by the next 9 lines. The trailing ? may be omitted. If count is given. number Set the current line to the given line number.Unix India CompuMasterLtd. An argument beginning with < or > causes redirection for standard input or output. w Window. it specifies the number of breakpoints to be ignored. regular expression as in ed(C). r s[count] . The commands for controlling the execution of the source program are: r [count]rargs [count]R Run the program with the given arguments. The breakpoint is deleted when the command finishes.. respectively. linenumber g [count] Continue after a breakpoint with execution resumed at the given line. Print the new current line. it specifies thernumber of breakpoints to be ignored. p Print the current line. Print the 10 lines around the current line. Set the current line to the last line printed. Linenumberc[count] linenumber C [count] Continue after a breakpoint or interrupt. the program will stop when count breakpoints have been encountered. Print the new current line.

character. a breakpoint is placed at the current line. Since this command is done by software. a breakpoint is placed at the first line in the procedure even if it was not compiled with the -g or -Zi options. If count is omitted.. s. If no commands are given.. Arguments can be integer.. A v turns verbose mode off if it is on for any level.arg2. If level is omitted. or m commands. for use when single-stepping with the S.Unix India CompuMasterLtd. Otherwise the commands are executed when the breakpoint is encountered and . i I Single-step by one machine-language instruction. "proc:"). it can be very slow. each assembler statement is also printed.. it is effectively infinity. it defaults to d. If no linenumber is given.command. If a procedure name without a line number is given (for example. or string constants or names of variables accessible from the current procedure. [linenumber] b [command. The signal which caused the program to stop is reactivated with the I command and ignored with the i command.. S is equivalent to s except it steps through procedure calls.) procedure(arg1.. S[count] Single-step the program through count lines. if level is 2 or greater.. r variable:m[count] address:m[count] Single-step (as with s) until the specified location is modified with a new value. If no format is given. If no count is given.arg2. then just the current source file and/or subroutine name is printed when either changes.. If level is 1 or greater. k Killrthe program being debugged. The second form causes the value returned by the procedure to be printed according to format m. procedure(arg1..] Set a breakpoint at the given line.. Variable must be accessible from the current procedure. [level] v Toggle verbose mode. execution stops just before the breakpoint and control is returned to sdb.)/m Execute the named procedure with the given arguments. then the program is run for one line. This facility is only available if the program was compiled with the -g or -Zi options. each C source line is printed before it is executed.

Print the next 10 lines of instructions. The end-of-file character is usually <Ctrl>-D. source or data depending on which was printed last. the command effectively does a linenumber b l. If the k command is specified. If the previous command displayed a memory location. If the line begins with a y or d.. new-line If the previous command printed a source line. then advance the current line by one and print the new current line. display the next memory location. the command effectively does a proc:b T. execution continues. [linenumber] d Delete a breakpoint at the given line. If linenumber is of the form proc:.Unix India CompuMasterLtd. end-of-file character Scroll. D Delete all breakpoints. linenumber a Announce. If linenumber is of the form proc:number. Miscellaneous commands: ! command The command is interpreted by sh(C). then the breakpoints are deleted interactively. If no linenumber is given. then the breakpoint is deleted. control returns to sdb at the breakpoint. B Print a list of the currently active breakpoints. Each breakpoint location is printed and a line is read from standard input. l Print the last executed line. Multiple commands are specified by separating them with semicolons. .

the variable name is displayed along with the value. This command may not be nested. e2. The C escape sequences of the form \character are recognized. e1.out core . If fewer than three values are given. the second segment (b2. Y Toggle debug output. f1) is changed unless "*" is specified. and then continue to accept commands from standard input. where character is non-numeric. in which case. q Exit the debugger. M Print the address maps. The arguments "?" and "\/" specify the text and data maps. When sdb is told to display a variable by a command in such a file. "\<" may not appear as a command in a file. the remaining map parameters are left unchanged. f2) of the mapping is changed. \< filename Read commands from filename until the end of file is reached. string Print the given string. The following commands also exist and are intended only for debugging the debugger: V Print the version number. M [?/] [*] b e f Record new values for the address map. Q Print a list of procedures and files being debugged. respectively.Unix India CompuMasterLtd.. The first segment (b1. Files a.

all variables are initialized before the procedure is started.. and some information may be missing. Data which are stored in text sections are indistinguishable from functions. This makes it impossible to use a procedure which formats data from a core image. Line number information in optimized functions is unreliable. Notes: If a procedure is called when the program is not stopped at a breakpoint (such as when a core image is being debugged). . a warning is printed before the value.Unix India CompuMasterLtd. The size is assumed to be int (integer). Warnings When sdb prints the value of an external variable for which there is no debugging information.