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
12345#include <stdlib.h>void exit(int status);void _Exit(int status);#include <unistd.h>void _exit(int status);
_exitand_Exit, whichreturnto the kernelimmediately, andexit, whichperforms certain cleanup processingand then returns to the kernel.
- atexit()123#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
the environment strings are typically stored at the top
of a process’s memory space, above the stack.
chapter eight
- pid_t12345678910111213#include <unistd.h>pid_t getpid(void);Returns: process ID of calling processpid_t getppid(void);Returns: parent process ID of calling processuid_t getuid(void);Returns: real user ID of calling processuid_t geteuid(void);Returns: effective user ID of calling processgid_t getgid(void);Returns: real group ID of calling processgid_t getegid(void);Returns: effective group ID of calling process
fork
- 调用一次,返回两次,子进程返回0, 父进程返回子线程pid
- 子进程只保留调用fork 的线程副本
- 父进程中所有打开的文件描述符被复制到子进程
- 子进程不集成父进程的文件锁,未处理闹钟清除,未处理信号集置空
- 父进程先于子进程结束,子进程被init进程收养,其父进程ID为1
- 已经结束但未被父进程处理的进程:僵死进程
wait waitpid
|
|
- wait or waitpid do
•Block, ifall of its children are still running
•Return immediatelywith the termination status ofa child, if a child has terminated and is waiting for its termination status to be fetched
•Return immediately with an error, if itdoesn’t have any childprocesses- 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 childwhose processgroup IDequals that of the calling process.- pid < −1 Waits for
any childwhose processgroup IDequals the absolute value of pid
exec
|
|
lstands forlist,vstands forvector,estands forenvironment
The functionsexecl, execlp, and execlerequireeach of the command-line argumentsto the new program to be specified as separate arguments. Wemark the end of the argumentswitha null pointer.
For the other four functions (execv, execvp, execve, and fexecve), build an array of pointers to the arguments, andthe address of this arraypass to these three functions.
Process schedule
|
|
which : PRO_PROCESS , PRO_GROUP, PRO_USER