pn@stc.co.uk (03/17/86)
The way to implement a mutual exclusion zone between 2 processors and using two flags is as follows: Each processor has its own flag. When either processor wants to lock the zone it - 1. sets it's own flag to LOCKED 2. reads the other processor's flag 3. if the other processor's flag is UNLOCKED, it proceeds, otherwise it does something else. The something else depends on your application. Maybe all you need to do is spin in a loop waiting for the zone to be UNLOCKED. Maybe you need to go away and do something else for a while and try later; in this case you can leave your flag LOCKED, or you can UNLOCK it first, which is safer but may not achieve what is required. The worst case for this scheme is when both processors set their flag to LOCKED and then read each other's flag, to find it LOCKED. Deadlock, my dear Watson. The simplest way to overcome this is to have each processor sit in a tight loop (for a restricted number of iterations) reading the other processor's flag until it becomes UNLOCKED. If it does'nt, then the reading processor must give up, setting its own flag back to UNLOCKED to give the other processor a chance. What must be avoided is both processors jumping in and out of this LOCK-read-UNLOCK loop ad-nauseum. This means the time of execution for the read loop must be different for each processor. The one with the longest time wins. Note that this can be used to enforce priority when a prospective deadlock situation arises. Sitting in the read loop for a randomly-generated time might also be a possibility too. This scheme can be extended to more than 2 processors, but it gets more involved. Hope this helps. -- Phil Norris <pn@stc.UUCP> {root44, ukc, datlog, idec, stl, creed, iclbra, iclkid}!stc!pn