Readers & Writers Java Solution queries (conditional semaphore | Passing the Baton) -


i'm trying implement pseudo solution below in java part of assignment. pseudo readers preference program, reader process itself. there accompanying writer process, keep things spartan didn't bother pasting it.

process reader[i=1 m] {   while (true) {     /* implementing <await (nw == 0) nr = nr+1;> */     p(e);     if (nw > 0) {dr = dr+1; v(e); p(r);}     nr = nr + 1;     if (dr > 0) {dr = dr-1; v(r);}     else v(e);     read database;     /* implementing <nr = nr-1;> */     p(e);     nr = nr - 1;     if (nr == 0 , dw > 0) {dw = dw-1; v(w);}     else v(e);   } } 

originally assumed line:

/* implementing <await (nw == 0) nr = nr+1;> */ 

was kind of commenting on occurring, having re-read think it's supposed if statement control p(e) locking semaphore. below i've implemented in code based on above assumption.

if (nw == 0) {   nr = nr++;   try {     e.acquire();//p(e)   } catch (interruptedexception e) {   } }//end if 

the output rest of code has been bit of mess, seems work. used delays thread.sleep in order make output console more readable , again seemed ok, though wary using delays seemed quite interfere results, since giving writer processes longer delay. in theory though, locking system should sound , delays should make output console more readable.

so, correct use if statement above?

can give me tips on outputting data console prove works should (readers preference/writers preference)

the line

/* implementing <await (nw == 0) nr = nr+1;> */ 

is atomic process, code below carries out atomically.

if interested other question, add print statements under acquire/release.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -