Send a link to your students to track their progress
119 Terms
1
New cards
exit(status)
Terminate process execution and return status to OS, status \= \> 0 ERROR, status \= 0 no error
2
New cards
file
structure used to access a stream; every stream has its own FILE associated w/stream
3
New cards
File *f \= fopen ("chipmunk", "r")
chipmunk is name of file, r is mode; if fopen fails it returns null-\> file not found/binary file (diff function and don't have permission to open)
4
New cards
fopen modes
r \= read, if you pass a file that doesnt exist it fails; w \= write, will create file if one doesn't exit or if it does exist will delete and replace with new data; a \= append, will create file if it doesnt exist or will add to it if file does exist
5
New cards
scanf()
reads user input from the command prompt; reads from standard input
returns \# of input values converted or EOF
6
New cards
fscanf() int fscanf(FILE *stream, char *format, ... );
reads input from the stream given as an argument
scanf if is a special case of fscanf.
scanf("%d", &number); // is the same as fscanf(stdin, "%d", &number);
returns \# of input values converted or EOF
7
New cards
sscanf()
A function that accepts input from a string. It is a variation of the scanf() function. returns \# of input values converted or EOF
8
New cards
structures in c can have \____ to structures of the \____ \____
pointers; same type
9
New cards
if you need to use a field within a typedef for ex: typedef struct node { int data; struct node *next; } Node;
Node *head; (also pointer to a node)
struct node *next is a pointer to a node; needs incomplete type for the compiler, cant have field of the actual node w/o declaration of Node; this is why you make a ptr to structure bc compiler knows how much mem to allocate for ptr but not for structure
10
New cards
Node *head
in order to change head have to pass in ptr to ptr so if you dereference it you get actual pointer to modify it
11
New cards
IN C EVERYTHING IS PASSED BY \__________
VALUE
12
New cards
Graphs basics
need type Vertex (Edge * edge list, char * name, *next vert) , Edge (destination, next edge, weight), and String_graph(num vertices, num edges, Vertex *vertex list)
each graph has a head and each vertex has ptr to next vertex
each vert has a head pointer to first edge
each edge has a ptr to next edge
13
New cards
double linked list
A linked list where each node has a pointer to the previous and next nodes
4 case: insert to empty, beginning of list(change head), end of list (replace tail), middle of list
14
New cards
What/why is there runstack/heap?
runtime stack allows for recursion because for a recursive call to work every call needs it own local vars & param
heap -\> dynamically allocated memory, avoid compile time limits, heap is anonymous, if it has a variable name NOT heap
15
New cards
where do linked structures belong and why?
HEAP; allows you to creat memory that exists beyond the scope it was created; each node must be dynamically allocated bc otherwise you list will fall out of scope.
ptr to a function; can point to any function that has those param
19
New cards
char * (*h[5]) (int);
array of 5 ptrs to functions that take in an int and return a char
20
New cards
typedef void (*Ifp) (int); Ifp p;
name of a function w/o any parentheses -\> ptr to the memory address of the function
you can call a function through a ptr
21
New cards
argc
\# of cmd line arguments that appeared on cmd line including program
22
New cards
argv
array of pointers to strings of arguments that appeared on the cmd line
23
New cards
argv[argc] \=?
last element is always NULL
24
New cards
3 types of main
main(void); main(int argc, char*argv[]); main (int argc, char*argv[], char *envp[]);
25
New cards
shell
executes the command on the command line
26
New cards
shell variables
Variables that control the shell; you can set the shell variables to male the shell diff
Ex: 1. HOME 2. PATH
27
New cards
environmental variables
affect the current shell but are also seen by every program thats run by the current shell
28
New cards
HOME
Identifies a users home directory
29
New cards
PATH
Specifies the directories the shell should look when it tries to execute any command.
30
New cards
what does commans setenv
how you set an environmental variable
31
New cards
char * envp[]
array of NULL terminated strings, key/value of terms
32
New cards
Preprocessor steps
1. deletes comments 2. inserts code for \#include 3. substitutes the \#defines 4. handles conditional compilation
33
New cards
Compilation of C program Steps:
1. preprocessing 2. compilation 3. compilers produce assembly code and run the assembler on it 4. linker -\> links object files together
34
New cards
Compilation steps
1. scanner -\> takes the prgm and breaks it up into the low level syntax entities 2. parser -\> figures out which language constructs they belong to 3. type checker -\> makes sure things are being applied to the right type of operands 4. code generator -\> generates code \= relatively inefficient code 5. optimizer -\> makes code more efficient
35
New cards
Macros \#define NAME(parameters) replacement ex: \#define sum(a,b) a+b x \= sum (2,5) what does x \=?
when program is compiled, SUM(2,5) replaced 2+5, x \= 7 compiler sees the expression after the preprocessor has replaced it w/ "replacement"
36
New cards
what is a problem w/macros?
numeric expressions can yield unexpected results, side effects, macros can invoke another
37
New cards
what does this equal? s \= SQUARE(a+1) s \= a + 1 * a + 1 s \= 5 + 1 * 5 + 1
s \= 11 instead of 36 b cof precedence INCLUDE parentheses to avoid unexpected behavior
38
New cards
do macros have a type?
no typeless bc the preprocessor replaces the code
39
New cards
kernal
The part of the operating system that controls memory and file access
runs directly on the hardware
40
New cards
what are priveledged instructions?
handles through kernal mode by making a system call
41
New cards
how does a system call relate to a user?
ordinary users don't have permission to make changes/access memory files so need to make system call
42
New cards
process
instance of a program loaded into memory and running
43
New cards
whats the diff bw program and process
program isnt alive, just code on a disk
process is alive and actually running
44
New cards
multitasking
more than one process can be alive/active in memory at a time; more efficient
45
New cards
1 CPU 1 core -\> how many processes can run at once
1
46
New cards
how does cpu give illusion of multiple processes running at once
cycle between processes
47
New cards
program counter
register in the CPU that stored the mem address of the next instruction to be executed
48
New cards
process table
to keep track of all the processes, the scheduler maintains a list of all active processes at a point in main memory
49
New cards
page table
stores processes' address spaces (memory)
50
New cards
file table
lists which files are opened by each process
51
New cards
process state transitions: ready
ready but waiting for CPU
52
New cards
process state transitions: waiting
waiting for smth to finish before it can go back to ready state
53
New cards
process state transitions: interrupt
os sends process back to ready, why? to make sure time running is shared between processes
54
New cards
what are background processes and what are they used for?
processes running but not using the terminal, because you can run commands from the terminal while the processes run
55
New cards
what are signals?
how processes communicate; the kernal notifies processes using signals
56
New cards
can users send signals?
yes - you can define diff actions for when a process gets a signal, also default actions exist
57
New cards
which signals can't be ignored?
SIGKILL and SIGSTOP, but processes can also block/ignore signals
58
New cards
SIGSEGV
signal for seg fault
59
New cards
SIGFPE
Floating point exception
60
New cards
SIGILL
illegal instruction
61
New cards
how do processes communicate with each other?
send signals via kernal
62
New cards
how does a shell communicate with processes?
A shell can send signals to other processes using the "kill" command, or program can have kill()
63
New cards
how is a process created?
splits itself into two processes (forking)
64
New cards
how many times does pid_t fork(void) return?
once in each process, parent and child
65
New cards
what does parent return is child isn't created properly?
-1
66
New cards
what does child and parent return when forking is successful?
child \= 0 parent \> 0 -\> also child's ID
67
New cards
what is unique to the child process?
process ID, own copies of parent file descriptors and streams, process times, resource utilizations initially set to 0
68
New cards
what is inherited from parent to child process?
copy of parents memory (program code, runtime stack, heap), current working directory, the environment and all environment variables, controlling terminal (child and parent read/write input from same place)
69
New cards
pid_t getpid(void) vs pid_t getppid(void)
returns current process ID vs return process id of parent
70
New cards
Fork Bomb
Works by quickly creating a large number of processes to saturate the available processing space in the computer's operating system. can only hold a finite \# of processes
71
New cards
what are the ways for process termination?
process terminates itself using exit() system call, OS kernal kills a process, a process can kill another but only if they belong to the same user or one has priviledged process (kernal/root)
72
New cards
what is root?
the OS process
73
New cards
deceased children?
process exits, still tracked by the kernal, needs to be released from the table
74
New cards
how are deceased children released from table?
reaped by parent using wait() of waitpid(), system calls
75
New cards
what are zombie children?
no cpu cycles, no memory, just an entry on the process table
76
New cards
why is a zombie process still tracked?
parent might still want to get info from it
77
New cards
orphaned process
if the parent terminated first, child is adopted by closest ancestor that can adopt all the way up to systemd
78
New cards
What is systemd?
first process when system boats, automatically reaps its children when they terminate
79
New cards
pid_t wait(int *status)
reaps the most recently terminated child, blocking wait, pass a ptr to an int will be filled w info (exit status of child) about reaped child, returns process ID of reaped child if successful
80
New cards
what happens if you call wait w/o child
returns -1 and doesn't block
81
New cards
what happens if you call wait w some terminated children but not reaped
will wait and reap on of them
82
New cards
what happens if you call wait while all children are still running?
blocks parent
83
New cards
pid_t waitpid(pid_t pid, int *status, int options)
wait for a specific child, blocking wait but you can turn it off, with options (WNOHANG)
84
New cards
you can only wait for a \_____ child process
direct
85
New cards
WEXITSTATUS(status)
returns exit status of child, only valid if WIFEXITED is true
86
New cards
how to invoke a new program in a process?
use execv function
87
New cards
what does exec return if sucesfull and if failed?
sucesfull-\> NEVER returns failed-\> return -1 and sets errno to indicate cause of error
88
New cards
types of exec
v: call uses an argument vector (an array) that has the same form as ARGV
l: call uses an argument list where last argument must be null
p: means the call searches PATH to find the program to exec
e: specify environment for the exec'd program
89
New cards
what maintains a file descriptor table
kernel
90
New cards
file descriptor table
unique to each process and holds information about all files for each process
91
New cards
open()
process uses this to open a file, kernal returns a file descriptor, returns -1 if error
92
New cards
close()
returns 0 if sucessful, -1 on error closed by kernal, open files use system resources so they need to be closed once theyre not needed
93
New cards
what is a function pointer?
pointer that stores mem adress of function
94
New cards
how do fgets() and fscanf() compare?
fgets() will read an entire line from the input and stops only for newline fscanf reads a string using %s and stops at the first whitespace character
95
New cards
strcpy(str1, str2)
copies str2 to str1 str1 does not have to be null terminated but str2 does
96
New cards
what does sizeof(str) return
number of characters + null char
97
New cards
conditional compilation: can compilation fail if the compiler sees two def of a structure?
no, not if its because main includes a header and that header includes another with a struc
98
New cards
errno? how to print erno?
external global int variable and indicates an error occurred during any function call perror(error)
99
New cards
why would you use text stream or binary stream?
text stream is readable but binary is faster
100
New cards
how to determine exit status of a child process
call wait() - returns the process ID of the terminated child and the exit status of the child process