Obtain the process id and name in linux.

Source: Internet
Author: User
Linux obtains the process id and name as a shared library, and requires statistics on the usage frequency and usage of various applications in this library. In order to make better improvements to the main applications. Www.2cto.com, you need to record the caller's process id or name, and...
Linux obtains the process id and name as a shared library, and requires statistics on the usage frequency and usage of various applications in this library. In order to make better improvements to the main applications. Www.2cto.com, you need to record the caller's process id or name and save it. You can use shared memory or files to save the action. in the next blog, This article describes how to obtain the process id and name. Example: # include # Include # Define into mng_task_name_len 256 int main () {int ret; char ac_tmp [using mng_task_name_len]; ret = using mng_get_taskname (ac_tmp, using mng_task_name_len); if (0! = Ret) {printf ("Call into mng_get_taskname fail. \ n "); return-1;} printf (" The running task name is % s. \ n ", ac_tmp); return 0;} int character mng_get_taskname (char * ac, int len) {int count = 0; int nIndex = 0; char chPath [export mng_task_name_len] = {0}; char cParam [100] = {0}; char * cTem = chPath; int tmp_len; pid_t pId = getpid (); sprintf (cParam, "/proc/% d/exe", pId);/* printf ("cParam = % s. \ n ", cParam); */count = readlink (cParam, chPath, CFGMNG_TASK_NAME_LEN);/* printf (" count = % d. \ n ", count); */if (count <0 | count> = running mng_task_name_len) {printf (" Current System Not Surport Proc. \ n "); return-1;} else {nIndex = count-1; for (; nIndex> = 0; nIndex --) {if (chPath [nIndex] = '/') // filter the process name {nIndex ++; cTem + = nIndex; break ;}}} tmp_len = strlen (cTem); if (0 = tmp_len) {printf ("Get task fail. \ n "); return-1;} if (len <= tmp_len + 1) {printf (" len (% d) is less than taskname (% s)'s len. \ n ", len, cTem); return-1;} strcpy (ac, cTem); return 0;} as shown in the preceding experiment example, the main function used is getpid to get the id of the process, and then to/proc/pid/exe
To find the corresponding process name. There are a lot of process-related things in the/proc directory. you can use this method to bypass implementation.
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.