[Linux] Command line parameters and process priority

[Linux] Command line parameters and process priority

Command line parameters

The concept of command line parameters

Command line parameters refer to parameters entered on the command line when running a program.

Receiving command function parameters

The main function in C language has two parameters for receiving command line parameters, namely argc and argv. Even if the parameters of the main function are not explicitly stated, the compiler will automatically add and receive the parameters during compilation, but they will not be explicitly stated. Unable to use parameter:

int main(int argc, char *argv[]);

When running the program, you need to enter parameters on the command line. The essence of the parameters is a string. They will be separated by spaces before being passed to the main function, and then passed in separately. The argc parameter records the number of parameters passed in, and the argv parameter records the address of each parameter:

image-20230825134110559

Write code verification

Write the following code to verify that the main function accepts command line parameters:

#include <stdio.h>

int main(int argc, char *argv[])
{
    
    
  int i = 0;
  for (i = 0; i < argc; i++)
  {
    
    
      printf("argv[%d]->%s\n", i, argv[i]);
  }
  return 0;
}

Test after successful compilation:

image-20230825134818559

Supplement: It is precisely because the main function can receive command line parameters that the function of adding options when using Linux instructions is realized.

process priority

The concept of process priority

The priority of a process refers to the order in which CPU resources are allocated. Processes with higher priority have priority execution rights.

The reason for the emergence of process priority: There are many system processes, but there are only a small amount of CPU resources, or even one, so there is competition between processes. In order to complete tasks efficiently and compete for related resources more reasonably, priority is given.

PRI and NI

Enter the parameters to view the system process under the Linux system ps -l:

image-20230825140845597

Among the many data, the two parameters related to priority are PRI and NI.

  • PRI: represents the priority at which this process can be executed. The smaller the value, the earlier it will be executed.

  • NI: represents the nice value of this process, which represents the modified value of the priority at which the process can be executed.

PRI = initial priority value + nice value.

The range of nice value is [-20,19). If the priority of a certain process is too high or too low, it may cause other processes to be unable to be scheduled or itself cannot be scheduled, so the nice value has a range.

Use the top command to modify the nice value

Write the following code to test the nice value modification:

#include <stdio.h>
#include <unistd.h>

int main()
{
    
    
  while(1)
  {
    
    
    printf(".");
    fflush(stdout);
    sleep(1);
  }
  return 0;
}

Run after successful compilation, and open two additional terminals, enter topthe command in one and enter ps -althe command in the other:

image-20230825143626926

Press r in the top process:

image-20230825143651375

In the top process, enter the process ID to be modified to nice:

image-20230825143732282

Enter the nice value to be modified:

image-20230825143825468

Use ps -althe command to view:

image-20230825143853689

Since the initial value is 80 and the nice value is 10, the final PRI is 90.

Guess you like

Origin blog.csdn.net/csdn_myhome/article/details/132509755