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
Q

Shell Utility Toolkit:

Basic

Line Oriented Processing Tools (8)

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

Directory Structure:

How Move (mv)

operations work

A
  • 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

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

3 Major Categories of

Process Operations

A
  • Process Creation
  • Process Communication
  • Process Control
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Libaries:

What are

“Shared Libraries”?

A

Libraries that are prepared for

Dynamic Linking

These are also called

Dynamic Linking Libraries (DLLs)

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

Structure of a

Directory

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

dup:

Two Forms

and their use

A

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).

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

What is a Pipe?

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

Dynamic Linking:

Disadvantages

A

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

The Shell:

Redirection of

Output

A

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

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

How to change the

Effective uid

of a program?

A
  • 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:

-rws –x –x

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

Shell Command Equivalents:

mv

A

From stdio.h library:

rename( old_filepath, new_filepath);

Equivalent to:

%mv old new

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

Design Consequences of Inodes

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

Specialized File Operations

A
45
Q

Process Communication:

Key Routines

A

These facilitate read/write between processes:

  • pipe
  • dup
  • sockets
46
Q

3 Common Pointer Errors

A
  • 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
Q

Libraries:

Benefits of Using Libraries

A
  • 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
Q

inodes and Datablocks

are reclaimed only

when ________

A

Two conditions are met:

  • The Link Count becomes 0
  • The number of “opens” becomes 0
49
Q

exec() function:

Basic Use

A
  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
Q

Shell Command Equivalents:

rm -r

A

From stdio.h library:

remove( “old_file” );

Equivalent to:

%rm -r old_file

51
Q

The Shell:

Redirection of

Error

A

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
Q

Basic Types of

Inter-Process Communication (IPC) (2)

A

Pipes

Signals

53
Q

Pipe:

Read Rules

A
  • 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
Q

Directory Access Rules (3)

A
  • 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
Q

Directory Structure:

How do Link(ln) operations work?

A
  • 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
Q

Design Consequences of Inodes

A
  • 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
Q

Process Controlling:

Key Routines

A

Cause and Handle exeptions and interrupts:

  • signal
  • alarm
58
Q

Shell Utility Toolkit:

File System

Manipulation Tools (8)

A
  • 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
Q

Code Outline:

Setting up a pipe between

parent and child process

A

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
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”
61
Q

Convergent Programming Steps

A
  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
Q

The Shell:

Redirection of

Output

A

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
Q

Default File Descriptors

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

Process Creation:

Key Process Routines

A
  • fork
  • exec
  • wait
65
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
66
Q

Dynamic Linking:

Advantages

A

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
Q

Famous Unix Names

A
  • 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
Q

exec:

Two Formats

A
  • 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
Q

Forking:

Sequence of steps

A
  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
Q

Allocating a Structure Instance

with malloc

A
  • 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
Q

3 Identities for File Access

A

Access (bits)permissions are defined for:

  • user
  • group
  • other
72
Q

Shell Command Equivalents:

cp

A

No direct method.

Use read/write File I/O operations

to copy old file to new.

Equivalent to:

%cp old new

73
Q

Physical Disk Organization:

inode structure

A

An inode consists of

  • a status block
  • addresses to multiple data blocks
74
Q

Shell Scripts:

Overview

A
  • 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
Q

Important fields of the “stat” structure (9)

A

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
Q

Which Operations( Access Modes)

are files protected for?

A

Each file is protected for:

  • read
  • write
  • execute
77
Q

How many data blocks can an inode point to?

A

13

78
Q

Key Files

during

Compiling and Linking

A
  • 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
Q

Memory Layout for a program

A
  • 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
Q

Physical Disk Organization:

Partition Sections

A
  • boot and superblocks
  • ilist
    • contains inodes
  • datablocks
81
Q

Libraries:

Benefits of Using Libraries

A
  • 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
Q

Dynamic Linking:

Disadvantages

A

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
Q

Shell Command Equivalents:

rm

A

From unistd.h library:

unlink( “old_file” );

Equivalent to:

%rm old_file

84
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
85
Q

The Shell:

Redirection of

Error

A

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
Q

Program Memory:

Variables used to track Program State

A
  • 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
Q

Unix Innovations:

File System

A
  • Organization
    • Simple hierarchical structure
    • access control
  • Content
    • Simple linear byte streams
88
Q

What doe the Shell Provide?

A
  • 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
Q

Shell Command Equivalents:

rmdir

A

From unistd.h library:

rmdir( “path”);

Equivalent to:

%rmdir path

90
Q

Useful Names and Functions:

Utility Functions

A

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
Q

What doe the Shell Provide?

A
  • 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
Q

Dynamic Linking:

Advantages

A

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
Q

Using Malloc to Allocate storage

(Overview)

A
  • “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
Q

inodes:

Design Consequences

for

Move Operations

A
  • 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
Q

Shell Command Equivalents:

cd

A

From unistd.h library:

chdir( “path” );

Equivalent to:

%cd path

96
Q

Unix Innovations:

Processes

A
  • Simplicity
    • Unix has a simple model for spawning and coordinating processes
  • Uniformity
    • Single model for:
      • jobs
      • concurrency
      • memory
      • etc
97
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)
98
Q

C Pointer Variations

A
  • 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
Q

fork() function:

Basic Idea

A

Clone an existing process,

then have it run an alternate set of code from

the parent.

The child “forks” from the parent.

100
Q

(CRE)2 Criteria

(Program Fitness Checklist)

A

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
Q

stat structure:

How to check permissions

A

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
Q

Symbolic Links:

Overview

A
  • 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
Q

Possible Process States

A
  • 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
Q

How do Unix/Linux systems

distinguish

Users and Groups?

A

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
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
106
Q

Code:

Have Parent Process

wait for

Child Process to finish

A

int status;

if( fork() ) {

//parent process stuff

wait(&status); //wait for child to update status

exit(0); //parent exits

}

107
Q

When to use the Heap

A
  • When data should persist after a function returns
  • Size of data is not known until execution time
108
Q

Method used to

duplicate a file descriptor

A

dup( old_fd )

or

dup2( old_fd, new_fd )

109
Q

File Status:

Helpful operations defined in

sys/stat.h

(2)

A

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) ) …