Using pipelines to perform PS aux under Linux | grep process ID to get CPU and memory usage

Source: Internet
Author: User
Tags memory usage sprintf strtok

#include <stdio.h> #include <unistd.h>   int main () { Char castdoutline[1024];//A line of information in the standard output of the PS command char* pctmp = NULL; Points to a string after splitting with a space   Char caselfpid[10]; Self-process PID string 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 to facilitate control of branch level, easy to control branch flow     { //By creating a pipeline, call fork to produce a child process, //execute 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 use 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))         { //split string with space separator pctmp = strtok (Castdoutline, "");              //user name skipped, direct matching PID, mismatched skip pctmp = strtok (NULL, ""); if (0! = strncasecmp (Caselfpid, Pctmp, strlen (caselfpid)))             { continue;             }   //read out process itself CPU usage pctmp = strtok (NULL, ""); printf ("CPU =%s%%\n", pctmp);   //read out process self 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 return to the Shell's terminating state. //If the shell cannot be executed, //The terminating state returned by Pclose () is the same as the shell has executed exit. Pclose (FP);          }while (0); }   $ gcc main.c-o test $./testCPU = 1.0%MEM = 0.0
$ ps auxuser PID%cpu%MEM VSZ RSS TTY STAT START time COMMANDNSC 24505 1.0 0.0 2004 232 pts /0 s+ 09:46 0:00./test Turn from: http://blog.sina.com.cn/s/blog_4c451e0e0101cmrq.html

Using pipelines to perform PS aux under Linux | grep process ID to get CPU and memory usage

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.