Unix Chap 1 Continuity (27!1!12)

  • Upload
    imadpr

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    1/17

    Ihr Logo

    Unix&Network programming

    "Study Of Multiuser Operating System and their Features

    Course Instructor : K.JAVUBAR SATHICK

    University : BSA University

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    2/17

    Your Logo

    dup and dup2 functions An existing file descriptor is duplicated by the either of the following

    functions.

    The new file descriptor returned by dup fn is assumed to be the lowest

    numbered available.

    With dup2,we specify the value of the new descriptor with the filedes2 arg.

    If filedes2 is already open, it is first closed.

    If filedes equals filedes2 then dup2 returns filedes2 without closing it.

    DEPARTMENT OF COMPUTER APPLICATIONS

    #include

    int dup(int filedes);

    Int dup2(int filedes,int filedes2);

    both returns file descriptor if OK,-1 on error

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    3/17

    Your Logo

    Sync,Fsync and Fdatasync functions When the file operation takes place in unix system, It happens basically by

    consulting with kernel.

    For example: when we write a data to a file, data is copied by the kernel

    into one of its buffer and queued up for writing to the disk later,so we call

    that as a delayed write. Eventually data will be written successfully, but for

    the next disk block we should struggle again.

    So to maintain the consistency of file system on disk we use the following

    functions.

    DEPARTMENT OF COMPUTER APPLICATIONS

    #include

    int fsync(int filedes);Int fdatasync(int filedes);

    Void sync(void),

    Returns :0 if OK,-1 on error

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    4/17

    Your Logo

    Sync,Fsync and Fdatasync functions Sync fn:

    This fn simply queues all the modified block buffers for writing, It does not

    wait for the disk writes to takes place.

    This fn update after each 30secs and flushes the kernel block buffer when

    some data is read or write.

    Fsync fn:

    The function fsync refers only to single file, specified by the file

    descriptor and waits for the disk writes to complete before returning.

    Fdatasync fn:

    This fn is similar to to fsync fn, but it affects only the data portions of

    file, whereas in fsync fn, the files attribute are also updated synchronously.

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    5/17

    Your Logo

    Fcntl functions

    The fcntl fn can change the properties of file that is already open.

    The fcntl fn is used for five different purposes.

    1. Duplicate an existing descriptor(cmd=F_DUPFD).

    2.Get/set file descriptor flags(cmd=F_GETFD or F_SETFD).

    3.Get/set file status flag(cmd=F_GETFL or F_SETFL).

    4.Get/set asynchronous I/O ownership(cmd=F_GETOWN or F_SETOWN).

    5.Get/set record locks(cmd=F_GETLK,F_SETLK).

    DEPARTMENT OF COMPUTER APPLICATIONS

    #include

    int fcntl(int filedes, int cmd,./*int arg*/ );

    Returns: depends on cmd if OK,-1 on error

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    6/17

    Your Logo

    Files and directories The discussion in this chapter centers around the three stat functions and

    the information they return.

    Given a pathname, the stat fn retrieve a structure of information about thenamed file(file residing in the file system).

    Fstat talks about information about the file that is already open on filedescriptor.

    Lstat is similar to stat fn,but when a named file is symbolic link ,then lstatwill retrieve the information about sym.link not the file referred by sym.link.

    DEPARTMENT OF COMPUTER APPLICATIONS

    #include

    int stat(const char * restrict pathname, struct stat *restrict buf);

    Int fstat(int filedes,struct stat * buf);

    int lstat(const char * restrict pathname, struct stat *restrict buf);

    all three return:0 if OK,-1 on error

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    7/17

    Your Logo

    Files and directories Struct stat

    {

    mode_t st_mode; /*File type *mode(permission)*/

    ino_t st_ino; /*i-node number(serial no)*/

    dev_t st_dev; /*device number (file system)*/

    dev_t st_rdev; /*device number for special File*/

    nlink_t st_nlink; /*no of links*/

    uid_t st_uid; /*user ID of the owner*/

    gid_t st_gid; /*group ID of owner*/

    off_t st_size; /*size in bytes,for regular files*/

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    8/17

    Your Logo

    Files and directories

    time_t st_atime; /*time of last access*/

    time_t st_mtime; /*time of last modification*/

    time_t st_ctime; /*time of last file status change*/

    .......... ..............

    };

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    9/17

    Your Logo

    File Types

    Most of the files on unix system are either regular file or directory file.

    1. Regular file: It is one of the most common type of file and it as data ofsome form.

    2. Directory file:A file that contains the names of other files and pointersto information on these files.

    3. Block special file:A type of file providing buffered I/O access in fixedsize units to devices such as disk drives.

    4. Character special file:A type of file providing unbuffered I/O access invariable-sized units to devices. All devices on a system are either blockspecial or character special files.

    5. FIFO: A type of file used for communication between processes.

    6. Socket: A type of file used for network communication betweenprocesses.

    7. Symbolic link:A type of file that points to another file.

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    10/17

    Your Logo

    Files Access permission The unix file system provides various file access permission methods. To

    describe the few,

    st_mode value encodes the access permission bits for file.

    When we say file, it means any type of file,directories,character special file

    so on. There are 9permission bits for each file,these 9 bits are classified into 3

    categories:

    3 categories is split and specifies 3 important operations, they are

    1. Read2. Write

    3.Execute

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    11/17

    Your Logo

    Files Access permission

    CATEGORY St_mode mask Meaning

    C1 S_IRUSR USER-READ

    S_IWUSR USER-WRITE

    S_IXUSR USER-EXECUTE

    C2 S_IRGRP GROUP-READ

    S_IWGRP GROUP-WRITE

    S_IXGRP GROUP-EXECUTE

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    12/17

    Your Logo

    Files Access permission

    CATEGORY St_mode mask Meaning

    C3 S_IROTH OTHER-READ

    S_IWOTH OTHER-WRITE

    S_IXOTH OTHER-EXECUTE

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    13/17

    Your Logo

    Rules to be followed for Files Access permission

    The first rule is that whenever we want to open any type of file by name, wemust have execute permission in each directory mentioned in thename,including the current directory.

    The read permission for a file determines whether we can open an existingfile for reading: the O_RDONLY and O_RDWR flags for the open fn.

    The write permission for a file determines whether we can open an existingfile for writing: the O_WRONLY and O_RDWR flags for the open fn.

    We cannot create a new file in a directory unless we have writepermission and execute permission in the directory.

    Execute permission for a file must be on if we want to execute the fileusing any of the six exec functions.

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    14/17

    Your Logo

    Access Test performed by the kernel to authenticate the right user

    T1: if the effective user ID of the process is 0(the super user), access isallowed , this gives the superuser free rein throughout the entire file system.

    T2: if the effective user ID of the process equals to the owner ID of thefile(i.e , the process owns the filesingle user who owns the file ), access isallowed if the appropriate user access permission bit is set. Eg: if the

    process is opening the file for reading, the user-read bit must be on.

    T3: if the effective group ID of the process equals the group ID of the file,the access is allowed if the appropriate group access permission bit isset,otherwise,permission is denied.

    T4: if the appropriate other access permission bit is set,then access is

    allowed,otherwise permission is denied.

    These four steps are tried in sequence to authenticate and to giveaccessibility.

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    15/17

    Your Logo

    Standard I/O libraryIn this chapter we describe the Standard I/O library

    Streams and File objects:

    All the I/O routines centered around file descriptors.

    When a file is opened, a fd describes the actual status, therefore thedescriptor is used for all subsequent I/O operations.

    Streams: when we open(or) create a file with Std I/O library, we say that itis associated a stream with the file.

    Std I/O file streams can be used with single byte and multibyte(wide).

    A stream orientation determines whether the character that are read andwritten are single byte ormultibyte.

    If a multibyte I/O fn is used on stream without orientation, then it is set towide orientation

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    16/17

    Your Logo

    Standard I/O libraryStreams and File objects:

    Similarly if single byte I/O function is used on stream without orientation,then the stream orientation is set to byte oriented.

    Only 2 function can change the orientation once it is set, they are as

    follows,freopen(): this fn will clear a streams orientation

    fwide(): it can be used to set a streams orientation.

    if mode isve, fwide will specify byte orientation.

    if mode is +ve,fwide will specify wide orientation.

    if mode is 0, fwide will not specify any orientation.

    DEPARTMENT OF COMPUTER APPLICATIONS

    Int fwide(file *fp, int mode);

  • 7/28/2019 Unix Chap 1 Continuity (27!1!12)

    17/17

    Your Logo

    Standard I/O libraryThere are 3 different streams available to process they are,

    STD-INPUT

    STD-OUTPUT

    STD-ERROR these streams refers to the same file and allthese included under .

    DEPARTMENT OF COMPUTER APPLICATIONS