UNIX Advanced Environment Programming (11) Process Control-process snapshot, user identifier, process scheduling

Source: Internet
Author: User
Tags terminates

1 Progress Snapshot (process Accounting)

When a process terminates, the kernel saves some data for the process, including a small portion of the command's binary data, CPU time, startup times, user ID, and group ID. Such a process is called process accounting, which is translated into a snapshot of processes.

The function acct can turn the process snapshot feature on or off.

The data structure responsible for recording the snapshot is as follows:

Member Description:

    • Ac_flag members record specific events during process execution (detailed in the table later);
    • When a process is created, the data that initializes the process snapshot is in the process table, but the process snapshot is not written until the process terminates. Such a mechanism resulted in two results:
      • We cannot get the process snapshot if the process does not terminate
      • The order of process snapshot files depends on the order in which the processes are terminated, not the order in which the processes are created
    • A process snapshot corresponds to a process, not a program. The process snapshot is initialized when the process is created (fork), not when the program is executed, so if we have a program chain: a execs B, B execs C and then C exits, only one process snapshot is saved. The name of the command is the C decision of the program, and the CPU time information is the sum of ABC.

Ac_flag different values represent the meaning of the event as shown in the following table:

2 user identifier (identification)

The process can know its real users and groups (real user ID and group ID), valid Users and groups (effective user ID and group ID).

Then sometimes we want to know the login user name of the running program. Function Getpwuid can be implemented.

function declaration:

#include <unistd.h>

char* getlogin (void);

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

Function Details:

    • If the process is not associated with a terminal, that is, there is no logged-on user, the function fails, and the process that is not associated with the terminal is called daemon
    • With the login user name, we can find the user in the password file and then determine the login terminal, for example, using the Getpwnam function.

3 Progress scheduling (process scheduling)

The nice value determines the running priority of the process.

Value range: 0 ~ (2 * NZERO-1)

The lower the Nice value, the higher the priority.

This means: the better the process, the higher the priority of the process.

The default value for the nice value is Nzero.

function Nice can get and modify nice values.

The process calls the Nice function to modify its own nice value only.

function declaration:

#include <unistd.h>

int nice (int incr);

Returns:new Nice Value-nzero If OK,-1 On Error

Function Description:

    • The function is to add the nice value of the current process with the parameter incr as the new nice value.
    • If the value of the incr is too large, the system automatically adjusts the result to the maximum valid value;
    • If the value of incr is too small, the system automatically adjusts the result to the minimum valid value, which is-1.
    • Since 1 is a valid return value, it is necessary to judge the function return value and errno when the nice function is successfully called. Clear the errno 0 before calling the nice function. If the return value is -1,errno to 0, the call succeeds and the call fails if both the return value and errno are 1.

The function getpriority can be used to get the nice value of a process.

function declaration:

#include <sys/resource.h>

int getpriority (int which, id_t who);

Returns:nice value between Between-nzero and NZERO-1 if OK,-1 On Error

Parameter description:

Which: there are three desirable values: Prio_process indicates a process, PRIO_PGRP indicates a process group, and Prio_user indicates that the target is a user.

The role of the which parameter is to control who's meaning and how the WHO parameter chooses the process.

If the WHO value is 0, the function returns the Nice value of the currently calling process, process group, or user.

If the which value is prio_user,who 0, the real user ID of the current process is returned.

If which is prio_group, it returns the Nice value of the process with the highest priority in all processes, the least nice value.

Set the nice value using the function setpriority.

function declaration:

#include <sys/resource.h>

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

The function of the parameter is the same as getpriority.

Value is added to the Nice default value of Nzero, and the value is set to the new nice value.

4 Progress time (process times)

The process time includes: The Wall time (wall clock tie), the user CPU time, the kernel CPU time (System CPU times).

The function times can get these three kinds of time.

function declaration:

#include <sys/times.h>

clock_t times (struct TMS *buf);

Function Details:

The function populates the three-time variable in the struct-struct-TMS type with the declaration of the struct TMS struct as follows:

The time on the wall is the return value of the function. The wall time is the time from the past to the present, so it cannot be used directly as time.

In most cases, we can calculate the relative time by The Times return value, for example, call the Times function at the beginning and end of the process, and subtract to calculate the run time of the process.

Two child process time variables are used to save the time that the parent process calls the wait function to wait for the child process to exit.

Example

Program functions: Executes command-line incoming commands, calculates the time each command executes, and prints the member values in the struct-body 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);

}

Operation Result:

Summary

The Process Control section describes the following:

    • Fork
    • Exec
    • _exit
    • Wait and Waitpid
    • System
    • Interpreter file (interpreter files) and how they work
    • Process time

In the next chapter we will learn about process relationship, keyword: sessions,job control.

Resources:

"Advanced programming in the UNIX envinronment 3rd"

UNIX Advanced Environment Programming (11) Process Control-process snapshot, user identifier, process scheduling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.