Table of Contents:
Unix operating System
1.1 Objectives
Study of
1.2 History
The evolution of Unix dates back to 1969. An interactive multi-user operating system called MULTICS was developed at AT & T Bell laboratories. Berkeley Software Distribution ( BSD ), University of California at Berkeley, released its first version of UNIX in 1978. The other variants include SCO ( Santa Cruz Operations ) Unix, Xenix developed by Microsoft, Sun Solaris Unix and AIX ( Advanced Interactive Exchange ) from IBM. Linux was originally created by Linus Torvalds of the University of Helsinki in Finland in 1991. Common Linux distributions are Slackware, Caldera and Redhat.
1.3 Features
Multitasking
A user can run more than one task at a time. More than one task can be executed in the background while working on a task in the foreground. A simple example would be sorting a mailing list in the background while typing a document in the foreground. The overall system response becomes slower as more tasks compete for the CPU.
Multi-user
A multi-user system permits more than one user to use the system simultaneously. The operating system manages the requests made by different users and assigns priorities when more than one user wants to use the same resource. A multi-user system costs less than the equivalent number of single user systems.
Portability
Portability means that a software can operate on different platforms. More than 90% of the Unix operating system is written in C. Only minor adjustments are to be made when installing UNIX in a system with a different architecture. Application programs written in higher languages like C, Fortran etc. are easily portable across different Unix systems.
1.4 Structure
The Unix system consists of 3 levels.
1.5 File System
All utilities, application and data are stored as files. The file structure consists of
Files are addressed by using
1.6 Logging in to UNIX system


Unix Commands
2.1 Basic ideas
|
> |
Redirects the output of the command into a new file. |
|
< |
Redirects the contents of a file into a command. |
|
>> |
Redirects the output of command so that it is appended to the end of the file. |
|
| |
Directs the output of one command so that it becomes the input of another command. |
|
* |
Interpreted as a string of zero or more characters. |
|
? |
Is matched with a single character. |
|
[abc] |
Is matched with any one of the specified set of characters given inside the brackets. |
2.2 Command Set 1
|
Sl. No. |
Command |
Meaning |
|
1 |
$clear |
Clears the screen. |
|
2 |
$date |
Prints the current date and time. |
|
3 |
$pwd |
Prints the working directory. |
|
4 |
$ls |
Lists the names of files & directories. |
|
$ls -a |
Lists all files including those starting with a ".". | |
|
$ls -l |
Lists in long format. In addition to the name of each file, print the file type, permissions, number of hard links, owner name, group name, size in bytes, and timestamp. | |
|
5 |
$cat |
Used to display the contents of a file or create a new file. |
|
$cat > file1 |
Entered text will be stored in file1. Press [Ctrl]+[D] to terminate. | |
|
$cat file1 |
Displays the contents of the file named file1. | |
|
$cat < file1 |
Displays the contents of the file named file1. | |
|
$cat file1 > file2 |
Send the contents of file1 to file2, overwrites file2 if it is an existing file. | |
|
6 |
$mkdir |
Making a directory. |
|
7 |
$rmdir |
Removing a directory. |
|
8 |
$cd |
Change directory. |
|
9 |
$man |
Displays the manual pages. |
|
$man pwd |
Displays help about the command pwd. | |
|
10 |
$passwd |
Facilitates changing of password. |
Examples - 1

Examples - 2
2.3 Command set 2
| Sl. No. | Command | Meaning |
| 1 | $cp file1 target | Make a copy of file1 to a file called target. If the target file exists, it is overwritten. If the target is a directory then a new file file1 is created in the directory target. |
| 2 | $rm file1 | Removes file1 from the current directory. |
| $rm file1 file2 | Removes files file1 and file2. | |
| $rm fil* | Removes all files that start with the string fil. | |
| $rm fil * | Removes file named fil and all files in the current directory. | |
| $rm -i fil* | Prompts before deleting the file. | |
| $rm -r directory | Removes the directory recursively even if it is not empty. | |
| $rm -v fil* | Prints the name of each file before removing it. | |
| 3 | $ln file1 file2 | Creates an additional name for the same file. If file1 is pathname then it refers to a file in different directory. The command rm removes files by breaking the link between the filename and the physical file. When the number of links to any physical file reduces to zero, then that file can't be accessed further. |
| $ln /etc/hosts | Creates a link named h for the files hosts in the directory /etc. | |
| 4 | $mv file1 file2 | Used to rename files and directories. It establishes a link between the new file name and the physical file and then removes the link between the old file name and the physical file. |
| $mv dir1 dir2 | Renames the directory dir1 as dir2. | |
| 5 | $more file1 | Used to display the contents of a file page by page. |
| $ls -l | more | Used when the directory listing is beyond one screen. | |
| 6 | $less file1 | Used to display the contents of a file page by page. User can go back and forth using this command. |
| 7 | $wc filename | Prints the number of lines, words, bytes in the filename. |
| $wc -c filename | Prints only the byte count. | |
| $wc -w filename | Prints only the word count. | |
| $wc -l filename | Prints only the line count. | |
| 8 | $head file1 | Prints the first 10 lines from file1. |
| $head -n N file1 | Prints the first N lines form fiel1. | |
| 9 | $tail file1 | Prints last 10 lines from file1. |
| $tail -n N file1 | Prints last N lines from file1. | |
| $tail -n +N file1 | Begins printing from Nth line from the beginning of file. | |
| 10 | $cal | Prints the calendar for the current year. |
| $cal 2000 | Prints the calendar for year 2000. | |
| $cal 6 1998 | Prints the calendar for 6th month in year 1998. |
Examples - 3

Examples - 4

2.4 Unix directories
| /bin | Holds most commonly used user
programs such as
|
| /sbin | Holds system programs or useful
maintenance programs such as
|
| /etc | Holds configuration files such as
|
| /home | Holds home directories for all the users on the system. |
| /tmp | Used for temporary storage. Programs that use /tmp generally delete all the files created temporarily. The system automatically deletes the contents of this directory periodically. |
| /var | Contains some additional directories. /var/log contains most of the messages generated by the system. |
| /lib | Contains library of functions needed by the application programs. |
| /dev | Contains device specific information. |
| /usr | Holds most of files directly related to the users of the system. |
| /usr/bin
/usr/sbin |
Contains most of the executable files. |
| /usr/X11
/usr/X11R6 |
Hold all X window related man pages, libraries and executables. |
| /usr/dict | Holds the local dictionary for the system. |
| /usr/include | Holds the header files for C compiler. |
A
section of typical directory listing at the root directory is shown
below

A typical directory listing is shown above. It contains the following details.
There are three classes of system users
Each user can access the file as shown below
| Access mode | Ordinary file | Directory |
| Read | Allows reading the contents. | Allows listing of files in the directory. |
| Write | Allows changing the contents. | Allows creating and removing contents. |
| Execute | Allows execution of files. | Allows searching directory. |
The file access permissions are indicated for the three groups in 9 fields as shown below
|
Owner |
Group |
Other | ||||||
| Read | Write | Execute | Read | Write | Execute | Read | Write | execute |
| R | w | x | R | w | x | r | w | x |
Each group of three is represented by an octal number.
Changing file access permissions is done using chmod command.
$chmod [who] operator permission
The different fields of the chmod command are as shown below.
| Who | Operator | Permission | |||
| u | Owner of the file | + | Adds the mode | r | Read permission |
| g | Group of owner | - | Subtracts the mode | w | Write permission |
| o | Other users | = | Assign the modes | x | Execute permission |
| a | All users | ||||
Examples - 5

Link between directory and actual data
| i-node | An i-node is a data structure defining all the specifics about a file except its name. It contains file size, number of links to the file, permission, owner, group, its last access, modification and pointers to the blocks in the file system that contain the data in the file. All access to a file is through its related i-node. |
| i-list | All i-nodes are stored as an array in the file system as i-list. |
| i-number | Access to the specific i-node in the i-list is by i-number. Each file in a file system has an i-number. |
| Link | Each i-number that names an i-node is called a link and the links field of the i-node tells how many i-numbers reference that i-node. |
| Directory | This relates a filename with an i-number. While aliasing, the same i-number is copied for the new file name. |
2.5 Command set 3
| Sl.No. | Command | Meaning |
| 1 | $file | Provides information about the type of file such as a text file or executable file or a directory. |
| 2 | $grep | Used to search for a pattern in a file. |
| $grep -v | Display all lines not containing the pattern. | |
| $grep -c | Display the number of matching lines. | |
| $grep -l | List the names of matching files. | |
| $grep -n | Precede each line by the line number in the source file. | |
| $grep -I | Ignore case while matching the pattern. | |
| $grep -w | Match only whole words. | |
| 3 | $find pathname condition action | Facilitates finding the whereabouts of a file given in pathname using the condition and perform the specified action. |
| Condition
-type x where x is |
d - directory
f - ordinary file c - character device file b - block device file p - named pipe | |
| -size n
-links n -atime n -mtime n -ctime n |
Find files of size n blocks.
Find files with n links. Find files accessed n days ago. Find files modified n days ago. Find files created n days ago. | |
| Above n means
-n means +n means |
Indicates a decimal integer
exactly n.
Indicates a decimal integer less than n. Indicates a decimal integer greater than n. | |
| Action
-exec -ok -cpio device |
Display the pathnames found.
Execute the unix system command on files found. Execute the unix system command on files on replying yes. Copy the files meeting the conditions to the special device. A pair of braces {} must appear in the place where a file argument for the command normally would appear. In such case the find command line must also end with a semicolon preceded by a backslash. Eg : $find -atime +30 -size +10 -ok rm -f {} \; This removes the files not accessed in the last 30 days and having size greater than 10 blocks. | |
| 4 | $gzip
$gunzip $zcat |
Gzip reduces the size of the named files using Lempel-Ziv coding. Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. Compressed files can be restored to their original form using gzip -d or gunzip or zcat. The amount of compression obtained depends on the size of the input and the distribution of common substrings. Typically, text such as source code or English is reduced by 60-70%. gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, with an uncompressed file without the original extension. zcat is identical to gunzip -c. zcat uncompresses either a list of files on the command line or its standard input and writes the uncompressed data on standard output. |
| 5 | $tar |
This is an archiving program designed to store and extract files from an archive file known as a tarfile. A tarfile can be stored on a tape drive. However, it is also common to write a tarfile to a normal file in other storage media. |
| $tar -c |
Creates an archive. | |
| $tar -x |
Extracts or restores files from the archive. | |
| $tar -f h3 h2 h1 |
Creates the archive h3. | |
| $tar -z |
Compresses or decompresses the tar archive with gzip. | |
| $tar -v |
Verbose mode. |

Examples - 7

Examples - 8


Examples - 10

2.6 vi editor
An editor is an utility program enabling the user to create or modify text files. The visual editor or vi displays a window of text for edit operations. The vi editor is invoked by the following command - $vi filename
If the filename does not exist, a new file will be created. If the filename exists, then its contents are read into memory and displayed. Initially, the editor will be in command mode. The ESC key is used to switch over from command mode to edit mode.
|
Cursor movement commands | |
|
h or left arrow |
Move one character left. |
|
j or right arrow |
Move one character right. |
|
k or down arrow |
Move one character down. |
|
l or up arrow |
Move one character up. |
|
W/e |
Move to the beginning/end of next word. |
|
B |
Move cursor backward to the beginning of a word. |
|
^o/$ |
Move to the beginning/end of current line. |
|
-/+ |
Position cursor at the beginning of prior/next line. |
|
L |
Move cursor to the lowest line on the screen. |
|
M |
Move cursor to the midpoint on the screen. |
|
H |
Move cursor to the highest point on the screen. |
|
Edit commands | |
|
X |
Delete the character at the cursor position. |
|
Dw/dd/D |
Delete one word/delete current line/delete the rest of the line. |
|
Ndd |
Delete n number of lines of text. |
|
Ndw |
Delete n number of words from text. |
|
Nx |
Delete n number of characters from text. |
|
R |
Replace the current character by the next character typed. |
|
U |
Undo the last change. |
|
a/I |
Append/insert text. |
|
o/O |
Inserts a line above/below the cursor. |
|
Yy |
Cut a line. |
|
p/P |
Put the cut text after/before the cursor position. |
|
J |
Join two lines. |
|
Search and save commands | |
|
/pattern , ?pattern |
Searches forward/backward for the pattern. |
|
W |
Write and continue editing. |
|
Wq |
Write and quit. |
|
q/q! |
Quit/quit without saving. |
2.7 Exercises
Session 1
Session 2
Session 3
Session 4
SHELL PROGRAMMING
3.1 Introduction
The Shell is the UNIX system's command interpreter. It acts as an interface between user and the system. It is a program that reads the lines typed at the terminal and performs the required operations.
The Shell also performs the following operations other than interpreting the command.
There are THREE popular shells available with UNIX.
3.2 Features of Shell scripts
A Shell program is simply a file containing shell commands. We normally use vi editor to create a shell program file and using chmod command we can make the shell program file executable.
Example
Create a file named test which has the following shell commands. .
pwd
ls -l
who
Make test executable using
$ chmod +x test
Execute test using
$ test
3.3 Shell Variables
A shell variable name starts with a letter or underscore and is followed by a sequence of letters, underscore or numeric characters.
Shell variables are of two types
A shell variable can be created as follows
Name = value
e.g.,
Weight = 100
color = red
Note : The shell variable can take only string data.
3.4 UNIX System Shell variables
But we can redefine it merely by assigning a new value. For example, the command PS1= ' # ' resets the prompt to # symbol.
Set is used to assign values to variables.
$ set | more
lists the values assigned to variables.
Using Variables
The read command is used to input single line of input. After entering the data from the input device, the typed data is placed in the named variable. A read command can be followed by a list of variables. If there are more words than the variables, all the left over words get assigned to the last variable. If there are more variables than words, the unused variables are not assigned values.
A variable is referenced in a command by preceding the variable name with $ symbol.
e.g.,
Create a file shell1 containing the following statements.
echo Enter your name
read Name
echo The Name Entered $ Name
Execute the file by changing the permission or by
$ sh echo
Consider the form $ { variable = word }
If the variable exits then the previous value is retained; otherwise variable = word.
$ set Manipal. MAHE
$ echo $2 $1
$ flower = ROSE
$ echo $flower
$ echo
${flower = Jasmine}
$ echo ${animal =Dog}
3.5 Positional Parameters
$0 - Name of the command itself
$* - All the arguments from $1 onwards
$# - The number of arguments
$$ - Process number (PID) of the current shell
$! - PID of the last background command
$? - Exit status of the last executed command
Shift command is used to shift $2 to $1, $3 to $2 .
e.g.,
Create a file named dip having the following commands
echo $1
shift
echo $*
execute using $dip m a h e
In the above example m, a, h, e are command line arguments and represented by $1, $2, $3 respectively.
3.6 for loop
Syntax :
for variable in value1 value2 .
do
body of for loop
don
The list of variables must be separated by one or more blanks
e.g.,
for I in 1 2 3
do
echo $I
done
e.g.
Create a file named shell_example containing the following commands
echo Welcome to shell programming
echo Enter your name
read name
for N in $name
do
echo $N
done
and execute shell_example
Create 5 data files d1, d2, d3, d4 and d5 to contain 5 names each.
Create a file containing the following
for name in 'cat d1'
do
echo $name
done
execute it.
Create a file containing the following
set d1 d2 d3 d4 d5
for file in $*
do
sort -o $file $file
done
and execute it.
3.7 case command
Syntax :
case variable in
choice 1)
commands ;;
choice2)
commands ;;
esac
e.g.,
Create a file named case_test containing the following commands
clear
banner $LOGNAME
echo
echo '1 : DATE'
echo
echo '2 : WHO'
echo
echo '3 : WORKING DIRECTORY'
echo
echo '4: LIST DIRECTORY'
echo
echo Enter your choice
read choice
case $choice in
*) echo 'Invalid choice. Quitting ' ;;
esac
and execute it.
Create a file named star_test containing the following commands
case $1 in
[aeiouAEIOU]*)echo Word beginning with a vowel. ;;
??) echo Two letter word. ;;
*) echo No idea. ;;
esac
execute star_test.
3.8 test command
This command is used to test the status of files and the values of variables. The output of test command is always true or false.
File checking
-f file True if the file exits and is not a directory
-d file True if file exits and is a directory
-r file True if file exits and is readable
-w file True if the file exits and is writable
-x file True if the file exists and is executable
-s file True if the file exists and is not empty
String checking
A string is a series of characters, which represents a file name, command name or the value of a variable.
string1 = string2 True if the strings are same
string1 != string2 True if the strings are not same
Note : There should be space on either side of the = sign
-n string True if the strings has non-zero length.
-z string True if length of the string is zero
string True if the string is not null.
Numerical comparisons
n1 -eq n2 True if the integers are equal
n1 -ne n2 True if the strings are not equal.
n1 -gt n2 True if n1 > n2.
n1 -lt n2 True if n1 < n2.
n1 -ge n2 True if n1 >= n2.
n1 -le n2 True if n1 <= n2
Logical operations
! Negates the following expression
-a Binary and operator
-o Binary or operator
-a operator has priority than the -o operator
3.9 while command
Syntax :
while expression
do
command-list
done
e.g.,
Create 4 files data.1, data.2, data.3 and data.4 . Each file has 4 sessional marks.
Create a file named search containing the following commands.
word =$1
shift
while grep $word $1 > /dev/null
do
shift
done
echo First file not containing $word is $1
execute search using $ search 25 data.[1-4]
Create a file named disp_file1 containing the following commands.
while test -f data.$1
do
cat data.$1
shift
done
execute disp_file1.
Create a file named disp_file2 containing the following commands.
limit = $1
while test $limit -lt 4
do
cat $ limit
limit = 'expr $limit + 1 '
echo The name of the file displayed is $limit
done
execute disp_file2.
3.10 until command
Syntax :
until expression
do
command-list
done
Create a file named count_down containing the following commands.
limit =5
until test $limit -lt 1
do
echo $limit
limit = 'expr $limit - 1'
done
execute count_down.
Create 5 data files named 1, 2, 3, 4 and 5
Create a file named disp_file3 containing the following commands.
limit =$1
until test $limit -lt 1
do
cat $limit
echo File name is $limit
limit = 'expr $limit - 1'
echo
done
execute it.
3.11 if then statement
Syntax :
if control commands
then
commands
fi
If the command following if is successful then the commands between then and fi are executed.
3.12 if then else statement
Syntax :
if control commands
then
commands
else
commands
fi
If the command following if is successful then the commands between then and else are executed; otherwise commands between else and fi are executed.
3.13 if then elif then else statement
Syntax :
if control command1
then
commands
elif control command2
then
command
else
commands
fi
e.g.,
Create a file named if_test containing the following commands.
# tests the number of arguments
if test $# -ne 2
then
echo Number of arguments is not equal to 2
fi
execute if_test.
Create a file named rem_dup containing the following commands.
# removes duplicate files
if cmp -s $1 $2
then
rm $2
fi
execute rem_dup.
Exit command
The exit command is used to exit from the shell script.
3.14 expr command
The expr command is used to evaluate arithmetic expressions on numeric variables. The arithmetic operators must be preceded and followed by a blank character. The result of expr command can be used by assigning the output of expr to a shell variable. Backquotes are used to substitute the output of a command for the command itself.
The while true command lets the loop run indefinitely.
The sleep command causes the program to wait for the specified number of seconds.
The break command is used to break the loop, causing the program flow to move onto the next step
The continue command is used to resume the execution at the top of the for, while and until loops. Continue causes the program to skip the remaining steps in the loop and to start the next cycle of the loop.
e.g.,
Create a file containing the following commands.
# test program foe menu
clear
echo press q to quit
#infinite loop
while true
do
echo ' 1 . data '
echo ' 2. who'
echo ' 3. ls'
echo ' 4. pwd'
read choice
case $choice in
q) break ;;
# break takes out of the infinite loop
*) echo invalid choice! ;;
# for any other choice proceed
esac
done
Create a file containing the following commands.
# displays the filenames
for filename in 'ls $1 '
do
if test -d $1 /$filename
then
continue
else
echo file is $filename
done
3.15 Exercises
UNIX System Calls 4.1 Background
Generally, programmers rely on C library for file management or UNIX process control. While the C library support suffices for most of the application development purposes, there are times when one needs to have more control over the process environment than what these library routines can support. If a C program, for instance, needs to create multiple cooperating processes, the program cannot rely solely on C library. Programs have to resort to UNIX system calls to perform such complex tasks.
A system call is an entry point into the kernel. A C program invokes a system call by a normal function call, supplying appropriate parameters. Unix system calls can be used to manage files, control processes, or communicate over a network. In some cases, certain library routines of C language act as counterparts to system calls. In such cases, system calls perform faster than their library equivalents. The reason is simple: C library perform a variety of house keeping jobs that brings down its performance.
We will deal with a set of system calls that can be broadly classified asIn the following sections we shall study some of these interesting and versatile system calls in depth.
4.2 File System Related System calls
In this section we shall use UNIX system calls that help applications in managing file systems. A file system in UNIX houses an assortment of files. Among file system related system calls, we shall be interested in using those which
There are some system-calls which do not alter the file system information in any fashion. Such system calls help programs in collecting important information either about the entire file system or about particular files. We shall look at one or two such helper functions soon.
All system calls return -1 on failure. Upon success, the type and meaning of returned value depends solely on the system call. Additionally, system calls leave an error number in an external integer called errno. By using errno a program can, for example, determine whether an attempt to open a file failed because it did not exist or because your program lacked permissions to read it. There is also an array of character strings sys_errlist indexed by errno that translates the number into a meaningful string. errno is initially zero, and should always be less than sys_nerr. It is not reset to zero when things go well, a program must reset it after each error if it intends to continue.
Here is a snippet to give you an idea of how all this is used in a program. If you have a "catch-all" error handing routine called 'Error ( )' it can contain some of these statements:
/* Application specific declarations */
extern int errno, sys_nerr;
extern char *sys_errlist[ ];
if ( errno > 0 && errno < sys_nerr ) {
fprintf (stderr, " (%s)", sys_errlist[errno] );
}
C library provides a function perror() which does what the code shown above achieves. A program can also make use of perror() instead of directly taking control over such details
.4.2.1 System calls for basic file operations
4.2.1.1
Opening an existing fileLet us first run down a list of important system calls that perform elementary file operations. These include the open(), close(), read(), write(), lseek(), link(), unlink(), stat() and access() functions. All these functions return -1 on failure.
Before a file could be read or written it should be successfully opened. The Open() function could be used to open an existing file as well as create a new one. It's prototype is given below:
int open(const char * pathname, int flags [, mode_t mode])
As you can see, the mode is optional and is used only when open() is called to create a new file. Let us first consider opening an existing file.
The pathname in open() specifies the file and
flags is one of
These can also be bitwise ORed with one or more of the following constants to change the default behavior
Open() returns an integer value called file descriptor ( not "file pointer"). Kernel uses file descriptor internally to associate a physical file with operating system related data. If you change returned integer value, your program may not work the way you expect it to - so be careful to store the value and not to modify.
4.2.1.2
Creating a new fileWhen a program wants to create a new file it can use open() with an extra mode parameter and set some more bits in the flags parameter. flags could be one or more of the following constants ORed together:
The modes indicate user permissions that the new file inherits. It can be obtained by ORing the normal read-write-execute permission bits.
As stated earlier, open( ) returns a file descriptor. File descriptor is a logical handle to a named file in the file system. Therefore, each call to open( ) returns a unique ( new ) descriptor. The first three descriptors 0, 1, and 2 are called standard input, standard output, and standard error file descriptors. Processes on UNIX conventionally use the standard input descriptor to read input data, the standard output descriptor to write output data, and the standard error descriptor to report error data. Adoption of this convention by all user programs makes it easy for them to communicate via pipes, as we shall see later.
4.2.1.3
Collecting file informationIf a program needs to be extra cautious about the kinds of file objects it deals with, it can make use of stat() or fstat() system calls:
int stat (const char *FileName, struct stat *buf);
int fstat (int FileDescriptor, struct stat *buf);
Both stat() and fstat() fill in a special structure called stat structure. stat structure contains all the details contained in the inode corresponding to filename in case of stat(), and FileDescriptor in case of fstat(). For example, to distinguish if a file is a directory or a regular file, call stat() and check for a specific bit pattern in a filed called st_mode inside stat structure:
struct stat FileInfo;if (stat ("Myfile", &FileInfo) != -1) {
if ( FileInfo.st_mode & S_IFDIR ) {
/* "MyFile" is a directory */
} else if if ( FileInfo.st_mode & S_IFREG ) {
/* "MyFile" is a regular file */
}
}
See the program fileinfo.c further details about stat() function.
4.2.1.4
Reading and writing into a fileThe syntax of read system call is
int read (int file_descriptor, char *buffer, int buffer_size)
Where file_descriptor is the descriptor returned by open(), buffer is the address of contiguous memory locations in users address space, and buffer_size is the number of bytes the user wants to read from the file. This function returns the number of bytes actually read from the file. One should always be careful not to assume that the number of bytes read will always be same as the number of bytes requested.
The write system call shares the same syntax as read():
int write(int file_descriptor, char *buffer, int buffer_size)
Again, write returns the number of bytes actually written to the stream, which could be less than the count requested in the call.
4.2.1.5
Closing a fileA process uses close() system call when it no longer wants to access it. The syntax for this function is
int close(int file_descriptor)
where file_descriptor has been obtained by a call to successful open().
A code snippet shows how these system calls could be used in a typical application. In the example, a file is opened for reading and its contents are displayed on the standard output until end of file is reached. Here is a snippet:
File_Descriptor = open(FileName, O_RDONLY);
if (File_Descriptor == -1) {
perror("open");
return 1;
}
while ( (nBytes = read (FileDescriptor, Buffer, BUFFER_SIZE)) > 0 )
{
nBytes = write(1, Buffer, BUFFER_SIZE);
if ( nBytes == -1) break;
}
close(File_Descriptor);
4.2.1.6
Random access in a fileThe ordinary use of read and write system calls provides sequential access to a file. In cases where random access to a file is desired, lseek() is employed.
size_t lseek(int file_descriptor, size_t offset, size_t reference)
where file_descriptor identifies the file, offset is a byte offset into the file and reference indicates whether offset should be considered from the beginning of the file, from the current position of the read, write offset, or from the end of file. The return value indicates the byte offset from where the next read or write will start. Three constants are used in specifying the reference point for lseek operation:
Note that negative offsets can also be specified and lseek will move the file offset backwards in such a case.
Code listing for tail.c demonstrates the use of lseek. This program prints last ten lines of a given file. The first thing that this program does is to move to the end of file once a file has been opened for reading. From there it starts moving back skipping newline characters found on its way back.
Here is the code snippet
File_Descriptor = open(FileName, O_RDONLY);
if (File_Descriptor == -1) {
perror("open");
return 1;
}
if ( lseek (File_Descriptor, 0, SEEK_END) != -1) {
.
}
4.2.1.7
Deleting files and Creating new linksunlink() system call is used to delete an existing file. Its syntax is as follows
int unlink(char *PathName)
Any subsequent attempt to open the same file is bound to fail.
The link() system call, on the other hand, creates a new alias for an existing file. After a successful link(), the kernel will not know which name was the original file name. Hence, no file is treated specially.
int link (char *SourceFileName, char *TargetFileName)
4.2.1.8
Changing directory, file permissions, and file ownershipWhen an application decides to switch to a new working directory, it uses chdir() system call:
int chdir (char *PathName)
Changing the access permissions of a file is achieved by
int chmod (char *PathName, int NewMode)
Changing file ownership is done by
int chown (char *PathName, int OwnerId, int GroupId)
4.2.1.9
Inspecting file permissionsaccess()
system call checks if the calling process has read, write, or execute permissions for the file, according to the value of mode.The value of mode is a combination of the bit patterns 4 for read, 2 for write, and 1 for execute.
4.3 Process related system calls
4.3.1 Process Fundamentals
It is not quite uncommon for one process to spawn off new processes. As you will see later, a concurrent server forks off new instances of itself, or other types throughout its lifetime. Problems that could be parallelized are best solved when multiple processes are created and controlled by a central process.
Processes in UNIX share a parent-child relationship. If process A spawns a new process B, we call process B the child of process A. Process B will inherit all open files of its parent, that is, Process A. So if a process opens a file and forks child processes, all child processes can share data stored in that file. Programs need to be aware of such concurrent accesses to a shared resource and guard themselves by employing appropriate synchronization mechanisms.
Each process in UNIX is identified by a unique integer called process id ( PID for short). A process can find out its PID at runtime through getpid () system call:
int getpid()
Similarly, getppid () returns the PID of the parent ( PPID for short ) of a process executing this system call :
int getppid()
Please note that there are other system calls that sound similar to these functions which, however, return a different sort of information. getuid () is one such function that returns the real user id of the executing process.
uid_t getuid()
To be more specific, there could be ( and normally are ) many processes with same UID but each with different ( and unique ) PIDs. Moreover, there could be many processes with same PPID too. While PID and PPID remain the same once allotted ( and can not be modified ) during the lifetime of a process , its UID can be changed to something else by the setuid() system call:
int setuid ( uid_t EffectiveUID )
4.3.2 Creating new processes
A process can spawn off a new process by the fork () system call. As usual, it returns -1 on failure, but has interesting behavior on success.
int fork ()
fork () returns the PID of new child process to the parent, and zero to the new child process. Child process can later determine its PID by calling getpid (). After fork returns successfully, there will be two instances of the same process running in the system. This may sound a little confusing initially but you need to think about it a little carefully. Since both parent and child start executing almost simultaneously after fork () and since a process will not have any say in process scheduling performed by kernel, a program must first determine if it is a parent or a child!. Here is a snippet that shows how to do this:
int ChildProcessId;
.
ChildProcessId = fork(); /* create a new process */
/* both parent & child start running from the nezt statement ! */
if (ChildPocessId != -1) { /* fork successful */
if ( ChildProcessId == 0 ) { /* fork() returns zero to child */
/* perform child specific task */
ChildProcessId = getpid(); /* set appropriate value ! */
exit (0); /* everything went well, should quit */
} else { /* parent process; fork() returns PID of the child */
/* parent specific code */
}
As you can see, a process can always find out who's who by looking at the return value of fork ().
After the fork (), parent & child will have separate copy of data, heap and stack. So any changes to data, heap or stack in one process will not affect the other. However, remember that the child inherits already open file descriptors from the parent and both can start vying for input from the open file files
4.3.3 Loading a new process image
fork () creates a new copy of the process calling the function. This in itself will not be interesting if, there were no provision to create a new process .Suppose that our program needs to conditionally run different programs, in which case, the program needs to have a way of loading and running other programs. execl () system call is used precisely in such circumstances:
int execl (const char *Path, char *arg, )
Path specifies the executable file, and arg specifies first argument to the new process. Subsequent parameters are all passed as char * s with NULL representing end of the parameter list.
However, on success, execl () overlays the calling program !; This means that any code following execl () in parent process will not execute. Instead, the executable file specified in Path will replace calling process' image and the process starts running the new image.
As an example, look at the following code snippet. It creates a new shell ( which is "/bin/sh" ):
int NewProcessId;
extern int errno;
NewProcessId = fork();
if ( NewProcessId != -1 ) {
if ( NewProcessId == 0 ) { /* child process */
execl("/bin/sh", "/bin/sh", NULL);
/* if the program reaches next line, there is an error */
perror("exec failed");
} else { /* parent peocess */
if ( wait ( &Staus ) == -1 ) {
/* some error ? */
}
}
}
4.3.4 Parent-child rendezvous
The wait () system call is used by a parent process to wait on a child process. If there is no child process, then wait () returns immediately. In the above example, the child tries to run /bin/sh and the parent waits for the child to run to completion. In this way a parent can always synchronize or join with a child.
4.3.5 Signals
This section is concerned with gracefully reacting to external as well as internal interrupts. Signals are sent to a process when user types DEL key ( or CTRL + C ), or when a program attempts an illegal memory access or floating point errors and so on. The default action of a signal is to terminate the process. For most signals, a core image file is written for potential debugging.The signal () system call alters the default behavior. It has two arguments. The first is a number that specifies the signal. The second is either an address of a function, or a code which requests that the signal be ignored or be given the default action. Thus :
signal ( SIGINT, SIG_IGN)
causes signals to be ignored, while
signal ( SIGINT, SIG_DFL)
restores the default action of process termination. In all cases , signal returns the previous value of the signal. If the second argument to signal is the name of a function ( which must have been declared already in the same source file ), the function will be called when the signal occurs. Commonly, this facility is used to allow the program to clean up unfinished tasks before terminating.
Here is a small piece of code to demonstrate safe deletion of temporary file.
char *TempFile = "temp.xxxx";
void SignalHandler (int SignalNo)
{
unlink(tempfile);
exit(1);
}
main()
{
if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
signal(SIGINT, SignalHandler);
mktemp(TempFille);
/* process */
exit(0);
}
Note that there are two calls to signal () and a check to the return value. Since signal () returns the status of signal handling, we first check to see that the SIGINT is not being ignored by the program; if the program starts by saying that it ignores SIGINT then the program doesn't alter that behavior. Otherwise, new signal handler is installed.
4.3.6 Inter process Communication using pipes
In some circumstances, a parent and one or more child processes might be required to communicate by sending and/or receiving data among themselves. For example, the piping facility provided by shell allows each process to filter data in a pipeline manner. Shell actually sets up a pipe internally between each process participating in the pipeline. Pipes allow processes to access the data in a FIFO fashion.
In this section, we shall see how such a pipe is created and used. There is a direct system call that creates a pipe:
int pipe ( int FileDescriptors[2] )
pipe () takes an array of two integers as parameter and fills in with two file descriptors representing the read-end & write-end of the pipe. In the above line, FileDescriptors[0] is for reading & FileDescriptors[1] is for writing.
As stated earlier, a child process inherits all open files from the parent and pipes are no exception to this rule. So a parent can continuously write to FileDescriptors[1] ( the writing-end of pipe ) and the child could go on reading from FileDescriptors[0] ( the reading-end of pipe ) or vice-versa. Thus parent and child can send/receive arbitrary stream of bytes as long as they wish.
Unlike regular files, a pipe can not be read/written at arbitrary offsets. One can not lseek () into a pipe.
Threads
5.1 Introduction
The previous section demonstrated the UNIX processes model. Recent enhancements to this traditional model include thread support also. Threads are necessary in various circumstances becauseThreads are designed to reduce both these overheads. Threads are sometimes called light weight processes. Thread creation could be 10 - 100 times faster than process creation.
All threads within a processes share global memory. This makes sharing information easy between threads, but one should be cautious because of race conditions. Moreover, all threads within a process share
But each thread has its own
In this section we shall see thread creation and thread synchronization as stated by the POSIX standard.
5.2 Thread Related Functions
Note: All thread related functions return zero on success and a nonzero value on failure
When a process is created using exec (), a single thread is created. In other words, every process contains at least one thread; initially there is only the primary thread. Additional threads are created by
int pthread_create ( pthread_t *thread_id, const pthread_attr_t *attr,
void *(*thread_function) ( void *), void *arg)
pthread_t is a abstract type representing the thread object.
pthread_attr_t is also an abstract type which is used in setting certain thread properties during creation. This parameter could be NULL, in which case default properties would be set.
thread_function is the name of a function within the program from where thread starts executing. When this function executes a return, thread completes its execution.
arg is any value that is sent as a parameter to the thread function. Normally, the thread function would typecast this argument to a type that it expects inside the function.
Here is a code snippet to show how a thread gets created:
/* user defined data */
struct MyData {
};
void *thread_function (void *arg)
{
/* this thread expects a pointer to 'struct MyData' */
struct MyData *pData = (struct MyData *) arg;
/* use the data here */
.
return 0;
}
some_function()
{
pthread_t thread_id;
struct MyData *pData;
pData = malloc (sizeof (struct MyData) );
/* initialize data */
/* Create the thread */
ret = pthread_create ( &thread_id, /* note the adress operator */
NULL, /* default thread properties */
thread_function, /* the thread function */
pData); /* dynamically allocated data */
if ( ret != 0 ) {
/* thread creation failed */
free ( pData );
}
}
We can wait for a particular thread to complete its execution by calling
int pthread_join ( pthread_t thread_id, void **status)
thread_id is one that is obtained by a successful call to pthread_create ()
status, if non-NULL, stores the return value from the thread.
As could be seen from the prototype, we can wait on a single specific thread at a time. We can not wait on an arbitrary thread.
A thread can determine its own ID by calling
pthread_t pthread_self ( )
A thread can terminate itself by calling
void pthread_exit( void *status)
status pointer should not point to an address local to the thread, since, the lifetime of that object gets over when thread exits.
5.3 Thread Synchronization
As stated in the previous section threads compete for data and resources within a process. A program making use of threads should be extra cautious in designing interfaces that access shared resources. This includes accessing a file, updating a global variable etc. In order to simplify this sort of thread synchronization, POSIX supports mutex objects. Using these objects, two or more threads could use shared resources in a co-operative fashion.
A mutex has two possible states: unlocked ( not owned by any thread ), and locked ( owned by a thread ). A mutex can never be owned by two different threads simultaneously. A thread attempting to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first.
In order to use a mutex, a program has to first instantiate an object of type
pthread_mutex_t, initialize it with a particular constant
PTHREAD_MUTEX_INITIALIZER, and call
int pthread_mutex_init ( pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutexattr )
mutex is the mutex object to be initialized.
mutexattr could be null, in which case default properties will be assigned to a mutex.
In order to gain access to the resource by locking the mutex, call
int pthread_mutex_lock ( pthread_mutex_t *mutex )
pthread_mutex_lock locks the given mutex. If mutex is currently unlocked, it becomes locked and owned by the calling thread, and pthread_mutex_lock returns immediately. pthread_mutex_lock suspends the calling thread until mutex is unlocked.
After the resource is used, a thread unlocks the mutex to give other threads an access to the same resource by calling
int pthread_mutex_unlock(pthread_mutex_t *mutex)
To give an idea of how a mutex object could be used, here is a code fragment:
/*
this is one of the threads in the system. arg is intended to be a mutex object. This thread assumes that the primary thread has already created the mutex and initialized it appropriately .
*/
void * writer_thread ( void *arg )
{
pthread_mutex_t *mutex;
mutex = ( pthread_mutex_t * ) arg;
pthread_mutex_lock ( mutex );
/* use the resource */
/* resource used. unlock the mutex */
pthread_mutex_unlock ( mutex );
return 0;
}
SOCKETS
6.1 Introduction
Sockets are a form of IPC provided by the Unix (BSD) that provide communication between process on a single machine and between processes on different systems.
Communication is normally between a server and a client.
Server : Server is the process that is waiting to be contacted by a client process so that the server can do something for the client. Server process is started on a computer system. It initalizes itself and then goes to sleep waiting for another process to contact it requesting some service.
Client : Client is the process that takes some service from the server process. Client process is started, either on the same system or an another system that is connected to the server's system with a network. The client process sends a request across the network to the server requesting service of some form.

Sockets can be used to write client-server applications using
6.2 Connection Oriented Communication
In this process both the client and the server come to an agreement that they are ready for transmission and reception of data. A connection is established between a client and a server. An acknowledgement is sent after receiving a packet.
Fig below explains this process.

6.3 Connectionless Communication
Figure below shows a time line of the typical scenario that takes place for a connection-less communication. The system calls which are used for connectionless communication are different from that of connection-oriented communication. In this type of communication, the client does not establish a connection with a server, so that there is no connection termination process also. Instead, the client just sends a datagram to the server using the sendto system calls, which requires the address of the destination (the server) as a parameter. Similarly, the server does not have to accept a connection from a client.

Instead, the server just issues a recvfrom system call that waits until data arrives from some client. The recvfrom returns the network address of the client process, along with the datagram, so that the server can send its response to the correct process.
6.4 System calls used in sockets
Socket() :
To perform network I/O the first thing a process must do is call the socket function, specifying the type of communication protocol desired.
# include <sys/socket.h>
int socket(int family, int type, int protocol);
This function returns a socket descriptor. The family specifies the protocol family and is one of the constants shown below
AF_UNIX Unix Internal protocol
AF_INET Internet protocol
AF_NS Xerox NS protocols
AF_IMPLINK IMP link layer
The AF_ stands for the address family,
The type is one of the constants given below
SOCK_STREAM stream socket
SOCK_DGRAM datagram socket
SOCK_RAW raw socket
Normally, the protocol argument to the socket function is set to 0.
bind() :
The bind system call assigns a name to an unnamed socket.
#include <sys/types.h>
#include <sys/socket.h>
int bind (int sockfd, struct sockaddr *myaddr, int addrlen);
sockfd is the socket descriptor that was returned by the socket function. The second argument is a pointer to a protocol specified address and the third argument is a size of this address structure. The structure sockaddr is of the format
Struct sockaddr {
_short sa_family; /*address family :AF_xx value*/
Char sa_data[14]; /*upto 14 bytes of protocol specified address*/
};
For the internet family, the following structures are defined in <netinet/in.h>
Struct in_addr {
_long s_addr; /*32 bit netid/hostid network byte ordered */
};
struct sockaddr_in {
short sin_family; /*AF_INET*/
u_short sin_port; /*16 bit port number*/
struct in_addr sin_addr; /*32 bit netid/hostid*/
char sin_zero[8] /*unused*/
};
Ports (sin_port) in the region 1-255 are reserved by TCP/IP. The system may reserve more. User processes may have their own ports above 1023.
connect () :
The connect function is used by a TCP client to establish a connection with a TCP server.
# include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
This system call returns 0 if OK ,-1 on error.
sockfd is a socket descriptor that was returned by the socket function. The second arguments is a pointer to a socket address structure, and the third argument is its size.
listen() :
This system call is used by a connection-oriented server to indicate that it is willing to receive connections.
#include <sys/socket.h>
int listen(int sockfd, int backlog);
The function is normally called after both the socket and bind functions and must be called before calling accept function. The backlog argument specifies how many connection requests can be queued by the system while waits for the server to execute the accept system call. This argument is usually specified as 5, the maximum value currently allowed.
accept() :
After connection oriented server executes the listen() system call, an actual connection from some client process is awaited by having the server execute the accept() system call.
# include <sys/types.h>
#inlcude <sys/socket.h>
int accept(int sockfd, struct sockaddr *peer, int *addrlen);
accept() takes the first connection request on the queue and creates another socket with the same properties as sockfd. If there is no connection request pending, this call blocks the caller until a request arrives.
The peer and addrlen arguments are used to return the address of the connected peer process (the client). The addrlen is called a value-result argument : the caller sets its value before the system call, and the system call stores a result in the variable. For this system call, the caller sets addrlen to the size of the sockaddr structure whose address is passed as the peer argument. On return, addrlen contains the actual number of bytes that the system call stores in the peer argument.
Close() :
The normal Unix close() system call is also used to close a socket.
Int close(int fd);
If the socket being closed is associated with a protocol that promises reliable delivery, the system must assure that any data within the kernel that still has to be transmitted or acknowledged, is sent.
read() :
Data is read from an open file using
int read (int fildes, char *buff, unsigned int nbytes);
If the read is successful, the number of bytes read is returned. This can be less than n bytes that was requested. If the end of file is encountered, zero is returned. If an error is encountered, -1 is returned.
write() :
Data is written to an open file using
Int write (int fildes, char *buff, unsigned int nbytes);
The actual number of bytes written is returned by the system call. This is usually equal to the nbytes argument. If an error occurs, -1 is returned. Both read and write specify their nbytes argument as an unsigned integer, so that more than 32767 bytes can be read or written in one operation on a system that has 16 bit integers.
6.5 CLIENT-SERVER EXAMPLES
6.5.1 Sending message from the server to the client.
Steps :
Server side :
Client side :
6.5.2 Sending a file from the server to the client.
Steps :
Server side :
Client side :
6.5.3 Remote command execution.
Steps :
Server side :
Client side :
6.5.4 Remote procedure calls.
Steps :
Server side :
Client side :
Steps :
Server side :
Client side :
Linux installation and administration
7.1 What is Linux?
Linux is an operating system for several types of computer platforms. Primarily Intel based PCs. The system has been designed and built by hundreds of programmers scattered around the world. The goal has been to create a UNIX like clone, free of any commercially copyrighted software, that the entire world can use.
Actually, Linux was started as a hobby by Linus Torvalds as a student at the University of Helsinki in Finland. He wanted to create a replacement for the Minix operating system, a UNIX like operating system available for Intel based PCs.
7.2 Why use Linux?
One should use Linux because it is the only operating system that is available free of cost. It provides multitasking, multiprocessing capabilities for multiple users on IBM - PC compatible hardware platforms. No other operating system gives you the same features with the power that Linux enjoys. Linux also separates you from marketing whims of the various commercial providers. Many applications of Linux are freely available on the Internet and you have the free access to the source code to modify and expand the operating system to your needs, some thing you cannot do with commercial operating systems such as Windows NT, Windows 95, MS-DOS or OS/2.
7.3
Who owns Linux?IBM owns the right to OS/2, Microsoft owns the rights to MS-DOS and MS Windows, but various components of Linux are copyrighted by many people. Linus Torvalds holds the copyright to the basic Linux kernel. Red Hat Inc. owns the rights to the Red Hat distribution version and Paul Volkerding holds the copyright to the Slackware distribution. Many Linux utilities are GNU General Public License.
7.4 Basic features of Linux:
7.5 Advantages of using Linux:
Disadvantages:
7.6 Installing the Red Hat Linux
System requirements;
Intel 386 or higher.
Minimum of 40MB hard disk space.
Minimum of 4 MB memory.
CD ROM drive & 3.5 inch floppy disk drive.
Two operating systems such as DOS and Linux can be installed on the same machine. For this purpose ,the hard disk has to be partitioned using the DOS fdisk utility.
Using the fdisk utility, create the primary DOS partition of the required size so as to load other operating systems such as DOS or Windows.
The remaining portion of the hard disk is left as a non DOS partition for the Linux.
Installing Red Hat 5.2 on a single non DOS partition
This version of the Linux can be installed directly from the CD and the different steps for the installation are given below.
Command (m for help): m
Command action
a toggle a bootable flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
p print the partition table
q quit without saving changes
t change a partitions system id
u change display/entry units
v verify the partition table
w write table to disk and exit
Installing Windows and Linux on a single hard disk:
Note: Many times one encounters unexpected problem., In such cases install again.
7.7 Linux administration
Each Linux system has a single user who can perform virtually any operation on the computer. This user is called the superuser and has a special login name called 'root'.
The home directory for the root is typically '/' (the root directory of the file system) or a specific home directory such as /home/root.
Booting and Shutting down
7.7.1 Booting from a floppy
Linux boot floppy contains a copy of the Linux kernel that points to the root Linux file system on the appropriate hard drive partition.
Note: One should make bootable Linux floppy disc during the installation, even though you intend to install a boot manager on your hard drive. If your hard disk crashes, the bootable floppy might be the only way to boot the system.
7.7.2 Booting from the boot manager
Linux comes with the boot manager known as LILO (Linix Loader). This program modifies the master boot sector of your hard disk and allows you to choose the operating system that you want to boot, when you turn on your computer.
7.7.3 Configuring LILO
LILO reads a configuration file, /etc / lilo.conf, and uses it to figure out what operating systems are installed on your computer and where their boot information is located. The typical configuration file appears as shown below.
# Section for the Linux partition
image = /vmlinuz
label = Linux
root = /dev/hda1
# Section for MS-DOS
other = /dev/hda3
table = /dev/had
label = msdos
When you boot your computer with LILO installed, you get a prompt that reads as LILO:. At this point, you have several options. You can wait and have Linux boot your default operating system, or type the name of the operating system to boot. Pressing <Tab> displays the list of different available operating systems.
7.8 Shutting down Linux:
You should never turn off a Linux system without shutting down properly. The file systems need to synchronize properly before the system shuts down. You can cause severe damage to the Linux file system if you just power the system off.
The best way to shut down a Linux system is with the shutdown command whose syntax is given below.
shutdown [flags] time [warning message]
[warning message] is a message sent to all users that are now logged on, and the time is the time that the shutdown is to occur. The time argument can be specified in the format hh:mm or +m where m stands for minutes.
The list of some of the flags that can be used in shutdown are listed below.
-k Do not really shut down the system; just send the warning message to all users.
-r reboot after shut down.
-h halt after shut down.
7.9 Managing user accounts
7.9.1 Adding a user
The adduser command is used to add a user to a Linux system.
e.g.,
# adduser Jones
When you add a user, an entry for the user in the password file , /etc/passwd. The entry has the following format
login_name:encrypted_password:user_id:group_id:user_information:login_directory:login_shell
login_name The name used to login.
encrypted_password The password required to authenticate the user.
user_id A unique number that the operating system uses to identify the user.
group_id A unique number or name used to identify the primary group for this user.
user_information A description of the user, such as the users name or title.
login_directory The users home directory.
login_shell The shell used by a user when logging in.
Setting user Passwords:
e.g.,
# passwd Jones
The pass word is encrypted and put into the /etc/passwd file
Pass words should be atleast six characters long.
7.9.2 Removing a user
To remove only the capability to log in, (whereas the users directory files are intact) put a * in the second field of the users entry.
Jones: * :123:21:
To delete the user completely, you must delete the users entry from the passwd file.
7.10 Back ups and restoring files
Several different utilities are available for backing up and restoring files in a Linux system
tar Tape archieve utility available on every Linux or UNIX system.
cpio General purpose utility for copying files.
Using tar:
tar cf /dev /fd0 /home
the above command copies / home to the floppy drive /dev / fd0
common options for the tar command
C create an archive
x extracts or restores files from the archive that is on the default device or on the device specified by the f option.
f name creates an archive or reads the archive from name where name is a file name or a device specified.
z compresses or decompresses the tar archive.
t creates an index of all files stored in an archive and lists on stdout.
Using cpio:
The following command copies the files in the directory / home to the device /dev / fd0
ls /home | cpio o > / dev / fd0
Commonly used options for cpio
-o copy out. Create an archive on standard out.
-i copy in. Extracts files from the standard input.
-t creates a table of contents of the input.
7.11 Accessing the floppy drive
You can create a file system on a floppy just as you would on a hard drive partition. For example
# mke2fs /dev /fd0 1440
creates a file system on the floppy in /dev /fd0. The size of the file system must correspond to the size of the floppy.
In order to access a floppy, you must mount the file system containing it. The command is
# mount -t ext2 /dev /fd0 /mnt
will mount the floppy in /dev /fd0 on the directory /mnt. Now all the files on the floppy will appear under /mnt on your drive. The -t ext2 specifies an ext2fs file system type.
The mount point (the directory where you are mounting the file system) needs to exist when you use the mount command. If it does not exist, you can create it with mkdir command.
7.12 Managing the file system
File system form the basis for all data on a Linux system. Linux programs, libraries, system files and user files all reside on the file systems.
To users, the directory tree looks like a seamless entity - they just see directories and files. In reality, many of the directories in the file tree are physically located as different partitions on a disk, on different disks, or even on different computers. When one of these disk partitions is attached to the file tree at a directory known as a mount point, the mount point and all directories below it are referred to as a file system .
Mounting and unmounting file systems
To mount a file in the Linux directory tree, you must have a physical disk partition, CD-ROM, or floppy disk that you want to mount. You also must make sure that the directory to which you want to attach the file system, known as the mount point actually exists.
Mounting a file system does not create the mount point directory. The mount point must exist before you try to mount the file system. Suppose you want to mount the CD-ROM drive /dev /sr0 under the mount point /mnt , a directory named /mnt must exist, or the mount fails. After you mount the file system under that directory, all the files and subdirectories on the file system appear under the /mnt directory. Otherwise, the /mnt directory is empty.
The syntax of the mount command is
mount device mount point
device the physical device you want to mount
mount point point in the file system tree you want it to appear
Note: The mount command can only be used by the superusers. This is to help the system security.
Command line argument for the mount command is:
-w mounts the file system with read and write permissions.
-r mounts the file system with read only permissions.
-n mounts without writing an entry in the / etc /mtab file.
-t type specifies the type of the file system being mounted.
-a causes mount to try to mount all file systems in /etc/fsatb.
Unmounting file systems
There are three basic forms of the umount command:
umount device | mountpoint
umount -a ; unmounts all file systems.
umount -t fstype ; acts only on the file systems of the type specified.
device ; name of the physical device to unmount.
mountpoint ; mount point directory name.
7.13 Maintaining file systems
In a normal shut down process, the kernel unmounts the file system by writing a special signature on the file system to indicate that data is intact. When the file system is mounted again for writing, this signature is removed.
File system check (fsck) command is used to check the damaged or corrupt files.
The syntax for the fsck command is
fsck argument filesys
argument list
-A goes through the /etc/fstab file and tries to check all file systems in one pass.
-a automatically repairs any problems found in the file system without asking any questions.
-l list all the file names in the file system.
-r asks for confirmations before repairing the file system.
filesys specifies which file system is to be checked (this can be a device such as /dev/hda1 or mount point such as /usr).
Glossary
$HOME
- Environment variable that points to the login directory.$PATH - Environment variable that contains the set of directories to be searched for commands.
$TERM - Contains the terminal setting.
alias - Used to create alternative names for commands.
API - Application program interface using which an application can make requests to the operating system.
Background process - Processes usually running at lower priority without requiring any user interaction. Any input or output are from other files or processes.
Bash - Stands for GNU Bourne Again Shell
bg - Used to force a suspended process to run in the background.
Block-special - A device file that is used to communicate with block oriented I/O devices such as disk and tape drives.
BSD - Berkeley Software Distribution.
CGI - Common Gateway Interface. A method of transmitting data between web pages and programs executing on the server.
Character special - A device file that is used to communicate with character oriented I/O devices like terminals, printers.
chgrp - Used to change the group associated with the permissions of the file or directory. The owner of the file has the right to change the group associated with the file.
chmod - Used to change the permissions associated with a file or a directory.
chown - Used to change the User ID associated with the permissions of the file or directory.
Daemon - A system related background process that generally runs with the permissions of root and services requests from other processes.
DNS - Domain Name Server. Used to convert the name of a machine to IP address.
Emacs - An editor.
env - Used to see the exported environment variables.
Filesystem - A collection of disk storage that is connected to the directory structure at some mount point.
find - Used to find the whereabouts of a file.
finger - User information lookup program.
Firewall - A system used to provide a controlled entry point to the internal network from outside.
FTP - File Transfer Protocol.
GNU - Stands for free useful software packages commonly found in UNIX environments.
grep - Used to search for a pattern in a file.
Hostname - Used to display the current host or domain name of the system or to set the hostname of the system.
i-node - Used to describe a file and it's storage.
ICMP - Internet Control Message Protocol.
Kernel - The core of the operating system that handles tasks like process allocation, memory allocation, I/O handling and user access.
kill - Sends the specified signal to the specified process. If no signal is specified, the TERM signal is sent
Link hard - Directory entry that provides an alias to another file in another filesystem.
Link symbolic - Directory entry that provides an alias to another file that could be in another filesystem.
Metacharacter - A printing character having a special meaning to the shell or another command.
MIME - Multipurpose Internet Mail Extensions. A set of protocols of attaching binary data such as executables, images or additional text to e-mail.
mkfs - Used to build a filesystem on a device, usually a hard disk partition.
mount - Attaches the filesystem to the specified directory specified as the parameter.
NFS - Network File System. Method of connecting disks that are mounted on the remote system to the local system as if they are physically connected.
Parent process - Process that controls another process referred as sub process or child process.
pine - Interactive mail program.
Pipe - A method of sending the output of one program to become the input of another.
POSIX - Portable Operating System Interface, Unix. It is the name for a group of open system standards based on Unix.
PPP - Point to point protocol.
Process - An instance of a program in execution.
Process ID - An unique identifier assigned to every process running in the system.
ps - Gives details about the current processes.
Redirection - The process of directing a data flow.
Regular expression - A way of specifying and matching strings for shells.
RFC - Request For Comment. Document used for creation of Internet and TCP/IP related standards.
RPC - Remote Procedure Call.
Signal - A special flag or interrupt that is used to communicate special events to programs by the OS and other programs.
SLIP - Serial Line Internet Protocol.
tar - Tape archiving utility.
Talk - Used to communicate between two users.
TCP/IP - Transport Control Protocol/Internet Protocol.
Telnet - Protocol for interactive terminal access to remote systems.
UDP - User Datagram Protocol.
UUCP - Unix to Unix Copy Program.
wall - Displays the contents of standard input on all terminals of all currently logged in users.
yacc - Yet Another Compiler Compiler.
Bibliography
homepage Material's Software's Miscellaneous
Disclaimer: I offer the following hacking ideas for educational purposes only. I assume no responsibility for what they are used for nor do the authors. I recommend that you are very careful in handling these ideas as well. The reader is responsible for his own actions. By downloading any of the following files or using them you accept all responsibility and this disclaimer.