1/23
Looks like no tags are added yet.
common gcc commands
gcc <source code>: compiles and links an executable
gcc <source file> -o <output file> OR gcc -o <source file> <output file>: compiles and links an executable using given name
gcc -c <source file>: compiles into an object file
gcc <object file(s)> -o <output file>: link 1 or more object files into an exectuable
process
OS abstraction representing a running program and its execution state
OS creates processes
OS process details
process control block
process ID
parent process ID
process group, session IDs
owner and group user accounts
address space: code, data for variables, heap objects, etc
flags
process state
process states
Running: being/scheduled to be executed by CPU
Sleeping: waiting for event to happen before execution (i.e. user input, timer, etc); it’s in wait queue
Uninterruptible sleep: similar to sleep
T (stopped): not in queue; has to be told to continue
Zombie: dead but still taking space
int and char types
int
short and long int
unsigned int
unsigned short and unsigned long int
char
unsigned char
short int > char
long int > short int
compiler automatically truncates if expression has a bigger size than allowed
floating point math
always done with doubles
bitwise operators
a « b: shifts bits of [a] left by [b] places ( = a * 2b)
a » b: shifts bits of [a] right by [b] places ( = a/2b)
objects
contiguous segment of memory that has some sort of identity
to use, know the location in memory and type of data it holds
pointer
holds location/address of a type
type of pointer = type of object
ex) &x //gets the pointer/address to x
x = 5 //changes value in x
arrays
linear contiguous sequence of elements of the same type
to use, know where it is (tracked by a pointer) and size of elements
compiler automatically creates/destroys storage for array variables
strcpy (variable, value)
copies value into variable
variable has to be big enough to hold enough value
structs
used to collect a fixed amount of data of different types
when you have the struct value, use “struct.variable”
when you have a pointer to a struct, use “struct→variable”
sizeof(struct) = multiple of largest alignment, including any padding needed
typedef <type expression> <new name>
sets nickname
ex) typedef struct list_node list_t //new name = list_t
enumuerated types
set of named int constants
ex) enum direction {north, east, south, west} //each is assigned a number starting from 0
union
stores different types
sizeof(union) = largest alignment
use dot operator to call variables
dynamic allocation
ability to create new objects at run time
void *malloc (size_t length)
returns pointer to requested block size
void *calloc (size_t count, size_t size)
allocates count *size chars and intitializes all to 0
void realloc (void ptr, size_t length)
changes size of ptr to length
rounding down to multiple of 8
n & ~7
sets the last 3 bits to 0
rounding up to a multiple of 8
(n+7) & ~7
makes sure 1-8 = 9-16 = 16, etc
preprocessor
runs before compilation
functions
macro replacement
conditional compilation: decide what version of code to use
avoids problems
caused by including a header file multiple times
automatically converts number-like macro values to
integers and supports some limited arithmetic and comparison operators
header components
type definitions
function prototypes
global varaible declaration
macros
process termination
returning from main: 0 = success
call exit(): closes open files, releases memory pages, etc
atexit (void