基本概念
fork 和 vfork
exit
wait 函数
竞态
exec 函数以及脚本解释器
system 函数
进程记帐
用户和组标识符
进程时间
=============================================================================== #include <unistd.h> pid_t getpid (void); pid_t getppid (void); ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> uid_t getuid (void); uid_t geteuid (void); uid_t getgid (void); uid_t getegid (void); ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> pid_t fork (void); ------------------------------------------------------------------------------- EXAMPLE: /* 派生子进程 */ #include <sys/types.h> #include <unistd.h> void err_sys (const char* info) { perror (info); exit (1); } int main (void) { pid_t pid; if ( (pid = fork ()) < 0) err_sys ("fork error"); else if (pid > 0) { // this is parent. } else { // this is child. } exit (0); } ------------------------------------------------------------------------------- * fork 之后, 子进程和父进程共享打开的文件. 这是实现管道等 IPC 机制的基础. * Linux 利用按需分页 (paging on demand) 和写时复制 (copy-on-write) 方式派生子进程. ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/types.h> pid_t vfork (void); ------------------------------------------------------------------------------- * 不复制父进程的全部地址空间. * vfork 保证子进程首先运行, 直到调用 exec 或 exit 之后, 父进程才能得到运行机会. ===============================================================================
=============================================================================== #include <stdlib.h> void exit (int status); #include <unistd.h> void _exit (int status); ===============================================================================
=============================================================================== #include <sys/types.h> #include <sys/wait.h> pid_t wait (int *status) pid_t waitpid (pid_t pid, int *status, int options); ------------------------------------------------------------------------------- * wait 等待某个子进程退出. * waitpid 可等待指定的子进程退出, 可指定 WNOHANG 选项而不阻塞调用进程的执行. ------------------------------------------------------------------------------- 用来帮助判断退出状态的宏: WIFEXITED (status) is non-zero if the child exited normally. WEXITSTATUS (status) evaluates to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call to exit() or as the argument for a return state- ment in the main program. This macro can only be evaluated if WIFEXITED returned non-zero. WIFSIGNALED (status) returns true if the child process exited because of a signal which was not caught. WTERMSIG (status) returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero. WIFSTOPPED (status) returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED. WSTOPSIG (status) returns the number of the signal which caused the child to stop. This macro can only be evaluated if WIFSTOPPED returned non-zero. ===============================================================================
=============================================================================== #define _USE_BSD #include <sys/types.h> #include <sys/resource.h> #include <sys/wait.h> pid_t wait3 (int *status, int options, struct rusage *rusage) pid_t wait4 (pid_t pid, int *status, int options, struct rusage *rusage) ------------------------------------------------------------------------------ * wait3 和 wait4 分别是 wait 和 waitpid 的扩展调用, 它们可以返回子进程的 资源使用情况. ===============================================================================
=============================================================================== #include <unistd.h> extern char **environ; int execl ( const char *path, const char *arg, ...); int execlp ( const char *file, const char *arg, ...); int execle ( const char *path, const char *arg, ..., char * const envp[]); int execv ( const char *path, char *const argv[]); int execvp ( const char *file, char *const argv[]); int execve (const char *filename, char *const argv [], char *const envp[]); ------------------------------------------------------------------------------ * 以可变参数形式传递命令行时, 最后一个参数应该为 NULL 指针. * execve 是实际的系统调用, 而其他的函数以库函数实现, 最终要调用 execve. ===============================================================================
=============================================================================== #include <unistd.h> char * getlogin ( void ); #include <stdio.h> char * cuserid ( char *string ); ===============================================================================
=============================================================================== #include <unistd.h> #include <sys/times.h> clock_t times (struct tms *buf); ------------------------------------------------------------------------------- struct tms { clock_t tms_utime; /* user time */ clock_t tms_stime; /* system time */ clock_t tms_cutime; /* user time of children */ clock_t tms_cstime; /* system time of children */ }; ------------------------------------------------------------------------------- * times 返回的时间均以系统启动以来的时钟滴答计算. ===============================================================================