1 #ifndef CORELIB___NCBI_PROCESS__HPP
2 #define CORELIB___NCBI_PROCESS__HPP
46 #if defined(NCBI_OS_UNIX)
47 # include <sys/types.h>
48 #elif defined(NCBI_OS_MSWIN)
51 # error "CProcess is not implemented on this platform"
70 #if defined(NCBI_OS_LINUX)
71 # define NCBI_THREAD_PID_WORKAROUND
76 #if defined(NCBI_OS_UNIX)
79 #elif defined(NCBI_OS_MSWIN)
169 static TPid GetPid(
void);
174 static TPid GetParentPid(
void);
216 static bool GetTimes(
double* real,
double* user,
double* sys,
EWhat what = eProcess);
230 static int GetThreadCount(
void);
248 static int GetFileDescriptorsCount(
int* soft_limit =
NULL,
int* hard_limit =
NULL);
261 fFF_AllowExceptions = 32
324 fDF_AllowExceptions = 32,
325 fDF_AllowThreads = 64
359 #if defined NCBI_THREAD_PID_WORKAROUND
369 static TPid sx_GetPid(EGetPidFlag flag);
402 #if defined(NCBI_OS_MSWIN)
429 bool IsCurrent(
void);
443 bool IsAlive(
void)
const;
466 bool Kill(
unsigned long timeout = kDefaultKillTimeout);
492 bool KillGroup(
unsigned long timeout = kDefaultKillTimeout)
const;
517 static bool KillGroupById(
TPid pgid,
unsigned long timeout = kDefaultKillTimeout);
532 bool IsPresent(
void)
const;
535 bool IsAlive(
void)
const;
538 bool IsExited(
void)
const;
541 bool IsSignaled(
void)
const;
545 int GetExitCode(
void)
const;
550 int GetSignal(
void)
const;
783 #if defined(NCBI_OS_MSWIN)
816 virtual const char* GetErrCodeString(
void)
const override;
820 const CException* prev_exception, EErrCode err_code,
821 const string& message,
TPid pid = 0,
830 out <<
"pid " << m_PID;
896 CPIDGuard(
const string& filename,
const string& dir);
920 void UpdatePID(
TPid pid = 0);
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
CPIDGuard – Process guard.
Extended exit information for waited process.
CProcess(TProcessHandle process, EType type=eHandle)
std::ofstream out("events_result.xml")
main entry point for tests
ETriState
Enumeration to represent a tristate value.
EDiagSev
Severity level for the posted diagnostics.
@ eDiag_Error
Error message.
virtual void x_Assign(const CException &src)
Helper method for copying exception data.
#define NCBI_EXCEPTION_DEFAULT_IMPLEMENTATION(exception_class, base_class)
Helper macro for default exception implementation.
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
bool GetTimes(double *real, double *user, double *sys, EWhat what=eProcess)
Get process execution times.
size_t resident_peak
Peak resident set size ("high water mark")
size_t total_peak
Peak total memory usage.
int WaitTimeout(unsigned long timeout, CExitInfo *info=0) const
Wait until the process terminates or timeout expires.
TPid x_GetPid(void) const
unsigned int TDaemonFlags
Bit-wise OR of FDaemonFlags.
const unsigned long kInfiniteTimeoutMs
Infinite timeout in milliseconds.
TProcessHandle x_GetHandle(DWORD desired_access, DWORD *errcode=0) const
static TPid GetParentPid(void)
Get process identifier (pid) for the parent of the current process.
EType
How to interpret the used process identifier.
size_t resident
Resident/working set size (RSS).
static TPid Daemonize(const char *logfile=0, TDaemonFlags flags=0)
Go daemon.
TProcessHandle GetHandle(void) const
Get stored process handle.
static TPid GetPid(void)
Get process identifier (pid) for the current process.
int status
Process status information.
unsigned int TDaemonFlags
Bit-wise OR of FDaemonFlags.
static TPid ForkForExec(TForkFlags flags=0)
Fork the process for "replacing" a child process with a new process.
static TProcessHandle GetCurrentHandle(void)
EType m_Type
Type of process identifier.
EWhat
Process information "target".
size_t total
Total memory usage.
unique_ptr< CInterProcessLock > m_MTGuard
size_t shared
Shared memory usage.
size_t text
Text (code) segment size.
virtual void ReportExtra(ostream &out) const override
Constructor.
FDaemonFlags
Daemonization flags.
int state
Process state (unknown/alive/terminated).
ETriState m_IsCurrent
Status that m_Process represent the current process.
static TPid Daemonize(const char *logfile=0, TDaemonFlags flags=0)
Go daemon.
static TPid Fork(TForkFlags flags=fFF_UpdateDiag)
Fork the process.
FDaemonFlags
Daemonization flags @ deprecated Please use CCurrentProcess::FDaemonFlags instead.
int WaitInfinite(CExitInfo *info=0) const
Wait indefinitely until the process terminates.
TPid GetOldPID(void)
Returns non-zero if there was a stale file.
unique_ptr< CInterProcessLock > m_PIDGuard
size_t swap
Swap space usage.
static const unsigned long kInfiniteTimeoutMs
Infinite timeout (milliseconds).
intptr_t m_Process
Process identifier.
int Wait(unsigned long timeout=kInfiniteTimeoutMs, CExitInfo *info=0) const
Wait until process terminates.
void x_CloseHandle(TProcessHandle handle) const
int GetThreadCount(void)
Get the number of threads in the process.
static const unsigned long kDefaultKillTimeout
Default wait time (milliseconds) between "soft" and "hard" attempts to terminate a process.
unsigned TForkFlags
Bit-wise OR of FForkFlags.
pid_t TPid
Process identifier (PID) and process handle.
size_t lib
Shared library code size.
unsigned TForkFlags
Bit-wise OR of FForkFlags.
virtual void x_Assign(const CException &src) override
static TPid GetCurrentPid(void)
size_t data
Data segment size.
TPid GetPid(void) const
Get stored process identifier (pid).
static TProcessHandle GetHandle(void)
Get process handle for the current process (esp.
int GetFileDescriptorsCount(void)
Get the number of file descriptors consumed by the current process.
static TPid Fork(TForkFlags flags=fFF_UpdateDiag)
Fork the process.
EType GetType(void) const
Get type of stored process identifier.
size_t stack
Stack size of the initial thread in the process.
static TPid GetParentPid(void)
@ ePid
A real process identifier (pid).
@ eStillRunning
The process listed in the file is still around.
@ eChildren
All children of the calling process.
@ eProcess
Current process.
@ fFF_UpdateDiag
Reset diagnostics timer and log an app-start message in the child process.
@ fFF_AllowExceptions
Throw an exception on error.
@ fDF_KeepCWD
Don't change CWD to "/".
@ fDF_KeepParent
Do not exit the parent process but return.
@ fDF_KeepStdout
Keep stdout open as "/dev/null" (WO)
@ fDF_AllowExceptions
Throw an exception in case of an error.
@ fDF_KeepStdin
Keep stdin open as "/dev/null" (RO)
@ fDF_AllowThreads
Do not fail if pre-existing threads are detected.
@ fDF_ImmuneTTY
Make daemon immune to re-acquiring a controlling terminal.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
#define NCBI_XNCBI_EXPORT
#define HANDLE
An abstraction for a file handle.
Simple inter-process lock.
Defines MS Windows specifics for our "C++" code.
bool Daemonize(const char *logfile=0, TDaemonFlags flags=0)
bool GetMemoryUsage(size_t *total, size_t *resident, size_t *shared)
Process memory usage information, in bytes.