POSIX Processes API
Creating processes
fork
// Returns new Process ID in parent
// Returns 0 in child
int fork(void);
-
Create a process that is an exact copy of the current one
- Called once returned twice
Why
fork?
- Most calls to
forkare followed byexecve- could combine into one
spawnsystem call - load and execute new child process
- could combine into one
- Occasionally useful to fork one process
- For webservers, parallelism
- Create one process per core
- Simplicity
- no arguments needed
waitpid
int waitpid(int pid, int *status, int options);
-
Attempt to get exit status of child
pid: process ID of childstatus: will contain exit value or signal if crashedoptions:- 0 -> wait for child to terminate
- WNOHANG -> do not wait for terminate
- Returns process ID or -1 on error
Deleting Processes
exit
void exit(int status);
-
have the current process exit
status: shows up in encoded format if waitpid is called- status of 0 is success, non-zero is error
Kill
int kill(int pid, int sig);
-
Sends signal
sigtopid
A signal is a software interrupt
- SIGTERM is most common
- kills process by default
- applications can catch to do clean up
- SIGKILL is stronger
- kill process always
Running Programs
execve
int execve (char *prog, char **argv, char **envp)
-
Replaces the current running program with a new one
prog prog: full pathname of the program to runargv: argument vector that is passed tomainofprogenvp: environment variables
Execve is usually called through wrapper functions
int execvp(char *prog, char **argv);
- searches
PATHforprog - then runs execve
int execlp(char *prog, char *arg, …);
lpstands for list parameters- list arguments one at a time
- end with
NULL
- end with