NCBI C++ ToolKit
Classes | Typedefs | Enumerations | Enumerator | Functions | Variables | Friends
Process Management
+ Collaboration diagram for Process Management:

Classes

class  CInterProcessLock
 CInterProcessLock –. More...
 
class  PInterProcessLock
 
class  CInterProcessLockException
 CInterProcessLockException –. More...
 
class  CProcessBase
 CProcessBase –. More...
 
struct  CProcessBase::SMemoryUsage
 Process memory usage information, in bytes. More...
 
class  CCurrentProcess
 CCurrentProcess –. More...
 
class  CProcess
 CProcess –. More...
 
class  CProcess::CExitInfo
 Extended exit information for waited process. More...
 
class  CPIDGuardException
 CPIDGuardException –. More...
 
class  CPIDGuard
 CPIDGuard – Process guard. More...
 

Typedefs

typedef int CInterProcessLock::TLockHandle
 OS-specific lock handle. More...
 
typedef CInterProcessLock PInterProcessLock::resource_type
 
typedef CGuard< CInterProcessLock, PInterProcessLockCInterProcessLock_Guard
 Convenience typedef for PInterProcessLock. More...
 
typedef pid_t TPid
 Process identifier (PID) and process handle. More...
 
typedef TPid TProcessHandle
 
typedef unsigned CCurrentProcess::TForkFlags
 Bit-wise OR of FForkFlags. More...
 
typedef unsigned int CCurrentProcess::TDaemonFlags
 Bit-wise OR of FDaemonFlags. More...
 
typedef unsigned CProcess::TForkFlags
 Bit-wise OR of FForkFlags. More...
 
typedef unsigned int CProcess::TDaemonFlags
 Bit-wise OR of FDaemonFlags. More...
 

Enumerations

enum  CInterProcessLockException::EErrCode {
  CInterProcessLockException::eLockTimeout , CInterProcessLockException::eNameError , CInterProcessLockException::eCreateError , CInterProcessLockException::eLockError ,
  CInterProcessLockException::eUnlockError , CInterProcessLockException::eMultipleLocks , CInterProcessLockException::eNotLocked
}
 Error types that file operations can generate. More...
 
enum  CProcessBase::EWhat { CProcessBase::eProcess , CProcessBase::eChildren , CProcessBase::eThread }
 Process information "target". More...
 
enum  CCurrentProcess::FForkFlags { CCurrentProcess::fFF_UpdateDiag = 1 , CCurrentProcess::fFF_Exec = 2 , CCurrentProcess::fFF_AllowExceptions = 32 }
 Forking flags. More...
 
enum  CCurrentProcess::FDaemonFlags {
  CCurrentProcess::fDF_KeepCWD = 1 , CCurrentProcess::fDF_KeepStdin = 2 , CCurrentProcess::fDF_KeepStdout = 4 , CCurrentProcess::fDF_ImmuneTTY = 8 ,
  CCurrentProcess::fDF_KeepParent = 16 , CCurrentProcess::fDF_AllowExceptions = 32 , CCurrentProcess::fDF_AllowThreads = 64
}
 Daemonization flags. More...
 
enum  CProcess::EType { CProcess::ePid , CProcess::eHandle }
 How to interpret the used process identifier. More...
 
enum  CProcess::FForkFlags { CProcess::fFF_UpdateDiag = CCurrentProcess::fFF_UpdateDiag , CProcess::fFF_AllowExceptions = CCurrentProcess::fFF_AllowExceptions }
 Forking flags. More...
 
enum  CProcess::FDaemonFlags {
  CProcess::fDF_KeepCWD = CCurrentProcess::fDF_KeepCWD , CProcess::fDF_KeepStdin = CCurrentProcess::fDF_KeepStdin , CProcess::fDF_KeepStdout = CCurrentProcess::fDF_KeepStdout , CProcess::fDF_ImmuneTTY = CCurrentProcess::fDF_ImmuneTTY ,
  CProcess::fDF_KeepParent = CCurrentProcess::fDF_KeepParent , CProcess::fDF_AllowExceptions = CCurrentProcess::fDF_AllowExceptions , CProcess::fDF_AllowThreads = CCurrentProcess::fDF_AllowThreads , CProcess::fDontChroot = CCurrentProcess::fDF_KeepCWD ,
  CProcess::fKeepStdin = CCurrentProcess::fDF_KeepStdin , CProcess::fKeepStdout = CCurrentProcess::fDF_KeepStdout , CProcess::fImmuneTTY = CCurrentProcess::fDF_ImmuneTTY , CProcess::fKeepParent = CCurrentProcess::fDF_KeepParent
}
 Daemonization flags @ deprecated Please use CCurrentProcess::FDaemonFlags instead. More...
 
enum  CPIDGuardException::EErrCode { CPIDGuardException::eStillRunning , CPIDGuardException::eWrite }
 

Functions

 CInterProcessLock::CInterProcessLock (const string &name)
 
 CInterProcessLock::~CInterProcessLock (void)
 Call Unlock() More...
 
bool CInterProcessLock::TryLock (void)
 Try to acquire the lock. More...
 
void CInterProcessLock::Remove (void)
 Call Unlock() and removes lock object from the system. More...
 
const stringCInterProcessLock::GetName (void) const
 Get the original name of the lock – exactly as it was specified in the constructor. More...
 
const stringCInterProcessLock::GetSystemName (void) const
 Get the internal name of the lock – as it is being used in the calls to the OS (such as '/var/tmp/<name>' rather than just '<name>' on UNIX) More...
 
 PInterProcessLock::PInterProcessLock (const CTimeout &timeout, const CTimeout &granularity=CTimeout(CTimeout::eInfinite))
 
void PInterProcessLock::operator() (resource_type &resource) const
 
virtual const char * CInterProcessLockException::GetErrCodeString (void) const override
 Translate from an error code value to its string representation. More...
 
 CInterProcessLockException::NCBI_EXCEPTION_DEFAULT (CInterProcessLockException, CCoreException)
 
static TProcessHandle CCurrentProcess::GetHandle (void)
 Get process handle for the current process (esp. More...
 
static TPid CCurrentProcess::GetPid (void)
 Get process identifier (pid) for the current process. More...
 
static TPid CCurrentProcess::GetParentPid (void)
 Get process identifier (pid) for the parent of the current process. More...
 
static bool CCurrentProcess::GetTimes (double *real, double *user, double *sys, EWhat what=eProcess)
 Get current process execution times. More...
 
static bool CCurrentProcess::GetMemoryUsage (SMemoryUsage &usage)
 Get current process memory usage. More...
 
static int CCurrentProcess::GetThreadCount (void)
 Get the number of threads in the current process. More...
 
static int CCurrentProcess::GetFileDescriptorsCount (int *soft_limit=NULL, int *hard_limit=NULL)
 Get the number of file descriptors consumed by the current process, and optional system wide file descriptor limits. More...
 
static TPid CCurrentProcess::Fork (TForkFlags flags=fFF_UpdateDiag)
 Fork the process. More...
 
static TPid CCurrentProcess::ForkForExec (TForkFlags flags=0)
 Fork the process for "replacing" a child process with a new process. More...
 
static TPid CCurrentProcess::Daemonize (const char *logfile=0, TDaemonFlags flags=0)
 Go daemon. More...
 
 CProcess::CProcess (void)
 Default constructor. Uses the current process pid. More...
 
 CProcess::CProcess (TPid process, EType type=eHandle)
 Constructor. More...
 
EType CProcess::GetType (void) const
 Get type of stored process identifier. More...
 
TProcessHandle CProcess::GetHandle (void) const
 Get stored process handle. More...
 
TPid CProcess::GetPid (void) const
 Get stored process identifier (pid). More...
 
bool CProcess::IsCurrent (void)
 Checks that stored process identifier (pid) represent the current process. More...
 
bool CProcess::IsAlive (void) const
 Check for process existence. More...
 
bool CProcess::Kill (unsigned long timeout=kDefaultKillTimeout)
 Terminate process. More...
 
bool CProcess::KillGroup (unsigned long timeout=kDefaultKillTimeout) const
 Terminate a group of processes. More...
 
static bool CProcess::KillGroupById (TPid pgid, unsigned long timeout=kDefaultKillTimeout)
 Terminate a group of processes with specified ID. More...
 
 CProcess::CExitInfo::CExitInfo (void)
 Constructor. More...
 
bool CProcess::CExitInfo::IsPresent (void) const
 TRUE if the object contains information about the process state. More...
 
bool CProcess::CExitInfo::IsAlive (void) const
 TRUE if the process is still alive. More...
 
bool CProcess::CExitInfo::IsExited (void) const
 TRUE if the process terminated normally. More...
 
bool CProcess::CExitInfo::IsSignaled (void) const
 TRUE if the process terminated by a signal (UNIX only). More...
 
int CProcess::CExitInfo::GetExitCode (void) const
 Get process exit code. More...
 
int CProcess::CExitInfo::GetSignal (void) const
 Get the signal number that has caused the process to terminate (UNIX only). More...
 
int CProcess::Wait (unsigned long timeout=kInfiniteTimeoutMs, CExitInfo *info=0) const
 Wait until process terminates. More...
 
int CProcess::WaitTimeout (unsigned long timeout, CExitInfo *info=0) const
 Wait until the process terminates or timeout expires. More...
 
int CProcess::WaitInfinite (CExitInfo *info=0) const
 Wait indefinitely until the process terminates. More...
 
static TProcessHandle CProcess::GetCurrentHandle (void)
 
static TPid CProcess::GetCurrentPid (void)
 
static TPid CProcess::GetParentPid (void)
 
bool CProcess::GetTimes (double *real, double *user, double *sys, EWhat what=eProcess)
 Get process execution times. More...
 
bool CProcess::GetMemoryUsage (SMemoryUsage &usage)
 Get process memory usage. More...
 
int CProcess::GetThreadCount (void)
 Get the number of threads in the process. More...
 
int CProcess::GetFileDescriptorsCount (void)
 Get the number of file descriptors consumed by the current process. More...
 
static TPid CProcess::Fork (TForkFlags flags=fFF_UpdateDiag)
 Fork the process. More...
 
static TPid CProcess::Daemonize (const char *logfile=0, TDaemonFlags flags=0)
 Go daemon. More...
 
TPid CProcess::x_GetPid (void) const
 
TProcessHandle CProcess::x_GetHandle (DWORD desired_access, DWORD *errcode=0) const
 
void CProcess::x_CloseHandle (TProcessHandle handle) const
 
virtual const char * CPIDGuardException::GetErrCodeString (void) const override
 
virtual void CPIDGuardException::ReportExtra (ostream &out) const override
 Constructor. More...
 
TPid CPIDGuardException::GetPID (void) const throw ()
 
virtual void CPIDGuardException::x_Assign (const CException &src) override
 
 CPIDGuard::CPIDGuard (const string &filename)
 Create/check PID file. More...
 
 CPIDGuard::CPIDGuard (const string &filename, const string &dir)
 Create/check PID file. More...
 
 CPIDGuard::~CPIDGuard (void)
 Destructor. More...
 
void CPIDGuard::Release (void)
 Release PID. More...
 
void CPIDGuard::Remove (void)
 Remove the file. More...
 
void CPIDGuard::UpdatePID (TPid pid=0)
 Update PID in the file. More...
 
TPid CPIDGuard::GetOldPID (void)
 Returns non-zero if there was a stale file. More...
 

Variables

string CInterProcessLock::m_Name
 Original name of the lock. More...
 
string CInterProcessLock::m_SystemName
 Adjusted name of the lock. More...
 
TLockHandle CInterProcessLock::m_Handle
 
CTimeout PInterProcessLock::m_Timeout
 
CTimeout PInterProcessLock::m_Granularity
 
const unsigned long kInfiniteTimeoutMs = kMax_ULong
 Infinite timeout in milliseconds. More...
 
size_t CProcessBase::SMemoryUsage::total
 Total memory usage. More...
 
size_t CProcessBase::SMemoryUsage::total_peak
 Peak total memory usage. More...
 
size_t CProcessBase::SMemoryUsage::resident
 Resident/working set size (RSS). More...
 
size_t CProcessBase::SMemoryUsage::resident_peak
 Peak resident set size ("high water mark") More...
 
size_t CProcessBase::SMemoryUsage::shared
 Shared memory usage. More...
 
size_t CProcessBase::SMemoryUsage::data
 Data segment size. More...
 
size_t CProcessBase::SMemoryUsage::stack
 Stack size of the initial thread in the process. More...
 
size_t CProcessBase::SMemoryUsage::text
 Text (code) segment size. More...
 
size_t CProcessBase::SMemoryUsage::lib
 Shared library code size. More...
 
size_t CProcessBase::SMemoryUsage::swap
 Swap space usage. More...
 
static const unsigned long CProcessBase::kDefaultKillTimeout = 1000
 Default wait time (milliseconds) between "soft" and "hard" attempts to terminate a process. More...
 
static const unsigned long CProcessBase::kInfiniteTimeoutMs = kMax_ULong
 Infinite timeout (milliseconds). More...
 
int CProcess::CExitInfo::state
 Process state (unknown/alive/terminated). More...
 
int CProcess::CExitInfo::status
 Process status information. More...
 
intptr_t CProcess::m_Process
 Process identifier. More...
 
EType CProcess::m_Type
 Type of process identifier. More...
 
ETriState CProcess::m_IsCurrent
 Status that m_Process represent the current process. More...
 
TPid CPIDGuardException::m_PID
 
string CPIDGuard::m_Path
 
TPid CPIDGuard::m_PID
 
unique_ptr< CInterProcessLockCPIDGuard::m_MTGuard
 
unique_ptr< CInterProcessLockCPIDGuard::m_PIDGuard
 

Friends

class CProcess::CExitInfo::CProcess
 

Lock/guard interface (to use with CGuard<> or CInterProcessLock_Guard)

Try to acquire the lock.

On any error, or if the lock is already held by another process and it could not be acquired within the allotted time period, throw CInterProcessLockException.

Parameters
timeoutHow much time Lock() should wait before giving up (with error). Default timeout value is interpreted as infinite.
granularityOn the systems (such as Unix) where there is no support for waiting for the lock with a timeout, the waiting is implemented using a loop of "try to lock" calls. This parameter specifies how much time to wait between these calls. It will be auto-limited to the closest whole delimiter of the 'timeout' value (and it will never exceed it). Default or infinite granularity timeout is converted into: MIN(timeout/5, 0.5sec).
Note
If 'timeout' is passed infinite, then the 'granularity' parameter actually will not be used at all – both on Windows and on Unix – as they both support a "honest" infinite waiting.
One process can acquire the lock more than once – but it need to use the same CInterProcessLock object for Lock(), and then it will need to call Unlock() as many times in order to release the lock for other processes.
If the lock object (such as file on UNIX or system mutex on Windows) doesn't exist it will be created.
See also
TryLock, SetLockTimeout
void CInterProcessLock::Lock (const CTimeout &timeout=CTimeout(CTimeout::eInfinite), const CTimeout &granularity=CTimeout(CTimeout::eInfinite))
 
void CInterProcessLock::Unlock (void)
 Release the lock. More...
 

Detailed Description

Typedef Documentation

◆ CInterProcessLock_Guard

◆ resource_type

◆ TDaemonFlags [1/2]

Bit-wise OR of FDaemonFlags.

See also
FDaemonFlags

Definition at line 328 of file ncbi_process.hpp.

◆ TDaemonFlags [2/2]

typedef unsigned int CProcess::TDaemonFlags

Bit-wise OR of FDaemonFlags.

See also
FDaemonFlags @ deprecated Please use CCurrentProcess::TDaemonFlags instead

Definition at line 749 of file ncbi_process.hpp.

◆ TForkFlags [1/2]

typedef unsigned CCurrentProcess::TForkFlags

Bit-wise OR of FForkFlags.

See also
FForkFlags

Definition at line 264 of file ncbi_process.hpp.

◆ TForkFlags [2/2]

typedef unsigned CProcess::TForkFlags

Bit-wise OR of FForkFlags.

See also
FForkFlags @ deprecated Please use CCurrentProcess::TForkFlags instead

Definition at line 721 of file ncbi_process.hpp.

◆ TLockHandle

OS-specific lock handle.

Definition at line 185 of file interprocess_lock.hpp.

◆ TPid

typedef pid_t TPid

Process identifier (PID) and process handle.

Definition at line 77 of file ncbi_process.hpp.

◆ TProcessHandle

Definition at line 78 of file ncbi_process.hpp.

Enumeration Type Documentation

◆ EErrCode [1/2]

Error types that file operations can generate.

Enumerator
eLockTimeout 

The lock could not be acquired in the time allotted.

eNameError 

Incorrect name for a lock.

eCreateError 

Cannot create the lock object in the OS.

eLockError 

Cannot acquire a lock (not eLockTimeout, eCreateError)

eUnlockError 

Cannot release the lock.

eMultipleLocks 

Attempt to lock already locked object in the same process.

eNotLocked 

Attempt to unlock a not-yet-acquired lock.

Definition at line 244 of file interprocess_lock.hpp.

◆ EErrCode [2/2]

Enumerator
eStillRunning 

The process listed in the file is still around.

eWrite 

Unable to write into the PID file.

Definition at line 811 of file ncbi_process.hpp.

◆ EType

How to interpret the used process identifier.

Enumerator
ePid 

A real process identifier (pid).

eHandle 

A process handle (MS Windows).

Definition at line 392 of file ncbi_process.hpp.

◆ EWhat

Process information "target".

Enumerator
eProcess 

Current process.

eChildren 

All children of the calling process.

eThread 

Current thread.

Definition at line 100 of file ncbi_process.hpp.

◆ FDaemonFlags [1/2]

Daemonization flags.

Enumerator
fDF_KeepCWD 

Don't change CWD to "/".

fDF_KeepStdin 

Keep stdin open as "/dev/null" (RO)

fDF_KeepStdout 

Keep stdout open as "/dev/null" (WO)

fDF_ImmuneTTY 

Make daemon immune to re-acquiring a controlling terminal.

fDF_KeepParent 

Do not exit the parent process but return.

fDF_AllowExceptions 

Throw an exception in case of an error.

fDF_AllowThreads 

Do not fail if pre-existing threads are detected.

Definition at line 318 of file ncbi_process.hpp.

◆ FDaemonFlags [2/2]

Daemonization flags @ deprecated Please use CCurrentProcess::FDaemonFlags instead.

Enumerator
fDF_KeepCWD 
fDF_KeepStdin 
fDF_KeepStdout 
fDF_ImmuneTTY 
fDF_KeepParent 
fDF_AllowExceptions 
fDF_AllowThreads 
fDontChroot 
fKeepStdin 
fKeepStdout 
fImmuneTTY 
fKeepParent 

Definition at line 732 of file ncbi_process.hpp.

◆ FForkFlags [1/2]

Forking flags.

Enumerator
fFF_UpdateDiag 

Reset diagnostics timer and log an app-start message in the child process.

fFF_Exec 

User plans to use exec(3) or execve(2) to "replace" forked child process with another program as soon as possible after fork, so we can skip some initialization and checks.

Cancels fFF_UpdateDiag.

fFF_AllowExceptions 

Throw an exception on error.

Definition at line 255 of file ncbi_process.hpp.

◆ FForkFlags [2/2]

Forking flags.

@ deprecated Please use CCurrentProcess::FForkFlags instead

Enumerator
fFF_UpdateDiag 
fFF_AllowExceptions 

Definition at line 715 of file ncbi_process.hpp.

Function Documentation

◆ CExitInfo()

CProcess::CExitInfo::CExitInfo ( void  )

Constructor.

Definition at line 113 of file ncbi_process.cpp.

References eExitInfo_Unknown, and CProcess::CExitInfo::status.

◆ CInterProcessLock()

CInterProcessLock::CInterProcessLock ( const string name)
Parameters
nameName of the lock. The name must be a Unix-path like, and its length must not exceed the maximum path length that is specific for the OS where the application runs.
Note
This constructor will not try to acquire a lock on creation.
  • On Unix, if a pathless file name is given, then the corresponding file will be created in a well-known system-specific location (usually as '/var/tmp/<name>'); if the name represents an absolute file path, then the exact path will be used to create the lock file.
  • On Windows, no actual files will be created at the specified path.
Attention
  • Relative paths are not allowed (regardless of the OS).
  • Empty name is not allowed.
  • Locking may not work for the files located on network filesystems (like NFS).
See also
Lock, TryLock, GetName

Definition at line 72 of file interprocess_lock.cpp.

References CDirEntry::IsAbsolutePath(), kInvalidLockHandle, CInterProcessLock::m_Handle, CInterProcessLock::m_Name, CInterProcessLock::m_SystemName, NCBI_THROW, NPOS, PATH_MAX, and NStr::Replace().

◆ CPIDGuard() [1/2]

CPIDGuard::CPIDGuard ( const string filename)

Create/check PID file.

If the file already exists and identifies a live process, throws CPIDGuardException.

Parameters
filenameName of the file to store PID. If "filename" contains path, it should be absolute and points to an existing directory. If "filename" contains no path, that $TMPDIR will be used on Unix, and HOME% on Windows to store it.

Definition at line 1720 of file ncbi_process.cpp.

References CDir::GetTmpDir(), CPIDGuard::m_MTGuard, CPIDGuard::m_Path, CDirEntry::MakePath(), CDirEntry::SplitPath(), and CPIDGuard::UpdatePID().

◆ CPIDGuard() [2/2]

CPIDGuard::CPIDGuard ( const string filename,
const string dir 
)

Create/check PID file.

If the file already exists and identifies a live process, throws CPIDGuardException.

Parameters
filenameName of the file to store PID. If should not include any path, relative or absolute.
dirAn absolute path to the existing directory on the file system to store PID file "filename". If "dir" is empty and "filename" contains no path, that $TMPDIR will be used on Unix, and HOME% on Windows to store it.
Deprecated:
Deprecated:

Definition at line 1738 of file ncbi_process.cpp.

References CDir::GetTmpDir(), CPIDGuard::m_MTGuard, CPIDGuard::m_Path, CDirEntry::MakePath(), CDirEntry::SplitPath(), and CPIDGuard::UpdatePID().

◆ CProcess() [1/2]

CProcess::CProcess ( TPid  process,
EType  type = eHandle 
)

Constructor.

Definition at line 525 of file ncbi_process.cpp.

◆ CProcess() [2/2]

CProcess::CProcess ( void  )

Default constructor. Uses the current process pid.

Definition at line 516 of file ncbi_process.cpp.

◆ Daemonize() [1/2]

TPid CCurrentProcess::Daemonize ( const char *  logfile = 0,
CCurrentProcess::TDaemonFlags  flags = 0 
)
static

Go daemon.

Return TPid(-1) in the daemon thread (the parent thread doesn't return unless fKeepParent is set), or TPid(daemon) in the parent thread. On error return 0 in the parent thread (no daemon created), see errno.

Reopen stderr/cerr in the daemon thread if "logfile" specified as non-NULL (stderr will open to "/dev/null" if "logfile" has been passed as ""), otherwise stderr is closed in the daemon thread.

Unless instructed by the "flags" parameter, the daemon thread has its stdin/cin and stdout/cout closed, and the current working directory changed to the root directory ("/").

If kept open, stdin and stdout are both redirected to "/dev/null". Opening a terminal device as a controlling terminal is allowed, unless fImmuneTTY is specified in the flags, which then causes a second fork() so that the resultant process won't be allowed to open a TTY as its controlling TTY (but only with an explicit O_NOCTTY, see open(2)), thus protecting the process from any blocking via TTY signaling.

Note that this call is somewhat destructive and may not be able to restore the process that called it to a state prior to the call in case of an error. So that calling process can find the standard

Definition at line 485 of file ncbi_process.cpp.

References ERR_POST_X, CCurrentProcess::fDF_AllowExceptions, flags, NCBI_THROW, s_Daemonize(), and CException::what().

Referenced by CProcess::Daemonize(), SGridWorkerNodeImpl::Run(), CBDBEnvKeeperApp::Run(), CNetScheduleDApp::Run(), CNetStorageDApp::Run(), and CPubseqGatewayApp::Run().

◆ Daemonize() [2/2]

static TPid CProcess::Daemonize ( const char *  logfile = 0,
TDaemonFlags  flags = 0 
)
inlinestatic

Go daemon.

Return TPid(-1) in the daemon thread (the parent thread doesn't return unless fKeepParent is set), or TPid(daemon) in the parent thread. On error return 0 in the parent thread (no daemon created), see errno.

Reopen stderr/cerr in the daemon thread if "logfile" specified as non-NULL (stderr will open to "/dev/null" if "logfile" has been passed as ""), otherwise stderr is closed in the daemon thread.

Unless instructed by the "flags" parameter, the daemon thread has its stdin/cin and stdout/cout closed, and the current working directory changed to the root directory ("/").

If kept open, stdin and stdout are both redirected to "/dev/null". Opening a terminal device as a controlling terminal is allowed, unless fImmuneTTY is specified in the flags, which then causes a second fork() so that the resultant process won't be allowed to open a TTY as its controlling TTY (but only with an explicit O_NOCTTY, see open(2)), thus protecting the process from any blocking via TTY signaling.

Note that this call is somewhat destructive and may not be able to restore the process that called it to a state prior to the call in case of an error. So that calling process can find the standard

Deprecated:
Please use CCurrentProcess::Daemonize() instead

Definition at line 778 of file ncbi_process.hpp.

References CCurrentProcess::Daemonize(), and flags.

Referenced by Daemonize(), and main().

◆ Fork() [1/2]

TPid CCurrentProcess::Fork ( CProcess::TForkFlags  flags = fFF_UpdateDiag)
static

Fork the process.

Update PID and GUID used for logging (by default).

Warning
The child process is created with a single thread, the one that called fork(). The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables, open file descriptors, pthreads objects, and etc. But child process doesn't inherit or reset many other properties. Be aware. Consult fork(2) man-pages for a full details.
Fork() should be called from a single-threaded application, even if it built in MT configurations. If you have many running threads in the parent process, this can lead to unpredictable results and various deadlocks. "After a fork() in a multithreaded program, the child can safely call only async-signal-safe functions." until it calls execve(2) to replace a forked child process as soon as possible after Fork(). See https://man7.org/linux/man-pages/man2/fork.2.html
Returns
In the parent process, the call returns the child process ID. In the child process, the call returns zero. In case of an error, unless the fFF_AllowExceptions flag is specified, the call returns -1.
Exceptions
Ifthe fFF_AllowExceptions flag is specified, throws a CCoreException in case of a fork(2) failure. If the platform does not support process forking, an exception is always thrown regardless of whether the fFF_AllowExceptions flag is specified.
Note
Implemented for Unix only.
See also
ForkForExec

Definition at line 308 of file ncbi_process.cpp.

References ERR_POST_X, f(), CProcess::fFF_AllowExceptions, CProcess::fFF_UpdateDiag, flags, CDiagContext::fOnFork_AsyncSafe, CDiagContext::fOnFork_PrintStart, CDiagContext::fOnFork_ResetTimer, CCurrentProcess::GetThreadCount(), NCBI_THROW, NCBI_THROW_FMT, s_ErrnoToString(), CDiagContext::UpdateOnFork(), and Warning().

Referenced by CProcess::Fork(), CCurrentProcess::ForkForExec(), SGridWorkerNodeImpl::Run(), and s_Daemonize().

◆ Fork() [2/2]

static TPid CProcess::Fork ( TForkFlags  flags = fFF_UpdateDiag)
inlinestatic

Fork the process.

Deprecated:
Please use CCurrentProcess::Fork() instead

Definition at line 726 of file ncbi_process.hpp.

References flags, and CCurrentProcess::Fork().

◆ ForkForExec()

static TPid CCurrentProcess::ForkForExec ( TForkFlags  flags = 0)
inlinestatic

Fork the process for "replacing" a child process with a new process.

Special version of Fork() with a different name for easier recognizing. Assumes that the user will replace child process after this call with using exec(3) or execve(2) calls as soon as possible. The child can safely call only async-signal-safe functions after Fork() until the child process has replaced. This mean no memory allocation, streams, logging and etc. Please consult the list of sync-signal-safe functions before using, like: https://man7.org/linux/man-pages/man7/signal-safety.7.html

Returns
In the parent process, the call returns the child process ID. In the child process, the call returns zero. In case of an error, unless the fFF_AllowExceptions flag is specified, the call returns -1.
See also
Fork

Definition at line 315 of file ncbi_process.hpp.

References CCurrentProcess::fFF_Exec, flags, and CCurrentProcess::Fork().

◆ GetCurrentHandle()

static TProcessHandle CProcess::GetCurrentHandle ( void  )
inlinestatic
Deprecated:
Please use CCurrentProcess::GetHandle() instead

Definition at line 605 of file ncbi_process.hpp.

References CCurrentProcess::GetHandle().

◆ GetCurrentPid()

static TPid CProcess::GetCurrentPid ( void  )
inlinestatic
Deprecated:
Please use CCurrentProcess::GetPid() instead

Definition at line 611 of file ncbi_process.hpp.

References CCurrentProcess::GetPid().

◆ GetErrCodeString() [1/2]

const char * CInterProcessLockException::GetErrCodeString ( void  ) const
overridevirtual

◆ GetErrCodeString() [2/2]

const char * CPIDGuardException::GetErrCodeString ( void  ) const
overridevirtual

◆ GetExitCode()

int CProcess::CExitInfo::GetExitCode ( void  ) const

Get process exit code.

Works only if IsExited() returns TRUE, otherwise returns -1.

Definition at line 163 of file ncbi_process.cpp.

Referenced by SGridWorkerNodeImpl::Run(), s_Close(), s_GetBigBedFeats(), s_GetBigBedSummary(), s_GetData(), CBLASTSeqToolJob::x_CreateProjectItems(), and CWigGraph::x_RunBigWigScript().

◆ GetFileDescriptorsCount() [1/2]

int CCurrentProcess::GetFileDescriptorsCount ( int soft_limit = NULL,
int hard_limit = NULL 
)
static

Get the number of file descriptors consumed by the current process, and optional system wide file descriptor limits.

Parameters
soft_limitPointer to a variable that receives system wide soft limit. -1 means it was impossible to get the limit.
hard_limitPointer to a variable that receives system wide hard limit. -1 means it was impossible to get the limit.
Returns
Number of file descriptors consumed by the process, or -1 on error.
Note
NULL arguments will not be filled in.
See also
CProcess::GetFileDescriptorsCount
Note
Unix inly

Definition at line 1659 of file ncbi_process.cpp.

References CNcbiError::eNotSupported, CNcbiError::eUnknown, CCurrentProcess::GetPid(), n, and CNcbiError::Set().

Referenced by CheckFDLimit(), CProcess::GetFileDescriptorsCount(), CPubseqGatewayApp::OnInfo(), and CNetScheduleHandler::x_ProcessHealth().

◆ GetFileDescriptorsCount() [2/2]

int CProcess::GetFileDescriptorsCount ( void  )

Get the number of file descriptors consumed by the current process.

For current process pid/handle it is the same as CCurrentProcess::GetFileDescriptorsCount().

Returns
Number of file descriptors consumed by the process, or -1 on error.
See also
CCurrentProcess::GetFileDescriptorsCount
Note
Unix inly

Definition at line 1643 of file ncbi_process.cpp.

References CNcbiError::eNotSupported, CCurrentProcess::GetFileDescriptorsCount(), CProcess::GetPid(), CProcess::IsCurrent(), and CNcbiError::Set().

◆ GetHandle() [1/2]

TProcessHandle CCurrentProcess::GetHandle ( void  )
static

Get process handle for the current process (esp.

on Windows).

Note
On Windows process identifiers and process handles are different. On UNIX handle/pid are the same and GetHandle()/GetPid() return the same value.
See also
GetPid

Definition at line 237 of file ncbi_process.cpp.

References CProcess::GetPid().

Referenced by CProcess::GetCurrentHandle(), CProcess::IsCurrent(), and CTar::x_Append().

◆ GetHandle() [2/2]

TProcessHandle CProcess::GetHandle ( void  ) const
inline

Get stored process handle.

See also
GetCurrentHandle

Definition at line 419 of file ncbi_process.hpp.

References CProcess::m_Process.

Referenced by CProcess::IsCurrent(), CRemoteAppReaperTask::operator()(), CProcess::x_GetHandle(), and CProcess::x_GetPid().

◆ GetMemoryUsage() [1/2]

bool CCurrentProcess::GetMemoryUsage ( SMemoryUsage usage)
static

◆ GetMemoryUsage() [2/2]

bool CProcess::GetMemoryUsage ( SMemoryUsage usage)

Get process memory usage.

For current process pid/handle it is the same as CCurrentProcess::GetMemoryUsage().

Parameters
usageTotal memory usage by a process, in bytes. See SMemoryUsage structure.
residentResident set size (RSS), in bytes. RSS is the portion of memory occupied by a process that is held in main memory (RAM).
shared(optional) Shared memory usage, in bytes.
data(optional) Data + stack usage, in bytes.
Returns
TRUE on success; or FALSE otherwise.
Note
NULL arguments will not be filled in.
See also
CCurrentProcess::GetMemoryUsage, SMemoryUsage

Definition at line 1445 of file ncbi_process.cpp.

References CNcbiError::eNotSupported, GetMemoryUsage(), CCurrentProcess::GetMemoryUsage(), CProcess::GetPid(), HANDLE, CProcess::IsCurrent(), s_Win_GetMemoryUsage(), CNcbiError::Set(), usage, CProcess::x_CloseHandle(), and CProcess::x_GetHandle().

◆ GetName()

const string& CInterProcessLock::GetName ( void  ) const
inline

Get the original name of the lock – exactly as it was specified in the constructor.

Definition at line 171 of file interprocess_lock.hpp.

References CInterProcessLock::m_Name.

◆ GetOldPID()

TPid CPIDGuard::GetOldPID ( void  )
inline

Returns non-zero if there was a stale file.

Always return 0, because new implementation don't allow any stale file left on the file system.

Deprecated:

Definition at line 928 of file ncbi_process.hpp.

◆ GetParentPid() [1/2]

TPid CCurrentProcess::GetParentPid ( void  )
static

Get process identifier (pid) for the parent of the current process.

See also
GetPid

Definition at line 259 of file ncbi_process.cpp.

References CWinFeature::FindProcessEntry(), and CNcbiError::SetFromWindowsError().

Referenced by CProcess::GetParentPid().

◆ GetParentPid() [2/2]

static TPid CProcess::GetParentPid ( void  )
inlinestatic
Deprecated:
Please use CCurrentProcess::GetParentPid() instead

Definition at line 617 of file ncbi_process.hpp.

References CCurrentProcess::GetParentPid().

◆ GetPid() [1/2]

TPid CCurrentProcess::GetPid ( void  )
static

◆ GetPid() [2/2]

TPid CProcess::GetPid ( void  ) const
inline

◆ GetPID()

TPid CPIDGuardException::GetPID ( void  ) const
throw (
)
inline

Definition at line 833 of file ncbi_process.hpp.

References CPIDGuardException::m_PID.

◆ GetSignal()

int CProcess::CExitInfo::GetSignal ( void  ) const

Get the signal number that has caused the process to terminate (UNIX only).

Works only if IsSignaled() returns TRUE, otherwise returns -1.

Definition at line 176 of file ncbi_process.cpp.

Referenced by SGridWorkerNodeImpl::Run(), s_Close(), and CWigGraph::x_RunBigWigScript().

◆ GetSystemName()

const string& CInterProcessLock::GetSystemName ( void  ) const
inline

Get the internal name of the lock – as it is being used in the calls to the OS (such as '/var/tmp/<name>' rather than just '<name>' on UNIX)

Definition at line 175 of file interprocess_lock.hpp.

References CInterProcessLock::m_SystemName.

◆ GetThreadCount() [1/2]

int CCurrentProcess::GetThreadCount ( void  )
static

Get the number of threads in the current process.

Returns
Number of threads in the current process, or -1 on error.
See also
CProcess::GetThreadCount

Definition at line 1624 of file ncbi_process.cpp.

References CNcbiError::eNotSupported, CWinFeature::FindProcessEntry(), CNcbiError::Set(), and CNcbiError::SetFromWindowsError().

Referenced by CCurrentProcess::Fork(), CPubseqGatewayApp::OnInfo(), and CNetScheduleHandler::x_ProcessHealth().

◆ GetThreadCount() [2/2]

int CProcess::GetThreadCount ( void  )

Get the number of threads in the process.

For current process pid/handle it is the same as CCurrentProcess::GetThreadCount().

Returns
Number of threads in the current process, or -1 on error.
See also
CCurrentProcess::GetThreadCount

Definition at line 1605 of file ncbi_process.cpp.

References CNcbiError::eNotSupported, CWinFeature::FindProcessEntry(), CProcess::GetPid(), CNcbiError::Set(), CNcbiError::SetFromWindowsError(), and CProcess::x_GetPid().

◆ GetTimes() [1/2]

bool CCurrentProcess::GetTimes ( double *  real,
double *  user,
double *  sys,
EWhat  what = eProcess 
)
static

Get current process execution times.

Parameters
realPointer to a variable that receives the amount of time in second that the current process/thread has executed, when available.
Note
Here is no portable solution to get 'real' process execution time, not all OS have an API to get such information from the system, or it can include children times as well... but it is available on some platforms like Windows, Linux and BSD.
Parameters
userPointer to a variable that receives the amount of time in seconds that the process, process tree or thread has executed in user mode. Note, that this value can exceed the amount of "real" time if the process executes across multiple CPU cores (OS dependent).
sysPointer to a variable that receives the amount of time in second that the current process, process tree or thread has executed in kernel mode.
whatApplies to user and system time parameters only, real time calculates for the current process only. Defines what times to measure: current thread, process or children processes:
  • eProcess: Current process, which is the sum of resources used by all threads in the process.
  • eChildren: (limited support) All children of the calling process that have terminated already and been waited for. These statistics will include the resources used by grandchildren, and further removed descendants, if all of the intervening descendants waited on their terminated children.
  • eThread: (limited support) Usage statistics for the calling thread.
Returns
TRUE on success; or FALSE on error. If some value cannot be calculated on a current platform for "what" target, it will be set to a negative value.
Note
NULL arguments will not be filled in.
See also
CProcess::GetTimes

Definition at line 1276 of file ncbi_process.cpp.

References _TROUBLE, buf, CProcessBase::eChildren, CNcbiError::eNotSupported, CProcessBase::eProcess, CProcessBase::eThread, CSystemInfo::GetClockTicksPerSecond(), kUndefined, s_Win_GetHandleTimes(), CNcbiError::Set(), CNcbiError::SetFromErrno(), and t.

Referenced by CProcess::GetTimes(), CPubseqGatewayApp::OnInfo(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().

◆ GetTimes() [2/2]

bool CProcess::GetTimes ( double *  real,
double *  user,
double *  sys,
CProcess::EWhat  what = eProcess 
)

Get process execution times.

For current process pid/handle it is the same as CCurrentProcess::GetTimes().

Parameters
realPointer to a variable that receives the amount of time in second that the process/thread has executed, when available.
Note
Here is no portable solution to get 'real' process execution time, not all OS have an API to get such information from the system, or it can include children times as well... but it is available on some platforms like Windows, Linux and BSD.
Parameters
userPointer to a variable that receives the amount of time in seconds that the process, process tree or thread has executed in user mode. Note, that this value can exceed the amount of "real" time if the process executes across multiple CPU cores (OS dependent).
sysPointer to a variable that receives the amount of time in second that the current process, process tree or thread has executed in kernel mode.
whatDefines what times to measure: current thread, process or children processes:
  • eProcess: Current process, which is the sum of resources used by all threads in the process.
  • eChildren: (limited support) All children of the process that have terminated already and been waited for. These statistics will include the resources used by grandchildren, and further removed descendants, if all of the intervening descendants waited on their terminated children.
  • eThread: (limited support) Usage statistics for the calling thread. CProcess should be initialized with the current process pid/handle, or result is undefined.
Returns
TRUE on success; or FALSE on error. If some value cannot be calculated on a current platform for "what" target, it will be set to a negative value.
Note
NULL arguments will not be filled in.
Usually it is possible to query information from users own processes, but you should have an appropriate permissions to query other processes.
On Windows it is possible to get process times even for terminated processes, that are Wait()-ed for, even if IsAlive() returns false.
See also
CCurrentProcess::GetTimes

Definition at line 1225 of file ncbi_process.cpp.

References CProcessBase::eChildren, CNcbiError::eNotSupported, CProcessBase::eProcess, CProcessBase::eThread, CProcess::GetPid(), CCurrentProcess::GetTimes(), HANDLE, CProcess::IsCurrent(), kUndefined, s_Win_GetHandleTimes(), CNcbiError::Set(), CProcess::x_CloseHandle(), and CProcess::x_GetHandle().

◆ GetType()

EType CProcess::GetType ( void  ) const
inline

Get type of stored process identifier.

Note
On Windows process identifiers and process handles are different. On UNIX handle/pid is the same, and GetHandle()/GetPid() return the same value.

Definition at line 414 of file ncbi_process.hpp.

References CProcess::m_Type.

Referenced by CProcess::IsCurrent(), CProcess::Kill(), CProcess::x_CloseHandle(), CProcess::x_GetHandle(), and CProcess::x_GetPid().

◆ IsAlive() [1/2]

bool CProcess::IsAlive ( void  ) const

Check for process existence.

Returns
TRUE - if the process is still running. FALSE - if the process does exist or has already been terminated.
Note
On Unix this method returns TRUE also for "zombie" processes, those finished executing but waiting to post their exit code. Usually the parent process should call Wait() for such processes and release (aka reap) them.
See also
Wait

Definition at line 609 of file ncbi_process.cpp.

References _ASSERT, CProcess::GetPid(), HANDLE, CProcess::x_CloseHandle(), and CProcess::x_GetHandle().

Referenced by CProcess::Kill(), and CBamLoadingJob::x_CreateDataLoader().

◆ IsAlive() [2/2]

bool CProcess::CExitInfo::IsAlive ( void  ) const

TRUE if the process is still alive.

Definition at line 126 of file ncbi_process.cpp.

References eExitInfo_Alive, and EXIT_INFO_CHECK.

Referenced by s_Close().

◆ IsCurrent()

bool CProcess::IsCurrent ( void  )

◆ IsExited()

bool CProcess::CExitInfo::IsExited ( void  ) const

TRUE if the process terminated normally.

Definition at line 133 of file ncbi_process.cpp.

References eExitInfo_Terminated, and EXIT_INFO_CHECK.

Referenced by CRemoteAppReaperTask::operator()(), SGridWorkerNodeImpl::Run(), and s_Close().

◆ IsPresent()

bool CProcess::CExitInfo::IsPresent ( void  ) const

TRUE if the object contains information about the process state.

All other methods' return values are good only if this method returns TRUE, otherwise they may throw exceptions.

Definition at line 120 of file ncbi_process.cpp.

References eExitInfo_Unknown.

Referenced by SGridWorkerNodeImpl::Run(), and s_Close().

◆ IsSignaled()

bool CProcess::CExitInfo::IsSignaled ( void  ) const

TRUE if the process terminated by a signal (UNIX only).

Definition at line 148 of file ncbi_process.cpp.

References eExitInfo_Terminated, and EXIT_INFO_CHECK.

Referenced by CRemoteAppReaperTask::operator()(), SGridWorkerNodeImpl::Run(), s_Close(), and CWigGraph::x_RunBigWigScript().

◆ Kill()

bool CProcess::Kill ( unsigned long  timeout = kDefaultKillTimeout)

Terminate process.

First try "soft" and then try "hard" attempt to terminate the process.

Note
Even the "hard" attempt does not guarantee that the process will be actually killed. Process termination can take some time, and the process may remain "alive" even after the "hard" termination attempt.
Parameters
timeoutWait time in milliseconds between first "soft" and second "hard" attempts to terminate the process. If timeout is zero then use an unsafe process termination: just try to terminate the process without checks that it is really gone.
Note
On UNIX in case of a zero or very small timeout, the killed process may not be released immediately (and continue to persist as a zombie process) even after this call.
Returns
TRUE - if the process did not exist or was successfully terminated. FALSE - if the process is still running, cannot be terminated, or it is terminating right now (but is still incomplete).
See also
KillGroup, KillGroupById

Definition at line 628 of file ncbi_process.cpp.

References _ASSERT, CStopWatch::Elapsed(), CProcess::ePid, FALSE, CProcess::GetPid(), CProcess::GetType(), HANDLE, INVALID_HANDLE_VALUE, CProcess::IsAlive(), CProcess::IsCurrent(), kMilliSecondsPerSecond, kWaitPrecisionMs, NULL, CNcbiError::SetFromErrno(), CNcbiError::SetFromWindowsError(), CNcbiError::SetWindowsError(), SleepMilliSec(), CStopWatch::Start(), CProcess::x_CloseHandle(), and CProcess::x_GetHandle().

Referenced by CBLASTSeqToolJob::RequestCancel(), CWindowMaskerJob::RequestCancel(), s_Close(), s_Win_KillGroup(), CBamLoadingJob::x_CreateDataLoader(), and CWNJobWatcher::x_KillNode().

◆ KillGroup()

bool CProcess::KillGroup ( unsigned long  timeout = kDefaultKillTimeout) const

Terminate a group of processes.

This method tries to terminate all processes in the group to which process, specified in the constructor, belongs.

Parameters
timeoutWait time in milliseconds between first "soft" and second "hard" attempts to terminate the process group. If timeout is zero then use an unsafe process termination: just try to terminate the group without checks that it is really gone.
Note
On UNIX in case of a zero or very small timeout, some processes may not be released (and continue to persist as zombie processes) even after this call. On MS Windows this method tries to terminate the process itself and all of its children. But in case when one of the processes, which should be terminated, spawns a new process at the very moment that this method gets called, the new process may not be terminated.
Returns
TRUE - if the process group did not exist or was successfully terminated. FALSE - if the process group is still running, cannot be terminated, or it is terminating right now (but is still incomplete).
See also
Kill

Definition at line 919 of file ncbi_process.cpp.

References CProcess::GetPid(), CProcess::KillGroupById(), result, s_Win_KillGroup(), CNcbiError::SetFromErrno(), CStopWatch::Start(), and CProcess::x_GetPid().

Referenced by CRemoteAppReaperTask::operator()(), and s_Close().

◆ KillGroupById()

bool CProcess::KillGroupById ( TPid  pgid,
unsigned long  timeout = kDefaultKillTimeout 
)
static

Terminate a group of processes with specified ID.

Note: Implemented on UNIX only, on Windows returns FALSE.

Parameters
pgidProcess group ID to terminate. if "pgid" parameter is zero, terminate the process group of the current process
timeoutWait time in milliseconds between first "soft" and second "hard" attempts to terminate the process group. If timeout is zero then use an unsafe process termination: just try to terminate the process group without checks whether it is really gone.
Note
On UNIX in case of a zero or very small timeout, some processes from the process group to be killed may not be released immediately (and continue to persist as zombie processes) even after this call.
Returns
TRUE - if the process group did not exist or was successfully terminated. FALSE - if the process group is still running, cannot be terminated, or it is terminating right now (but is still incomplete).
See also
Kill

Definition at line 951 of file ncbi_process.cpp.

References _ASSERT, CNcbiError::eNotSupported, kWaitPrecisionMs, CNcbiError::Set(), CNcbiError::SetFromErrno(), and SleepMilliSec().

Referenced by CProcess::KillGroup().

◆ Lock()

void CInterProcessLock::Lock ( const CTimeout timeout = CTimeout(CTimeout::eInfinite),
const CTimeout granularity = CTimeout(CTimeout::eInfinite) 
)

◆ NCBI_EXCEPTION_DEFAULT()

CInterProcessLockException::NCBI_EXCEPTION_DEFAULT ( CInterProcessLockException  ,
CCoreException   
)

◆ operator()()

void PInterProcessLock::operator() ( resource_type resource) const
inline

◆ PInterProcessLock()

PInterProcessLock::PInterProcessLock ( const CTimeout timeout,
const CTimeout granularity = CTimeout(CTimeout::eInfinite) 
)
inline

◆ Release()

void CPIDGuard::Release ( void  )

Release PID.

Decrease reference counter for current PID and remove the file if it is not used more (reference counter is 0).

Definition at line 1768 of file ncbi_process.cpp.

References _T_CSTRING, in(), CPIDGuard::m_MTGuard, CPIDGuard::m_Path, CPIDGuard::m_PID, CPIDGuard::m_PIDGuard, NCBI_THROW, NcbiSys_strerror, out(), CGuard< Resource, Lock, Unlock, ReportExceptions >::Release(), CDirEntry::Remove(), and trunc.

Referenced by CPIDGuard::~CPIDGuard().

◆ Remove() [1/2]

void CInterProcessLock::Remove ( void  )

Call Unlock() and removes lock object from the system.

On Unix, the Unlock() method do not remove used lock file from the system, it just release a lock. If you don't need this file anymore, use this method to remove it. We cannot remove it automaticaly in the Unlock(), because on Unix locking/unlocking is not an atomic operations and race condition is possible if at time of deleting some other process wait to acquire a lock using the same lock file.

Note
On Windows, it works almost as Unlock().
See also
Unlock

Definition at line 373 of file interprocess_lock.cpp.

References _T_XCSTRING, kInvalidLockHandle, CInterProcessLock::m_Handle, CInterProcessLock::m_SystemName, NcbiSys_unlink, and CInterProcessLock::Unlock().

◆ Remove() [2/2]

void CPIDGuard::Remove ( void  )

Remove the file.

Remove PID file forcibly, ignoring any reference counter.

Definition at line 1821 of file ncbi_process.cpp.

References CPIDGuard::m_MTGuard, CPIDGuard::m_Path, CPIDGuard::m_PIDGuard, and CDirEntry::Remove().

◆ ReportExtra()

virtual void CPIDGuardException::ReportExtra ( ostream &  out) const
inlineoverridevirtual

Constructor.

Definition at line 828 of file ncbi_process.hpp.

References CPIDGuardException::m_PID, and out().

◆ TryLock()

bool CInterProcessLock::TryLock ( void  )

Try to acquire the lock.

Acquire the lock if it can be done right away. Return immediately.

Returns
  • TRUE, if the lock has been successfully acquired.
  • FALSE, if the lock is already held by another process or if any error occurs.
See also
Lock

Definition at line 382 of file interprocess_lock.cpp.

References CInterProcessLock::Lock().

◆ Unlock()

void CInterProcessLock::Unlock ( void  )

Release the lock.

On any error (including when the lock is not held by the process), throw CInterProcessLockException.

See also
Lock, TryLock, Remove

Definition at line 320 of file interprocess_lock.cpp.

References _VERIFY, close(), kInvalidLockHandle, CInterProcessLock::m_Handle, CInterProcessLock::m_SystemName, NCBI_THROW, s_Locks, and SEEK_SET.

Referenced by CInterProcessLock::Remove(), and CInterProcessLock::~CInterProcessLock().

◆ UpdatePID()

void CPIDGuard::UpdatePID ( TPid  pid = 0)

Update PID in the file.

Parameters
pidThe new process ID to store (defaults to the current PID); useful when the real work occurs in a child process that outlives the parent.

Definition at line 1842 of file ncbi_process.cpp.

References _T_CSTRING, CProcess::ePid, CCurrentProcess::GetPid(), in(), CPIDGuard::m_MTGuard, CPIDGuard::m_Path, CPIDGuard::m_PID, CPIDGuard::m_PIDGuard, NCBI_THROW, NCBI_THROW2, NcbiSys_strerror, out(), and trunc.

Referenced by CPIDGuard::CPIDGuard().

◆ Wait()

int CProcess::Wait ( unsigned long  timeout = kInfiniteTimeoutMs,
CExitInfo info = 0 
) const

Wait until process terminates.

Wait until the process terminates or timeout expires. Return immediately if the specified process has already terminated.

Parameters
timeoutTime interval in milliseconds (infinite by default) to wait.
infoExtended exit information for terminated process. Note that if CProcess::Kill() was used to terminate the process then the extended information may not be available.
Returns
  • Exit code of the process, if the call completed without errors.
  • (-1) if an error occurred or it was impossible to get the exit code of the process. If 'info' parameter is specified, then it is filled with additional information about the process.
Note
It is recommended to call this method for all processes started in eNoWait or eDetach modes (except on Windows for eDetach), because it releases "zombie" processes, i.e. those finished running and waiting for their parent to obtain their exit code. If Wait() is not called somewhere, then the child process will be completely removed from the system only when its parent process ends.
Infinite timeout can cause an application to stop responding.
See also
IsAlive, CExitInfo, CExec, WaitInfinite, WaitTimeout

Definition at line 1013 of file ncbi_process.cpp.

References _ASSERT, eExitInfo_Alive, eExitInfo_Terminated, eExitInfo_Unknown, CProcess::GetPid(), HANDLE, info, CProcessBase::kInfiniteTimeoutMs, kWaitPrecisionMs, CNcbiError::SetFromErrno(), SleepMilliSec(), CProcess::x_CloseHandle(), and CProcess::x_GetHandle().

Referenced by CRemoteAppReaperTask::operator()(), SGridWorkerNodeImpl::Run(), s_Close(), s_GetBigBedFeats(), s_GetBigBedSummary(), s_GetData(), CExec::Wait(), CProcess::WaitInfinite(), CProcess::WaitTimeout(), CBamLoadingJob::x_CreateDataLoader(), CBLASTSeqToolJob::x_CreateProjectItems(), and CWigGraph::x_RunBigWigScript().

◆ WaitInfinite()

int CProcess::WaitInfinite ( CExitInfo info = 0) const
inline

Wait indefinitely until the process terminates.

See also
Wait

Definition at line 595 of file ncbi_process.hpp.

References info, CProcessBase::kInfiniteTimeoutMs, and CProcess::Wait().

◆ WaitTimeout()

int CProcess::WaitTimeout ( unsigned long  timeout,
CExitInfo info = 0 
) const
inline

Wait until the process terminates or timeout expires.

See also
Wait

Definition at line 589 of file ncbi_process.hpp.

References info, and CProcess::Wait().

◆ x_Assign()

virtual void CPIDGuardException::x_Assign ( const CException src)
inlineoverrideprotectedvirtual

Definition at line 836 of file ncbi_process.hpp.

References CPIDGuardException::m_PID, and CException::x_Assign().

◆ x_CloseHandle()

void CProcess::x_CloseHandle ( TProcessHandle  handle) const
private

◆ x_GetHandle()

TProcessHandle CProcess::x_GetHandle ( DWORD  desired_access,
DWORD errcode = 0 
) const
private

◆ x_GetPid()

TPid CProcess::x_GetPid ( void  ) const
private

◆ ~CInterProcessLock()

CInterProcessLock::~CInterProcessLock ( void  )

◆ ~CPIDGuard()

CPIDGuard::~CPIDGuard ( void  )

Destructor.

Just calls Release();

Definition at line 1760 of file ncbi_process.cpp.

References CPIDGuard::m_MTGuard, CPIDGuard::m_PIDGuard, and CPIDGuard::Release().

Variable Documentation

◆ data

size_t CProcessBase::SMemoryUsage::data

◆ kDefaultKillTimeout

const unsigned long CProcessBase::kDefaultKillTimeout = 1000
static

Default wait time (milliseconds) between "soft" and "hard" attempts to terminate a process.

Definition at line 133 of file ncbi_process.hpp.

Referenced by CPipe::ExecWait(), and s_Close().

◆ kInfiniteTimeoutMs [1/2]

const unsigned long kInfiniteTimeoutMs = kMax_ULong

Infinite timeout in milliseconds.

Deprecated:
Need to be removed to use constants from CProcessBase and derived classes

Definition at line 64 of file ncbi_process.hpp.

Referenced by SGridWorkerNodeImpl::Run(), CExec::Wait(), and CBLASTSeqToolJob::x_CreateProjectItems().

◆ kInfiniteTimeoutMs [2/2]

const unsigned long CProcessBase::kInfiniteTimeoutMs = kMax_ULong
static

Infinite timeout (milliseconds).

Definition at line 136 of file ncbi_process.hpp.

Referenced by CProcess::Wait(), and CProcess::WaitInfinite().

◆ lib

size_t CProcessBase::SMemoryUsage::lib

Shared library code size.

Definition at line 127 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), and CNetScheduleHandler::x_ProcessHealth().

◆ m_Granularity

CTimeout PInterProcessLock::m_Granularity
private

◆ m_Handle

TLockHandle CInterProcessLock::m_Handle
private

◆ m_IsCurrent

ETriState CProcess::m_IsCurrent
private

Status that m_Process represent the current process.

Definition at line 798 of file ncbi_process.hpp.

Referenced by CProcess::IsCurrent().

◆ m_MTGuard

unique_ptr<CInterProcessLock> CPIDGuard::m_MTGuard
private

◆ m_Name

string CInterProcessLock::m_Name
private

Original name of the lock.

Definition at line 179 of file interprocess_lock.hpp.

Referenced by CInterProcessLock::CInterProcessLock(), and CInterProcessLock::GetName().

◆ m_Path

string CPIDGuard::m_Path
private

◆ m_PID [1/2]

TPid CPIDGuardException::m_PID
private

◆ m_PID [2/2]

TPid CPIDGuard::m_PID
private

Definition at line 932 of file ncbi_process.hpp.

Referenced by CPIDGuard::Release(), and CPIDGuard::UpdatePID().

◆ m_PIDGuard

unique_ptr<CInterProcessLock> CPIDGuard::m_PIDGuard
private

◆ m_Process

intptr_t CProcess::m_Process
private

Process identifier.

Definition at line 796 of file ncbi_process.hpp.

Referenced by CProcess::GetHandle(), and CProcess::GetPid().

◆ m_SystemName

string CInterProcessLock::m_SystemName
private

◆ m_Timeout

CTimeout PInterProcessLock::m_Timeout
private

◆ m_Type

EType CProcess::m_Type
private

Type of process identifier.

Definition at line 797 of file ncbi_process.hpp.

Referenced by CProcess::GetType().

◆ resident

size_t CProcessBase::SMemoryUsage::resident

Resident/working set size (RSS).

RSS is the portion of memory occupied by a process that is held in main memory (RAM).

Definition at line 119 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().

◆ resident_peak

size_t CProcessBase::SMemoryUsage::resident_peak

Peak resident set size ("high water mark")

Definition at line 122 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().

◆ shared

size_t CProcessBase::SMemoryUsage::shared

◆ stack

size_t CProcessBase::SMemoryUsage::stack

Stack size of the initial thread in the process.

Definition at line 125 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().

◆ state

int CProcess::CExitInfo::state
private

Process state (unknown/alive/terminated).

Definition at line 553 of file ncbi_process.hpp.

◆ status

int CProcess::CExitInfo::status
private

Process status information.

Definition at line 554 of file ncbi_process.hpp.

Referenced by CProcess::CExitInfo::CExitInfo().

◆ swap

size_t CProcessBase::SMemoryUsage::swap

Swap space usage.

Definition at line 128 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), and CNetScheduleHandler::x_ProcessHealth().

◆ text

size_t CProcessBase::SMemoryUsage::text

Text (code) segment size.

Definition at line 126 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), and CNetScheduleHandler::x_ProcessHealth().

◆ total

size_t CProcessBase::SMemoryUsage::total

◆ total_peak

size_t CProcessBase::SMemoryUsage::total_peak

Peak total memory usage.

Definition at line 118 of file ncbi_process.hpp.

Referenced by CPubseqGatewayApp::OnInfo(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().

Friends

◆ CProcess

friend class CProcess
friend

Definition at line 556 of file ncbi_process.hpp.

Modified on Tue Jul 16 13:16:41 2024 by modify_doxy.py rev. 669887