'How to get current runing process number of current user?

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



Solution 1:[1]

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1