ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
processinfo.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of sub-namespace #alib::lang::system of module \alib_basecamp of
4/// the \aliblong.
5///
6/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7/// Published under \ref mainpage_license "Boost Software License".
8//==================================================================================================
9#ifndef HPP_ALIB_LANG_SYSTEM_PROCESSINFO
10#define HPP_ALIB_LANG_SYSTEM_PROCESSINFO 1
11#pragma once
13
15
16namespace alib { namespace lang::system {
17
18//==================================================================================================
19/// This class represents process information
20//==================================================================================================
22{
23 // #############################################################################################
24 // Fields
25 // #############################################################################################
26 protected:
27 /// Information on the current process. This is initialized once, with the first invocation
28 /// of #Current.
30
31 public:
32 /// The process id as AString.
34
35 /// The command line which invoked this process.
37
38 /// The name of the process.<br> Under GNU/Linux this is read from /proc/nnn/stat and may
39 /// differ from #ExecFileName. Under Windows OS, it is he same as field #ExecFileName.
41
42 /// The path of the executable (if available to us).
44
45 /// The file name of the executable (excluding #ExecFilePath). Under GNU/Linux,
46 /// if we have no access to read that value, field #ExecFilePath will be empty
47 /// while this field is filled with #Name.
49
50 // #############################################################################################
51 // Fields under unix like OS
52 // #############################################################################################
53 #if defined( __unix__ ) || DOXYGEN
54
55 /// The parent's process id as AString. (Unix like OS / Mac OS only.)
57
58 /// The contents of /proc/PID/stat file. (Unix like OS only.)
60
61 /// The state field (2) within \ref Stat. (Unix like OS only.)
63
64 /// The process group field (4) within \ref Stat. (Unix like OS only.)
66 #endif
67
68 // #############################################################################################
69 // Fields under Mac OS
70 // #############################################################################################
71 #if defined( __APPLE__ )
73 #endif
74
75 // #############################################################################################
76 // Fields under Windows OS
77 // #############################################################################################
78 #if defined( _WIN32 ) || DOXYGEN
79 public:
80
81 /// For console processes, this is the title displayed in the title bar. (Windows OS only.)
83 #endif
84
85
86 // #############################################################################################
87 // Protected methods
88 // #############################################################################################
89 protected:
90 //==========================================================================================
91 /// Default constructor to create an empty instance
92 //==========================================================================================
94
95
96 #if defined( __unix__ )
97 //======================================================================================
98 /// Reads a field from the data found in /proc/PID/stat.
99 /// @param fieldNo The field number to read
100 /// @param target The variable to store the result in
101 /// @returns \c true if successful.
102 //======================================================================================
103 bool getStatField( int fieldNo, AString& target );
104 #endif
105
106 //==========================================================================================
107 /// Internal get() method with different platform implementations.
108 ///
109 /// @param PID The ID of the process information is requested on.
110 /// If a \e nulled string is provided, the current process is addressed.
111 ///
112 /// @return \c true on success, \c false otherwise.
113 //==========================================================================================
114 ALIB_API bool get( uinteger PID );
115
116 // #############################################################################################
117 // Interface
118 // #############################################################################################
119 public:
120 #if defined( __unix__ ) || defined(__APPLE__)
121 //======================================================================================
122 /// Constructor that initializes this instance according to the process information
123 /// received from the system.
124 ///
125 /// \note
126 /// This method is implemented for Unix like OS (incl. Mac OS) only.
127 /// On Windows OS, only information about the current process can be received
128 /// using static method #Current.
129 ///
130 /// @param processID The ID of the process information is requested on.
131 /// If a \e nulled string is provided, the current process is
132 /// addressed.
133 //======================================================================================
134 ProcessInfo( uinteger processID ) { get( processID ); }
135
136 //======================================================================================
137 /// Fills the fields of this instance according to the process information received from
138 /// the system.
139 ///
140 /// \note
141 /// This method is implemented for Unix like OS (incl. Mac OS) only.
142 /// On Windows OS, only information about the current process can be received
143 /// using static method #Current.
144 ///
145 /// @param processID The ID of the process information is requested on.
146 /// If a \e nulled string is provided, the current process is
147 /// addressed.
148 ///
149 /// @return \c true on success, \c false otherwise.
150 //======================================================================================
151 ALIB_API bool Get( uinteger processID ) { return get( processID ); }
152 #endif
153
154 //==========================================================================================
155 /// Static method that receives information on the current process.
156 ///
157 /// @return A constant reference to a \c %ProcessInfo object representing the current process.
158 //==========================================================================================
159 ALIB_API static
160 const ProcessInfo& Current();
161};
162
163} // namespace [ lang::system]
164
165/// Type alias in namespace \b alib.
167
168} // namespace [alib]
169
170#endif // HPP_ALIB_LANG_SYSTEM_PROCESSINFO
171
This class represents process information.
ALIB_API bool Get(uinteger processID)
AString CmdLine
The command line which invoked this process.
AString ConsoleTitle
For console processes, this is the title displayed in the title bar. (Windows OS only....
ProcessInfo()
Default constructor to create an empty instance.
bool getStatField(int fieldNo, AString &target)
AString ExecFilePath
The path of the executable (if available to us).
static ALIB_API const ProcessInfo & Current()
uinteger PID
The process id as AString.
ALIB_API bool get(uinteger PID)
AString Stat
The contents of /proc/PID/stat file. (Unix like OS only.)
AString StatState
The state field (2) within Stat. (Unix like OS only.)
AString StatPGRP
The process group field (4) within Stat. (Unix like OS only.)
ProcessInfo(uinteger processID)
uinteger PPID
The parent's process id as AString. (Unix like OS / Mac OS only.)
static ALIB_API ProcessInfo current
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_API
Definition alib.hpp:639
platform_specific uinteger
Definition integers.hpp:47
Definition alib.cpp:69