Add process cleanup functionality

Introduced a `close` method to ensure proper cleanup of child processes and shared memory resources, preventing zombie processes. Added a `clean` method to iterate through unfinished promises and apply the close operation, enhancing resource management.
This commit is contained in:
netkas 2024-12-03 15:50:11 -05:00
parent 6b40c56473
commit ecd2446788

View file

@ -101,6 +101,8 @@
{
// Wait for the process to finish
pcntl_waitpid($p->getPid(), $status);
pcntl_signal(SIGTERM, SIG_DFL);
posix_kill($p->getPid(), SIGTERM);
// Read the serialized data from shared memory
$shm = $p->getShm();
@ -126,6 +128,24 @@
return $result;
}
/**
* Closes and cleans up resources associated with the given process.
*
* @param P $p The process instance to be closed.
* @return void
*/
private static function close(P $p): void
{
// Clean up the child process to prevent zombie processes
pcntl_waitpid($p->getPid(), $status);
pcntl_signal(SIGTERM, SIG_DFL);
posix_kill($p->getPid(), SIGTERM);
$shm = $p->getShm();
shmop_delete($shm);
unset(self::$promises[$p->getUuid()]);
}
/**
* Waits for the completion of all promises and returns their results.
*
@ -178,6 +198,29 @@
return $count;
}
/**
* Cleans up resources associated with promises that are not completed.
*
* Iterates through a collection of promises, and for each promise that is not
* yet marked as done, calls a method to close and clean up the resource.
*
* @return int The number of promises that were closed and cleaned up.
*/
public static function clean(): int
{
$count = 0;
foreach(self::$promises as $uuid => $p)
{
if(!self::isDone($p))
{
self::close($p);
$count++;
}
}
return $count;
}
/**
* Returns the size of the shared memory.
*