PHP进程通信基础之信号量与共享内存通信_php技巧
这篇文章主要介绍了PHP进程通信基础知识中的信号量与共享内存通信的相关资料,有需要的小伙伴可以查看下
由于进程之间谁先执行并不确定,这取决于内核的进程调度算法,其中比较复杂。由此有可能多进程在相同的时间内同时访问共享内存,从而造成不可预料的错误。信号量这个名字起的令人莫名其妙,但是看其英文原意,就十分容易理解。
semaphore 英[ˈseməfɔ:(r)] vt. 发出信号,打旗语;
类似于指挥官的作用。
下面我们看下一个伪代码信号量的使用。
1、创建信号量唯一标识符
$ftok = ftok(__FILE__, 'a');
2、创建信号量资源ID
$sem_resouce_id = sem_get($ftok);
3、接受信号量
sem_acqure($sem_resource_id);
4、释放信号量
sem_release($sem_resource_id);
5、销毁信号量
sem_remove($sem_resource_id);
举个不文雅的例子,使我们容易理解这个信号量在生活中的用法。理解之后可以套用到我们编程领域。
一家公司只有一个卫生间。那么当有人上厕所的时候,都要获取一把锁(信号量),表示卫生间正在使用。代码如下:
sem_acqure($sem_resource_id);
那么员工上完厕所之后,就需要将锁打开,释放锁(信号量),表示现在可以允许别人使用。代码如下:
sem_release($sem_resource_id);
通过一个简单的锁,我们就能够知道当前的厕所(共享内存)是否可以使用。这个例子不雅观,但说明了问题。这篇博客也是有味道的博客,真是不容易。。。。以下是示例代码:
<?php //创建共享内存区域 $shm_key = ftok(__FILE__, 'a'); $shm_id = shm_attach($shm_key, 1024, 0755); //var_dump($shm_id);die(); resource(4) of type (sysvshm) const SHARE_KEY = 1; $child_list = []; //加入信号量 $sem_id = ftok(__FILE__, 'b'); $signal = sem_get($sem_id); //$signal resource(5) of type (sysvsem) for ($i = 0; $i < 3; $i++) { $pid = pcntl_fork(); if ($pid == -1) { exit("Fork fail!".PHP_EOL); } elseif ($pid == 0) { //获取信号量 sem_acquire($signal); if (shm_has_var($shm_id,SHARE_KEY)) { $count = shm_get_var($shm_id, SHARE_KEY); $count++; //模拟业务处理 $sec = rand(1, 3); sleep($sec); shm_put_var($shm_id, SHARE_KEY, $count); } else { $count = 0; $sec = rand(1, 3); sleep($sec); shm_put_var($shm_id, SHARE_KEY, $count); } echo "child process: ".getmypid()." is writing! now count is: $count ".PHP_EOL; //释放信号量 sem_release($signal); exit("child process".getmypid()."end".PHP_EOL); } else { $child_list[] = $pid; } } while (count($child_list) > 0) { foreach ($child_list as $key => $pid) { $status = pcntl_waitpid($pid, $status); if ($status > 0 || $status == -1) { unset($child_list[$key]); } } sleep(1); } $count = shm_get_var($shm_id, SHARE_KEY); echo " $count ".PHP_EOL; //销毁信号量 sem_remove($signal); shm_remove($shm_id); shm_detach($shm_id);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Friends who know something about computers must know that GPUs have shared memory, and many friends are worried that shared memory will reduce the number of memory and affect the computer, so they want to turn it off. Here is how to turn it off. Let's see. Turn off win10gpu shared memory: Note: The shared memory of the GPU cannot be turned off, but its value can be set to the minimum value. 1. Press DEL to enter the BIOS when booting. Some motherboards need to press F2/F9/F12 to enter. There are many tabs at the top of the BIOS interface, including "Main, Advanced" and other settings. Find the "Chipset" option. Find the SouthBridge setting option in the interface below and click Enter to enter.

As a highly concurrent programming language, Golang's built-in coroutine mechanism and multi-threaded operations enable lightweight multi-tasking. However, in a multi-process processing scenario, communication and shared memory between different processes have become key issues in program development. This article will introduce the application method of realizing shared memory between multiple processes in Golang. 1. How to implement multi-process in Golang In Golang, multi-process concurrent processing can be implemented in a variety of ways, including fork, os.Process,

PHP shared memory function usage and application Shared memory refers to a technology where multiple processes access the same memory space at the same time. In concurrent programming, shared memory can be used for inter-process communication to achieve data sharing between different processes. PHP also provides related shared memory functions. This article will introduce the usage of PHP shared memory functions and discuss some practical application scenarios. Use of shared memory functions PHP provides the shmop extension module, which allows PHP to operate on system shared memory. The functions provided by this extension module

With the development of the Internet, more and more websites need to carry a large number of user access requests. When faced with high concurrency, a single-process server will quickly reach a bottleneck, causing users to be unable to access the website normally. Therefore, multi-process has become one of the effective solutions to solve high concurrency problems. This article will introduce the multi-process technology in PHP to improve the program's ability to handle concurrent requests while ensuring program quality. 1. Introduction to multi-process In computer science, a process refers to an executing program instance. Each process has its own memory space and system resources.

Inter-process communication (IPC) in Go is implemented through pipes, channels and shared memory. Pipes allow coroutines to write and read data through pipe endpoints, while channels guarantee the atomicity of send and receive operations. Shared memory enables fast data exchange by allowing processes to access the same memory, but requires synchronization to prevent concurrent access.

In C++, shared memory and message queues are two commonly used inter-process communication methods. They can help us share data and information between different processes, allowing for more efficient programming. Shared memory is a special memory area that can be shared by multiple processes. Using shared memory avoids the overhead of copying data and reduces the delay in transferring data between processes. To use shared memory in C++, you need to include the <sys/shm.h> header file and use shmget, shmat, sh

Golang development: Using RPC to achieve cross-process communication requires specific code examples 1. Introduction RPCRPC (RemoteProcedureCall) is a remote procedure call protocol, which allows the client to call functions or methods of the server program located on the remote computer, just like Same as calling local functions. RPC can be implemented using different network protocols, such as TCP, HTTP, etc. In distributed systems, RPC is an important communication mechanism, often used for communication across processes or across network nodes.

How to use the multiprocessing module for inter-process communication in Python 3.x. With the development of computer technology, we often need to perform multiple tasks at the same time in programming. To take better advantage of multi-core processors, Python's multiprocessing module provides a simple yet powerful set of tools for creating concurrent programs. The multiprocessing module allows us to use multiple processes in Python, which can execute simultaneously and progress when needed.
