PHP Doku:: Message Queue anlegen oder an existierende Queue anbinden - function.msg-get-queue.html

Verlauf / Chronik / History: (3) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur ProzesskontrolleSemaphore, Shared Memory and IPCSemaphore Funktionenmsg_get_queue

Ein Service von Reinhard Neidl - Webprogrammierung.

Semaphore Funktionen

<<ftok

msg_queue_exists>>

msg_get_queue

(PHP 4 >= 4.3.0, PHP 5)

msg_get_queue Message Queue anlegen oder an existierende Queue anbinden

Beschreibung

resource msg_get_queue ( int $key [, int $perms ] )

msg_get_queue() gibt eine ID zurück die für den Zugriff auf die mit dem gegbenen key bezeichnete System V Message Queue genutzt werden kann. Existiert die Message Queue noch nicht so wird sie mit den im optionalen Parameter perms angegebenen Rechten angelegt. Ein weiterer Aufruf von msg_get_queue() mit dem gleichen key liefert eine andere ID, beide IDs greifen aber intern auf die gleiche Message Queue zu.

Parameter-Liste

key

Numerische ID der Message Queue.

perms

Zugriffsrechte der Queue. Vorgabewert ist 0666. Existiert die Message Queue bereits so wird dieser Parameter ignoriert.

Rückgabewerte

Liefert ein Resourcehandle für den Zugriff auf eine System V Message Queue.

Siehe auch


6 BenutzerBeiträge:
- Beiträge aktualisieren...
Michael Iatrou
2.07.2010 18:47
If you are getting the following message (on Linux):

Warning: msg_get_queue() [function.msg-get-queue]: failed for key 0x12345678: No space left on device in /path/to/script.php on line 1

aside from what [others have] suggested, you should also check and set an appropriate value for kernel parameter kernel.msgmni, e.g. sysctl -w kernel.msgmni=256
Morten Brrup
17.03.2010 22:08
IPC_CREAT has no effect in this function. The PHP source code (file sysvmsg.c) reveals that only if msgget(key,0) fails, msgget(key, IPC_CREAT | IPC_EXCL | perms) is used.
pail dot luo at gmail dot com
28.02.2009 18:22
A simple Sample to introduce Message Queue.

<?php
if ( sizeof($argv)<2 ) {
        echo
"Usage: $argv[0] stat|send|receive|remove msgType MSG [msg] \n\n" ;
        echo
"   EX: $argv[0] send 1 \"This is no 1\" \n" ;
        echo
"       $argv[0] receive ID \n" ;
        echo
"       $argv[0] stat \n" ;
        echo
"       $argv[0] remove \n" ;
        exit;
}

$MSGKey = "123456" ;

## Create or attach to a message queue
$seg = msg_get_queue($MSGKey) ;

switch (
$argv[1] ) {
    case
"send":
       
msg_send($seg, $argv[2], $argv[3]);
        echo
"msg_send done...\n" ;
        break;
       
    case
"receive":
       
$stat = msg_stat_queue( $seg );
        echo
'Messages in the queue: '.$stat['msg_qnum']."\n";
        if (
$stat['msg_qnum']>0 ) {
           
msg_receive($seg, $argv[2], $msgtype, 1024, $data);
           
var_dump($msgtype);
           
var_dump($data);
            echo
"\n";
        }
        else {
            echo
"No Msg...\n";
        }
        break;
   
    case
"stat":
     
print_r( msg_stat_queue($seg) );
        break;
       
    case
"remove":
       
msg_remove_queue($seg);
        break;
}
?>
david dot schueler at tel-billig dot de
17.12.2008 17:17
If you are getting this message on your *NIX box:

Warning: msg_get_queue() [function.msg-get-queue]: failed for key 0x12345678: No space left on device in /path/to/script.php on line 1

you may use the command "ipcrm" as root to clear the message queue. Use "man ipcrm" to get more info on it.
The default setting for maximum messages in the queue is stored in /proc/sys/fs/mqueue/msg_max. To increase it to a maximum of 100 messages, just run:
echo 100 > /proc/sys/fs/mqueue/msg_max

Please ensure to follow a good programming style and close/free all your message queues before your script exits to avoid those warning messages.
martin dot velek at gmail dot com
1.08.2007 13:23
Lack of IPC_EXCL makes me unhappy. Of course, you can use ftok() to generate a unique key. This code is not allmighty, another process under the same user can open the queue and function returns true. But in some situation it could help.

code:

<?
function ipcEXCL($res,$perm)
{
 
$pole = msg_stat_queue($res);
 
 if(
$pole['msg_perm.uid']==posix_getuid() &&
   
$pole['msg_perm.gid']==posix_getgid()  &&
   
$pole['msg_perm.mode']==$perm  &&
   
$pole['msg_stime']==&&
   
$pole['msg_rtime']==&&
   
$pole['msg_qnum']==&&
   
$pole['msg_lspid']==&&
   
$pole['msg_rspid']==0)
        return
true;
 else
        return
false;
}

$res=msg_get_queue($key,$perm);

if(
ipcEXCL($res,$perm))
   echo
"probably I am a creator:";
else
   echo
"probably not";
?>
joeldg AT listbid.com
2.05.2003 21:10
// Here is an example of working with message queues.
// if you have forked processes, this could be a great way of passing
// out work to them

// create the message queue
// pick a file..
  $key_t = msg_get_queue(ftok("/tmp/php_msgqueue.stat", 'R'),0666 | IPC_CREAT);

    // place two messages on the queue
    if (!msg_send ($key_t, 1, 'This is message #1', true, true, $msg_err))
       echo "Msg not sent because $msg_err\n";
    if (!msg_send ($key_t, 1, 'This is message #2 ', true, true, $msg_err))
       echo "Msg not sent because $msg_err\n";
     
    // lets look at the queue structure 'msg_qnum' is really what we want to see
    // it should be '2'
    print_r(msg_stat_queue($key_t));
   
    // pull off the stack
        if (msg_receive ($key_t, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
           if ($msg == 'Quit');
           echo "$msg\n"; // prints 'This is message #1'
        } else {
           echo "Received $msg_error fetching message\n";
        }
    // look at the structure again, ms_qnum should be '1'   
    print_r(msg_stat_queue($key_t));
        if (msg_receive ($key_t, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
           if ($msg == 'Quit');
           echo "$msg\n"; // prints 'This is message #2'
        } else {
           echo "Received $msg_error fetching message\n";
        }
   // look at the structure again, ms_qnum should be '0', no more messages on the queue
   print_r(msg_stat_queue($key_t)); 

   // get rid of the queue we created
   msg_remove_queue ($key_t);



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)