Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems
A tree structure prohibits sharing of files. An acyclic graph allows directories to have shared subdirectories and files. The same file may be in two different directories.
A shared file is not the same as two copies of the file. Only one actual copy exists, so
any changes made by one user are immediately visible to the other. A common way of
implementing shared files and directories is to create a new directory entry called a link,
which is effectively a pointer to another file or subdirectory. A link can be implemented
as an absolute or relative path name. A file may now have multiple absolute path names.
This problem is similar to the aliasing problem in programming languages. Consequently
distinct file name may refer to the same files. If we are traversing the entire file system-to
find a file, to accumulate statistics, etc, this problem becomes significant since we do not
want to traverse the shared structures more than once. Another problem involves deletion.
If the file is removed when anyone deletes it, we may end up with dangling pointers to
the now-nonexistent file.
Solutions: Another approach is to preserve the file until all references to it are
deleted. When a link or a copy of the directory entry is establishes, a new entry is added
to the file-reference list. When a link is deleted, we remove its entry on the list. The file is
deleted when its file-reference list is empty. Since the reference list can be very large we
can keep a count of the number of references. A new link or directory increments the reference count, deleting a link or entry decrements the count. When the count is 0, the
file can be deleted. UNIX uses this solution for hard links. Backpointers can also be
maintained so we can delete all pointers.
One serious problem with using an acyclic-graph structure is ensuring that there are no cycles. A solution is to allow only links to files not subdirectories. Also every time a new link is added use a cycle detection algorithm to determine whether it is OK. If cycles are allowed, we want to avoid searching any component twice. A similar problem exists when we are trying to determine when a file can be deleted. A value of 0 in the reference count means no more references to the file/directory can be deleted. However, cycles can exist, e.g, due to self-referencing. In this case we need to use a garbage collection scheme, which involves traversing the entire file system, marking everything that can be accessed. Then a second pass collects everything that is not marked onto a list of free space. However this is extremely time consuming and is seldom used. However it is necessary because of possible cycles in a graph.
UNIX supports two types of links:
The ln command is used to create both links, ln –s is used to create a soft link
Examples: The first command creates a hard link ~/courses/OS/programs/prog1_hard.c to an existing file ~/prog1.c. The second command creates a soft link ~/prog2_soft.c to an existing file ~/courses/OS/programs/prog2.c. The diagrams below show the directory structures after these links have been created. Note that directory entries for hard links to the same file have the same inode number.
ln ~/prog1.c ~/courses/OS/programs/prog1_hard.c
ln –s ~/courses/OS/programs/prog2.c ~/prog2_soft.c
When a hard link is created, a directory entry for the existing file is created—there is still only one file. Both entries have the same inode number. The link count is incremented by one in the inode for the file. No hard links can be created for directories. Also hard links cannot be established between files that are on different file systems. In UNIX, a file is removed from the file system only if its hard link count is 0.
A file of type ‘link’ is created, which contains the pathname for the existing file as specified in the ln command. The existing file and the new (link) files have different inode numbers. When you make a reference to the link file, the UNIX system sees that the type of file is link and reads the link file to find the pathname for the actual file to which you are referring. When the existing file is removed, you have a ‘dangling pointer’ to it in the link file. Soft links take care of all the problems inherent in hard links. They are flexible. You may have soft links to directories and across file systems. However, UNIX has to support an additional file type, the link type, and a new file is created for every link, slowing down file operations.
A file system is best visualized as a tree, rooted at /. /dev, /etc, /usr, and other directories
in the root directory are branches, which may have their own branches, such as
/etc/passwd, /usr/local, and /usr/bin. Filling up the root file system is not a good idea, so
splitting /var from / is a good idea.Another common reason to contain certain directory
trees on other file systems is if they are to be housed on separate physical disks, or are
separate virtual disks, or CDROM drives.
Mounting makes file systems, files, directories, devices, and special files available
for use at a particular location. Mount point is the actual location from which the file
system is mounted and accessed. You can mount a file or directory if you have access to
the file or directory being mounted and write permission for the mount point
There are types of mounts:
Remote mounts are done on a remote system on which data is transmitted over a telecommunication line. Local mounts are mounts done on your local system.
All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the file system found on some device to the big file tree. Conversely, the umount command will detach it again. Here is the syntax of the mount command
mount -t type device dir
This command tells the kernel to attach the file system found on device (which is of type type) at the directory dir. The previous contents (if any) and owner and mode of dir become invisible. As long as this file system remains mounted, the pathname dir refers to the root of the file system on device.
New Tree after mounting Filesystem
Sharing of files on multi-user systems is desirable. People working on the same project
need to share information. For instance: software engineers working on the same project
need to share files or directories related to the project
Sharing may be done through