msg_send() - semaphore函数
msg_send()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
Send a message to a message queue
说明
msg_send(resource $queue,int $msgtype, mixed $message[,bool $serialize=TRUE
[,bool $blocking= TRUE
[,int &$errorcode]]]): boolmsg_send() sends a$messageof type$msgtype(which MUST be greater than 0)to the message queue specified by$queue.
参数
$queueMessage queue resource handle
$msgtypeThe type of the message(MUST be greater than 0)
$messageThe body of the message.
Note: If$serializeset to FALSE
is supplied, MUST be of type:string,integer,float or bool. In other case a warning will be issued.$serialize
The optional$serializecontrols how the$messageis sent.$serializedefaults to TRUE
which means that the$messageis serialized using the same mechanism as the session module before being sent to the queue. This allows complex arrays and objects to be sent to other PHP scripts, or if you are using the WDDX serializer, to any WDDX compatible client.
If the message is too large to fit in the queue, your script will wait until another process reads messages from the queue and frees enough space for your message to be sent. This is called blocking; you can prevent blocking by setting the optional$blockingparameter to FALSE
, in which case msg_send() will immediately return FALSE
if the message is too big for the queue, and set the optional$errorcodeto MSG_EAGAIN
, indicating that you should try to send your message again a little later on.
If the function fails, the optional errorcode will be set to the value of the system errno variable.
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
Upon successful completion the message queue data structure is updated as follows:$msg_lspidis set to the process-ID of the calling process,$msg_qnumis incremented by 1 and$msg_stimeis set to the current time.
参见
msg_remove_queue()
Destroy a message queuemsg_receive()
Receive a message from a message queuemsg_stat_queue()
Returns information from the message queue data structuremsg_set_queue()
Set information in the message queue data structure
I created example how to comunnicate with programe written in C throught messages queues. First run C program (it will create queue) then PHP script. C code compile with: gcc -std=c99 -o test_queue test_queue.c test_queue.c: /** * Example how to use System V Messages Queues with PHP and C program. * This is simple server which create message queue and receive message from it. * Based on Beej's Guide to Unix IPC * Autor: Jan Drazil, */ #include #include #include #include #include #include #include /* Buffer struct for receiving messages */ struct php_buf { long mtype; char msg[200]; }; int main(void) { struct php_buf buf; int msqid; key_t key; /* Generate key (/var/www/index.php must be accessible file) */ if((key = ftok("/var/www/index.php", 'G')) == -1) { perror("ftok"); exit(EXIT_FAILURE); } /* Create message queue */ if((msqid = msgget(key, 0666 | IPC_CREAT)) == -1) { perror("msgget"); exit(EXIT_FAILURE); } printf("Ready to get string from PHP!\n"); /* Receive message */ if(msgrcv(msqid, &buf, sizeof(buf.msg)-1, 0, 0) == -1) { perror("msgrcv"); exit(EXIT_FAILURE); } /* Eliminate segmentation fault */ buf.msg[199] = '\0'; printf("Recieved from PHP: %s\n", buf.msg); /* Destroy message queue */ if(msgctl(msqid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(EXIT_FAILURE); } return EXIT_SUCCESS; } test_queue.php:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!