catlock

Catlock - catlock.c GWA Stub functions created for CS161 Asst1 NB Please use LOCKS/CV'S to solve the HP syncronization problem in this file

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * catlock.c * * 30-1-2003 : GWA : Stub functions created for CS161 Asst1. * * NB: Please use LOCKS/CV'S to solve the HP syncronization problem in * this file. */ /* * * Includes * */ #include <types.h> #include <lib.h> #include <test.h> #include <thread.h> #include <synch.h> //added the synch.h include /* * * Constants * */ #define NREADERS 10 #define NLIBRARIANS 2 #define NBOOKS 5 /* * * Function Definitions * */ struct lock *book_lock[5]; struct cv *book_cv[5]; /* * reader_i_lock() defines the three different classes of Harry Potter book readers * * Arguments: * void * unusedpointer: currently unused. * unsigned long catnumber: holds the reader identifier from 0 to NREADERS - * 1. * * Returns: * nothing.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
* * Notes: * Write and comment this function using locks/cv's. * */ static void reader_0_lock(void * unusedpointer, unsigned long reader_0_number) { /* * Avoid unused variable warnings. */ int read_book[5]={0}, last_book_read=-1, i=0; for(i=-1;i<4;i++){ if(last_book_read == i){ kprintf("reader_0 %d wants book %d \n", reader_0_number,i+1); lock_acquire(book_lock[i+1]); if(lock_do_i_hold(book_lock[i+1]) == 1){ kprintf("reader_0 %d holds lock %d\n", reader_0_number, i+1); kprintf("reader_0 %d reading book %d\n",reader_0_number,i+1); clocksleep(1); last_book_read=(i+1); lock_release(book_lock[i+1]); kprintf("reader_0 %d returns book %d\n", reader_0_number,i+1); read_book[i+1]=1; } } }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/10/2009 for the course CS 536 taught by Professor Lums during the Spring '09 term at Indiana.

Page1 / 7

Catlock - catlock.c GWA Stub functions created for CS161 Asst1 NB Please use LOCKS/CV'S to solve the HP syncronization problem in this file

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online