06.08.2013 Views

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

JAVA-BASED REAL-TIME PROGRAMMING

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4. Exercises and Labs<br />

/**<br />

* The buffer.<br />

*/<br />

class Buffer {<br />

Semaphore mutex; // For mutual exclusion blocking.<br />

Semaphore free; // For buffer full blocking.<br />

Semaphore avail; // For blocking when no data is available.<br />

String buffData; // The actual buffer.<br />

}<br />

Buffer() {<br />

mutex = new MutexSem();<br />

free = new CountingSem (1);<br />

avail = new CountingSem ();<br />

}<br />

void putLine(String input) {<br />

free.take(); // Wait for buffer empty.<br />

mutex.take(); // Wait for exclusive access.<br />

buffData = new String(input); // Store copy of object.<br />

mutex.give(); // Allow others to access.<br />

avail.give(); // Allow others to get line.<br />

}<br />

String getLine() {<br />

// Exercise 2 ...<br />

// Here you should add code so that if the buffer is empty, the<br />

// calling process is delayed until a line becomes available.<br />

// A caller of putLine hanging on buffer full should be released.<br />

// ...<br />

}<br />

102 2012-08-29 16:05

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!