• [08]APUE:进程控制


    [a] getpid / getppid / getuid / geteuid / getgid / getegid

    #include <unistd.h>
    pid_t getpid(void)
    pid_t getppid(void)
    uid_t getuid(void)
    uid_t geteuid(void)
    gid_t getgid(void)
    gid_t getegid(void
    • 获取当前进程的进程id、父进程id、实际用户id、有效用户id、实际组id、有效组id 

    [b] fork

    #include <unistd.h>
    pid_t fork(void) //若成功,子进程返回 0,父进程返回子进程 ID;若出错,返回 -1 
    • 父子进程的执行没有固定的先后順序,需要手动控制
    • 子进程不继承父进程设置的文件锁
    • 子进程的未处理闹钟被清除
    • 子进程的未处理信号集设置为空集

    [c] wait / waitpid

    #include <sys/wait.h>
    pid_t wait(int *wstatus)
    pid_t waitpid(pid_t pid, int *wstatus, int options)
    //成功返回进程 ID,出错返回 -1 或 0(非阻塞返回值) 
    • 子进程终止状态写入 wstatus,可使用 WIFEXITED(wstatus)、WIFSIGNALED(wtatus) 等宏提取子进程的退出状态编号,前者返回非 0 表示正常终止,后者返回非 0 表示异常终止
    • 任一子进程终止将使 wait 返回,无子进程终止将阻塞
    • waitpid 可用于等待指定 ID 的子进程,options 常用取值为 WNOHNAG ,表示非阻塞,若子进未终止,立即返回,此时返回值为 0

    [d] execve / fexecve

    #include <unistd.h>
    int execve(const char *path, char *const argv[], char *const envp[])
    int fexecve(int fd, char *const argv[], char *const envp[])
    //成功无返回值,出错返回 -1 
    • argv 是以 结尾的字符串数组,内含可执行程序的参数,按惯例,首个参数通常设置为程序名称,可设置为其它值如 NULL 
    • envp 是以 结尾的字符串数组,内含需要指定的环境变量键值对(name=value)
    • 其它可用的 exec 类函数有:execl / execlp /execv /execvp,exec*p 类函数只需要可执行文件名称,无须指定绝对路径

    [e] setuid / setgid / seteuid / setegid

    #include <unistd.h>
    int setuid(uid_t uid)
    int setgid(gid_t gid)
    int seteuid(uid_t uid)
    int setegid(gid_t gid)
    //成功返回 0,出错返回 -1 
    • 特权进程将 real-id、effective-id、saved-set-user/grp-id 等三项均设置为参数指定的 ID,非特权进程只设置有效用户ID或有效组ID
    • sete*id 函数只设置有效用户ID或有效组ID

    [f] system

    #include <stdlib.h>
    int system(const char *cmdstring)
    //成功返回 SHELL 的终止状态,若出错,不同执行阶段有不同的返回值,fork 出错返回 -1,exec* 出错返回 127 
    • 通常仅用于执行简单的无参数命令,如:date  

    [g] getlogin

    #include <unistd.h>
    char *getlogin(void)
    //成功返回指向登陆名称的字符串指针,出错返回 NULL 
    • 鉴于同一个用户 ID 可能存在多个登陆名称(配置不同的 shell),应使用 getlogin 函数提取当前登陆的用户名 

    [h] nice / getpriority / setpriority

    #include <unistd.h>
    int nice(int incr) //成功返回新的 nice 值(-NZERO~NZERO -1),出错返回 -1
    #include <sys/resource.h>
    int getpriority(int which, id_t who) //成功返回 新的 nice 值(-NZERO~NZERO -1),出错返回 -1
    int setpriority(int which, id_t who, int value) //成功返回 0,出错返回 -1 
    • nice 只作用调用进程本身,不能更改其它进程的 nice 值,返回的新 nice 值是参数 incr 与 原 nice 值之和,若新值超过上限或下限,则自动设置为 -NZERO 或 NZERO
    • setpriority 可修改其它进程的 nice 值,which 参数可选的值有 PRIO_PROCESS / PRIO_PGRP / PRIO_USER,who 参数的含义由 which 参数决定;若 who 为 0,则使用调用进程自身的信息;value 参数与原 nice 值之和即新 nice 的值

    [i] times

    #include <sys/times.h>
    clock_t times(struct tms *tp) //成功返回 Wall time(以 cpu tick 为单位),出错返回 -1 
    struct tms {
        clock_t    tms_utime; //用户 CPU 时间
        clock_t    tms_stime; //系统 CPU 时间
        clock_t    tms_cutime; //包含调用进程及其 wait 子进程所消耗的用户 CPU 时间
        clock_t    tms_cstime; //包含调用进程及其 wait 子进程所消耗的系统 CPU 时间
    • 进程开始及结束时各运行一次,取函数返回值及 tms 结构体内各项的时间差
    • 需要使用 sysconf(_SC_CLK_TCK) 的返回值,将结果转换成以秒为单位
    • 类似函数:getrusage
  • 相关阅读:
    【SQLite】教程04-SQLite数据类型
    【SQLite】教程03-SQLite语法
    【SQLite】教程02-SQLite命令
    [原创]java WEB学习笔记22:MVC案例完整实践(part 3)---多个请求对应一个Servlet解析
    [原创]java WEB学习笔记21:MVC案例完整实践(part 2)---DAO层设计
    [原创]java WEB学习笔记20:MVC案例完整实践(part 1)---MVC架构分析
    [转]Mysql命令
    [原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题
    [原创]java WEB学习笔记18:java EE 中的MVC 设计模式(理论)
    [原创]java WEB学习笔记17:关于中文乱码的问题 和 tomcat在eclipse中起动成功,主页却打不开
  • 原文地址:https://www.cnblogs.com/hadex/p/6128434.html
Copyright © 2020-2023  润新知