Home > Enterprise >  How to get current runing process number of current user?
How to get current runing process number of current user?

Time:04-05

I need to find out the current runing process number of current user in a c program, how can I do this? Working os is Centos8

CodePudding user response:

As your question is tagged linux so my answer is only valid for linux based operating systems and POSIX-compliant systems.

unistd.h is a part of POSIX environment.

The <unistd.h> header defines miscellaneous symbolic constants and types, and declares miscellaneous functions.

Read more about unistd.h. I'm using a function called getpid(), which returns the process ID of the current instance of the program which is being executed on the kernel.

Code:

#include <unistd.h> // getpid() function
#include <iostream> // std::cout and std::endl

int main(void)
{
    std::cout << "Process ID: " << getpid() << std::endl;
    return EXIT_SUCCESS;
}

CodePudding user response:

In Linux distros (as well as other POSIX systems), you can get the current process pid with getpidfunction.

See this manual page: https://man7.org/linux/man-pages/man2/getpid.2.html

I am not familiar with Centos8, but if it s flavor of Linux it might work for you.

  • Related