Midterm Review Flashcards
What is the “stat” structure?
A structure that can be obtained at runtime,
it contains statistics and information about a specific file.
Often used to check the access mode bits on a file before attempting to use it.
Shell Command Equivalents:
ln
From unistd.h library:
link( old_filepath, new_filepath);
Equivalent to:
%ln old new
Libraries:
Static Linking
vs
Dynamic Linking
- Static Linking
- Every routine needed from the library is included in the executable file
- Routines are PART OF the program
- Dynamic Linking
- References to library routines are left to resolve during execution
- Library objects not loaded until they are needed at runtime
How to list inodes on the shell
ls -i
Shell Command Equivalents:
mkdir
From unistd.h library:
mkdir( “newpath”, 0777);
Note: Not sure what the 0777 flag does
Equivalent to:
%mkdir newpath
How to get the
Status of a file
(in code)
Need to use the “stat” struct
- located in the “sys/stat.h” library
Use one of the functions:
- stat( “pathname”, stat s)
- fstat()
- lstat()
To return a stat structure, placed in s
Then check s->st_mode to determine the file type and if the program has the correct permissions to access
Physical Disk Organization:
Key Points
- Disk is arranged into blocks
- some for data
- some for management
- Each inode corresponds to one file and the data blocks associated with that file
- Physical layout is not consecutive, file blocks can be all over the place
How do inodes handle larger file sizes?
- The inode only has 13 pointers, so an indirect approach is used
- File blocks can each contain many pointers
- Pointers are classified into levels L0, L1, L2, …
- This allows a hierarchy of blocks to be created to handle arbitrary file sizes
- Level L0 is a pointer that points directly to an actual datablock
- L1 points to a block of L0 pointers, L2 to a block of L1 pointers, and so on.
2 “Families” of Common Errors
- Exogenous (Outward error)
- Hardware Issues
- Computer Crash/error
- Software Issues
- Data Issues:
- incorrect data
- too much data
- too fast/slow
- Hardware Issues
- Endogenous (Problems with development process/team)
- Analysis
- Wrong Problem
- Wrong Goals
- Wrong User
- Specification
- incorrect
- incomplete
- inconsistent
- Logic
- forgetfulness
- stupidity
- laziness
- Analysis
unistd.h
Methods that are equivalent to
common shell commands (5)
- link()
- equivalent to “ln”
- unlink()
- equivalent to “rm”
- chdir()
- equivalent to “chdir”
- mkdir()
- equivalent to “mkdir”
- rmdir()
- equivalent to “rmdir”
Useful Names and Functions:
Input/Output (I/O)
Library: stdio.h
- Named Values:
- NULL = 0
- EOF = -1
- Standard File Descriptors:
- stdin
- stdout
- stderr
- Functions:
- getchar() - gets next char from file or EOF
- putchar(c) - write one char to file
- printf(…) - high level output formatting to file
- fgets() - read one line from file
Shell Utility Toolkit:
Miscellaneous Tools (5)
- echo - echo arguments on stdout
- cat - copy a file, or files, to stdout
- tr - translate characters
- date - echo current date and time
- man - see manual page of a command
The Shell:
Redirection of
Input
By Default, input is normally stdin
Use “
This allows a file to be used as input.
Example:
% grep -e “abc” < myFile.txt
Layers of the Unix Architecture
- 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

Difference between
real uid
and
effective uid
Real uid
- the uid of the actual user who is logged in
Effective uid
- uid temporarily used for the lifetime of a process
Most of the time, the effective uid and real uid are the same,
but Administrative programs are able to operate with a different uid than the invoker.
Library for Working with Files
fcntl.h
Opening a File:
Basic Flags
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
Unix Innovations:
5 Areas
- File System
- Organization
- Content
- Processes
- SImplicity
- Uniformity
- Programming
- C language
- OS Interface
- Shell
- Pipes
- Tools
- Malleability
- Portability
- Open Source
Which library contains the “fork()” function?
unistd.h
What is a
Process?
- An instance of an executing program
- A fundamental concept in Computer Science and a powerful coding technique
- Performs independent computations
- Processes interact via messages
- Identified using a process id (pid)
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
Two Building Blocks
of
Unix Data
- Files
- Actual storage areas on disk
- Store data
- Processes
- Manipulate data
- Running programs in memory
Program Memory:
Two Methods to get an area from the Heap
- malloc(size)
- memory allocation
- allocates a single area of the heap of the specified size, then is casted to a pointer
- calloc(n, size)
- allocate multiple n chunks of memory, for use by arrays