Talk C chestnuts together (128th back: C language instance -- view the current process information)

Source: Internet
Author: User

Talk C chestnuts together (128th back: C language instance -- view the current process information)

Hello, everyone. In the previous session, we talked about the example of checking the main function parameters. The example we mentioned here is:View Current process information. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

In the previous article, we introduced the use of the special file system: proc to view the command line parameters of the main function. Later, some readers raised some questions and I will answer them here.

The first question is the code in the example. The detailed code is as follows:

#include 

int main(int argc, char *argv[])
{
    int i = 0;

    while(i++ < argc*3)
        sleep(1);

    return 0;
}

I think it may take a long time to get the desired results after the official use of this example. I use this code because it allows you to ensure that processes with PID are still running in the system when you view the content In proc/PID. The loop in the code will allow the program to perform sleep operations. Each sleep1 second, the number of sleep is three times the number of parameters in the program. For example, there are two parameters a and B in the program, the program will sleep3 times (the program name will also be treated as one parameter), each sleep1 second, a total of sleep3 seconds. You can view the content In proc/PID as long as the waiting time does not exceed 3 seconds.

The wait time for each viewer may be different. To solve this problem, we can use the self keyword so that we do not need the code in the above example. Next, we will explain through actual examples:

cat / proc / self / cmdline // Execute the command in the terminal, the self keyword is used in the command
cat / proc / self / cmdline // The command line parameters of the current process are displayed in the terminal, and the prompt information of the terminal is omitted here 

From the above results, we can see that the self keyword is used to replace/proc/PID, and the command line parameters of the currently running process are obtained, that is, the parameter of the main function we mentioned in the previous response. In addition, we can also see that the command line parameters displayed in the Command running result are exactly the same as the command line parameters we entered when executing the command.

The second question is about the process. Some readers said that the process has other information besides the command line parameters, such as the commonly used PID. Here I will make a unified answer: we can use the ps command to view the PID of the process, as well as the command line parameters. Here is an example:

ps -x // Execute the ps command in the terminal, and add the x parameter
   PID TTY STAT TIME COMMAND
  2731 pts / 1 Ss 0:00 bash // Other processes are omitted and not written
  2943 pts / 1 R + 0:00 ps -x // this is the current process 

As shown in the preceding running results, the ps command displays the current process PID and command line parameters. However, the ps command shows that all processes are in the system, and we only list some of them.

In addition to the ps command, we can also use the special file system proc to view the information of the current process. In the proc file system, in the/proc/PID/status file of process-related information storage, we can use the cat command to view the content of the file and obtain process-related information. Next, we will give an example to illustrate:

cat / proc / self / status // Use the cat command in the terminal to view the contents of the file in proc
Name: cat // The following is the content of the file, this content represents the name of the process
State: R (running)
Tgid: 2994
Ngid: 0
Pid: 2994 // PID of the process
PPid: 2731
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 24 27 30 46 108 110 1000
VmPeak: 5924 kB
VmSize: 5924 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 540 kB
VmRSS: 540 kB
VmData: 160 kB
VmStk: 136 kB
VmExe: 44 kB
VmLib: 1828 kB
VmPTE: 24 kB
VmSwap: 0 kB
Threads: 1 // The number of threads in the process, there is only one thread here
SigQ: 0/31070
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000001fffffffff
Seccomp: 0
Cpus_allowed: f
Cpus_allowed_list: 0-3
Mems_allowed: 1
Mems_allowed_list: 0
voluntary_ctxt_switches: 1
nonvoluntary_ctxt_switches: 1 

We use the self keyword again to view the files in proc, And then we mention the information of the current process. The process information displayed in this file is more than that displayed by the ps command. You can view the process information based on your needs. In addition, this method only displays information about the current process, but does not display information about other processes in the system. The ps command displays information about all processes in the system. In the preceding running results, I added comments to common process information for your convenience. Other information without comments will be explained later. Currently, you can use this method to view process information.

Let's talk about the example of viewing information about the current process. I want to know what examples will be provided later, and I will try again.


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.