5

BACKGROUND

I am using Apache and PHP to build a web application and I need to synchronize access to a region of shared memory. Since different instances of PHP have different process ID's, I am wondering if PHP's SyncMutex class with named mutexes can be used for this. Doing a Google search, I see quite a bit of information about using files as mutexes, but not much for the SyncMutex class. Not even the manual has much more information beyond the definition of the class and a few examples on how to use it.

QUESTION

Will a SyncMutex class named mutex in one process be visible in a different process?

RESEARCH

https://helperbyte.com/questions/470561/how-to-prevent-simultaneous-running-of-a-php-script

PHP mutual exclusion (mutex)

PHP rewrite an included file - is this a valid script?

Most reliable & safe method of preventing race conditions in PHP

FINALLY

The data involved is VERY transient and can change on a moment's notice (think volatile keyword in C). The data becomes useless after 30 seconds or so, and is purged periodically. For performance reasons, I'm storing it in the server's shared memory where the access is much faster than writing to a file or a database. Is this a valid use case or am I barking up the wrong tree? Should I use something else? Like a semaphore?

ADDITIONAL INFORMATION (EDIT 8/25/2021)

Upon further research, I have discovered that the pthreads module for PHP has been depreciated by the module owner several months ago and is no longer maintained. PHP is now using something called parallel to facilitate multithreading. Because of the design of the setup that I'm using, parallel is not compatible with what I am trying to do. So it looks like that I will have to use MySQL to handle this after all for right now.

My idea of using a memory server is still viable, but the server needs to be written in a language other than PHP due to the change in PHP's multithreading architecture. That will be done using C++, but not right now.

Thank you to everyone who responded.

Daniel Rudy
  • 1,411
  • 12
  • 23
  • I just tried to implement it on my local Windows development environment and an Amazon Linux AMI v2 and neither one supports the SyncMutex PECL library, at least, not natively. Another thought, is that if you ever see enough traffic that you load balance your application, your single server level variable is no longer accurate. – pendo Aug 23 '21 at 04:54
  • If you want to use shmop (https://www.php.net/manual/en/shmop.examples-basic.php), that's probably a good option, but you have to recompile PHP with --enable-shmop If you want something portable (that works load balanced), you could look at Redis or good ole MySQL. – pendo Aug 23 '21 at 04:57
  • @pendo I'm already using shared memory to store things like the configuration, which generally doesn't change. I haven't heard of Redis before, and I did consider using MySQL. However, the major problem with MySQL is, although it will work (even in the face of load balancing), I'm worried about the performance. Right now, MySQL and Apache are running on the same machine. I have a project in the works that will allow the app to query the database over the network (for security reasons). – Daniel Rudy Aug 23 '21 at 05:23

1 Answers1

4

According to the user supplied example below the documentation page for SyncMutex::unlock, SyncMutex lock created in one process is visible in another.

Additionally SyncSharedMemory class description says it explicitly:

Shared memory lets two separate processes communicate without the need for complex pipes or sockets. [...] Synchronization objects (e.g. SyncMutex) are still required to protect most uses of shared memory.

  • It seems like all the little pieces that you need to know about are scattered all over the documentation. Besides, I've been using shared memory for config info for the last 5 years. Never needed a lock on it because it's mostly read only, and only when a process first starts up. The start routine copies the data from shared memory into an array that's in the global scope. That way, I save a database query which improves performance. – Daniel Rudy Aug 23 '21 at 20:35
  • @cexaryta Besides, I'm going to use the shared memory for a single server solution. In the load balance case where multiple servers are involved, I'm creating a memory server. One machine is setup to allow it's memory to be accessed over the network. There are access controls involved as well as other things, but basically it's shared memory over a network. I already have the basic network server code in place, just have to flesh it out a bit. – Daniel Rudy Aug 23 '21 at 20:39