Final Superdeck Flashcards
3 special files with commands
stdin
stdout
stderr
0, 1, 2, respecitively
List of special/default directories.
/bin
/sbin
/lib
/usr
/etc
/home
/boot
/dev
/opt
/var
/tmp
/proc
/bin
binaries or executables. System programs like gcc, vim. Essential for the operation of the system itself.
/sbin
System binaries that should only be executed by the root (admin) user.
/lib
Libraries
/usr
user binaries, libraries, etc.
/etc
Editable text configurations, where configuration files are found
/home
User home (or personal) directories are here.
/boot
contains files needed to boot the system like the OS kernel
/dev
Device files. Allows interfacing with I/O devices with special files that are mapped to directories.
/opt
Optional software. Rarely used.
/var
Variables files that are changed as the system operates. Like log files.
/tmp
Temporary files not persisted between system rebootst.
/proc
An imaginary directory made by the kernel to keep track of processes.
How to go to home directory?
~ (tilde)
What does cat do?
Takes in a file as an argument, and outputs the contents to stdout. If a file is not given, it takes from stdin.
How to EOF in a command?
ctrl-d
What does > do in Bash?
Output redirection.
Redirects outputs of a command to a file.
Note that > creates (or overwrites).
» creates (or appends)
What does < do in bash?
Input redirection.
Tells the shell to get the input for a command from a specified file instead of a keyboard.
I.e.
command [arguments] < filename
Pipelines
Take stdout prior command and uses it as stdin to another.
How to increment a number (variable) in bash?
let x=x+1
Bash if else
if test-command
then
commands
elif test-command
then
commands
else
commands
fi
Bash while
while test-command
do
commands
done
Assembly is a ___ level language.
Low-level, since there is a 1-to-1 correspondence between statements and machine instructions.
Assembly is also machine dependent.
What does the OS do?
Controls the entire operation of the system. All input and output performed on a computer system are channeled through the OS.
Steps to getting an executable from source code.
Pre-processor takes and expands symbol definitions. Inserts header file text.
Compiler - Syntactically correct programs are translated into a lower form, assembly.
Assembler - Translates assembly language statements into actual machine instructions (object code or .o files).
Linker - links all object files together with system libraries to produce an executable file.
Building
Compiling/linking a program. Leads to an executable.
Parts of a makefile.
target dependencies recipe
recipe must be tab-indented
Integer format character
%i or %d (d for decimal, i detects base)
or %o or %x for octal and hexadecimal
int minimum size
Guaranteed to be no smaller than 16 bits
Float format specifier
%f
or %e for exponential notation
In class, how many bits were floats and doubles?
Float: 32
Double: 64
How to make a constant (literal) long int?
Append an L on the end.
Unsigned int format specifier.
%u
Short-<type> format specifier</type>
%h<typespecifier></typespecifier>
int to float and vice versa?
Floats are truncated (i.e 3/2 is 1).
Ints have no change.
Control structures with only 1 line
Simply omit curly braces. Ends at the semi-colon.
do while
do
statement;
while (condition);
or
do {
statements;
} while (condition);
if else with single lines?
if (condition)
statement;
else
statement;
C switch case.
switch (expression)
{
case value1:
statement1;
….
statementn;
break;
case value2:
statement1;
….
statementn;
break;
default:
statement1;
….
statementn;
break;
}
Note:
ONLY WORKS FOR INTEGER TYPES i.e. int, short, long, long long, char…
Ways to initialize arrays.
type name[N] = …
{1, 2, 3, 4,…} initialize in order (doesn’t have to be all of them)
or
{[0] = 1, [2] = 3,…} specific indices
In C, strings are…
Just char arrays.
Must always end with null-terminator (sometimes called zero-terminator).
Defining a multidimensional array in C
type name[n][m]
Notes:
int A[2][2] = {1 2 3 4} is equivalent to { {1,2}, {3,4}} (but not recommended).
can also do
int A[2][2] = {[1][0]=3, [2][2]=4};
Variables passed to a function are passed…
By value.
This means that giving it an int x gives it an equivalent value, but not x. However, giving it a pointer to x lets it access the value at the pointer to change x.
Global variables.
Outside of other scopes.
I.e. same scope as the include statements.
Multidimensional arrays implicit size.
Cannot be explicit, aside from first.
I.e. A[][] is invalid.
A[10][10] is valid
A[][10] is valid
What do git commits include?
added/removed files
files renamed/moved
insertions/deletions in a file (lines changed)
timestamp
user message
more…
CFLAGS we use
-Wall -Werror -std=c99 -pedantic -g
-Wall
Enables all warnings. can catch questionable constructions.
-Werror
Turns warnings into errors. Forces you to fix questionable code.
-std=
Selects a specific standard. Standards keep things portable.
-pedantic
Issue all warnings demanded by strict C standard.
-g
Generate a debugging section in the executable. Needed to use gdb
How to reference a Makefile variable?
$(VAR)
Useful gdb commands
step
next (may jump over one line loops, function calls, etc).
info break (list and give info about breakpoints).
clear line-or-funciton-name (removes a breakpoint)
delete breakpoints (duh)
info locals
set var x=2 (sets a variables value)
bt (stacktrace (backtrace). useful when many functions)
Structs without names
struct
{
stuff;
} var_name = {stuff};
Note, could use commas on the last line to declare many variables.
This is not recommended.
Program memory layout.
Top to bottom. (high to low address)
Command line arguments.
Stack
Heap
Uninitialized data segment
initialized data segment
text/code segment
Dynamic includes from command line arguments to heap. Static elsewhere.
What is the text segment in program memory?
Instructions reside here.
What is initialized data segment in program memory?
Holds literals and constants.
What is uninitialized data segment in program memory?
Contains static and global variables.
What is the stack in program memory?
Only can push and pop, which makes stack perfect for nested scopes (functions calling functions).
What happens in program memory when a function is called?
An entry is pushed onto the stack including arguments, space for automatically allocated variables inside the function, and a return address.
Once done, it’s popped from the stack.
What is a stack overflow?
When the stack overflows. i.e. too many recursive functions.
The stack runs out of space, or runs into the heap.
What is the heap used for in program memory?
Dynamic memory allocation.
What happens when a C program is compiled and run?
From high level language, compiler translates to assembly. Assembler then translates to machine code to make an object file. Linker combines objects and libraries, resolves references, and makes an executable.
Finally, the loader loads the executable into a suitable memory location and begins execution (which may involve more linking during runtime).
What is a process?
An OS concept that can be though of as a container for a single executable. Programs usually only have 1 process, but they could have many.
There can be many running processes at the same time.
The OS provides a method to switch between them (scheduling).
What is a thread?
A path of execution.
One process can have one or many threads.
Many threads can run at any given time.
Like processes, they are scheduled by the OS or within each process.
Each thread will need its own stack if multiple in a program.
Where do commands live?
Most in standard directories; /bin, /usr/bin, usr/local/bin, /opt/X11/bin
Some commands aren’t binaries, but are shell programs.
How can you find where a binary, header, or source file, is?
locate command
How can you find where a command binary/shell-script is?
Use which or whereis command
What does
char * const charPtr = &c;
do?
Makes a constant pointer. c can change, but charPtr will always point to it.
What does
const char *charPtr = &c;
do?
Notes that the contents of &c do not change, although charPtr can change.
What does
const char * const charPtr = &c;
do?
Makes the pointer and the value both constant.
How do you make a pointer to an array?
Make it point to the type held by the array.
I.e. A pointer to an int array is the same as a pointer to an int.
What does the name of an array do without any other operators?
Represents the pointer.
Can pointers be compared?
Yes. This can be useful at times, for example when checking if something may exceed the bounds of an array.
Function pointer variable declaration syntax.
return-type (*pointer-var-name) (arg-type1, …);
i.e. int (*fnPtr) (void);
How are who can access a file controlled?
Traditional access permissions and ACLs (Access Control Lists).
Three types of users who can access a file.
The owner of the file (owner/user),
a member of a group that the file is associated with (group),
everyone else (other)
Three ways an ordinary file can be accessed
read form it
write to it
execute it
How to see permissions?
ls -l
What is the first bit that comes before permissions?
- for ordinary files
d for directories
How to use chmod?
symbolic arguments
i.e.
chmod a+rw gives (+) read-write (rw) permissions to all users (a).
Also can use o (other) g (group) u (user or owner)
chmod numeric arguments
just give it an octal number.
i.e.
chmod 600 somefile
somefiles permissions would be rw——-
Note that this sets permissions.