[Techtalk] Priority inheritance

Mary Gardiner linuxchix at puzzling.org
Fri Jan 18 13:23:13 EST 2002


On Thu, Jan 17, 2002 at 07:45:22PM -0500, coldfire wrote:
> > > For questions like this, it seems like you have to start talking about
> > > semaphores and spinlocks and stuff.
> >  
> > 	Okay, I'm game.  What are those, and how do they work? 
> 
> "a semaphore is an integer variable that is accessed only through two
> standard atomic operations."  wait and signal.

And the reason this is important is that a program can be interrupted
between any two atomic steps.

What we want to do is "lock" a resource with one of these semaphore
variables. For example. say I wanted to write to a file.

But what if I open the file, and then get interrupted for another
process before I can write to it? And then this process goes and adds more
data to the file. Then I might write over what was written.

So what I want to do is get a lock on the file.

so the pseudocode is something like:

        wait on the WRITE_LOCK for file
                open file
                write to file
        signal that I'm done with the WRITE_LOCK

Of course, you can see that the problem is that I might hang on to the
WRITE_LOCK for ages, or forever, and noone else can write. This is
resource-hogging. You should get a lock at the last possible step, and
release it as soon as you can.

The reason that the semaphores must be atomic is in case of this
situation:

        I check that the WRITE_LOCK is free and it is.

        Another program is swapped in and also sees that the WRITE_LOCK
        is free.

        I take the write lock. (Normally the lock is an integer. When it
        is set to 0, the lock is free. When I take it, I add 1 to it.
        When I let it go, I subtract 1 from it. In this way, you can
        also limit reading a file to 5 processes, by making sure that
        READ_LOCK is < 5 before you read.)

        The other program also takes the WRITE_LOCK.

        Now we both write over each other.

Making the operation of testing the lock and setting it "atomic" means
that no other process can be swapped in between checking and setting the
value of the lock.

-Mary.

-- 
Mary Gardiner
<mary at puzzling.org>
GPG Key ID: 77625870 (wwwkeys.eu.pgp.net)



More information about the Techtalk mailing list