From de1af0a3f26277abe8b4da2a0fe54436539a8ead Mon Sep 17 00:00:00 2001 From: Martin Dias Date: Thu, 18 Dec 2025 23:22:07 -0300 Subject: [PATCH] Use waitpid with WNOHANG option to know if the child process is running --- .../OSSubprocess/OSSUnixSubprocess.class.st | 5 ++++- repository/OSSubprocess/OSSVMProcess.class.st | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/repository/OSSubprocess/OSSUnixSubprocess.class.st b/repository/OSSubprocess/OSSUnixSubprocess.class.st index 273e6bf..2a16b28 100644 --- a/repository/OSSubprocess/OSSUnixSubprocess.class.st +++ b/repository/OSSubprocess/OSSUnixSubprocess.class.st @@ -583,7 +583,10 @@ OSSUnixSubprocess >> isComplete [ { #category : 'status' } OSSUnixSubprocess >> isRunning [ "Answers whether the process is still running or not (to our best knowledge)" - ^ pid notNil and: [ self isComplete not ] + + ^ pid notNil and: [ + self isComplete not and: [ + OSSVMProcess vmProcess isChildRunning: pid ] ] ] { #category : 'status' } diff --git a/repository/OSSubprocess/OSSVMProcess.class.st b/repository/OSSubprocess/OSSVMProcess.class.st index 99fc6e7..4e8f44c 100644 --- a/repository/OSSubprocess/OSSVMProcess.class.st +++ b/repository/OSSubprocess/OSSVMProcess.class.st @@ -184,6 +184,12 @@ OSSVMProcess >> finalizePreviousSession [ self activeChildren do: [ :each | each stopWaiting ] ] +{ #category : 'childs management' } +OSSVMProcess >> includesChildPid: aPid [ + + ^ childProcessList anySatisfy: [ :process | process pid = aPid ] +] + { #category : 'initialize - release' } OSSVMProcess >> initialize [ "Set my instance variables to reflect the state of the OS process in which @@ -259,6 +265,17 @@ OSSVMProcess >> initializeSignalHandlers [ ] +{ #category : 'child watching' } +OSSVMProcess >> isChildRunning: childPid [ + "Use waitpid with WNOHANG option to know if the child process is running. + See: https://man7.org/linux/man-pages/man3/wait.3p.html" + + ^ (self + primitiveWaitpid: childPid + statusPointer: nil + options: WNOHANG) = 0 +] + { #category : 'cwd' } OSSVMProcess >> lockCwdWithValue: cwdNewValue encoding: encoding during: aBlock [ "This method is a complete hack in order to support a #cmd: option in OSSUnixSubprocess. @@ -306,7 +323,8 @@ OSSVMProcess >> pid [ { #category : 'child watching' } OSSVMProcess >> primitiveWaitpid: aProcessId statusPointer: statusPointer options: optionBits [ - + "See: https://linux.die.net/man/2/waitpid" + ^ self ffiCall: #( int waitpid(int aProcessId, void* statusPointer, int optionBits) ) ]