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.
Process identifier (PID) and process handle.
Definition at line 77 of file ncbi_process.hpp.
◆ TProcessHandle ◆ EErrCode [1/2]Error types that file operations can generate.
Enumerator eLockTimeoutThe lock could not be acquired in the time allotted.
eNameErrorIncorrect name for a lock.
eCreateErrorCannot create the lock object in the OS.
eLockErrorCannot acquire a lock (not eLockTimeout, eCreateError)
eUnlockErrorCannot release the lock.
eMultipleLocksAttempt to lock already locked object in the same process.
eNotLockedAttempt to unlock a not-yet-acquired lock.
Definition at line 244 of file interprocess_lock.hpp.
◆ EErrCode [2/2] Enumerator eStillRunningThe process listed in the file is still around.
eWriteUnable to write into the PID file.
Definition at line 811 of file ncbi_process.hpp.
◆ ETypeHow to interpret the used process identifier.
Enumerator ePidA real process identifier (pid).
eHandleA process handle (MS Windows).
Definition at line 392 of file ncbi_process.hpp.
◆ EWhatProcess information "target".
Enumerator eProcessCurrent process.
eChildrenAll children of the calling process.
eThreadCurrent thread.
Definition at line 100 of file ncbi_process.hpp.
◆ FDaemonFlags [1/2]Daemonization flags.
Enumerator fDF_KeepCWDDon't change CWD to "/".
fDF_KeepStdinKeep stdin open as "/dev/null" (RO)
fDF_KeepStdoutKeep stdout open as "/dev/null" (WO)
fDF_ImmuneTTYMake daemon immune to re-acquiring a controlling terminal.
fDF_KeepParentDo not exit the parent process but return.
fDF_AllowExceptionsThrow an exception in case of an error.
fDF_AllowThreadsDo 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 fKeepParentDefinition at line 732 of file ncbi_process.hpp.
◆ FForkFlags [1/2]Forking flags.
Enumerator fFF_UpdateDiagReset diagnostics timer and log an app-start message in the child process.
fFF_ExecUser 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_AllowExceptionsThrow an exception on error.
Definition at line 255 of file ncbi_process.hpp.
◆ FForkFlags [2/2] ◆ CExitInfo() CProcess::CExitInfo::CExitInfo ( void ) ◆ CInterProcessLock() CInterProcessLock::CInterProcessLock ( const string & name )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() [2/2] ◆ CProcess() [1/2] ◆ CProcess() [2/2] CProcess::CProcess ( void )Default constructor. Uses the current process pid.
Definition at line 540 of file ncbi_process.cpp.
◆ Daemonize() [1/2]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 509 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]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 778 of file ncbi_process.hpp.
References CCurrentProcess::Daemonize(), and flags.
Referenced by Daemonize(), and main().
◆ Fork() [1/2]Fork the process.
Update PID and GUID used for logging (by default).
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] ◆ ForkForExec()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
Definition at line 315 of file ncbi_process.hpp.
References CCurrentProcess::fFF_Exec, flags, and CCurrentProcess::Fork().
◆ GetCurrentHandle() ◆ GetCurrentPid() ◆ 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 ◆ GetFileDescriptorsCount() [1/2] int CCurrentProcess::GetFileDescriptorsCount ( int * soft_limit =NULL
, int * hard_limit = NULL
) static ◆ GetFileDescriptorsCount() [2/2] int CProcess::GetFileDescriptorsCount ( void ) ◆ GetHandle() [1/2] ◆ GetHandle() [2/2] ◆ GetMemoryUsage() [1/2]
Get current process memory usage.
Definition at line 1498 of file ncbi_process.cpp.
References _TROUBLE, CNcbiError::eNotSupported, CNcbiError::eUnknown, CFile::GetLength(), GetMemoryUsage(), len, s_Win_GetMemoryUsage(), CNcbiError::Set(), CNcbiError::SetFromErrno(), and usage.
Referenced by CCgiApplication::CheckMemoryLimit(), CProcess::GetMemoryUsage(), CWNJobWatcher::Notify(), CPubseqGatewayApp::OnInfo(), ReportMemoryUsage(), CAsnSubCacheCreateApplication::Run(), CAlignSort::SortAlignments(), CNcbiApplicationAPI::x_LogOptions(), and CNetScheduleHandler::x_ProcessHealth().
◆ GetMemoryUsage() [2/2]Get process memory usage.
For current process pid/handle it is the same as CCurrentProcess::GetMemoryUsage().
Definition at line 1469 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 ◆ GetOldPID() TPid CPIDGuard::GetOldPID ( void ) inlineReturns 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.
Definition at line 928 of file ncbi_process.hpp.
◆ GetParentPid() [1/2] TPid CCurrentProcess::GetParentPid ( void ) static ◆ GetParentPid() [2/2] ◆ GetPid() [1/2] TPid CCurrentProcess::GetPid ( void ) staticGet process identifier (pid) for the current process.
Definition at line 247 of file ncbi_process.cpp.
Referenced by CDbapiSampleApp::CDbapiSampleApp(), CMessageDumperSender::CMessageDumperSender(), CFileIO::CreateTemporary(), CRtProfiler::DoReport(), CProcess::GetCurrentPid(), CAppExplorerDataObject::GetDataHere(), CCgiApplication::GetFastCGIWatchFileRestartDelay(), CCurrentProcess::GetFileDescriptorsCount(), CDiagContext::GetPID(), CNcbiTest::GetRandomSeed(), TDataExchange< T >::Init(), CProcess::IsCurrent(), TDataExchange< T >::IsLocal(), SProjectSelectorParams::LoadSettings(), CBDB_Cache::Open(), CNCStat::PrintToSocket(), CGetStatisticsProcessor::Process(), CRandom::Randomize(), SGridWorkerNodeImpl::Run(), s_GenerateSessionTag(), s_InitConstants(), s_LockInstanceGuard(), s_ReportPid(), SProjectSelectorParams::SaveSettings(), CAppExplorerDataObject::SetData(), CPIDGuard::UpdatePID(), CDiagContext::UpdatePID_AsyncSafe(), CNCServer::WriteEnvInfo(), CNetScheduleServer::x_GenerateGUID(), CNetStorageServer::x_GenerateGUID(), CAgpFastaComparator::CTmpSeqVecStorage::x_GetTmpDir(), CWNJobWatcher::x_KillNode(), and CFastCgiApplicationMT::x_ProcessThreadedRequest().
◆ GetPid() [2/2] TPid CProcess::GetPid ( void ) const inlineGet stored process identifier (pid).
Definition at line 424 of file ncbi_process.hpp.
References CProcess::m_Process.
Referenced by CProcess::GetFileDescriptorsCount(), CCurrentProcess::GetHandle(), CProcess::GetMemoryUsage(), CProcess::GetThreadCount(), CProcess::GetTimes(), CProcess::IsAlive(), CProcess::IsCurrent(), CProcess::Kill(), CProcess::KillGroup(), CProcess::Wait(), CProcess::x_GetHandle(), and CProcess::x_GetPid().
◆ GetPID() TPid CPIDGuardException::GetPID ( void ) const throw ( ) inline ◆ GetSignal() int CProcess::CExitInfo::GetSignal ( void ) const ◆ GetSystemName() const string& CInterProcessLock::GetSystemName ( void ) const inline ◆ GetThreadCount() [1/2] int CCurrentProcess::GetThreadCount ( void ) static ◆ GetThreadCount() [2/2] int CProcess::GetThreadCount ( void ) ◆ GetTimes() [1/2] bool CCurrentProcess::GetTimes ( double * real, double * user, double * sys, EWhat what =eProcess
) static
Get current process execution times.
Definition at line 1300 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]Get process execution times.
For current process pid/handle it is the same as CCurrentProcess::GetTimes().
Definition at line 1249 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 ◆ IsAlive() [1/2] bool CProcess::IsAlive ( void ) const ◆ IsAlive() [2/2] bool CProcess::CExitInfo::IsAlive ( void ) const ◆ IsCurrent() bool CProcess::IsCurrent ( void )Checks that stored process identifier (pid) represent the current process.
Definition at line 613 of file ncbi_process.cpp.
References CProcess::eHandle, CProcess::ePid, eTriState_False, eTriState_True, CCurrentProcess::GetHandle(), CProcess::GetHandle(), CCurrentProcess::GetPid(), CProcess::GetPid(), CProcess::GetType(), and CProcess::m_IsCurrent.
Referenced by CProcess::GetFileDescriptorsCount(), CProcess::GetMemoryUsage(), CProcess::GetTimes(), and CProcess::Kill().
◆ IsExited() bool CProcess::CExitInfo::IsExited ( void ) const ◆ IsPresent() bool CProcess::CExitInfo::IsPresent ( void ) const ◆ IsSignaled() bool CProcess::CExitInfo::IsSignaled ( void ) const ◆ Kill()Terminate process.
First try "soft" and then try "hard" attempt to terminate the process.
Definition at line 652 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()Terminate a group of processes.
This method tries to terminate all processes in the group to which process, specified in the constructor, belongs.
Definition at line 943 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()Terminate a group of processes with specified ID.
Note: Implemented on UNIX only, on Windows returns FALSE.
Definition at line 975 of file ncbi_process.cpp.
References _ASSERT, CNcbiError::eNotSupported, kWaitPrecisionMs, CNcbiError::Set(), CNcbiError::SetFromErrno(), and SleepMilliSec().
Referenced by CProcess::KillGroup().
◆ Lock()Definition at line 142 of file interprocess_lock.cpp.
References _T_XCSTRING, _VERIFY, close(), CDirEntry::fRead, CDirEntry::fWrite, CTimeout::GetAsMilliSeconds(), HANDLE, CTimeout::IsDefault(), CTimeout::IsInfinite(), kInvalidLockHandle, CInterProcessLock::m_Handle, CInterProcessLock::m_SystemName, CDirEntry::MakeModeT(), min(), ms(), NCBI_THROW, NULL, s_Locks, s_UnixLock(), SleepMilliSec(), strerror(), and TRUE.
Referenced by PInterProcessLock::operator()(), and CInterProcessLock::TryLock().
◆ NCBI_EXCEPTION_DEFAULT() ◆ operator()() void PInterProcessLock::operator() ( resource_type & resource ) const inline ◆ PInterProcessLock() ◆ 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 1792 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.
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 ) ◆ ReportExtra() virtual void CPIDGuardException::ReportExtra ( ostream & out ) const inlineoverridevirtual ◆ TryLock() bool CInterProcessLock::TryLock ( void )Try to acquire the lock.
Acquire the lock if it can be done right away. Return immediately.
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.
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.
Definition at line 1866 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()Wait until process terminates.
Wait until the process terminates or timeout expires. Return immediately if the specified process has already terminated.
Definition at line 1037 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() ◆ WaitTimeout() int CProcess::WaitTimeout ( unsigned long timeout, CExitInfo * info =0
) const inline ◆ x_Assign() inlineoverrideprotectedvirtual ◆ x_CloseHandle() ◆ x_GetHandle()
Definition at line 572 of file ncbi_process.cpp.
References CNcbiError::eBadAddress, CProcess::eHandle, FALSE, CProcess::GetHandle(), CProcess::GetPid(), CProcess::GetType(), INVALID_HANDLE_VALUE, NULL, CNcbiError::Set(), CNcbiError::SetFromWindowsError(), and CNcbiError::SetWindowsError().
Referenced by CProcess::GetMemoryUsage(), CProcess::GetTimes(), CProcess::IsAlive(), CProcess::Kill(), and CProcess::Wait().
◆ x_GetPid() TPid CProcess::x_GetPid ( void ) const private ◆ ~CInterProcessLock() CInterProcessLock::~CInterProcessLock ( void ) ◆ ~CPIDGuard() CPIDGuard::~CPIDGuard ( void ) ◆ data size_t CProcessBase::SMemoryUsage::data ◆ kDefaultKillTimeout const unsigned long CProcessBase::kDefaultKillTimeout = 1000 static ◆ kInfiniteTimeoutMs [1/2] const unsigned long kInfiniteTimeoutMs = kMax_ULong ◆ kInfiniteTimeoutMs [2/2] const unsigned long CProcessBase::kInfiniteTimeoutMs = kMax_ULong static ◆ lib size_t CProcessBase::SMemoryUsage::lib ◆ m_Granularity CTimeout PInterProcessLock::m_Granularity private ◆ m_Handle ◆ m_IsCurrent ◆ m_MTGuard ◆ m_Name string CInterProcessLock::m_Name private ◆ m_Path ◆ m_PID [1/2] TPid CPIDGuardException::m_PID private ◆ m_PID [2/2] ◆ m_PIDGuard ◆ m_Process ◆ m_SystemName string CInterProcessLock::m_SystemName private ◆ m_Timeout ◆ m_Type ◆ resident size_t CProcessBase::SMemoryUsage::resident ◆ resident_peak size_t CProcessBase::SMemoryUsage::resident_peak ◆ shared size_t CProcessBase::SMemoryUsage::shared ◆ stack size_t CProcessBase::SMemoryUsage::stack ◆ state int CProcess::CExitInfo::state private ◆ status int CProcess::CExitInfo::status private ◆ swap size_t CProcessBase::SMemoryUsage::swap ◆ text size_t CProcessBase::SMemoryUsage::text ◆ total size_t CProcessBase::SMemoryUsage::total ◆ total_peak size_t CProcessBase::SMemoryUsage::total_peak ◆ CProcessRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4