1

For a hypothetical database, there are three operations: Search, Append, Modify

Search: can run concurrently with any number of other search operations

Append: database can only run ONE append operation at a time along with any number of searches

Modify: database should be locked from all other search, append and modify operations.

So far this is what I have down as pseudo-code. Will I have any problems with deadlock even though I lock and unlock things in the same order? The problem I have is that the modify operation does not have exclusive access to the database. Could this be solved with a semaphore with one flag?

sem_t append;
sem_init(&append, 0, 1);

I don't need help implementing it into a programming language. I just want to figure out the right placement of locks, unlocks and maybe semaphores so that the rules above are satisfied.

pthread_mutex_t searchLock;
pthread_mutex_t appendLock;
pthread_mutex_t modifyLock;

ENTRY SECTION:
if(search operation)
    lock(modifyLock);
    unlock(modifyLock);
    lock(searchLock);
    unlock(searchLock);
else if(append operation) 
    lock(modifyLock);
    lock(appendLock);
else //modify operation
    lock(modifyLock);
    lock(appendLock);
    lock(searchLock);
--------------------------

CRITICAL SECTION:
    //do something
--------------------------

EXIT SECTION: 
if(append operation)
    unlock(appendLock);
    unlock(modifyLock);

if(modify operation)
    unlock(appendLock);
    unlock(searchLock);
    unlock(modifyLock);
4

0 回答 0