404
◾
Linux with Operating System Concepts
5 0 3 5341 input directory
5 0 1 2455364 log socket
11 0 2 1 pts directory
5 69 1 5185 vcs character special file
In this second listing,
we see the devices autofs, dm-0, dvdrw, input, log, pts, and vcs.
All except pts are located on device number 5. All except vcs are owned by user 0 (root);
vcs is owned by vcsa, the virtual console memory owner account. Most of the items have
only one hard link, found in /dev. Both input and pts are directories and have more than
one hard link. The inode numbers vary from 1 to 2,455,364. The file type demonstrates
that “files” can make up a wide variety of entities from block or character files (devices) to
symbolic links to directories to domain sockets. This last field varies in length from one
word (directory) to three words (character special file, block special file).
You might want to explore the inode numbers in your file system to see how diverse they
are. Each new file is given the next inode available. As your file system is used, you will
find newer files have higher inode numbers although deleted files return their inodes. The
following script will output the largest and smallest inode numbers
of a list of files passed
in as parameters.
#!/bin/bash
largest
=
‘stat –c "%i" $1‘
largestFile
=
$1
smallest
=
‘stat –c "%i" $1‘
smallestFile
=
$1
shift
for item in $@; do
number
=
‘stat –c "%i" $item‘
if [ $number –gt $largest ]; then
largest
=
$number; largestFile
=
$item;
fi
if [ $number –lt $smallest ]; then
smallest
=
$number; smallestFile
=
$item;
fi
done
echo The largest inode from the files provided is
echo $largest of file $largestFile. The smallest
echo inode from the files provided is $smallest
echo of file $smallestFile
Notice in this script the use of the shift command so that all of the parameters after the
first are shifted down. Since we had already processed the first parameter ($1), we no longer
need it.
Whenever any file is used in Linux, it must first be opened. The opening of a file requires
a special designator known as the
file descriptor
. The file descriptor is an integer assigned to
the file while it is open.
In Linux, three file descriptors are always made available:
The Linux File System
◾
405
• 0 – stdin
• 1 – stdout
• 2 – stderr
Any remaining files that are utilized during Linux command or program execution
need to be opened and have a file descriptor given to that file.
When a file is to be opened, the operating system kernel gets involved. First, it deter-
mines if the user has adequate access rights to the file. If so, it then generates a file descrip-
tor. It then creates an entry in the system’s file table, a data structure that stores file pointers
for every open file. The location of this pointer in the file table is equal
to the file descriptor
generated. For instance, if the file is given the descriptor 185, then the file’s pointer will be
the 185th entry in the file table. The pointer itself will point to an inode for the given file. As
devices are treated as files, file descriptors will also exist for every device, entities such as
the keyboard, terminal windows, the monitor, the network interface(s), the disk drives, as
well as the open files. You can view the file descriptors of a given process by looking at the
fd subdirectory of the process’ entry in the /proc directory (e.g., /
proc/16531/fd
). There
will always be entries labeled 0, 1, and 2 for STDIN, STDOUT, and STDERR, respectively.
Other devices and files in use will require additional entries.
Alternatively, the
lsof
com-
mand will list any open files.
10.4 PARTITIONS
The disk drive(s) making up your Linux storage space is(are) divided into partitions. Each
Do'stlaringiz bilan baham: