API Flashcards
fork
int fork(void);
create a new process that is an exact copy of the old process
- returns pid of new process in parent
- returns 0 in child
gets called once, returns twice
waitpid
int waitpid(int pid, int * status, int options);
attempt to get exit status of child
- returns pid or -1 on error
- pid = pid to wait for, -1 for any
- status = exit value/signal of child
- options = 0 means wait for child to terminate, WNOHANG means do not wait
exit
void exit(int status);
exit the current process
- status = shows up if waitpid called
0 is success, non-zero is failure
kill
int kill(int pid, int sig);
sends signal to a process
- pid = pid of target
- sig = code
SIGTERM most common, kills process by default but app can catch for cleanup
SIGKILL is stronger, always kills
execve
int execve(char * prog, char ** argv, char ** envp);
replace the current program with a new one
- prog = path to new program
- argv = arguments to prog’s main
- envp = environment for new program
execvp
int execvp(char * prog, char ** argv);
searches PATH for prog, uses current environment
execlp
int execlp(char * prog, char * arg, …);
execvp, but listing each argument terminated by NULL
dup2
int dup2(int oldfd, int newfd);
makes newfd an exact copy of oldfd
closes newfd, if it was valid
both fds share the same offset (lseek on one affects both)
pipe
int pipe(int fds[2]);
output of fds[1] is input to fds[0]
- returns 0 on success, -1 on error
- when fds[1] is invalidated, fds[0] returns EOF
x86 lock
makes the following instruction atomic
x86 lfence
waits for all reads to be done before continuing
sfence
waits for all writes to be done before continuing
mutex_lock implementation
while(xchg(mutex, true)) {};
uses atomic test and set
_Atomic
wrap most basic types with _Atomic() and all standard operations are seqcon
atomic_flag
special type:
- atomic bool, no load/store
- uses test_and_set or clear instead