msg_get_queue

(PHP 4 >= 4.3.0, PHP 5)

msg_get_queue --  Create or attach to a message queue

Description

resource msg_get_queue ( int key [, int perms] )

msg_get_queue() returns an id that can be used to access the System V message queue with the given key. The first call creates the message queue with the optional perms (default: 0666). A second call to msg_get_queue() for the same key will return a different message queue identifier, but both identifiers access the same underlying message queue. If the message queue already exists, the perms will be ignored.

See also msg_remove_queue(), msg_receive(), msg_send(), msg_stat_queue() and msg_set_queue().


add a note add a note User Contributed Notes
zxd at zhangxiaodong dot net
16-Dec-2004 01:55
#
# require --enable-sysvmsg configure option
#
# see phpinfo() to find how to check
#
joeldg AT listbid.com
03-May-2003 03: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);