Let us imagine a system in which we have two processes:
Process ‘A’ is called at will and it’s calling is not controlled. This process creates a bunch of files. files’ names are always different so there is no concurrency issue here. I want as many ‘A’s to run at the same time, don’t care.
Process ‘B’ is invoked programmatically at set intervals and works upon the files created by process ‘A’. ‘B’ grabs the list of files and this operation should never run while any ‘A’ is running, so they must wait until ‘A’ finishes. Conversely, if any ‘A’ is running while ‘B’ is doing these sensible operation, they should all wait.
I could wrap around a semaphore/flock the whole process ‘A’ and the file-listing of process ‘B’, but doing this will also result into never allowing two concurrent ‘A’, which I don’t want.
How can I overcome this issue?
PS: this is for PHP on Unix.