Spread Knowledge

CS604 - Operating Systems - Lecture Handout 43

User Rating:  / 0

Related Content: CS604 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Operating Systems


  • Directory Structures
  • Links in UNIX/Linux
  • File System Mounting
  • File Sharing
  • File Protection

Acyclic-Graph Directories

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.

Acyclic-graph directory structure

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.

General graph directory

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.

Links in UNIX

UNIX supports two types of links:

  • Hard links
  • Soft (symbolic) links

The ln command is used to create both links, ln –s is used to create a soft link

  • ln [options] existing-file new-file
  • ln [options] existing-file-list directory

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

Hard Links

Hard Links

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.

Soft Links

Soft Links

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.

File System Mounting

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 mount
  • Local mount

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.

Mounting in UNIX

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.

Mounting in UNIX

New Tree after mounting Filesystem

File System Space Usage

On SuSE Linux

On SuSE Linux

On Solaris 2

On Solaris 2

File Sharing

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

  • Duplicating files: Make copies of the file and give them to all team members.
    This scheme works well if members of the team are to work on these shared files sequentially. If they work on the files simultaneously, the copies become inconsistent and no single copy reflects the works done by all members. However it is simple to implement.
  • Common login for members of a team: The system admin creates a new user group and gives the member access to the new account. All files and directories created by any team member under this account and are owned by the team. This works well if number of teams is small and teams are stable. However a separate account is needed for the current project and the system administrator has to create a new account for every team
  • Setting appropriate access permissions. Team members put all shared files under one member’s account and the access permissions are set so all the members can access it. This scheme works well if only this team’s members form the user group. File access permissions can be changed using the chmod system call:
    chmod [options] octal-mode file list
    chmod [options] symbolic –mode file-list

    A few examples:
    • To let people in your UNIX group add, delete, and rename files in a directory of yours - and read or edit other people's files if the file permissions let them - use chmod 775 dirname.
    • To make a private file that only you can edit, use chmod 600 filename. To protect it from accidental editing, use chmod 400 filename.
  • Common groups for members of a team. : System admin creates a new user group consisting of the members of team only. All team members get individual logins and set access permissions for their files so that they are accessible to other group members
  • Links. A link is a way to establish a connection between the file to be shared and the directory entries of the users who want to have aces to this file. The two types of links supported by UNIX:
    • Hard link
    • Soft/symbolic link