如何做本地网站,wordpress支付下载插件,wordpress外贸站gdpr,济南网站建设开发公司消息队列中的数据同样受到大小的约束#xff0c;具体约束范围可通过msg_stat_queue的msg_qbytes看到。这段代码唯一有点小改动的地方就在接受消息时#xff0c;指定了MSG_IPC_NOWAIT#xff0c;不然如果目标队列没有数据#xff0c;默认会一直等待。 一般会用到共享内存或消…消息队列中的数据同样受到大小的约束具体约束范围可通过msg_stat_queue的msg_qbytes看到。这段代码唯一有点小改动的地方就在接受消息时指定了MSG_IPC_NOWAIT不然如果目标队列没有数据默认会一直等待。 一般会用到共享内存或消息队列的情况都会涉及消息队列中的数据同样受到大小的约束具体约束范围可通过msg_stat_queue的msg_qbytes看到。这段代码唯一有点小改动的地方就在接受消息时指定了MSG_IPC_NOWAIT不然如果目标队列没有数据默认会一直等待。一般会用到共享内存或消息队列的情况都会涉及到多线程/进程或跨语言的数据传递。如果是php脚本/进程间共享数据那只要小心点操作就没什么问题。如果要求跨语言那很可能遇到千奇百怪的问题呵呵我还没试过但在网上看到别人发的苦水贴以后有机会一定实验一下。在调试共享内存、信号量、消息队列时可以配合Linux系统命令观察数据存储情况及信号量、消息队列资源分配情况如ipcs, ipcrm命令。利用PHP操作Linux消息队列完成进程间通信当我们开发的系统需要使用多进程方式运行时进程间通信便成了至关重要的环节。消息队列(message queue)是Linux系统进程间通信的一种方式。关于Linux系统进程通信的概念及实现可查看http://www.ibm.com/developerworks/cn/linux/l-ipc/关于Linux系统消息队列的概念及实现可查看http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/PHP的sysvmsg模块是对Linux系统支持的System V IPC中的System V消息队列函数族的封装。我们需要利用sysvmsg模块提供的函数来进进程间通信。先来看一段示例代码_1$message_queue_key ftok(__FILE__,a);$message_queue msg_get_queue($message_queue_key, 0666);var_dump($message_queue);$message_queue_status msg_stat_queue($message_queue);print_r($message_queue_status);//向消息队列中写msg_send($message_queue, 1,Hello,World!);$message_queue_status msg_stat_queue($message_queue);print_r($message_queue_status);//从消息队列中读msg_receive($message_queue, 0,$message_type, 1024,$message, true, MSG_IPC_NOWAIT);print_r($message.\r\n);msg_remove_queue($message_queue);?这段代码的运行结果如下resource(4) of type (sysvmsg queue)Array([msg_perm.uid] 1000[msg_perm.gid] 1000[msg_perm.mode] 438[msg_stime] 0[msg_rtime] 0[msg_ctime] 1279849495[msg_qnum] 0[msg_qbytes] 16384[msg_lspid] 0[msg_lrpid] 0)Array([msg_perm.uid] 1000[msg_perm.gid] 1000[msg_perm.mode] 438[msg_stime] 1279849495[msg_rtime] 0[msg_ctime] 1279849495[msg_qnum] 1[msg_qbytes] 16384[msg_lspid] 2184[msg_lrpid] 0)Hello,World!可以看到已成功从消息队列中读取“Hello,World!”字符串下面列举一下示例代码中的主要函数ftok ( string$pathname , string$proj )手册上给出的解释是Convert a pathnameand a project identifier to a System V IPC key。这个函数返回的键值唯一对应linux系统中一个消息队列。在获得消息队列的引用之前都需要调用这个函数。msg_get_queue ( int$key [, int$perms ] )msg_get_queue()会根据传入的键值返回一个消息队列的引用。如果linux系统中没有消息队列与键值对应msg_get_queue()将会创建一个新的消息队列。函数的第二个参数需要传入一个int值作为新创建的消息队列的权限值默认为0666。这个权限值与linux命令chmod中使用的数值是同一个意思因为在linux系统中一切皆是文件。msg_send ( resource$queue , int$msgtype , mixed$message [, bool$serialize [, bool$blocking [, int $errorcode ]]] )顾名思义该函数用来向消息队列中写数据。msg_stat_queue ( resource$queue )这个函数会返回消息队列的元数据。消息队列元数据中的信息很完整包括了消息队列中待读取的消息数、最后读写队列的进程ID等。示例代码在第8行调用该函数返回的数组中队列中待读取的消息数msg_qnum值为0。msg_receive ( resource$queue , int$desiredmsgtype , int $msgtype , int$maxsize , mixed $message [, bool$unserialize [, int$flags [, int $errorcode ]]] )msg_receive用于读取消息队列中的数据。msg_remove_queue ( resource$queue )msg_remove_queue用于销毁一个队列。示例代码_1只是展示了PHP操作消息队列函数的应用。下面的代码具体描述了进程间通信的场景$message_queue_key ftok(__FILE__,a);$message_queue msg_get_queue($message_queue_key, 0666);$pids array();for ($i 0;$i 5;$i) {//创建子进程$pids[$i] pcntl_fork();if ($pids[$i]) {echo No.$i child process was created, the pid is $pids[$i]\r\n;}elseif ($pids[$i] 0) {$pid posix_getpid();echo process.$pid is writing now\r\n;msg_send($message_queue, 1,this is process.$pids data\r\n);posix_kill($pid, SIGTERM);}}do {msg_receive($message_queue, 0,$message_type, 1024,$message, true, MSG_IPC_NOWAIT);echo $message;//需要判断队列是否为空如果为空就退出//break;}while(true)?运行结果为No.0 child process was created, the pid is 5249No.1 child process was created, the pid is 5250No.2 child process was created, the pid is 5251No.3 child process was created, the pid is 5252No.4 child process was created, the pid is 5253process.5251 is writing nowthis is process.5251s dataprocess.5253 is writing nowprocess.5252 is writing nowprocess.5250 is writing nowthis is process.5253s datathis is process.5252s datathis is process.5250s dataprocess.5249 is writing nowthis is process.5249s data这段程序每次的运行结果都会不同这正说明了多进程的异步性。从结果也能看出消息队列FIFO特性。以上便是我研究的一点心得。接下来将会继续研究PHP利用信号、socket等进行进程间通信的方法。本文原创发布php中文网转载请注明出处感谢您的尊重