L4: Input/Output Flashcards
Unix Directory Structure:
Important Directories(7)
/ - Root Directory
/bin - commonly used commands
/dev - Device Files
/etc - System Maintenance Files
/lib - System Libraries
/tmp - Temporary Files
/usr - User Files
Unix Directory Structure:
User File Directory Subdirectories(4)
/usr
/usr/include - System Include Files
/usr/lib - More Library Files
/usr/bin - More Executable Files
/usr/man - Manual Pages
Unix Innovations:
Shell
- Pipes
- Simple model for connecting together programs
- Tools
- Rich set of utilities for common text manipulation tasks
Unix Innovations:
Programming
- C Language
- As efficient and flexible as assembly
- But more readable
- OS Interface
- Convenient subroutine interface to all system services
Unix Innovations:
Processes
- Simplicity
- Unix has a simple model for spawning and coordinating processes
- Uniformity
- Single model for:
- jobs
- concurrency
- memory
- etc
- Single model for:
Two Building Blocks
of
Unix Data
- Files
- Actual storage areas on disk
- Store data
- Processes
- Manipulate data
- Running programs in memory
Unix Innovations:
File System
- Organization
- Simple hierarchical structure
- access control
- Content
- Simple linear byte streams
Unix Minimalist Philosophy
- Build it for programmers
- Keep capabilities few
- Make the capabilities powerful
- Compose complex capabilities FROM simple ones
File Structure
- Regular Files are just arrays of bytes, starting at index 0
- Uses a byte offset to track the “position” while reading and writing to the file/data
- This increases flexibility:
- Can reposition offset to overwrite or reread
- Can move offset to end+1 to append without overwriting
Unix File System Model:
3 Types of Files
- Regular Files
- Array of bytes
- Simple sequence with arbitrary file size
- Directory Files:
- Single hierarchy of files
- Very deep nesting
- Special Files
- Things that LOOK like files, but are not actually on the disk
- terminals,
- network connections,
- memory,
- pipes,
- etc
- Things that LOOK like files, but are not actually on the disk
Error Handling:
Using ERRNO
- Every system routine reports errors this way
- Returns an integer value
- Sets an extern int called errno, from errno.h
- represents the specific error that occurred
- Use the function:
- strerror(errno)
- obtain a string with info about the error
How the Shell launches a program
Example:
%ls -l -t foo.c bar.c < abc
- Creates a new process
- Finds the program file (ls)
- Uses it for instructions and initial data
- Creates an array of command line arguments
- Sets file descriptors 0,1,2 (stdin, stdout,stderr)
- Runs the Process
- First instruction is C startup routine from library
- Calls “main” with the command line arguments
- Wait for process
- Ends with the return
- Shell waits( unless pipe or & )
Library for Working with Files
fcntl.h
File Operation Methods:
Opening a File
method:
int open( char* pathname, int flags, int mode)
- pathname
- The pathname of the file
- flags
- determine how the file is to be opened
- predefined set of flags in fcntl.h
- mode
- permissions(read/write)
File Descriptors
Overview
File Descriptors
- Used to manipulate open files
- Integer numbers used as indices into a (secret) array managed by the kernel
- Each entry in the array has information about the state of an open file