星空网 > 软件开发 > 操作系统

UNIX高级环境编程(11)进程控制(Process Control)

  

1 进程快照(Process Accounting)

当一个进程终止时,内核会为该进程保存一些数据,包括命令的小部分二进制数据、CPU time、启动时间、用户Id和组Id。这样的过程称为process accounting,本篇译为进程快照。

函数acct可以打开或关闭进程快照功能。

负责记录快照的数据结构如下所示:

UNIX高级环境编程(11)进程控制(Process Control)images/loading.gif' data-original="http://images.cnitblog.com/blog/526303/201504/011051378104502.png" width="599" height="395" border="0" />

成员说明:

  • ac_flag成员记录进程执行过程中的特定事件(稍后的表中会详细说明);
  • 进程创建时,初始化进程快照的数据在进程表(process table)中,但是只有在进程终止才会写进程快照。这样的机制导致了两个结果:
    • 如果进程不终止,我们无法获取进程快照
    • 进程快照文件的顺序依赖于进程终止的顺序,而不是进程被创建的顺序
  • 进程快照对应一个进程,而不是程序。进程快照在进程被创建(fork)的时候被初始化,而不是程序被执行时,所以如果我们有一个程序链:A execs B, B execs C然后C退出,则只有一个进程快照被保存。命令的名字是程序的C决定,而CPU时间这类信息则是ABC的总和。

ac_flag不同的取值代表的事件含义如下表所示:

UNIX高级环境编程(11)进程控制(Process Control)

 

2  用户标识符(User Identification)

进程可以知道它的真实用户和组(real user ID and group ID),有效用户和组(effective user ID and group ID)。

然后有时候我们想知道运行程序的登录用户名。函数getpwuid可以实现。

函数声明:

#include <unistd.h>

char* getlogin(void);

        // Returns: pointer to string giving login name if OK, NULL on error

函数细节:

  • 如果进程并没有和一个终端关联,即没有一个登录用户,则该函数失败,不和终端关联的进程被称为daemon
  • 有了登录用户名,我们可以在password文件中找到该用户,然后确定登录终端,例如使用getpwnam函数。

 

3 进程调度(Process Scheduling)

nice值决定了进程的运行优先级。

取值范围:0 ~ (2 * NZERO -1)

nice值越低,优先级越高。

意思就是:进程越好,进程的优先级越高。

nice值的默认值为NZERO。

函数nice可以获取并修改nice值。

进程调用nice函数只能修改它自己的nice值。

函数声明:

#include <unistd.h>

int nice(int incr);

    // Returns: new nice value - NZERO if OK, -1 on error

函数说明:

  • 函数的作用是将当前进程的nice值加上参数incr作为新的nice值。
  • 如果incr的值太大,则系统会自动将结果调整为最大的有效值;
  • 如果incr的值太小,则系统会自动将结果调整为最小的有效值,即-1。
  • 因为-1是一个合法的返回值,所以判断nice函数是否成功调用需要同时判断函数返回值和errno。在调用nice函数之前将errno清0。如果返回值为-1,errno为0.则调用成功,如果返回值和errno都为-1,则调用失败。

函数getpriority可以用来获取一个进程的nice值。

函数声明:

#include <sys/resource.h>

int getpriority(int which , id_t who);

    // Returns: nice value between -NZERO和NZERO-1之间 if OK, -1 on error

 参数说明:

which:有三个可取值:PRIO_PROCESS表明是一个进程,PRIO_PGRP表明是一个进程组,PRIO_USER表明目标是一个用户。

which参数的作用是控制who的含义,以及who参数如何选择进程。

如果who取值为0,则函数返回当前调用进程、进程组或用户的nice值。

如果which取值为PRIO_USER,who取值0,则返回当前进程的真实用户ID(real user Id)。

如果which取值为PRIO_GROUP,则返回所有进程中priority最高,即nice值最低的进程的nice值。

 

设置nice值使用函数setpriority。

函数声明:

 #include <sys/resource.h>

int setpriority(int which, id_t who, int value);

 参数的作用和getpriority相同。

value的值被加到nice默认值NZERO上,设置该值为新的nice值。

 

4 进程时间(Process Times)

进程时间包括:墙上时间(wall clock tie),用户CPU时间(user CPU time),内核CPU时间(system CPU time).

函数times可以获取这三种时间。

函数声明:

#include <sys/times.h>

clock_t times(struct tms *buf);

函数细节:

函数会向结构体struct tms类型的变量中填充上面的三个时间,struct tms结构体的声明如下:

UNIX高级环境编程(11)进程控制(Process Control)

墙上时间为该函数的返回值。墙上时间为从过去某个时间点到现在的时间,所以不能直接作为时间来使用。

大多情况下,我们可以通过times的返回值来计算相对时间,例如,在进程开始和结束时分别调用times函数,然后相减来计算进程的运行时间。

两个子进程时间变量用来保存父进程调用wait函数等待子进程退出的时间。

 

Example

程序功能:执行命令行传入的命令,计算每个命令执行的时间,并打印结构体tms中的成员值。

code

#include "apue.h"

#include <sys/times.h>

 

static void pr_times(clock_t, struct tms *, struct tms *);

staticvoid do_cmd(char *);

          

int      

main(int argc, char *argv[])

{

    int     i;

 

    setbuf(stdout, NULL);

    for (i = 1; i < argc; i++)

        do_cmd(argv[i]);    /* once for each command-line arg */

    exit(0); 

}

 

staticvoid 

do_cmd(char *cmd)       /* execute and time the "cmd" */

{   

    struct tms  tmsstart, tmsend;

    clock_t     start, end;

    int         status;

 

    printf("\ncommand: %s\n", cmd);

 

    if ((start = times(&tmsstart)) == -1)   /* starting values */

        err_sys("times error");

 

    if ((status = system(cmd)) < 0)         /* execute command */

        err_sys("system() error");

 

    if ((end = times(&tmsend)) == -1)       /* ending values */

        err_sys("times error");

 

    pr_times(end-start, &tmsstart, &tmsend);

    pr_exit(status);

}

 

staticvoid

pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)

{

    static long     clktck = 0;

 

 

    if (clktck == 0)    /* fetch clock ticks per second first time */

        if ((clktck = sysconf(_SC_CLK_TCK)) < 0)

            err_sys("sysconf error");

 

    printf("  real:  %7.2f\n", real / (double) clktck);

    printf("  user:  %7.2f\n",

      (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);

    printf("  sys:   %7.2f\n",

      (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck);

    printf("  child user:  %7.2f\n",

      (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);

    printf("  child sys:   %7.2f\n",

      (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);

}

 
运行结果:

UNIX高级环境编程(11)进程控制(Process Control)

 

小结

进程控制章节介绍了以下内容:

  • fork
  • exec
  • _exit
  • wait和waitpid
  • system
  • 解释器文件(interpreter files)和他们的工作原理
  • 进程时间

下一章我们将会学习到进程关系(process relationship),关键字:sessions,job control。

 

参考资料:

《Advanced Programming in the UNIX Envinronment 3rd》

 




原标题:UNIX高级环境编程(11)进程控制(Process Control)

关键词:进程

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流