Linux Process Control notes

Source: Internet
Author: User
Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://noyear.blog.51cto.com/819034/405844

1. Description of real and valid users and user groups:
The Group principle is similar to that described by users only.
Real users are the digital representation of the user names you use when logging on. It won't happen during your login Period
Being Changed is your essential quality.
A valid user is a user with another permission when executing a program. For example,
Shi, his real identity is usually a Shi, but when he wants to exercise a law beyond his own permissions, such
Punish an officer higher than him. At this moment, if he has a Shang fangbao sword (with the power of the Emperor), this
He is the Emperor. However, this valid identity is the same as his real identity.

Real user ID: getuid (void );
Valid user ID: geteuid (void );
Real user group ID: getgid (void );
Valid user group ID: getegid (void );
2. Obtain information about users and user groups.
The getlogin function returns the login name of the user executing the program. Then the login name can be used as a parameter
Pass to the getpwname function to return a complete line of information in the/etc/passwd file corresponding to the login name
. The other method is to pass the UID of the process to the getpwuid function, and also return to the/etc/passwd file.
A suitable entry.
# Include <unistd. h>
Char * getlogin (void );

# Include <PWD. h>
Struct passwd * getpwnam (const char * Name );
3. Three independent time values of Linux Processes
Wall Clock Time (wall clock time) is the elapsed time
User CPU time (User CPU time) is the process spent on the execution of user mode (non-kernel mode) Code
Total time
System CPU time is always the time spent on executing kernel code.
This information can be obtained through times or getrusage.
# Include <sys/times. h>
Clock_t times (struct TMS * BUF );
Returns the clock time on the wall. The Buf saves the time of the current process.
The TMS structure defined in <sys/times. h>
Struct TMS {
Clock_t tms_utime; // user CPU time
Clock_t tms_stime; // system CPU time
Clock_t tms_cutime; // user CPU time of chilren
Clock_t tms_cstime; // system CPU time of chilren.
The value of clock_t is the number of clock tick messages. to convert it to seconds, you must use the sysconf function to convert it.
_ SC _clk_tck is a macro that defines the number of tick answers per second.

4. Resource utilization information
Use the getrusage Function
# Include <sys/times. h>
# Include <sys/resource. h>
# Include <unistd. h>
Int getrusage (INT who, struct rusage * usage );
The focus here is on usage, while the WHO parameter can only be set to rusage_self or rusage_children, indicating
Whether to return the resource utilization information of the calling process or the resource utilization information of the sub-process. Here include
The following describes the rusage structure of <sys/resource. h>:
Struct rusage {
Struct timeval ru_utime; // user time used
Struct timeval ru_sutime; // system time used
Long ru_maxrss; // maximum resident SET SIZE
Long ru_maxixrss; // shared memory size
Long ru_maxidrss; // unshared data dize
Long ru_maxisrss; // unshared stack size
Long ru_minflt; // page reclaims
Long ru_majflt; // page faults
Long ru_nswap; // swaps
Long ru_inblock; // block input operations
Long ru_outblock; // block output operations
Long ru_msgsnd; // message sent
Long ru_msgrcv; // message received ed
Long ru_nsignals; // siganls encoded ed
Long ru_nvcsw; // voluntary contex Switches
Long ru_nivcsw; // involuntary contex Switches
}

Description
However, Linux only supports resources:
Ru_utime -- user mode (non-kernel) Code Time
Ru_stime -- kernel code time
Ru_minflt -- secondary invalidation count (access to memory without causing disk access)
Ru_majflt -- primary number of failures (disk access caused by memory access)

5. Concepts of sessions and processes
In some cases, the process cannot be simply summarized by the parent and child. If several processes are involved in pipelines,
At this time, these related processes become Process Groups and can be controlled in a unified manner (such as killing ). One Process
A process group is a collection of related processes.
Sequence. All processes in the process group have the same process group number, that is, pgid. Use the Process Group
To facilitate business control. For example, assume that you have run the command line pipeline LS-L/usr/include |
Sort | WC-L. If you want to kill it while it is still running (press Ctrl + C), the shell needs to terminate all
. It does this by killing the process group rather than every process.
A session is composed of one or more process groups. A session leader process is
The process of creating a session. Each session has a unique ID, called a session ID, which is only a session
Lead process PID. The role of a session on a process group is the same as that of a process group on a single process. Convenient
Control.

6, fork
# Include <unistd. h>
Pid_t fork (void );
The child process PID is returned to the parent process and 0 is returned to the child process.
The sub-process has the same real and valid uid, GID, process group and session ID, environment, and resource limits as the parent process.
Open files and shared memory segments. However, the child process does not inherit the timeout settings of the parent process (Alarm calls
), The file lock created by the parent process, pending signals.
7. Exec function family
I used to take notes and review them again.
# Include <unistd. h>
Int execl (const char * path, const char * Arg ,...);
Int execlp (const char * file, const char * Arg ,...);
Int execle (const * path, const char * Arg, char * cont envp []);
Int execv (const char * path, char * const argv []);
Int execve (const char * path, const argv [], char * const envp []);
Int execvp (const char * file, char * const argv []);

Related functions that control the environment in which a process runs:
# Include <stdlib. h>
Int utenv (const char * string );
Char * getenv (const char * Name );
Description: getenv searches for the environment variable named name and returns a pointer to its value. If no value is found
Returns null. Putenv adds or modifies the "name = value" pair specified in the string. If it runs
0 is returned. If the execution fails,-1 is returned.

8. popen Function
It is similar to the system function, but it is an external process that can be executed without using fork and exec.
Sort in sequence. Unlike system, popen uses pipelines to work. Prototype
# Include <stdio. h>
File * popen (const char * command, const char * type );
Int pclose (File * stream );
Note: popen calls the MPs queue and creates a program or script that passes standard input or specified by command.
But not both. The second parameter type is used when reading the stdout of the MPs queue.
R. When writing stdin, W. It and I/O usage is a little contrary to intuition. Write is relative to command
So the command output is read from stdout. To input to command, you need to input to its stdin
Write.
9. Wait for the process-Wait function family
There are only two functions
# Include <sys/Wait. H>
# Include <sys/types. h>
Pid_t wait (int * status );
Pid_t waitpid (pid_t PID, int * status, int options );
Status stores the exit status of the process, while the PID in waitpid may take the following values:
-1. Wait for any sub-process whose pgid = absolute PID Value
1. Wait for any sub-process
0 wait for any pgid = sub-process that calls the process
> 0: subprocesses waiting for PID = PID
For the behavior that option decides to call, it can be wnohang, which causes immediate return when no sub-process exits.
It can also be wuntraced, Which is returned when there is no reporting status.

10. Kill the function to kill the process.
# Include <signal. h>
# Include <sys/types. h>
Int kill (PID, int sig );
11. Signal
There are three solutions after receiving a signal:
Ignore this signal
Capture this signal to execute a special code called a signal processor.
Allows you to perform the default operation.
The kill function can be used to send signals.
Other Signals except sigstop and sigkill can be captured or ignored.
The pause function suspends the process that calls it until any signal arrives.
# Include <unistd. h>
Int pause (void );
Create and set a signal set, and then set or modify the current signal mask through sigprocmask.
Then register a signal processor (sigaction function) with the kernel, and then capture the second signal.
.

Create, set, and query a Signal Set
# Include <signal. h>
Int sigemptyset (sigset_t * Set );
Int sigfillset (sigset_t * Set );
Int sigaddset (sigset_t * Set, int SIGNUM );
Int sigdelset (sigset_t * Set, int SIGNUM );
Int sigismember (const sigset_t * Set, int SIGNUM );
Register a Signal Processor
# Include <signale. h>
Int sigprocmask (INT how, const sigset_t * Set, sigset_t * oldset );
Note: How values:
Sig_block -- set contains other signals to be blocked
Sig_unblock -- set contains the signal to remove Blocking
Sig_setmask -- set contains the new signal mask
If how is null, it is ignored. If set is null, the current mask is saved in the oldset. If
The oldset is null and ignored.
# Include <signal. h>
Int sigaction (int signum, const struct sigaction * Act, struct
Sigaction * oldact );
// Sigaction: Set the signal processor for the signal specified by SIGNUM.
Act (oldact) describes the deployment of signals. There is a sigaction structure in signal. h:
Struct sigaction {
Void (* sa_hundler) (INT );
Sigset_t sa_mask;
Int sa_flag;
Void (* sa_restorer) (void );
}
This structure describes the processor or function sa_hundler to be called after the signal in SIGNUM is generated,
It can also be assigned a value of sig_def, causing the default SIGNUM action to occur. assign a value of sig_ign to ignore this message.
.
Sa_mask defines the signal mask of other signal sets that should be blocked during the execution of the processor period.
Sa_flags is the mask used to modify the behavior of sa_handler. It can be the following values:
Sa_nocldstop -- the process ignores any sigstop, sigstp, sigttin, and sigttou generated by the sub-process.
Signal
Sa_oneshot or sa_resethand -- the registered custom signal processor is executed only once. After execution
The default action of the recovery signal after completion.
Sa_restart -- enable restart system calls
Sa_nomask or sa_nodefer -- do not receive the signal itself in the signal's own processor.
The sa_restorer element in the function has been deprecated.

12. Detection Signal
Sigpending allows the process to detect pending signals (signals suspended when blocking), and then you can decide to ignore
They still deliver them.
# Include <signal. h>
Int sigpending (sigset_t * Set );
The pending signal set is returned in the set. Then, you can use sigismember to determine whether the signal you are interested in is returned.
Pending signal.

13. Process Scheduling
# Include <sched. h>
# Include <unistd. h>
# Include <sys/time. h>
# Include <sys/resource. h>
Int sched_setscheduler (pid_t PID, int policy, const struct sched_param
* P );
Int sched_getscheduler (pid_t PID );
Int sched_get_priority_max (INT policy );
Int sched_get_priority_min (INT policy );
Int getpriority (INT which, int who );
Int setpriority (INT which, int who, int PRIO );
Int nice (INT Inc );

This article is from the "Wik" blog, please be sure to keep this source http://noyear.blog.51cto.com/819034/405844

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.