https://github.com/slince/process
:whale2: The process wrapper and manager based on PCNTL on the Unix-like systems using php
https://github.com/slince/process
child-process fifo fork pcntl pipe posix process semaphore shared-memory system-v
Last synced: 9 months ago
JSON representation
:whale2: The process wrapper and manager based on PCNTL on the Unix-like systems using php
- Host: GitHub
- URL: https://github.com/slince/process
- Owner: slince
- Created: 2017-04-15T14:08:55.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-10-13T15:28:14.000Z (about 2 years ago)
- Last Synced: 2025-03-30T00:31:38.323Z (9 months ago)
- Topics: child-process, fifo, fork, pcntl, pipe, posix, process, semaphore, shared-memory, system-v
- Language: PHP
- Homepage:
- Size: 76.2 KB
- Stars: 22
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Process Library
[](https://github.com/slince/process/actions)
[](https://codecov.io/github/slince/process)
[](https://packagist.org/packages/slince/process)
[](https://packagist.org/packages/slince/process)
[](https://scrutinizer-ci.com/g/slince/process/?branch=master)
The library help to work with processes. It provides a more readable api and various modes for IPC via pipe(FIFO) and system v.
# Installation
Install via composer
```bash
composer require slince/process
```
# Dependencies
The library replies on the following php's extension.
- ext-pcntl. Provides control processes (MUST)
- ext-sysvshm. Porvides system v shared memory (OPTIONAL)
- ext-sysvsem. Porvides system v semaphore (OPTIONAL)
- ext-sysmsg. Porvides system v message queue (OPTIONAL)
# Usage
## Basic usage
```php
$process = new Slince\Process\Process(function(){
echo 'hello, my pid is ' . getmypid();
});
$process->start();
var_dump($process->isRunning()); // echo true
var_dump($process->getPid()); // will output the pid of child process
//do something other
$process->wait(); //waiting for the process to exit
```
## Sends signal to the process
>Note: If your php version is less than 7.1, please add the statement `declare(ticks=1);` at the beginning of the file:
```php
$process = new Slince\Process\Process(function(){
Slince\Process\Process::current()->signal([SIGUSR1, SIGUSR2], function(){
echo 'trigger signal';
});
echo 'hello, my pid is ' . getmypid();
});
$process->start();
$process->signal(SIGUSER1);
//do something
$process->wait();
```
## Shared memory
```php
$memory = new Slince\Process\SystemV\SharedMemory();
$memory->set('foo', 'bar');
var_dump($memory->get('foo'));
```
The default size of shared memory is the sysvshm.init_mem in the php.ini, otherwise 10000 bytes. You can adjust this.
```php
$memory = new Slince\Process\SystemV\SharedMemory(__FILE__, '5M'); //Adjusts to 5m
```
## Semaphore
```php
$semaphore = new Slince\Process\SystemV\Semaphore();
$semaphore->acquire(); //Acquires a lock
// do something
$semaphore->release() //Releases a lock
```
## Message queue
```php
$queue = new Slince\Process\SystemV\MessageQueue();
$queue->send('hello');
echo $queue->receive(); //Will output hello
```
## Fifo
```php
$writeFifo = new Slince\Process\Pipe\WritableFifo('/tmp/test.pipe');
$writeFifo->write('some message');
$readFifo = new Slince\Process\Pipe\ReadableFifo('/tmp/test.pipe');
echo $readFifo->read();
```
## License
The MIT license. See [MIT](https://opensource.org/licenses/MIT)