Linux之进程处理函数

Linux的进程处理函数需包含头文件:#include <unistd.h>

一、进程函数

1 进程创建

  • pid_d fork(void);

2 进程终止:只有exit才会先清理I/O再进入内核,所以输出缓冲中的所有数据都会被写入到文件中,等同于return 0

  • void exit(int status); ---<stdlib.h>
  • void _Exit(int status);---<stdlib.h>
  • void _exit(int status);

3 进程等待---<sys/wait.h>

  • pid_t wait(int *statoc); //一直阻塞,直到任意一个子进程终止后则立即返回
  • pid_t waitpid(pid_t pid, int *statloc, int options); 

pid == -1:和wait等效;

pid > 0:等待进程ID为pid的子进程终止;

pid == 0:等待组ID为调用进程组ID的任意一个子进程终止;

pid < -1:等待组ID为pid绝对值的任意一个子进程终止;

4 进程取消---<stdlib.h>

  • void abort(void);

5 获取进程ID

  • pid_t getpid(void);---进程ID
  • pid_t getppid(void);---父进程ID
  • pid_t getuid(void);---实际用户ID
  • pid_t geteuid(void);---有效用户ID
  • pid_t getgid(void);---实际组ID
  • pid_t getegid(void);---有效组ID

(1)实际用户ID:标识谁在执行进程即登录用户的ID;

(2)有效用户ID:如果可执行文件没有设置用户ID位,则是实际用户ID,否则是文件所有者的ID

(3)实际组ID和有效组ID:和上面类似;

5 exec函数系列:fork创建子进程后,子进程往往会调用一种exec函数来执行另一个程序(从main函数开始,exec并不创建新进程,只是用新程序替换了当前进程的代码段、数据段、堆段和栈段),说明如下:

(1)参数1:路径名、文件名、文件描述符;

(2)l表示列表list(逐个列举参数),v表示矢量vector(所有参数整体构造成一个NULL结尾的指针数组),e表示环境变量environment(NULL结尾的指针数组),p表示根据PATH查找文件名,f表示文件描述符fd;

  • int execl(const char *pathname, const char *arg0, ......);
  • int execv(const char *pathname, char *const argv[]);
  • int execle(const char *pathname, const char *arg0, ......);
  • int execve(const char *pathname, char *const argv[], char *const envp[]); ///系统调用,其余6个都是库函数
  • int execlp(const char *filename, const char *arg0, ......);
  • int execvp(const char *filename, char *const argv[]);
  • int fexecve(int fd, char *const argv[], char *const envp[]);

7个函数分为四组:l和v只能选其一,(execl/exec)(execle/execve)(execlp/execvp)(fexecve)

posted @ 2019-08-20 20:31  博1990  阅读(444)  评论(0编辑  收藏  举报