UNIX advanced environment programming (11) Process Control-Process snapshot, User Identifier, Process scheduling, unixprocess

Source: Internet
Author: User

UNIX advanced environment programming (11) Process Control-Process snapshot, User Identifier, Process scheduling, unixprocess

1. Process Accounting)

When a process is terminated, the kernel saves some data for the process, including a small portion of the binary data, CPU time, startup time, user Id, and group Id of the command. This process is called process accounting.

The acct function enables or disables the process snapshot function.

The data structure that records snapshots is as follows:

Member description:

  • Ac_flag members record specific events during process execution (detailed descriptions will be provided later );
  • When a process is created, the data that initializes the process snapshot is in the process table, but the process snapshot is written only when the process is terminated. This mechanism leads to two results:
    • If the process is not terminated, we cannot obtain the process snapshot.
    • The order of process snapshot files depends on the order of Process Termination, rather than the order in which processes are created.
  • A process snapshot corresponds to a process, not a program. Process snapshots are initialized when A process is created (fork), rather than when the program is executed. Therefore, if we have A program chain: A execs B, B execs C, and C exits, only one process snapshot is saved. The command name is determined by program C, and the information such as CPU time is the sum of ABC.

The event meanings represented by different values of ac_flag are shown in the following table:

2. User Identification)

A process can know its real users and groups, valid users and groups ).

Then sometimes we want to know the login username of the running program. The getpwuid function 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 login user, the function fails. A process not associated with the terminal is called daemon.
  • With the logon username, we can find the user in the password file and then confirm the logon terminal, for example, using the getpwnam function.

 

3. Process Scheduling)

The nice value determines the priority of the process.

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

The lower the nice value, the higher the priority.

The better the process, the higher the priority of the process.

The default nice value is NZERO.

The nice function can get and modify the nice value.

A process can only modify its own nice value when calling the nice function.

Function declaration:

#include <unistd.h>

int nice(int incr);

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

Function Description:

  • The function adds the nice value of the current process with the incr parameter as the new nice value.
  • If the value of 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, that is,-1.
  • Because-1 is a valid return value, you must determine whether the nice function is successfully called and whether the function return value and errno are both returned. Before calling the nice function, clear errno to 0. If the returned value is-1 and errno is 0, the call is successful. If both the returned value and errno are-1, the call fails.

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

Function declaration:

# Include <sys/resource. h>

Int getpriority (int which, id_t who );

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

Parameter description:

Which: three optional values: PRIO_PROCESS indicates a process, PRIO_PGRP indicates a process group, and PRIO_USER indicates that the target user.

The which parameter controls the meaning of who and how to select a process.

If the value of who is 0, the function returns the nice value of the currently called process, process group, or user.

If the value of which is PRIO_USER and the value of who is 0, the real user Id of the current process is returned ).

If the value of which is PRIO_GROUP, the nice value of the process with the highest priority is returned, that is, the nice value of the process with the lowest nice value.

 

Set the nice value using the setpriority function.

Function declaration:

 #include <sys/resource.h>

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

The function of the parameter is the same as that of getpriority.

The value of the value is added to the default value of nice NZERO. Set this value to the new nice value.

 

4. Process Times)

The process time includes wall time (wall clock tie), user CPU time (user CPU time), and kernel CPU time (system CPU time ).

Function times can obtain these three time periods.

Function declaration:

#include <sys/times.h>

clock_t times(struct tms *buf);

Function details:

The function will fill in the above three times to the variable of the struct tms type. The declaration of the struct tms struct is as follows:

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

In most cases, we can use the return value of times to calculate the relative time. For example, when the process starts and ends, the times function is called separately and then subtract to calculate the running time of the process.

The two sub-process time variables are used to save the time when the parent process calls the wait function to wait for the sub-process to exit.

 

Example

Program function: Execute the command passed in by the command line, calculate the execution time of each command, and print the struct tms member value.

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 );

}

Running result:

 

Summary

The process control section describes the following:

  • Fork
  • Exec
  • _ Exit
  • Wait and waitpid
  • System
  • Interpreter files and how they work
  • Process Time

In the next chapter, we will learn the process relationship Keyword: sessions and job control.

 

References:

Advanced Programming in the UNIX Envinronment 3rd

 

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.