Talk C chestnuts together (127th back: C language instance -- view the parameters of the main function)
Hello, everyone. The last time we talked about the static keyword example, this example is:View the parameters of the main function.When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
Look, we introduced the parameters of the main function in 57th. If you forget the parameters, click here to view the previous content. In the previous sections, we introduced how to directly operate the main function parameters in the program. That is, directly operate argv and argc. This method requires you to write related content in the code in advance to operate argv and argc. What if we want to view the main function parameters while the program is running? You can add the argv and argc operations to the code. At this time, it is a little late to return the code for compiling argv and argc operations in the code. We also need to re-compile the code and waste a lot of time waiting for program compilation, it's a pity. The audience watched the audience shake their heads and sigh. We will introduce another method to view the main function parameters today. This method can be used to view the main function parameters during the program running.
In Linux, there is a special file system called proc. This file system stores information related to the Linux system. Today, we will only introduce one of the information, namely the command line parameters of the process, that is, the parameters of the main function to be introduced. Next we will illustrate how to view the command line parameters of the process through examples, that is, the parameters of the main function that we will introduce.
#include
int main(int argc, char *argv[]){ int i = 0; while(i++ < argc*3) sleep(1); return 0;}
Let's write some simple code. The sleep function is used in the code to temporarily allow the program to take a while. This time is equal to the number of program parameters multiplied by three, in seconds. We save the above Code to a file, then use gcc to compile the file, and generate an executable file named s.
Run the program on the terminal. The specific command is as follows:
. /S-a-B & // run the program in the background and pass two parameters to the program:-a and-B [1] 3231 // The program is running in the background, the program's IDcat/proc/3231/cmdline // command is displayed on the terminal to view the command line parameters of the process. /s-a-B // the command line parameters of the process are displayed on the terminal. The prompt information of the terminal is omitted [1] + Done. /s-a-B // The program runs in the background
As shown in the preceding running resultYou can use the cat command in the terminal to directly view the command line parameters of the process,That is, the parameter of the main function in the process:-a-B. This parameter is exactly the parameter that we pass to the program when running the program.
What does "/proc/3231/cmdline" mean? At this time, the reader asked a question, and the reader was impatient. We just mentioned that proc is a special file system, and there are many directories in the file system. Some of these directories are named numbers, these directories with numbers as the directory name contain process-related information, and the directory name is the PID of the process. There is a file named cmdline in the directory of this type. The content of this file is the command line parameter of the process, that is, the parameter of the main function. In the preceding example, run the command cat/proc/3231/cmdline to view the command line parameters of the PID 3231 process.
This method allows you to directly view the parameters of the main function during program running or program debugging. In this way, we do not need to write the argv and argc content in the code in advance, and it can save the time to re-compile the program. It's really just a couple of minutes!
Let's talk about the example of viewing the main function parameters. I want to know what examples will be provided later, and I will try again.