Unix Files Types

Embed Size (px)

Citation preview

  • 8/12/2019 Unix Files Types

    1/29

    UNIX FILES

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    2/29

    Files are the building blocks of any OS.

    When you execute a command in UNIX, the kernel fetchesthe executable file from the file system and also any filesrequired 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 filemanipulation.

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    3/29

    File Types

    Regular Files

    Directory File

    Character Device File

    Block Device File

    FIFO file

    link file

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    4/29

    Regular File

    It can be a text or binary file

    No distinction is made between these files.

    They can be created, browsed and modified byvarious means such as text editors and compilers.

    They can be removed by commands like rminUNIX.

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    5/29

    Directory File

    It is like a file folder

    It provides a means for users to organize their files intosome hierarchical structure based on file relationship or uses.

    Eg: /bindirectory contains executable programs such as cat,rm, sortetc.

    create a directory file mkdircommand.

    A directory is empty if it contains nothing other than .and

    .. files.Remove a directory rmdircommand

    Display contents lscommand

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    6/29

    Device Files

    Block device files represent a physical device that transmitsdata a block at a time.

    Eg: hard disk drives, floppy deivce drives.

    Character device files represent a physical device thattransmits 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 appropriatedevice driver to perform actual data transfer.

    create a device file mknodcommand.

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    7/29

    mknod /dev/cdsk c 115 5

    115 major device number: An index to a kernel table thatcontains the address of device driver functions known to thesystem.

    5minor device number: An integer value to be passed as an

    argument to a device driver function when it is called. It tellswhich actual device it is talking to.

    cCharacter device file

    bBlock device file Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    8/29

    Device Files Examples/dev/dsp

    Digital Signal Processor. It forms the interface between software which produces soundand 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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    9/29

    FIFO File

    It is a special pipe device file which provides temporarybuffer for two processes to communication by writing/readingdata from the buffer.

    Size of the buffer is fixed to PIPE_BUF.create a FIFO file mkfifocommand

    mkfifo /usr/prog/fifo_pipe

    mknod /usr/prog/fifo_pipe p

    Remove a fifo file rmcommand.

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    10/29

    Symbolic link file

    Supported by BSD UNIX and UNIX System V.4. No support byPOSIX.1

    It contains the path name which references another file in either

    a local or remote file system. Create a symbolic link file lncommand

    ln s /usr/jose/original /usr/mary/slink

    cat n /usr/mary/slink

    ls l /usr/mary/slink

    srrr-- 1 terry 20 Aug 20, 1994 slink->usr/jose/original

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    11/29

    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 pathname 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 oneor more hard links.

    ln /usr/foo/path1 / usr/prog/new/n1

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    12/29

    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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    13/29

    UNIX and POSIX FileAttributes

    File type

    Access permission

    Hard link count

    UIDGID

    File size

    Last access time

    Last modify time

    Last change time

    Inode number

    File system ID Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    14/29

    UNIX and POSIX File Attributes

    UNIXcommand

    SystemCall

    Attributes Changed

    chmod chmod Changes access permission, last changetime

    chown chown Changes UID, last change time

    chgrp chown Changes GID, last change time

    touch utime Changes last access time, modification time

    ln link Increases hard link count

    rm unlink Decreases hard link count, remove the fileif count is zero

    vi, emac Changes file size last access time, last

    modification time.Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    15/29

    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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    16/29

    Inodes in UNIX

    To access a file, for example /usr/joe

    The UNIX kernel knows the /directory inode noit 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 anddirectory 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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    17/29

    Application Program Interface toFiles

    Files are identified by path names

    Files must be created before they can be used.

    File type UNIX Command UNIX and

    POSIX.1 Systemcall

    Regular Files Vi, emacs..etc Open, creat

    Directory Files mkdir mkdir, mknod

    FIFO Files mkfifo mkfifo, mknod

    Device Files mknod Mknod

    Symbolic Files ln s Symlink

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    18/29

    Files must be opened before they can be accessed by applicationprograms

    A Process may open at most OPEN_MAXfiles of any types at anyone time.

    readand writesystem callsFile attributes can be queried by the stator fstatsystem call.

    File attributes can be changed by the chmod, chown, utimeandlinksystem calls.

    File hard links can be removed by the unlinksystem call.

    APIs for Files

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    19/29

    Defined in

    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 CallsDepartment of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    20/29

    UNIX Kernel Support for FilesFile tableAll open files

    Inode Tablecopy of file inodes most recently accessedFile descriptor TableAll files opened by the Process, OPEN_MAX

    OPEN function

    1. Search the File descriptor Table for first unused entry.index to theentry is returned to the process.

    2. Scan the File table to find an unused entry. If found,

    a) File descriptor Table entry will point to file table entry.

    b) File able entry will point to inode table entry.

    c) File table entry will contain the current file pointer of the open file.d) File table entry will contain an open mode. (read-only, write-only,

    read and write)

    e) The reference count in the file table entry is set to 1.

    f) The reference count of the in-memory inode of the file is increasedby 1. Department of MCA PESIT, UnixSystem Programming

    D

  • 8/12/2019 Unix Files Types

    21/29

    Data StructureFile descriptor Table

    File Table

    InodeTable

    Process Space

    r

    rc = 1rw

    rc = 1

    w

    rc = 1

    rc = 1

    rc = 2

    xyz

    abc

    Kernel Space

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    22/29

    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 appropriatedriver 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 thatthe file is not a character device file, a FIFO file or a symboliclink file.

    If file type is compatible, change the file pointer to the value inlseek.

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    23/29

    Close function

    1. The kernel sets the FD entry to be unused2. Decrement the reference count in file table by 1. If still non-

    zero, go to 6.

    3. The file table entry is marked as unused.

    4. Decrement the reference count in file inode table by 1, if stillnon-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 anddeallocates 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, UnixSystem Programming

    P d F l

  • 8/12/2019 Unix Files Types

    24/29

    C Stream Pointers and FileDescriptors

    A file descriptor allocated by anopensystem call

    A File descriptor is moreefficient for applications that dofrequent randomaccess of filedata. (No I/O buffering)

    File descriptors are used only inUNIX and POSIX.1 complientsystems

    C Stream pointers (FILE *) areallocated via the fopenCfunction call.

    A stream pointer is moreefficient to use for applicationsdoing extensive sequentialreadfrom or write to files. (I/Obuffering)

    Stream pointers are supportedon all Operating systems,(VMS, CMS, DOS, UNIX)

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    25/29

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

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

    fopenreturns a reference to this.

    Extract the file descriptor associated with a stream pointer.

    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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    26/29

    Directory Files

    Directory Function Purpose

    Opendir Opens a directory file

    Readdir Reads the next recordfrom the file

    Closedir Closes a directory file

    Rewinddir Sets file pointer tobeginning of file

    for UNIX System V and POSIX.1 systems

    for BSD UNIX

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    27/29

    Hard Link Vs Symbolic LinkA 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, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    28/29

    Hard Link Vs Symbolic Link

    Hard Link Symbolic Link

    Does not create a new inode Create a new inode

    Cannot link directories, unless thisis done by the root

    Can link directories

    Cannot link files across filesystems Can link files across file systems

    Increase the hard link count of thelinked inode

    Does not change the hard linkcount of the linked inode

    Department of MCA PESIT, UnixSystem Programming

  • 8/12/2019 Unix Files Types

    29/29

    ln Vs cpln creates a new directory entry to a referenced file whereas cp creates aduplicated copy of the file to another file with a different name.

    ln /usr/mary/abc /usr/mary/xyz

    Inodenumber

    Filename

    115

    89

    201 abc

    346 a.out

    Inodenumber

    Filename

    515

    989

    201 Xyz

    146 Fun.c

    Inodenumber

    Filename

    115

    89

    201 abc

    346 t

    Inodenumber

    Filename

    515

    989

    345 Xyz

    146 F

    (lns)/ cp /usr/mary/abc /usr/mary/xyz

    Department of MCA PESIT, UnixSystem Programming