UNIX進階環境編程(11)進程式控制制(Process Control)- 進程快照,使用者識別碼,進程調度,unixprocess

來源:互聯網
上載者:User

UNIX進階環境編程(11)進程式控制制(Process Control)- 進程快照,使用者識別碼,進程調度,unixprocess

  

1 進程快照(Process Accounting)

當一個進程終止時,核心會為該進程儲存一些資料,包括命令的小部分位元據、CPU time、啟動時間、使用者Id和組Id。這樣的過程稱為process accounting,本篇譯為進程快照。

函數acct可以開啟或關閉進程快照功能。

負責記錄快照的資料結構如下所示:

成員說明:

  • ac_flag成員記錄進程執行過程中的特定事件(稍後的表中會詳細說明);
  • 進程建立時,初始化進程快照的資料在進程表(process table)中,但是只有在進程終止才會寫進程快照。這樣的機制導致了兩個結果:
    • 如果進程不終止,我們無法擷取進程快照
    • 進程快照檔案的順序依賴於進程終止的順序,而不是進程被建立的順序
  • 進程快照對應一個進程,而不是程式。進程快照在進程被建立(fork)的時候被初始化,而不是程式被執行時,所以如果我們有一個程式鏈:A execs B, B execs C然後C退出,則只有一個進程快照被儲存。命令的名字是程式的C決定,而CPU時間這類資訊則是ABC的總和。

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

 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結構體的聲明如下:

牆上時間為該函數的傳回值。牆上時間為從過去某個時間點到現在的時間,所以不能直接作為時間來使用。

大多情況下,我們可以通過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);

}

 運行結果:

 

小結

進程式控制制章節介紹了以下內容:

  • fork
  • exec
  • _exit
  • wait和waitpid
  • system
  • 解譯器檔案(interpreter files)和他們的工作原理
  • 進程時間

下一章我們將會學習到進程關係(process relationship),關鍵字:sessions,job control。

 

參考資料:

《Advanced Programming in the UNIX Envinronment 3rd》

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.