{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

catlock - catlock.c GWA Stub functions created for CS161...

Info icon This 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.
Image of page 1

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

View Full Document Right 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; } } }
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern