百科狗-知识改变命运!
--

msg_send() - semaphore函数

乐乐12个月前 (11-21)阅读数 10#技术干货
文章标签返回值

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]]]): bool

msg_send() sends a$messageof type$msgtype(which MUST be greater than 0)to the message queue specified by$queue.

参数

$queue

Message queue resource handle

$msgtype

The type of the message(MUST be greater than 0)

$message

msg_send() - semaphore函数

The 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.

$blocking

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.

$errorcode

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 queue
  • msg_receive()Receive a message from a message queue
  • msg_stat_queue()Returns information from the message queue data structure
  • msg_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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)