L4: Input/Output Flashcards

1
Q

Unix Directory Structure:

Important Directories(7)

A

/ - Root Directory

/bin - commonly used commands

/dev - Device Files

/etc - System Maintenance Files

/lib - System Libraries

/tmp - Temporary Files

/usr - User Files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Unix Directory Structure:

User File Directory Subdirectories(4)

A

/usr

/usr/include - System Include Files

/usr/lib - More Library Files

/usr/bin - More Executable Files

/usr/man - Manual Pages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Unix Innovations:

Shell

A
  • Pipes
    • Simple model for connecting together programs
  • Tools
    • Rich set of utilities for common text manipulation tasks
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Unix Innovations:

Programming

A
  • C Language
    • As efficient and flexible as assembly
    • But more readable
  • OS Interface
    • Convenient subroutine interface to all system services
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Unix Innovations:

Processes

A
  • Simplicity
    • Unix has a simple model for spawning and coordinating processes
  • Uniformity
    • Single model for:
      • jobs
      • concurrency
      • memory
      • etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Two Building Blocks

of

Unix Data

A
  • Files
    • Actual storage areas on disk
    • Store data
  • Processes
    • Manipulate data
    • Running programs in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Unix Innovations:

File System

A
  • Organization
    • Simple hierarchical structure
    • access control
  • Content
    • Simple linear byte streams
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Unix Minimalist Philosophy

A
  • Build it for programmers
  • Keep capabilities few
  • Make the capabilities powerful
  • Compose complex capabilities FROM simple ones
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

File Structure

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Unix File System Model:

3 Types of Files

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Error Handling:

Using ERRNO

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How the Shell launches a program

A

Example:

%ls -l -t foo.c bar.c < abc

  1. 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)
  2. Runs the Process
    • First instruction is C startup routine from library
    • Calls “main” with the command line arguments
  3. Wait for process
    • Ends with the return
    • Shell waits( unless pipe or & )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Library for Working with Files

A

fcntl.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

File Operation Methods:

Opening a File

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

File Descriptors

Overview

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

File Information

in the File Descriptor array

A

The File Descriptor Array manages information about the state of open files

  • File location on disk
  • Mode(read/write) the file is open in
  • Current offset
17
Q

Changing the

Current Offset

of a File Descriptor

A

Use the “lseek()” function from the unistd.h library

lseek( fd, value, flag)

Returns a new offset, or -1 if the fd is not a disk file

The flag determines the kind of seek operation

  • Set specific offset:
    • lseek(fd, i, SEEK_SET)
    • new offset = i
  • Move by i:
    • lseek(fd, i, SEEK_CUR)
    • new offset = old_offset + i
  • Move i bytes past end of file:
    • lseek(fd, i, SEEK_END)
    • new offset = size of file + i
18
Q

Reading from a File

A

Use the read() method from the unistd.h library

actual = read(fd, buffer, attempt);

  • bytes from the file will be copied into the buffer
  • the buffer must be allocated beforehand
  • attempt defines the expected number of bytes to read
  • returns the actual number of bytes read, or 0 if no more bytes to read
  • Read begins at the current offset
  • After reading, the offset is incremented by “actual”
19
Q

Opening a File:

Basic Flags

A

Used in the open(path, flag, mode_permissions)

Defined in the fcntl libary

  • O_CREAT
    • Create File
  • O_RDONLY
    • Open for reading
  • O_TRUNC
    • Set size to 0
  • O_WRONLY
    • Open for writing
  • O_APPEND
    • Set offset to EOF before each write
  • O_RDWR
    • Open for both reading and writing
20
Q

Writing to a File

A

Use the write() method from the unistd.h

actual = write(fd, buffer, actual)

  • writing begins at the current offset
  • bytes in buffer are written to the file
  • if “fd” was opened with the O_APPEND flag, the offset is set to EOF + 1, so each write appends
  • After the write, offset is incremented by “actual”, or the number of bytes written
21
Q

Layers of the Unix Architecture

A
  • Kernel
    • Essential Basic Services
    • Only one kernel
    • General functionality
  • Libraries
    • Useful additional services
    • Many libraries
    • Specific functionality
  • Programs
    • Executable files
    • Lots of programs
    • Specific functionality
  • Shell
    • Command Line environment
    • Few
    • Powerful
  • Users
    • Humans using the computer
22
Q

Default File Descriptors

A
  • 0 - stdin
  • 1 - stdout
  • 2 - stderr
23
Q

Debuggin/Error Handling:

Using Assert

A
  • Include the “assert.h” library
  • Verifies that a specified condition is true using an assert macro
  • If condition is false, program will abort
  • Specifying a condition in code:
    • assert( myVar < 2)
    • assert( myVar == 5), etc
24
Q

fprintf()

-Destination

A

Full function prototype

fprintf( destination, string, arg1, arg2, … )

  • destination is a file descriptor
    • could be stdout, stderr, fd…
  • arguments are substituted into the string following general C string substitution rules
25
Q

Common String Formatting

Substitution Symbols (5)

A
  • %s - string value
  • %c - char value
  • %d - decimal integer
  • %x - hex integer
  • %f - float

Note: This is far from comprehensive

26
Q

Unix Innovations:

5 Areas

A
  • File System
    • Organization
    • Content
  • Processes
    • SImplicity
    • Uniformity
  • Programming
    • C language
    • OS Interface
  • Shell
    • Pipes
    • Tools
  • Malleability
    • Portability
    • Open Source
27
Q

Bash command:

Pass a file to a program/process

A

%program < file_name

  • This file supplies initial content for a new process
28
Q

Unix Innovations:

Malleability

A
  • Portability
    • both OS and tools are written in C
    • both can be easily ported to other machines
  • Open Source
    • source can be licensed and modified for experiments
29
Q
A
30
Q

Specialized File Operations

A