1 / 29

UNIX FILES

UNIX FILES. 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.

horace
Download Presentation

UNIX FILES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX FILES Department of MCA PESIT, Unix System Programming

  2. 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

  3. File Types • Regular Files • Directory File • Character Device File • Block Device File • FIFO file • link file Department of MCA PESIT, Unix System Programming

  4. 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

  5. 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, sort…etc. • 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

  6. 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

  7. mknod /dev/cdsk c 115 5 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

  8. 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 minor node 0 with subsequent drives being on minor nodes 1, 2, 3 etc. Department of MCA PESIT, Unix System Programming

  9. 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 /usr/prog/fifo_pipe • mknod /usr/prog/fifo_pipe p • Remove a fifo file – rm command. Department of MCA PESIT, Unix System Programming

  10. 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 /usr/jose/original /usr/mary/slink cat –n /usr/mary/slink ls –l /usr/mary/slink sr—r—r-- 1 terry 20 Aug 20, 1994 slink->usr/jose/original Department of MCA PESIT, Unix System Programming

  11. 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

  12. UNIX and POSIX File Systems • /etc Stores system admin files and programs • /etc/passwd Stores all user information • /etc/shadow Stores user passwords (For UNIX System V only) • /etc/group Stores all group info • /bin Stores all system programs like cat, rm, cp..etc • /dev Stores all character and block device files. • /usr/include Stores standard header files/ • /usr/lib Stores standard libraries • /tmp Stores temporary file. Department of MCA PESIT, Unix System Programming

  13. UNIX and POSIX File Attributes • 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

  14. UNIX and POSIX File Attributes Department of MCA PESIT, Unix System Programming

  15. 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

  16. Inodes in UNIX • To access a file, for example /usr/joe • The UNIX kernel knows the /directory inode no – it is kepr in process-U 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

  17. Application Program Interface to Files • Files are identified by path names • Files must be created before they can be used. Department of MCA PESIT, Unix System Programming

  18. 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

  19. Defined in <sys/stat.h> Struct stat { dev_t st_dev /* file system ID */ ino_t st_ino /* File inode number */ mode_t st_mode /* Contains file type and access flags */ nlink_t st_nlink /* Hard link count */ uid_t st_uid /* File user Id */ gid_t st_gid /* File group Id */ dev_t st_rdev /* Contains major and minor device numbers */ off_t st_size` /* File size in number of bytes */ time_t st_atime /* Last access time */ time_t st_mtime ` /* Last modification time */ time_t st_ctime /* Last status change time */ } stat, fstat or lstat System Calls Department of MCA PESIT, Unix System Programming

  20. 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 • 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, • 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. • 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 by 1. Department of MCA PESIT, Unix System Programming

  21. InodeTable File descriptor Table Data Structure File Table Kernel Space r rc = 1 xyz rw rc = 1 rc = 1 abc w rc = 1 rc = 2 Process Space Department of MCA PESIT, Unix System Programming

  22. 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 file’s 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

  23. Close function • The kernel sets the FD entry to be unused • Decrement the reference count in file table by 1. If still non-zero, go to 6. • The file table entry is marked as unused. • Decrement the reference count in file inode table by 1, if still non-zero, go to 6. • 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. • It returns to the process with a 0 (success) status. Department of MCA PESIT, Unix System Programming

  24. 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

  25. Each process has a fixed-size stream table with OPEN_MAX entries. FILE : buffer, file I/O error status, EOF flag…etc • fopenreturns 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 UNIX system call used fopen open fread, fgetc, fscanf, fgets read fwrite, fputc, fprintf, fputs write fseek, ftell, frewind lseek fclose close Department of MCA PESIT, Unix System Programming

  26. Directory Files <dirent.h> for UNIX System V and POSIX.1 systems <sys/dir.h> for BSD UNIX Department of MCA PESIT, Unix System Programming

  27. 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

  28. Hard Link Vs Symbolic Link Department of MCA PESIT, Unix System Programming

  29. 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 /usr/mary/xyz (ln –s)/ cp /usr/mary/abc /usr/mary/xyz Department of MCA PESIT, Unix System Programming

More Related