Monday, September 15, 2014

A simple read-write lock implementation - Problem 2

This is the continued analysis of simple read-write lock implementation problem. This time it is starvation of writer locks.

Let me modify the thread routines and remove the sleep() after unlock, which means that readers or writers are assumed to have no pause in between. Here are modified thread routines.

void*
thread_routine1 (void *arg)
{
    for (;;) {
        my_rwlock_read_lock(&g_rw_lock);
        printf("Grabbed read-lock -- 1\n");
        sleep(10);
        my_rwlock_read_unlock(&g_rw_lock);
    }
}

void*
thread_routine2 (void *arg)
{
    for (;;) {
        my_rwlock_read_lock(&g_rw_lock);
        printf("Grabbed read-lock -- 2\n");
        sleep(10);
        my_rwlock_read_unlock(&g_rw_lock);
    }
}


void*
thread_routine3 (void *arg)
{
    for (;;) {
        my_rwlock_write_lock(&g_rw_lock);
        printf("Grabbed write-lock -- 1\n");
        sleep(5);
        my_rwlock_write_unlock(&g_rw_lock);
    }
}

void*
thread_routine4 (void *arg)
{
    for (;;) {
        my_rwlock_write_lock(&g_rw_lock);
        printf("Grabbed write-lock -- 2\n");
        sleep(5);
        my_rwlock_write_unlock(&g_rw_lock);
    }
}


What is the output?

Grabbed read-lock -- 1
Grabbed read-lock -- 2
Grabbed read-lock -- 2
Grabbed read-lock -- 1
Grabbed read-lock -- 2
Grabbed read-lock -- 1
Grabbed read-lock -- 1
Grabbed read-lock -- 2


:). As you see the writers are severely starved! As of now, I am not still have zero thoughts to fix this issue. If you find any suitable approach please let me know in the comment box.

No comments:

Post a Comment