Professional Documents
Culture Documents
Files are the building blocks of any OS. When you execute a command in UNIX, the kernel fetches the executable file from the file system and also any files required for reading and writing. Files in UNIX and POSIX cover a wide range of file types. They also provide a common set of interfaces to files. This chapter covers File Types File attributes and their uses.
Kernel and process specific data structures used for file manipulation.
Department of MCA PESIT, Unix System Programming
File Types
Regular Files
Directory File
Character Device File Block Device File FIFO file link file
Regular File
It can be a text or binary file
No distinction is made between these files. They can be created, browsed and modified by various means such as text editors and compilers. They can be removed by commands like rm in UNIX.
Directory File
It is like a file folder It provides a means for users to organize their files into some hierarchical structure based on file relationship or uses. Eg: /bin directory contains executable programs such as cat, rm, sortetc. create a directory file mkdir command. A directory is empty if it contains nothing other than . and .. files.
Device Files
Block device files represent a physical device that transmits data a block at a time. Eg: hard disk drives, floppy deivce drives. Character device files represent a physical device that transmits data in a character-based manner. Eg: line printers, modems and consoles. An application can perform read and write on device files like on regular files and the OS will automatically invoke appropriate device driver to perform actual data transfer. create a device file mknod command.
mknod /dev/cdsk
115
115 major device number: An index to a kernel table that contains the address of device driver functions known to the system.
5 minor device number: An integer value to be passed as an argument to a device driver function when it is called. It tells which actual device it is talking to. c Character device file b Block device file
Department of MCA PESIT, Unix System Programming
/dev/fd0
The first floppy drive. It is a character device on major node 2 and minor 0. /dev/lp0
The first parallel printer device. Subsequent printers are numbered lp1, lp2 etc. They are character devices on major mode 6 and minor nodes starting at 0 and numbered sequentially.
/dev/psaux
The PS/2 mouse port. This is a character device on major node 10, minor node 1.
/dev/pcd0 Parallel port CD ROM drives. All are block devices on major node 46. /dev/pcd0 is on Department of MCA PESIT, Unix minor node 0 with subsequent drivesSystem being on minor nodes 1, 2, 3 etc. Programming
FIFO File
It is a special pipe device file which provides temporary buffer for two processes to communication by writing/reading data from the buffer. Size of the buffer is fixed to PIPE_BUF. create a FIFO file mkfifo command mkfifo mknod /usr/prog/fifo_pipe /usr/prog/fifo_pipe p
File type
Access permission Hard link count UID GID File size Last access time Last modify time Last change time Inode number File system ID
Department of MCA PESIT, Unix System Programming
System Call
chmod chown chown utime link unlink
Attributes Changed
Changes access permission, last change time Changes UID, last change time Changes GID, last change time Changes last access time, modification time Increases hard link count Decreases hard link count, remove the file if count is zero Changes file size last access time, last modification time. Department of MCA PESIT, Unix
System Programming
Inodes in UNIX
In UNIX System V, a file system has an inode table which keeps track of all files. Each inode table is an inode record contains all attributes of a file including inode no and physical disk address.
Inodes in UNIX
To access a file, for example /usr/joe
The UNIX kernel knows the /directory inode no it is kepr in processU area.
It will scan the / directory file to find the inode no of usr file. Once it gets the inode no, it checks if the calling process has permission to search usr directory. It then looks for the joe file. Whenever a new file is created in directory, entry is made in inode table and directory file. Inode tables are kept in their file systems on disk, but the UNIX kernel maintains an in-memory inode table to contain a copy of the recently accessed inode records.
Department of MCA PESIT, Unix System Programming
Device Files
Symbolic Files
mknod
ln s
Department of MCA PESIT, Unix System Programming
Mknod
Symlink
Defined in <sys/stat.h> Struct stat { dev_t ino_t st_dev st_ino /* file system ID */ /* File inode number */
mode_t
nlink_t uid_t gid_t dev_t off_t time_t
st_mode
st_nlink st_uid st_gid st_rdev st_size` st_atime
time_t
time_t }
st_mtime `
st_ctime
OPEN function
1. 2. Search the File descriptor Table for first unused entry.index to the entry is returned to the process. Scan the File table to find an unused entry. If found, a) b) c) File descriptor Table entry will point to file table entry. File able entry will point to inode table entry. File table entry will contain the current file pointer of the open file.
d)
e) f)
File table entry will contain an open mode. (read-only, write-only, read and write)
The reference count in the file table entry is set to 1. The reference count of the in-memory inode of the file is increased Department of MCA PESIT, Unix by 1. System Programming
Data Structure
File Table Kernel Space
InodeTable
r rc = 1 rw rc = 1 rc = 1
xyz
w rc = 1
rc = 2
abc
Process Space
Department of MCA PESIT, Unix System Programming
Read/write function The file descriptor is the first argument to read/write system call. Kernel uses the file descriptor to index to the file descriptor table to find the file table entry. It checks the file table entry to see if the appropriate mode and permissions are there. Use the file table entry to access the files inode record. Use the file pointer in the file table entry to determine where read/write should occur. Checks the file type in inode record and invokes the appropriate driver function. lseek system call Invoked to change the file pointer to a different offset for next read/write operation. The kernel gets access to the file inode record and checks that the file is not a character device file, a FIFO file or a symbolic link file. If file type is compatible, change the file pointer to the value in lseek.
Department of MCA PESIT, Unix System Programming
Close function
1. The kernel sets the FD entry to be unused 2. Decrement the reference count in file table by 1. If still nonzero, go to 6. 3. The file table entry is marked as unused. 4. Decrement the reference count in file inode table by 1, if still non-zero, go to 6. 5. If hard-link count of inode is non zero, return to caller. Otherwise it marks the inode table entry as unused and deallocates all the physical disk storage of the file, as all the file path names have been removed by some process.
File descriptors are used only in UNIX and POSIX.1 complient systems
Directory Files
<dirent.h> for UNIX System V and POSIX.1 systems <sys/dir.h> for BSD UNIX
Directory Function
Opendir Readdir Closedir Rewinddir
Purpose
Opens a directory file Reads the next record from the file Closes a directory file Sets file pointer to beginning of file
ln
/usr/mary/abc
/usr/mary/xyz
ln s
/usr/mary/abc
/usr/mary/xyz
Limitations of hardlinks: Cannot create hardlinks for directories unless they have superuser privileges
Users cannot create hardlinks on a file system that references files on a different system.
Department of MCA PESIT, Unix System Programming
Symbolic Link
Create a new inode Can link directories Can link files across file systems
Increase the hard link count of the Does not change the hard link linked inode count of the linked inode
Department of MCA PESIT, Unix System Programming
ln Vs cp
ln creates a new directory entry to a referenced file whereas cp creates a duplicated copy of the file to another file with a different name.
ln
/usr/mary/abc
Inode number 115 89 201 346 abc a.out Filename
/usr/mary/xyz
Inode number 515 989 201 146 Xyz Fun.c Filename
(ln s)/ cp
/usr/mary/abc
Inode number 115 89 201 abc Department of MCA PESIT, Unix System Programming 346 a.out Filename
/usr/mary/xyz
Inode number 515 989 345 146 Xyz Fun.c Filename