Linux uses pipeline command to perform PS get CPU and memory footprint _linux Shell

Source: Internet
Author: User
Tags sprintf strtok

Copy Code code as follows:

#include <stdio.h>
#include <unistd.h>
int main ()
{
Char castdoutline[1024]; One line of information in the standard output of the PS command
char* pctmp = NULL; A string that points to a space split

Char caselfpid[10]; PID String for self process
Char capscmd[24]; "PS aux | grep PID Command string

memset (caselfpid, 0, sizeof (caselfpid));
sprintf (Caselfpid,
"%d",
Getpid ());

memset (capscmd, 0, sizeof (capscmd));
sprintf (Capscmd,
"PS aux | grep%d ",
Getpid ());

Do//non loop, just for easy control of branch level, easy to control branch flow
{
By creating a pipe, call fork to produce a child process,
Executes a shell to run a command to open a process.
This process must be closed by the Pclose () function.
file* fp = Popen (Capscmd,//A pointer to a NULL-terminated shell command string,
This line of command will be uploaded to Bin/sh and using the-C flag,
Then the shell will execute this command to read from this string.
"R"); Standard output of a file pointer connected to a shell command

if (NULL = fp)
{
printf ("Call Popen is failed\n");
Break
}

memset (castdoutline, 0, sizeof (castdoutline));
while (NULL!= fgets (castdoutline),
sizeof (Castdoutline),
FP))
{
and split the string with the space separator
Pctmp = Strtok (Castdoutline, "");

User name skipped, match PID directly, do not match skip
Pctmp = Strtok (NULL, "");
if (0!= strncasecmp (caselfpid,
Pctmp,
Strlen (Caselfpid))
{
Continue
}

read out process itself CPU occupancy rate
Pctmp = Strtok (NULL, "");
printf ("CPU =%s%%\n", pctmp);

read out the process's own MEM occupancy rate
Pctmp = Strtok (NULL, "");
printf ("MEM =%s%%\n", pctmp);

Break
}

Close the standard I/O stream, wait for the command to finish, and then return to the shell's termination state.
If the shell cannot be executed,
Then the Pclose () returns the same termination status as the shell has executed exit.
Pclose (FP);

}while (0);
}

Copy Code code as follows:

$ gcc Main.c-o test
$./test
CPU = 1.0%
MEM = 0.0%

$ ps aux
USER PID%cpu%mem VSZ RSS TTY STAT START time COMMAND
NSC 24505 1.0 0.0 and 232 pts/0 s+ 09:46.0:00

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.