Inter-Process Communication

suggest change

Interprocess communication allows programmers to communicate between different processes. For example let us consider we need to write an PHP application that can run bash commands and print the output. We will be using proc_open , which will execute the command and return a resource that we can communicate with. The following code shows a basic implementation that runs pwd in bash from php

<?php
    $descriptor = array(
        0 => array("pipe", "r"), // pipe for stdin of child
        1 => array("pipe", "w"), // pipe for stdout of child
    );
    $process = proc_open("bash", $descriptor, $pipes);
    if (is_resource($process)) {
        fwrite($pipes[0], "pwd" . "\n");
        fclose($pipes[0]);
        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);
        $return_value = proc_close($process);

    }
?>

proc_open runs bash command with $descriptor as descriptor specifications. After that we use is_resource to validate the process. Once done we can start interacting with the child process using $pipes which is generated according to descriptor specifications.

After that we can simply use fwrite to write to stdin of child process. In this case pwd followed by carriage return. Finally stream_get_contents is used to read stdout of child process.

Always remember to close the child process by using proc_close() which will terminate the child and return the exit status code.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Multiprocessing:
* Inter-Process Communication

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
74 Multiprocessing
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR