einpoklum
einpoklum

Reputation: 131544

How to obtain the process id portably in C++?

In standard C++, we can get an id for the current execution thread: std::this_thread::get_id(). But the language doesn't, at the time of writing, have an inherent notion of a process. I still want my process id, though.

So - what's the most portable, standards-friendly (albeit not language-standard) way to get the running process' ID in modern C++?

Notes:

Upvotes: 11

Views: 18111

Answers (2)

jspcal
jspcal

Reputation: 51904

Boost.Interprocess has boost::interprocess::ipcdetail::get_current_process_id.

The ACE library provides various OS-related functions and has been ported to many platforms. See here for a list. The library's ACE_OS namespace provides a getpid implementation.

In general, there's no universal way to get the process ID on every platform since that aspect of the OS's process management is outside the scope of the C++ language.

Upvotes: 10

Jonathan Wakely
Jonathan Wakely

Reputation: 171293

Well, then, what's the most portable, standards-friendly way to get the running process' ID in (modern) C++?

As Igor's comment says, there's no standard C++ way, so the most portable, standards-friendly way is getpid.

You won't find anything else defined by a standard that can be used on a wider set of platforms.

Upvotes: 8

Related Questions