Process Limiting in Unix with Semaphores
One common trick in the Wide World of Windows (Also known as the WWW, not to be confused with the World Wide Web or Weasley’s Wizard Wheezes) is to prevent the user from running more than 1 instance of an application. Try it sometime, open up something like Microsoft Word. Then, try to open it again and you’ll find that it doesn’t work, it simply refocuses the previous session. It’s a pretty useful trick and Windows gives you lots of ways to make this happen.
However, on a real multi-user operating system like Linux or Unix, such behavior is shunned. You expect multiple copies of everything to be open at once since you may have multiple users running the program simultaneously. Every now and then, however, it’s advantageous to get this similar behavior. I ran into a case-in-point this week. A user was running ezViz, my pride and joy, on one of the supercomputers. He submitted several jobs, each one running several instances of ezViz serially. Unfortunately, they all wound up on the same node, and he wound up running 8 versions simultaneously, each one wanted 3.5G of a 16G machine. It wasn’t pretty.
After thinking about it for a while, I thought it might be helpful to give the user a way to specify a maximum number of simultaneous runs. Runs beyond that number would simply wait their turn. First attempts were to implement something like ‘ps‘ to check the running processes and see how many were running. Not only is this difficult, but there are alot of race conditions and such from multiple versions trying to check simultaneously.
I did some research and a friend of mine suggested using a shared memory block with shmget to have each process ‘register’ itself in the shared space. Each process could register and know how many other processes were going, and then decide whether to wait or continue. While that’s definately an option, a similar but far better method is Semaphores. Come on inside for more..
[tag:linux][tag:unix][tag:source][tag:semaphore]

As you’ve probably noticed by now, I’ve been trying really hard to learn