It looks like msg_receive() allocates a memory with size $maxsize, and only then tries to receive a message from queue into allocated memory. Because my script dies with $maxsize = 1 Gib, but works with $maxsize = 10 Kib.(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
msg_receive — Riceve un messaggio da una coda
$coda,$tipo_desiderato,&$tipo_messaggio,$dimensione_max,&$messaggio,$unserialize = ?,$flags = ?,$codice_errore = ?,&$codice_errore = ?
     La funzione msg_receive() riceve il primo messaggio dalla coda 
     specificata in coda del tipo indicato in
     tipo_desiderato.
     Il tipo di messaggio che è stato ricevuto viene memorizzato in
     tipo_messaggio.
     La dimensione massima del messaggio accettata viene indicata
     in dimensione_max; se il messaggio nella coda è più grande,
     la funzione darà esito negativo (a meno che non sia
     impostato il parametro flags come descritto in seguito).
     Il messaggio ricevuto sarà memorizzato in messaggio,
     a meno che non si verifichino degli errori in ricezione, in tal caso il parametro
     opzionale errorcode sarà valorizzato con il valore
     della variabile errno per aiutare ad identificare la causa.
    
     Se il parametro tipo_desiderato è 0, verrà restituito il
     primo messaggio dalla coda. Se, invece, tipo_desiderato è
     maggiore di 0, sarà restuito il primo messaggio di quel tipo.
     Mentre se tipo_desiderato è minore di 0, sarà
     restituito dalla coda il primo messaggio con il tipo più basso o uguale al
     valore assoluto di tipo_desiderato.
     Se nessun messaggio soddisfa i criteri impostati, lo script attenderà fino
     all'arrivo nella coda di un messaggio adeguato. Si può evitare il blocco dello script
     indicando MSG_IPC_NOWAIT nel parametro flags.
    
     Il parametro unserialize (default true), se viene impostato
     a true indica di trattare il messaggio come se fosse serializzato utilizzando lo stesso
     meccanismo del modulo delle sessioni. In tal modo il messaggio può essere deserializzato e
     restituito allo script. Questo permette di ricevere facilmente array o complesse
     strutture oggetto da altri script PHP, o, se si sta utilizzando il
     serializzatore WDDX, da sorgenti compatibili con WDDX.
     Se unserialize è impostato a false, il messaggio sarà restituito
     come una stringa.
    
     Il parametro opzionale flags permette di passare flag alla
     chiamata di sistema msgrcv. Il default è 0, ma possono essere specificati uno
     o più dei seguenti valori (sommandoli o legandoli con OR).
     
| MSG_IPC_NOWAIT | Se non ci sono messaggi del tipo_desiderato, la funzione ritorna immediatamente senza
          aspettare. La funzione fallirà e restituirà un valore intero 
          corrispondente a ENOMSG. | 
| MSG_EXCEPT | Usando questo flag in combinazione con tipo_desideratomaggiore di 0, si forza la 
          funzione a ricevere il primo messaggio che non sia uguale atipo_desiderato. | 
| MSG_NOERROR | Se il messaggio è più lungo di dimensione_max,
          l'attivazione di questo flag troncherà il messaggio adimensione_maxe non sarà segnalato alcun errore. | 
     Una volta eseguita con successo la ricezione, la struttura dati della coda dei messaggi verrà aggiornata
     come segue: msg_lrpid sarà impostato all'ID di processo del 
     processo chiamante, msg_qnum verrà decrementato di 1 e
     msg_rtime sarà impostato all'ora corrente.
    
     La funzione msg_receive() restituisce true se ha successo oppure false
     se non riesce. Se la funzione fallisce, il parametro opzionale
     codice_errore verrà impostato al valore
     della variabile errno.
    
Vedere anche: msg_remove_queue(), msg_send(), msg_stat_queue() e msg_set_queue().
It looks like msg_receive() allocates a memory with size $maxsize, and only then tries to receive a message from queue into allocated memory. Because my script dies with $maxsize = 1 Gib, but works with $maxsize = 10 Kib.This is meant to be run as your apache user in a terminal, call script in note of msg_send and they will communicate.
#! /usr/bin/env php
<?php
    $MSGKEY = 519051; // Message
    $msg_id = msg_get_queue ($MSGKEY, 0600);
    while (1) {
        if (msg_receive ($msg_id, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
            if ($msg == 'Quit') break;
            echo "$msg\n";
        } else {
            echo "Received $msg_error fetching message\n";
            break;
        }
    }
    msg_remove_queue ($msg_id);
?>It seems that a maxsize of 2Mb is some sort of a threshold for php, above that msg_receive() starts to use a lot of CPU (with a sender that is pushing messages non-stop receiving 10000 messages jumps up from 0.01 sec to 1.5 sec on my computer) so try to stay below that thresholod if you can.<?php error_reporting(E_ALL);
/**
 * Example for sending and receiving Messages via the System V Message Queue
 *
 * To try this script run it synchron/asynchron twice times. One time with ?typ=send and one time with ?typ=receive
 *
 * @author          Thomas Eimers - Mehrkanal GmbH
 *
 * This document is distributed in the hope that it will be useful, but without any warranty;
 * without even the implied warranty of merchantability or fitness for a particular purpose.
 */
header('Content-Type: text/plain; charset=ISO-8859-1');
echo "Start...\n";
// Create System V Message Queue. Integer value is the number of the Queue
$queue = msg_get_queue(100379);
// Sendoptions
$message='nachricht';     // Transfering Data
$serialize_needed=false;  // Must the transfer data be serialized ?
$block_send=false;        // Block if Message could not be send (Queue full...) (true/false)
$msgtype_send=1;          // Any Integer above 0. It signeds every Message. So you could handle multible message
                          // type in one Queue.
// Receiveoptions
$msgtype_receive=1;       // Whiche type of Message we want to receive ? (Here, the type is the same as the type we send,
                          // but if you set this to 0 you receive the next Message in the Queue with any type.
$maxsize=100;             // How long is the maximal data you like to receive.
$option_receive=MSG_IPC_NOWAIT; // If there are no messages of the wanted type in the Queue continue without wating.
                          // If is set to NULL wait for a Message.
// Send or receive 20 Messages
for ($i=0;$i<20;$i++) {
  sleep(1);
  // This one sends
  if ($_GET['typ']=='send') {
    if(msg_send($queue,$msgtype_send, $message,$serialize_needed, $block_send,$err)===true) {
      echo "Message sendet.\n";
    } else {
      var_dump($err);
    }
  // This one received
  } else {
    $queue_status=msg_stat_queue($queue);
    echo 'Messages in the queue: '.$queue_status['msg_qnum']."\n";
    // WARNUNG: nur weil vor einer Zeile Code noch Nachrichten in der Queue waren, muss das jetzt nciht mehr der Fall sein!
    if ($queue_status['msg_qnum']>0) {
      if (msg_receive($queue,$msgtype_receive ,$msgtype_erhalten,$maxsize,$daten,$serialize_needed, $option_receive, $err)===true) {
              echo "Received data".$daten."\n";
      } else {
              var_dump($err);
      }
    }
  }
}
?>Consider this e.g. Linux situation:
<?php
//file send.php
$ip = msg_get_queue(12340);
msg_send($ip,8,"abcd",false,false,$err);
//-----------------------------------------------------
<?php
//file receive.php
$ip = msg_get_queue(12340);
msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";
msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";
?>
Now run: 
in terminal #1   php5 receive.php
in terminal #2   php5 receive.php
in terminal #3   php5 send.php
Showing messages from queue will flip-flop. It means you run once send.php, the message will be shown in terminal #1. Second run it will be in t#2, third #1 and so on.