Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mike Dahlins method for writing pthreads code. (If you want to know more about it, please read Prof. Dahlins note on Basic Threads Programming on

Mike Dahlins method for writing pthreads code. (If you want to know more about it, please read Prof. Dahlins note on Basic Threads Programming on our iLearn page.) Heres a reminder the basic method: add a lock to the shared object add code to each method to acquire the lock at the beginning of the method and release it at the end add zero or more condition variables to the object add wait calls on condition variables within loops finally, add signal calls

In this programming project you will start with some C code I provide (see below), and finish it by adding code where you see a comment that says YOUR CODE HERE. This purpose of the code is to implement a readers-writers lock. The idea of a readers-writers lock is that we have multiple programs wanting to concurrently access data. It is not okay to have two programs accessing the data at once if at least one of the programs is modifying the data, but it is okay for multiple programs to access the data at once if they are only reading the data. More precisely, with a reader-lock, if a writing thread is accessing the data, then no other threads can access the data, but if reading threads are accessing the data, then other reading threads (but no writing threads) can access the data.

Notice that the reader() function prints sr and dr, which are short for start read and done read. Similarly, the writer() function prints sw and dw. The code you submit must produce as output only lines with either sr, dr, sw, and dw.

#include #include #include #include

#define NUMOPS 5000

typedef struct { // sync. variables pthread_mutex_t lock; pthread_cond_t read_go; pthread_cond_t write_go;

// state variables int active_readers; int active_writers; int waiting_readers; int waiting_writers; } RWLOCK;

RWLOCK *rwlock_create() { // initialize state variables RWLOCK *rwlock; rwlock = malloc(sizeof(RWLOCK)); assert(rwlock != NULL); rwlock->active_readers = 0; rwlock->active_writers = 0; rwlock->waiting_readers = 0; rwlock->waiting_writers = 0;

// initialize synchronization variables pthread_mutex_init(&rwlock->lock, NULL); pthread_cond_init(&rwlock->read_go, NULL); pthread_cond_init(&rwlock->write_go, NULL);

return(rwlock); }

// diagnostic code -- use as you like // enable by using 1 instead of 0 in 'if' condition void print_state(char *id, RWLOCK *rwlock) { if (0) { printf("%s: wr=%d, ar=%d, ww=%d, aw=%d ", id, rwlock->waiting_readers, rwlock->active_readers, rwlock->waiting_writers, rwlock->active_writers); } }

void start_read(RWLOCK *rwlock) { pthread_mutex_lock(&rwlock->lock); print_state("sr1", rwlock);

rwlock->waiting_readers++;

// YOUR CODE HERE // your code will have the form: // while (...) { // pthread_cond_wait(...); // }

rwlock->waiting_readers--; rwlock->active_readers++;

print_state("sr2", rwlock); pthread_mutex_unlock(&rwlock->lock); }

void done_read(RWLOCK *rwlock) { pthread_mutex_lock(&rwlock->lock); print_state("dr1", rwlock);

// YOUR CODE HERE // your code will have the form: // update 0 or more state variables // if (...) { // pthread_cond_signal(...); // } // update 0 or more state variables

print_state("dr2", rwlock); pthread_mutex_unlock(&rwlock->lock); }

void start_write(RWLOCK *rwlock) { pthread_mutex_lock(&rwlock->lock); print_state("sw1", rwlock);

// YOUR CODE HERE

print_state("sw2", rwlock); pthread_mutex_unlock(&rwlock->lock); }

void done_write(RWLOCK *rwlock) { pthread_mutex_lock(&rwlock->lock); print_state("dw1", rwlock);

// YOUR CODE HERE

print_state("dw2", rwlock); pthread_mutex_unlock(&rwlock->lock); }

// reader: does a bunch of reads void *reader(void *arg) { RWLOCK *rwlock = (RWLOCK *)arg; int i; for (i = 0; i < NUMOPS; i++) { start_read(rwlock); printf("sr "); printf("dr "); done_read(rwlock); } pthread_exit(NULL); }

// writer: does a bunch of writes void *writer(void *arg) { RWLOCK *rwlock = (RWLOCK *)arg; int i; for (i = 0; i < NUMOPS; i++) { start_write(rwlock); printf("sw "); printf("dw "); done_write(rwlock); } pthread_exit(NULL); }

// test the read-write lock int main(int argc, char *argv[]) { pthread_t r1,r2,r3,w1,w2;

RWLOCK *rwlock = rwlock_create();

// create some reading and writing threads pthread_create(&r1, NULL, reader, (void *)rwlock); pthread_create(&r2, NULL, reader, (void *)rwlock); pthread_create(&r3, NULL, reader, (void *)rwlock); pthread_create(&w1, NULL, writer, (void *)rwlock); pthread_create(&w2, NULL, writer, (void *)rwlock);

pthread_exit(NULL); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions