UNIX File System and File Descriptors: Portability and UNIX API Review, Slides of Computer Programming

An in-depth review of unix file system concepts, including file system abstraction, directories, and file descriptors. It also covers the unix api, focusing on environment variables, exit status, process id, user id, and file i/o operations. Part of a unix programming course and includes examples and techniques for working with direct i/o, open file descriptions, and pipes.

Typology: Slides

2012/2013

Uploaded on 04/29/2013

parmita
parmita 🇮🇳

4.7

(17)

183 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Review
Portability
Standards: ANSI, POSIX, etc
32 bit vs 64 bit
Byte order: Little endian vs big endian
Introduction to the UNIX API
Environment variables
Exit status
Process ID
User ID
UNIX file system
File system abstraction
Directories
File descriptors
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download UNIX File System and File Descriptors: Portability and UNIX API Review and more Slides Computer Programming in PDF only on Docsity!

Review

  • Portability
    • Standards: ANSI, POSIX, etc
    • 32 bit vs 64 bit
    • Byte order: Little endian vs big endian
  • Introduction to the UNIX API
    • Environment variables
    • Exit status
    • Process ID
    • User ID
  • UNIX file system
    • File system abstraction
    • Directories
    • File descriptors

Week 3 Topics

  • UNIX file system
    • File system abstraction
    • Directories
    • File descriptors
  • Unix API Programming Examples and Techniques
    • Example with direct IO
      • open, close, fdopen, lseek, unlink
    • Variable argument list
  • HW1 hints

UNIX file system

• File system abstraction

• Directories

• File descriptors

File system abstraction

  • File: a sequence of bytes of data
  • Filesystem: a space in which files can be stored
  • Link: a named logical connection from a directory

to a file

  • Directory: a special kind of file, that can contain

links to other files

  • Filename: the name of a link
  • Pathname: a chain of one or more filenames,

separated by /'s

Directories

Directories ... continued

  • Names belong to links, not to files
  • There may be multiple hard links to one file
  • Renaming only renames one link to that file
  • Unix allows both hard and soft links
  • A file will exist even after the last hard link to it

has been removed, as long as there are

references to it from open file descriptions

  • Soft links do not prevent deletion of the file
  • A directory may have multiple (hard) links to it
  • But this capability is usually restricted, to prevent creation of directory cycles

File Descriptors ... continued

File Descriptors ... continued

  • The POSIX standard defines the following
    • File descriptor: A per-process, unique, nonnegative integer used to identify an open file for the purposes of file access
    • Open file description: A record of how a process or group of processes are currently accessing a file - Each file descriptor refers to exactly one open file description, but an open file description may be referred to by more than one file descriptor - A file offset, file status, and file access modes are attributes of an open file description
    • File access modes: Specification of whether the file can be read and written

File Descriptors ... continued

  • FIFO special file: A type of file with the property that

data written to such a file is read on a first-in-first-out basis

  • Pipe : An object accessed by one of the pair of file

descriptors created by the pipe() function

  • Once created, the file descriptors can be used to manipulate the pipe, and it behaves identically to a FIFO special file when accessed this way
  • It has no name in the file hierarchy

File Descriptors ... continued

  • Important points
    • A file descriptor does not describe a file
      • It is just a number that is ephemerally associated with a particular open file description
    • An open file description describes a past "open" operation on a file; its does not describe the file
    • The description of the file is in the inode
      • There may be several different open file descriptors (or none) referring at it any given time

Direct I/O

  • Using open()
    • The usual C-language stream-oriented I/O operations, like printf() , use buffers and process data character-by- character
    • They are implemented using the lower-level direct I/O operations read() and write()
    • In situations where we do not want to view the data as characters, where we want greater efficiency, or where the extra (stream) layer of buffering causes us problems with synchronization, it is better to use the direct I/O operations

Using man

  • Look at the man page for open()
    • If there is more than one page on a given name, man will give you the one that is first in the chapter order of the Unix manual.
    • Shell commands are in Section 1, I/O and OS interface calls are in Section 2 and Section 3 respectively
  • Specification of section number varies
  • On Red Hat Linux, type man 2 open or man -S 2 open to see the page on open from Section 2 of the Unix manual
  • On Solaris, you can type man -s 2 open

Variable Argument Lists

  • The ... indicates a variable number of

arguments

  • Similar to that in printf()
  • For more on variable argument lists, look at the

file /usr/include/stdarg.h

  • Functions with variable argument lists can be

dangerous

  • It is difficult to check types, and the use of a correct number of arguments

Example Programs

  • example1.c illustrates a common

programming error

  • Failure to provide the correct number of

arguments to a vararg function

  • example2.c illustrates opening a file