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)

转载于:https://www.cnblogs.com/bo1990/p/11385432.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值