APUE Process

chapter seven
  • Process tremination

    • Return from main
    • Calling exit
    • Calling _exit or _Exit
    • Return of the last thread from its start routine
    • Calling pthread_exit
    • from the last thread
    • Abnormal termination occurs in three ways:
      • Calling abort (Section 10.17)
      • Receipt of a signal (Section 10.2)
      • Response of the last thread to a cancellation request
  • Exit Functions

    1
    2
    3
    4
    5
    #include <stdlib.h>
    void exit(int status);
    void _Exit(int status);
    #include <unistd.h>
    void _exit(int status);

_exit and _Exit, which return to the kernel immediately, and exit, which performs certain cleanup processingand then returns to the kernel.

  • atexit()
    1
    2
    3
    #include <stdlib.h>
    int atexit(void (*func)(void));
    Returns: 0 if OK, nonzero on error
  • a process can register at least 32 functions that are automatically called by exit. These are called exit handlers and are registered by calling the atexit function.
  • The exit function calls these functions in reverse order of their registration.
  • Each function is called as many times as it was registered.
  • Environment List
    Environment
    the environment strings are typically stored at the top
    of a process’s memory space, above the stack.
chapter eight
  • pid_t
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <unistd.h>
    pid_t getpid(void);
    Returns: process ID of calling process
    pid_t getppid(void);
    Returns: parent process ID of calling process
    uid_t getuid(void);
    Returns: real user ID of calling process
    uid_t geteuid(void);
    Returns: effective user ID of calling process
    gid_t getgid(void);
    Returns: real group ID of calling process
    gid_t getegid(void);
    Returns: effective group ID of calling process

fork

- 调用一次,返回两次,子进程返回0, 父进程返回子线程pid
- 子进程只保留调用fork 的线程副本
- 父进程中所有打开的文件描述符被复制到子进程
- 子进程不集成父进程的文件锁,未处理闹钟清除,未处理信号集置空
- 父进程先于子进程结束,子进程被init进程收养,其父进程ID为1
- 已经结束但未被父进程处理的进程:僵死进程

wait waitpid

1
2
3
4
#include <sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, int options);
Both return: process ID if OK, 0 (see later), or −1 on error
  • wait or waitpid do
    Block, if all of its children are still running
    Return immediately with the termination status of a child, if a child has terminated and is waiting for its termination status to be fetched
    Return immediately with an error, if it doesn’t have any child processes
  • OSIX.1 waitpid function.
    The interpretation of the pid argument for waitpid depends on its value:
  • pid == −1 Waits for any child process. In this respect, waitpid is equivalent to wait.
  • pid > 0 Waits for the child whose process ID equals pid.
  • pid == 0 Waits for any child whose process group ID equals that of the calling process.
  • pid < −1 Waits for any child whose process group ID equals the absolute value of pid

exec

1
2
3
4
5
6
7
8
9
10
#include <unistd.h>
int execl(const char *pathname, const char *arg0, ... /* (char *)0 */ );
int execv(const char *pathname, char *const argv[]);
int execle(const char *pathname, const char *arg0, ...
/* (char *)0, char *const envp[] */ );
int execve(const char *pathname, char *const argv[], char *const envp[]);
int execlp(const char *filename, const char *arg0, ... /* (char *)0 */ );
int execvp(const char *filename, char *const argv[]);
int fexecve(int fd, char *const argv[], char *const envp[]);
All seven return: −1 on error, no return on success

l stands for list, v stands for vector, e stands for environment
The functions execl, execlp, and execle require each of the command-line arguments to the new program to be specified as separate arguments. We mark the end of the arguments with a null pointer.
For the other four functions (execv, execvp, execve, and fexecve), build an array of pointers to the arguments, and the address of this array pass to these three functions.
exec

Process schedule

1
2
3
4
5
6
7
8
#include <unistd.h>
int nice(int incr);
#include <sys/resource.h>
int getpriority(int which, id_t who);
Returns: new nice value − NZERO if OK, −1 on error
#include <sys/resource.h>
int setpriority(int which, id_t who, int value);
Returns: 0 if OK, −1 on error

which : PRO_PROCESS , PRO_GROUP, PRO_USER