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.msg_get_queue
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
msg_get_queue
Референца за `function.msg-get-queue.php` со подобрена типографија и навигација.
msg_get_queue
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
msg_get_queue — Креирај или прикачи се на порака опашка
= NULL
msg_get_queue() враќа ID што може да се користи за пристап до системската V порака опашка со даденото
key. Првиот повик ја креира пораката опашка со опционалното permissions. Вториот повик до msg_get_queue() за истото
key ќе врати различен идентификатор на порака опашка, но двата идентификатори пристапуваат до истата основна порака опашка.
Параметри
key- Нумерички ID на порака опашка
permissions-
Дозволи на опашката. Стандардно 0666. Ако пораката опашка веќе постои,
permissionsЛозинката што се користи за шифрирање на датотека, ако има. Ако заглавјата се исто така шифрирани, оваа опција ќе се игнорира во корист на
Вратени вредности
Патеката до PHP скриптата што треба да се провери. SysvMessageQueue инстанца што може да се користи за пристап до системската V порака опашка, или false при неуспех.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.0.0 | При успех, оваа функција враќа SysvMessageQueue инстанца сега; претходно, а resource . |
Види Исто така
- msg_remove_queue() - Уништи ред за пораки
- msg_receive() - Прими порака од ред за пораки
- msg_send() - Испрати порака до порака опашка
- msg_stat_queue() Клуч на редицата.
- msg_set_queue() - Set information in the message queue data structure
Белешки од корисници 5 белешки
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;
}
?>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=256I find it hard to work out how to really use this reliably particularly in respect to collisions.
It maps to SysV IPC msgget.
As I see it you have three options...
1. Manage the IDs yourself, allocating various ranges or using some kind of centralised mechanism.
2. Use ftok. This attempts to find a unique ID, though it's not guaranteed to be unique or constant in absolutely every circumstance. It relies on using a file, from which it uses bits from the inode and dev it expects to be unique. It's the standard way and as long as there's nothing too unusual it should probably work (but might not survive radical FS changes).
3. Use 0 as the key, which appears to map to IPC_PRIVATE, a magic value which if provided as a key creates a new queue each time (without a key in effect).
Unfortunately option #3 is of limited use in PHP.
In C that is useful might be useful as the queue resource is just identified by an int and can be passed around.
In PHP its utility is questionable as only the resource can be passed within a single process. It's not possible to pass the resource with serialize / unserialize even though it's just a wrapped int.
The msqid returned isn't exactly unpredictable either so can quite easily be accidentally accessed. The first one I got was 0.
If you create a queue like this you'll find it very annoying as it wont be possible to delete it via PHP.
Like all IO it's worth wrapping this function and launching an exception if the input is 0.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']==0 &&
$pole['msg_rtime']==0 &&
$pole['msg_qnum']==0 &&
$pole['msg_lspid']==0 &&
$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";
?>