linux-Process Description (4) Process priority and process creation execution

Source: Internet
Author: User

Process priority

Process CPU resource allocation refers to the priority of the process. High priority processes have priority rights to implementation.

Permissions and precedence. Permissions (privilege) refers to the management of a multiuser computer system in which a particular user has specific system resource usage rights, such as a folder, the use of specific system instructions, or the storage limit. Permissions are issues that have or are not, and the priority is the question of the size of the permission in which the permission is already being discussed. Configuring process precedence is useful for Linux in multitasking environments and can improve system performance. The process can also be run to the specified CPU, so that the non-important process to a certain CPU, can greatly improve the overall performance of the system.

viewing system processes

Notice a few important information, PID (the code of the process) and Ppid (the name of the parent process) already mentioned in the previous process identifier. The UID represents the identity of the performer. What do you mean? For example, why do you hold an ID card in order to do business in the bank, because the identity card proves that you are a Chinese legal citizen, with legal authority, you as a Chinese legal citizen in the status of the counter business, such as students can be in school classes, use of teaching facilities, and that is he through certain procedures to become the students, He studied at school as a student of the university, while the teacher taught in school as a teacher. When we create a user, we name and set the password for the new user, and the system associates a number with the user name we created, the so-called user uid, which is the user's identity. Usually the user's identity has regular users and super users. The process UID is the same as the UID of the user running the process. The PRI is the priority of the process, or the popular point is the order in which the program is executed by the CPU, and the smaller the value the higher the priority of the process, the sooner it is executed. NI is the nice value we're going to say, which represents the number of priority corrections that a process can perform. As mentioned earlier, the smaller the PRI value, the faster it is executed, then the addition of the Nice value will make the PRI:pri (new) =pri (old) +nice.

As a result, the PR is sorted according to Nice, the rule is nice smaller PR (small, more priority), that its priority will be higher, the faster it is executed. If Nice is the same, the process UID is root with greater precedence. Thus, when the nice value is negative, the program will lower the priority value, i.e. its priority will be higher, and the faster it is executed. So far, it is important to emphasize that the nice value of the process is not a process priority, they are not a concept, but the process nice value affects the process's priority change.if the original pri is a , not we give a nice=5, will let the pri into a . Because it is the system "dynamic" decision, so, although the nice value can affect the pri, but the final pri still have to go through the system analysis before deciding.
Modify Process Priority
There are two main commands to modify the priority of a process: Nice,renice
1. Start executing the program to specify a nice value: Nice
# nice-n -5/usr/local/mysql/bin/mysqld_safe &
Linux Nice command details
Function Description: Set priority.
Syntax: Nice [-n < priority level >][--help][--version][execution Instructions]
Additional note: The Nice directive can change the priority level of program execution.
Parameters:-n< Priority level > or-< priority level > or –adjustment=< priority > Set priority level for the instruction to be executed. Levels range from 20-19, where-20 is the highest, 19 lowest, and only system administrators can set negative levels.

Example one: use root to give a nice value of -5, to execute VI, and observe the process.

# Nice-n-5 VI &

Attention:

1. The nice value of the general user is 0~19;

2.root the available nice values are -20~19;

3. General users can only adjust the nice value higher, if the nice is 5, can only be adjusted to more than 5 Nice :

4. General users can only adjust the nice value of their own process .

That is, to adjust the priority of a process is to "adjust the nice value of the process ."

2, nice to adjust existing processes: Renice
1 renice-5-P 5200
2 #PID为5200的进程nice设为-5
Linux renice command details
Function Description: Adjust priority.
Syntax: renice [Priority] [-G < program group name; ...] [-p < Program identification Code, ....] [-u < user name; ...]
Supplemental Note: The Renice directive can readjust the priority level of the program execution. The preset is to adjust its priority with the program identification Code designation program, you can also specify the program group or user name to adjust the priority level, and modify all the programs that belong to the program group or user priority. Level range from -20–19, only system administrators can change the priority of other user programs, and only system administrators can set negative levels.
Parameters:
-G < program group name > Use program group name to modify the priority of all programs that belong to the program group.
-P < Program identification code > change the program's priority level, this parameter is a preset value.
-u < user name > Specify user name to modify the priority of all programs that belong to that user.

example Two  : < Span lang= "ZH-CN" > will 4222 that pid modify nice

You can also change the nice of an existing process with the top command:
1 Top
2 #进入top后按 "R" –> input process pid–> Enter Nice value
Process Creation Execution
When the process executes, it is loaded into virtual memory, allocates space for the program variables, and adds the relevant information to the TASK_STRUCT.
A process memory layout is divided into four different segments:
? A text segment that contains the program's source directives.
? A data segment that contains static variables.
? heap, dynamic memory partition area.
? Stacks, dynamically growing and shrinking segments, saving local variables.
Here are two ways to create a process, fork (), and Execve (). They are all system calls, but they run in a slightly different way. To create several processes, you can perform a fork () system call. The child process then gets a copy of the data segment, the stack segment, and the heap area in the parent process. Child processes can modify these memory segments independently. However, the text segment is a memory segment shared by the parent and child processes and cannot be modified by the quilt process.
The fork function is used to create a new process from an existing process, and the new process becomes a child process, and the original process becomes the parent process. The two processes return their respective return values, where the return value of the parent process is the process number of the child process, and the child process returns 0, so the return value greater than 0 identifies the parent process and equals 0 identifies the child process. So we can determine whether the process is a parent or child process by the return value. The fork function creates a new process after the parent-child process model is as follows: The following is a simple procedure to observe the running process between parent and child processes:

The result of this code operation is as follows:

A wonderful thing about a fork call is that it is called only once, but it can return two times, and it may have three different return values:
1) In the parent process, fork returns the process ID of the newly created child process;
2) in the sub-process, fork returns 0;
3) If an error occurs, fork returns a negative value;

There are two possible reasons for fork errors:
1) The current number of processes has reached the system-specified limit, when the value of errno is set to Eagain.
2) system memory is low, then the value of errno is set to Enomem.
After a successful creation of a new process, there are two fundamentally identical processes in the system, which do not have a fixed sequencing and which process first executes the process scheduling policy to look at the system.

The execve () function creates a new process. This system call destroys all memory segments to recreate a new memory segment. Then, EXECVE () requires an executable file or script as a parameter, which differs from fork (). Note that the processes created by EXECVE () and fork () are child processes that run the process.

Fork a child process in the parent process and call the EXEC function in the child process to start a new program. There are six exec functions, where execve is a kernel-level system call, and the other (EXECL,EXECLE,EXECLP,EXECV,EXECVP) is a library function that calls EXECVE. function definitions: int execve (const char *filename, char *const argv[], char *const envp[]), return value: The function does not return a value when execution succeeds, and the return value when execution fails is-1. Function Description: Execve () is used to execute the file path represented by the argument filename string, the second parameter is passed to the execution file using an array pointer, and it needs to end with a null pointer (null), and the last parameter is an array of new environment variables passed to the execution file.

1#include <unistd.h>2 Main ()3 {   4     Char*argv[]={"ls","-al","/etc/passwd", NULL}; 5     Char*envp[]={"Path=/bin", NULL}6Execve ("/bin/ls", argv, ENVP); 7}

The result is:-rw-r--r--1 root root 1659 Feb 20:13/etc/passwd This is the same as the result of executing ls-al/etc/passwd in the bin directory.

Fork is a fen, and Execve is a change of body.

The system call of the Exec series is to replace the current program with the program to be executed, and fork is used to produce a process that is the same as the current process (although it usually executes a different code flow). It is common to run another program while keeping the original program running, Fork+exec.

















linux-Process Description (4) Process priority and process creation execution

Related Article

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.