The following code sends data to the message queue and retrieves the test

<? php $key=ftok(__FILE__,'a'); $queue=msg_get_queue($key,0666); Msg_send ($queue, 1, "Hello, 1"); $queue, 1, $message_type, 1024, $message1); Msg_remove_queue ($queue); //var_dump($message1);Copy the code

<? PHP /** * This code simulates an everyday task. * The first parent produces a child. The child, in turn, acts as the parent, producing 10 children. A -> B -> c,d,e... Such process. * As for A, it only needs to produce the task and then hand it over to B. B will assign the task to 10 sub-processes for processing. Set_time_limit (0); $ftok = ftok(__FILE__, 'a'); $msg_queue = msg_get_queue($ftok); $pidarr = []; $pid = pcntl_fork(); If ($pid) {// The parent process simulates generating a large array. $arr = range (1100-000); Foreach ($arr as $val) {$status = msg_queue ($msg_queue,1, $val); usleep(1000); } $pidarr[] = $pid; msg_remove_queue($msg_queue); } else {// When a subprocess receives a task, fork10 subprocesses the task. for ($i =0; $i<10; $i++) { $childpid = pcntl_fork(); if ($childpid) { $pidarr[] = $childpid; Processid} else {while (true) {msg_receive($MSg_queue, 0, $MSg_type, 1024, $message); if (! $message) exit(0); echo $message.PHP_EOL; usleep(1000); }}}} // Prevent the main process from exiting before its children, While (count($pid) > 0) {foreach ($pidarr as $key => $pid) {$status = pcnTL_waitpid ($pid, $status); if ($status == -1 || $status > 0) { unset($pidarr[$key]); } } sleep(1); }Copy the code