Beruflich Dokumente
Kultur Dokumente
Processes in UNIX
#include <uninstd.h>
pid_t getpid (void);
pid_t getppid(void)
•The pid_t is an unsigned integer type that represents a process ID
• Usually the SIGTERM (15) signal is used to kill a process as thiswill try to shut
the process down properly:
# kill -15 <pid>
• Some processes do not respond to a SIGTERM and the SIGKILL (9) signal must
be used:
# kill -9 <pid>
• There are some processes which do not even respect the SIGKILL signal. For
such a process the last resort is to send the SIGHUP (1) signal to hang up the
connection:
# kill -1 <pid>
• To use kill in a program we use the following function:
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig)
• Sends a signal to a process. The process to which the signal is to be sent is
specified by pid. The signal that is to be sent is specified by sig.
• Upon successful completion, 0 is returned. Otherwise, -1 is returned, no signal is
sent, and errno is set to indicate the error.
#include <unistd.h>
pid_t fork(void)
Create a new process. The new (child) process is an exact copy of the
calling (parent) process. Upon successful completion, it will return 0 to
the child process and return the process ID of the child process to the
parent process. Otherwise, -1 is returned to the parent process, no child
process is created, and errno is set to indicate the error.
#include <stdio.h>
int main(void) {
pid_t c1, c2; /* process ids */
c2 = 0;
c1 = fork(); /* fork number 1 */
if (c1 == 0) c2 = fork(); /* fork number 2 */
fork(); /* fork number 3 */
if (c2 > 0) fork(); /* fork number 4 */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main(void) {
wait:
If a child terminated, return its pid therwise return -1 and set errno
waitpid:
Allows you to wait for a particular process, or all process if pid is -1.
Important option is NOHANG which will return 0 if there is a specified child
to wait for but it has not yet terminated.
BIL 244 – System Programming
The exec family of functions
int execl( char *path, char *arg0, ..., char *argn, NULL)
int execv( char *path, char *argv[])
int execle(char *path, char *arg0, ..., char *argn, NULL,
char *envp[])
int execve(char *path, char *const argv[], char *envp[])
int execvp(char *file, char *const argv[])
int execlp(char *file, char *arg0, ..., char *argn, NULL)
• If any of the exec functions returns, an error will have occurred. The
return value is -1, and the global variable errno will be set to
indicate the error.
int main(void) {
int c = 0;
pid_t pid;
int status = 0;