Midterm Review Flashcards

1
Q

What is the “stat” structure?

A

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.

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

Shell Command Equivalents:

ln

A

From unistd.h library:

link( old_filepath, new_filepath);

Equivalent to:

%ln old new

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

Libraries:

Static Linking

vs

Dynamic Linking

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

How to list inodes on the shell

A

ls -i

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

Shell Command Equivalents:

mkdir

A

From unistd.h library:

mkdir( “newpath”, 0777);

Note: Not sure what the 0777 flag does

Equivalent to:

%mkdir newpath

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

How to get the

Status of a file

(in code)

A

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

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

Physical Disk Organization:

Key Points

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

How do inodes handle larger file sizes?

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

2 “Families” of Common Errors

A
  • Exogenous (Outward error)
    • Hardware Issues
      • Computer Crash/error
    • Software Issues
    • Data Issues:
      • incorrect data
      • too much data
      • too fast/slow
  • Endogenous (Problems with development process/team)
    • Analysis
      • Wrong Problem
      • Wrong Goals
      • Wrong User
    • Specification
      • incorrect
      • incomplete
      • inconsistent
    • Logic
      • forgetfulness
      • stupidity
      • laziness
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

unistd.h

Methods that are equivalent to

common shell commands (5)

A
  • link()
    • equivalent to “ln”
  • unlink()
    • equivalent to “rm”
  • chdir()
    • equivalent to “chdir”
  • mkdir()
    • equivalent to “mkdir”
  • rmdir()
    • equivalent to “rmdir”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Useful Names and Functions:

Input/Output (I/O)

A

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

Shell Utility Toolkit:

Miscellaneous Tools (5)

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

The Shell:

Redirection of

Input

A

By Default, input is normally stdin

Use “

This allows a file to be used as input.

Example:

% grep -e “abc” < myFile.txt

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

Difference between

real uid

and

effective uid

A

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.

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

Library for Working with Files

A

fcntl.h

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

Which library contains the “fork()” function?

A

unistd.h

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

What is a

Process?

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

Program Memory:

Two Methods to get an area from the Heap

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Shell Utility Toolkit: Basic Line Oriented Processing Tools (8)
* grep * find patterns * sed * edit lines * cut * extract columns * sort * sort lines into specified order * uniq * display unique lines in a sorted file * tail * display last lines of a file * head * display fist lines of a file * wc * count words, lines or characters
26
Directory Structure: How Move (mv) operations work
* Physical data is not moved around on the disk, and will keep the same inode * The inode number is copied from the table in the source directory * Then added to the table in the destination directory with a new filename * The old reference is then deleted from the table in the source directory * Example: mv dir\_1/derp.txt dir\_2/samederp.txt
27
3 Major Categories of Process Operations
* Process Creation * Process Communication * Process Control
28
File Information in the File Descriptor array
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
29
Libaries: What are "Shared Libraries"?
Libraries that are prepared for Dynamic Linking These are also called Dynamic Linking Libraries (DLLs)
30
Structure of a Directory
* The directory is a specific type of file * Contains a table of _inodes_ paired with _filenames_ * inodes are identified with inode numbers * The table contains a reference to the directory itself (.) * and to it's parent directory( .. )
31
dup: Two Forms and their use
Form 1: new\_fd = dup( old\_fd ); When using the first form, the kernel picks a new file descriptor number and it is returned. It will be the smallest index that is not yet used for a file descriptor. Form 2: dup2( old\_fd, new\_fd); The second form lets the programmer specify the new file descriptor number. Either way, both old and new file descriptors now index the same value(file).
32
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
33
What is a Pipe?
* A kernel buffer with a "read" end and a "write" end * The kernel presents each end as a file descriptor * Processes can use the pipe with normal read/write routines, * One process writes * Another process reads * The writer _waits_ if the buffer is _full_ * The reader _waits_ if the buffer is _empty_ * A parent process creates file descriptors with the * pipe() routine * These descriptors are preserved during fork() and exec(), and can be used by a child process.
34
Unix Innovations: Programming
* C Language * As efficient and flexible as assembly * But more readable * OS Interface * Convenient subroutine interface to all system services
35
Unix Innovations: Shell
* Pipes * Simple model for connecting together programs * Tools * Rich set of utilities for common text manipulation tasks
36
Dynamic Linking: Disadvantages
Disadvantages: * Libary version control * Can the executable find the _right version_ of the library routine? * "Thrashing" of library routines in memory due to a constrained environment
37
Writing to a File
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
38
The Shell: Redirection of Output
By default, output of programs is to stdout. Use "\>" to redirect the output to a different file descriptor. This allows output of a program to be written to file. Alternatively, use "\>\>" to _append_ the file instead of writing over it. Example: % grep -e "abc" \> my\_output\_file.txt % grep -e "abc" \>\> my\_output\_file.txt
39
How to change the Effective uid of a program?
* Must use the _"set-uid"_ bit on executables * The Super User must turn on the set-uid bit for trusted programs * The effective uid becomes that of the executable's owner for the duration of the process Turning on the set-uid bit: Use chmod, as super user. It is the 4th of the permission bits: -rw**_s_** --x --x
40
Shell Command Equivalents: mv
From stdio.h library: rename( old\_filepath, new\_filepath); Equivalent to: %mv old new
41
Unix Innovations: Malleability
* 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
42
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
43
Design Consequences of Inodes
* Directories reference files only through inode numbers * moving files between directories is very fast * and, the operation is atomic (as are link and remove) * Two directories can include the same inode number * a file can be shared, appearing in several places with same or different name * also, no special cases for "shortcuts" etc. * Space is freed only when inode link & open counts are both 0 * removing a file may not create free space * files can't disappear while a process is working on them * also, no special cases for "sharing violation" etc. * An inode number is unique only within a file system * files can't span partitions or disks, or be moved to new disks * and, files can't be bigger than a single physical disk * Files don't have "type" (beyond regular vs. directory) * no optimizations for databases, no self-identifying objects, no new types * but, easy to code general programs such as "cp", "mv" * Plus, as we will see, file security information is in the inode * data is protected even when files are shared (details later)
44
Specialized File Operations
45
Process Communication: Key Routines
These facilitate read/write between processes: * pipe * dup * sockets
46
3 Common Pointer Errors
* Pointer is null * Unitialized fields * pointer is not null, but points to wrong thing or garbage data * Forgetting the asterisk: * person p - wrong * person\* p - correct
47
Libraries: Benefits of Using Libraries
* Save Development Time * Reusing routines that have already been throroughly debugged * Save Maintenance Time * Evolve a coherent capability * Using well known libraries with existing best practices
48
inodes and Datablocks are reclaimed only when \_\_\_\_\_\_\_\_
Two conditions are met: * The _Link Count_ becomes 0 * The number of "opens" becomes 0
49
exec() function: Basic Use
1. Use fork() to get a child process 2. In the child process, use "execlp()" or "execvp()" * This will "overlay" the image of the new program onto the child image * The existing context(such as the file descriptors) is preserved 3. The Child process now runs as the second program, and exits with a status code 4. The parent process waits for the child to complete
50
Shell Command Equivalents: rm -r
From stdio.h library: remove( "old\_file" ); Equivalent to: %rm -r old\_file
51
The Shell: Redirection of Error
By default, errors are printed to stderr Use " 2\> " to redirect to a different file descriptor Example: % grep -e "abc" 2\> my\_error\_file.txt
52
Basic Types of Inter-Process Communication (IPC) (2)
Pipes Signals
53
Pipe: Read Rules
* If there is any data in the pipe, return it. * It may be fewer bytes than requested * If the pipe is empty, and there is a writer, wait for data to be added to the pipe * Otherwise, return the value -1 (EOF)
54
Directory Access Rules (3)
* Search Rule * to open a file, the user or program must have _execute permission_ on _each directory_ in the full pathname * Write Rule * to _create_ or _delete_ a file, must have _write access_ to directory * Read Rule * to _read_ file names, must have _read access_ on the directory
55
Directory Structure: How do Link(ln) operations work?
* Works similarly to move operation, but old reference is kept * The inode number is copied from the first directory's table and added to the second specified directory's table with a new filename * Both directories now have a reference to the same inode, but may have different names for the same file. * File data is not duplicated * Example: ln dir\_1/myFile dir\_2/myFile2
56
Design Consequences of Inodes
* Directories reference files only through inode numbers * moving files between directories is very fast * and, the operation is atomic (as are link and remove) * Two directories can include the same inode number * a file can be shared, appearing in several places with same or different name * also, no special cases for "shortcuts" etc. * Space is freed only when inode link & open counts are both 0 * removing a file may not create free space * files can't disappear while a process is working on them * also, no special cases for "sharing violation" etc. * An inode number is unique only within a file system * files can't span partitions or disks, or be moved to new disks * and, files can't be bigger than a single physical disk * Files don't have "type" (beyond regular vs. directory) * no optimizations for databases, no self-identifying objects, no new types * but, easy to code general programs such as "cp", "mv" * Plus, as we will see, file security information is in the inode * data is protected even when files are shared (details later)
57
Process Controlling: Key Routines
Cause and Handle exeptions and interrupts: * signal * alarm
58
Shell Utility Toolkit: File System Manipulation Tools (8)
* **_cp_** - copy * **_mv_** - move * **_rm_** - remove * **_ls_** - list * **_chmod_** - change file access modes * **_chown_** - change file owner * **_find_** - navigate directory structure, testing files for attibutes * **_touch_** - change last-modified time on a file to current
59
Code Outline: Setting up a _pipe_ between parent and child process
int fd[2]; //declare array for file descriptors pipe(fd); //use the pipe function to create a pipe and set values for the file descriptors if(fork() ){ //Parent Process - if parent is writing close(fd[0] ); //Close the read end of the pipe write( fd[1], "stuff to write"); //parent writes into pipe // more work.... close( fd[1] ); // Finished, parent closes write end } else { //Child Process - reads what parent has written close( fd[1] ); // Closes the write end of the pipe read( fd[0] ); // Read from the pipe // more work close ( fd[0] ); //finished, close the read end }
60
Reading from a File
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"
61
Convergent Programming Steps
1. Understand the Problem * State the Problem * Specify the Interface * List representative and stressful cases 2. Code *into* C * Diagram your approach * Decide on variables and invariants * Outline overall logic * Convert logic into code 3. Verify the Solution * "Desk Check" -mental test cases * Perform test cases on computer * Use Fitness Test (CRE)2 * Peer Review 4. Return to earlier steps as needed
62
The Shell: Redirection of Output
By default, output of programs is to stdout. Use "\>" to redirect the output to a different file descriptor. This allows output of a program to be written to file. Alternatively, use "\>\>" to _append_ the file instead of writing over it. Example: % grep -e "abc" \> my\_output\_file.txt % grep -e "abc" \>\> my\_output\_file.txt
63
Default File Descriptors
* 0 - stdin * 1 - stdout * 2 - stderr
64
Process Creation: Key Process Routines
* fork * exec * wait
65
Changing the Current Offset of a File Descriptor
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
66
Dynamic Linking: Advantages
Advantages * Size of executable is reduced * Library routine not present in executable * Overal system speed increased * Library routine is only loaded once, then shared * Program Robustness increased * Changes to the library don't require relinking
67
Famous Unix Names
* Ken Thompson * Unix co-creator at Bell Labs, kernel creator * Dennis Ritchie * Unix co-creator at Bell Labs, C creator * Brian Kernighan * Unix early developer, C expert & author (deceased) * Bill Joy Sun * co-founder, BSD lead, Vi & Termcap implementor * Richard Stallman FSF founder & EMACS implementor * Linus Torvalds * Linux creator
68
exec: Two Formats
* execlp( filename, argv0, argv1, ..., (char \*)0); * The last parameter must be null * execvp(filename, argv); * filename can be a program or a script * image of the current process is replaced, can only return if that fails * Most Context is preserved * stdio buffers are NOT FLUSHED * must close or flush them manually using fflush or fclose before exec
69
Forking: Sequence of steps
1. execute "fork()" 2. The complete parent state is cloned to create the child process. * Child is created. 3. The "fork()" call returns * pid of child, in parent process * 0, in child process 4. Return value is used to determine next step: * parent executes parent code * child executes child code
70
Allocating a Structure Instance with malloc
- Use sizeof() to determine the number of bits to be allocated - Malloc returns a block of memory - Cast the memory as the required type name = (myType\*) malloc(sizeof(myType))
71
3 Identities for File Access
Access (bits)permissions are defined for: * user * group * other
72
Shell Command Equivalents: cp
No direct method. Use read/write File I/O operations to copy old file to new. Equivalent to: %cp old new
73
Physical Disk Organization: inode structure
An inode consists of * a status block * addresses to multiple data blocks
74
Shell Scripts: Overview
* A shell script is just a sequence of shell commands * Stored in an _executable_ text file * The "x" bit in the file permissions must be turned on with "chmod" * The shell finds programs by looking for executable files in the _path_ ( $PATH )
75
Important fields of the "stat" structure (9)
Given a stat structure "s" Not comprehensive. * st\_ino - inode * st\_nlink - link count * st\_uid - user id of the file * st\_gid - group id of the file * * st\_mode - mode bits * * st\_atime - last access time * st\_ctime - creation time * st\_mtime - last modification time * st\_size - size of the file
76
Which Operations( Access Modes) are files _protected_ for?
Each file is protected for: * read * write * execute
77
How many data blocks can an inode point to?
13
78
Key Files during Compiling and Linking
* Source Files * c language * text format * end in .c and .h * Object Files * machine instructions, binary format * external names * data initialization * end in .o * Libraries * Collections of object files * binary format * end in .a * Executable Files * instructions * data * entry point ("main") * other details
79
Memory Layout for a program
* Memory is a linear array of bytes * Stack changes with call and return * Heap changes with malloc/calloc and free Sections of Memory, in order: * Code * Data * Heap * Available(used for temporary storage) * Stack * Shell Data
80
Physical Disk Organization: Partition Sections
* boot and superblocks * ilist * contains inodes * datablocks
81
Libraries: Benefits of Using Libraries
* Save Development Time * Reusing routines that have already been throroughly debugged * Save Maintenance Time * Evolve a coherent capability * Using well known libraries with existing best practices
82
Dynamic Linking: Disadvantages
Disadvantages: * Libary version control * Can the executable find the _right version_ of the library routine? * "Thrashing" of library routines in memory due to a constrained environment
83
Shell Command Equivalents: rm
From unistd.h library: unlink( "old\_file" ); Equivalent to: %rm old\_file
84
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
85
The Shell: Redirection of Error
By default, errors are printed to stderr Use " 2\> " to redirect to a different file descriptor Example: % grep -e "abc" 2\> my\_error\_file.txt
86
Program Memory: Variables used to track Program State
* Key Addresses * Current _Instruction Counter (IC)_ * Stack Pointer, SP * Frame Pointer, FP * Heap Bottom, Stack Bottom * Register Values * Scheduling Priority * Current Working Directory * Process id's : * This process, pid * Parent Process, ppid * User and Group IDs
87
Unix Innovations: File System
* Organization * Simple hierarchical structure * access control * Content * Simple linear byte streams
88
What doe the Shell Provide?
* Interactive Interface * Allows use of all system capabilities * Navigate and manipulate file system * Invoke System Commands and Programs * Customize your environment * Programming Environment * For system programmers * Automate interactions * "glue" together smaller programs * Prototype solutions
89
Shell Command Equivalents: rmdir
From unistd.h library: rmdir( "path"); Equivalent to: %rmdir path
90
Useful Names and Functions: Utility Functions
from the "stdlib.h" library * atoi(s) * convert s to an integer (0 if not a number) * malloc(n) * allocates n bytes of new memory and returns a pointer to it * free(p) * frees the block of memory that p points to * exit(status) * end the program, with the specified status * getenv("PWD") * get the specified environment(shell) variable
91
What doe the Shell Provide?
* Interactive Interface * Allows use of all system capabilities * Navigate and manipulate file system * Invoke System Commands and Programs * Customize your environment * Programming Environment * For system programmers * Automate interactions * "glue" together smaller programs * Prototype solutions
92
Dynamic Linking: Advantages
Advantages * Size of executable is reduced * Library routine not present in executable * Overal system speed increased * Library routine is only loaded once, then shared * Program Robustness increased * Changes to the library don't require relinking
93
Using Malloc to Allocate storage | (Overview)
* "malloc" routine gets storage from the _heap_ * must include _stdlib_ * the heap is a storage pool that you manage * Anything allocated persists until explicitly freed using _free()_ * Initial bits in allocated storage are unpredictable(when using malloc())
94
inodes: Design Consequences for Move Operations
* Move speed is independent of the file size * data blocks do not have to be moved around * only the pointers change(unless moving partitions/drives) * Moves are easy to implement as atomic operations
95
Shell Command Equivalents: cd
From unistd.h library: chdir( "path" ); Equivalent to: %cd path
96
Unix Innovations: Processes
* Simplicity * Unix has a simple model for spawning and coordinating processes * Uniformity * Single model for: * jobs * concurrency * memory * etc
97
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)
98
C Pointer Variations
* Normal: * p * Returns pointer to itself * Pointer: * \*p * Returns thing pointed at * Pointer to a field within a structure: * q-\>f * Returns field f of structure q * Address Pointer: * &x * Returns address of x
99
fork() function: Basic Idea
Clone an existing process, then have it run an alternate set of code from the parent. The child "forks" from the parent.
100
(CRE)2 Criteria | (Program Fitness Checklist)
A Good Program should be: * Correct * Does what specification said * What client wants * What user needs * Clear * Understandable Implementation * Robust * Resilient wrt the real world, human abilities, future requirements * Ready * Delivered within schedule, budget constraints * Efficient * Within limits of time, space, cost * Ethical * Meets standards set by law, professional ethics, and safety needs
101
stat structure: How to check permissions
the sys/stat.h header file defines some useful values to use as bitmasks to check against s-\>st\_mode: * User permissions: * S\_IRUSR, S\_IWUSR, S\_IXUSR * Group permissions: * S\_IRGRP, S\_IWGRP, S\_IXGRP * Other permissions: * S\_IROTH, S\_IWOTH, S\_IXOTH These can be "anded" against s-\>st\_mode: * if (s-\>st\_mode && S\_IRUSR) ...
102
Symbolic Links: Overview
* A special file type that simply contains the name of another file * Different from regular links, called "hard links" * All file operations attempted on a symbolic link will follow the link and affect the destination file * Created using command: * ln -s * Also called sym links
103
Possible Process States
* running * actually executing on machine * sleeping * ready to run, waiting for its turn * idle * sleeping for more than 20 seconds * waiting * waiting for an event, such as disk I/O * zombie * processes ended, but the parent has not yet recieved notification from the system
104
How do Unix/Linux systems distinguish Users and Groups?
Each User has a unique identifier * uid * The etc/passwd file matches the user name and uid Each User belongs to one or more groups Each Group is identified by a unique identifier * gid * The etc/groups file matches group names and gid
105
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
106
Code: Have Parent Process wait for Child Process to finish
int status; if( fork() ) { //parent process stuff wait(&status); //wait for child to update status exit(0); //parent exits }
107
When to use the Heap
* When data should persist after a function returns * Size of data is not known until execution time
108
Method used to duplicate a file descriptor
dup( old\_fd ) or dup2( old\_fd, new\_fd )
109
File Status: Helpful operations defined in sys/stat.h (2)
S\_ISREG(m) * Check if the file is a regular file * pass the stat struct as the parameter "m" * if( S\_ISREG( s-\>st\_mode) ) ... S\_ISDIR(m) * Check if the file is a directory * if ( S\_ISDIR( s-\>st\_mode) ) ...