You are on page 1of 29

UNIX FILES

Department of MCA PESIT, Unix System Programming

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

Department of MCA PESIT, Unix System Programming

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.

Department of MCA PESIT, Unix System Programming

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.

Remove a directory rmdir command


Display contents ls command
Department of MCA PESIT, Unix System Programming

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.

Department of MCA PESIT, Unix System Programming

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

Device Files Examples


/dev/dsp Digital Signal Processor. It forms the interface between software which produces sound and your soundcard. It is a character device on major node 14 and minor 3.

/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

Remove a fifo file rm command.

Department of MCA PESIT, Unix System Programming

Symbolic link file


Supported by BSD UNIX and UNIX System V.4. No support by POSIX.1 It contains the path name which references another file in either a local or remote file system. Create a symbolic link file ln command
ln s cat n ls l srrr-/usr/jose/original /usr/mary/slink /usr/mary/slink /usr/mary/slink 1 terry 20 Aug 20, 1994 slink->usr/jose/original

Department of MCA PESIT, Unix System Programming

UNIX and POSIX File Systems


root directory / Absolute path name starts from / Relative path name may start with a . or .. A file name may not exceed NAME_MAX characters and path name PATH_MAX characters. legal file name characters A to Z a to z 0 to 9. The path name of a file is called the hard link. Can have one or more hard links. ln /usr/foo/path1 / usr/prog/new/n1

Department of MCA PESIT, Unix System Programming

UNIX and POSIX File Systems


/etc /etc/passwd /etc/shadow /etc/group /bin /dev /usr/include /usr/lib /tmp Stores system admin files and programs Stores all user information Stores user passwords (For UNIX System V only) Stores all group info Stores all system programs like cat, rm, cp..etc Stores all character and block device files. Stores standard header files/ Stores standard libraries Stores temporary file.

Department of MCA PESIT, Unix System Programming

File type

UNIX and POSIX File Attributes

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

UNIX and POSIX File Attributes


UNIX command
chmod chown chgrp touch ln rm vi, emac

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.

Each inode no is unique to a file system only.


An OS does not keep the name of a file in its inode record. The mapping from file names to inode nos is done through directory files.

Department of MCA PESIT, Unix System Programming

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

Application Program Interface to Files


Files are identified by path names Files must be created before they can be used.
File type UNIX Command UNIX and POSIX.1 System call Open, creat mkdir, mknod mkfifo, mknod

Regular Files Directory Files FIFO Files

Vi, emacs..etc mkdir mkfifo

Device Files
Symbolic Files

mknod
ln s
Department of MCA PESIT, Unix System Programming

Mknod
Symlink

APIs for Files


Files must be opened before they can be accessed by application programs A Process may open at most OPEN_MAX files of any types at any one time. read and write system calls File attributes can be queried by the stat or fstat system call. File attributes can be changed by the chmod, chown, utime and link system calls. File hard links can be removed by the unlink system call.

Department of MCA PESIT, Unix System Programming

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

/* Contains file type and access flags */


/* Hard link count */ /* File user Id */ /* File group Id */ /* Contains major and minor device numbers */ /* File size in number of bytes */ /* Last access time */

time_t
time_t }

st_mtime `
st_ctime

/* Last modification time */


/* Last status change time */
Department of MCA PESIT, Unix

stat, fstat or lstat System System CallsProgramming

UNIX Kernel Support for Files


File table All open files Inode Table copy of file inodes most recently accessed File descriptor Table All files opened by the Process, OPEN_MAX

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

File descriptor Table

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.

6. It returns to the process with a 0 (success) status.

Department of MCA PESIT, Unix System Programming

C Stream Pointers and File Descriptors


C Stream pointers (FILE *) are allocated via the fopen C function call. A stream pointer is more efficient to use for applications doing extensive sequential read from or write to files. (I/O buffering) Stream pointers are supported on all Operating systems, (VMS, CMS, DOS, UNIX) A file descriptor allocated by an open system call A File descriptor is more efficient for applications that do frequent random access of file data. (No I/O buffering)

File descriptors are used only in UNIX and POSIX.1 complient systems

Department of MCA PESIT, Unix System Programming

Each process has a fixed-size stream table with OPEN_MAX entries.

FILE : buffer, file I/O error status, EOF flagetc


fopen returns a reference to this. Extract the file descriptor associated with a stream pointer.

<stdio.h> int fileno( FILE* stream_pointer);


To convert a file descriptor to a stream pointer

FILE* fdopen(int file_descriptor, char * open_mode);


C LIBRARY function fopen fread, fgetc, fscanf, fgets fwrite, fputc, fprintf, fputs fseek, ftell, frewind fclose UNIX system call used open read write lseek close
Department of MCA PESIT, Unix System Programming

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

Department of MCA PESIT, Unix System Programming

Hard Link Vs Symbolic Link


A Hard link is a UNIX path name for a file

ln

/usr/mary/abc

/usr/mary/xyz

Symbolic links are created with the s option

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

Hard Link Vs Symbolic Link


Hard Link
Does not create a new inode Cannot link directories, unless this is done by the root Cannot link files across file systems

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

You might also like