Commit 5f937c51 authored by Mathias Haage's avatar Mathias Haage
Browse files

Add ht15 exercise 1 and 3

parent 1048f78c
......@@ -3,3 +3,5 @@
alarmclock/bin
lift/bin
washing/bin
exercise\ 1/bin
exercise\ 3/bin
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="/ljrt/ljrt.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>exercise 1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
#Tue Aug 21 13:58:31 CEST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
package buffer;
import se.lth.cs.realtime.semaphore.*;
/**
* The buffer.
*/
class Buffer {
Semaphore mutex; // For mutual exclusion blocking.
Semaphore free; // For buffer full blocking.
Semaphore avail; // For blocking when no data is available.
String buffData; // The actual buffer.
Buffer() {
mutex = new MutexSem();
free = new CountingSem(1);
avail = new CountingSem();
}
void putLine(String input) {
free.take(); // Wait for buffer empty.
mutex.take(); // Wait for exclusive access.
buffData = new String(input); // Store copy of object.
mutex.give(); // Allow others to access.
avail.give(); // Allow others to get line.
}
String getLine() {
// Exercise 2 ...
// Here you should add code so that if the buffer is empty, the
// calling process is delayed until a line becomes available.
// A caller of putLine hanging on buffer full should be released.
// ...
return null;
}
}
package buffer;
/**
* The Consumer.
*/
class Consumer extends Thread {
Buffer theBuffer;
Consumer(Buffer b) {
super();
theBuffer = b;
}
public void run() {
try {
sleep(10000); // 10s until work starts.
while (true) {
System.out.println(theBuffer.getLine());
}
}
catch (Exception e) {/* Let thread terminate. */};
} // run
} // Consumer
package buffer;
/**
* The producer.
*/
class Producer extends Thread {
Buffer theBuffer;
Producer(Buffer b) {
super(); // Construct the actual thread object.
theBuffer = b;
}
public void run() {
String producedData = "";
try {
while (true) {
if (producedData.length()>75) break;
producedData = new String("Hi! "+producedData);
sleep(1000); // It takes a second to obtain data.
theBuffer.putLine(producedData);
}
}
catch (Exception e) {
// Just let thread terminate (i.e., return from run).
}
} // run
} // Producer
package buffer;
/**
* Simple producer/consumer example using semaphores.
* The complete example, including all classes, is put in one outer class.
* That lets us keep it all in one file. (Not good for larger programs.)
*/
public class RTsemBuffer {
/**
* Static stuff which permits the class to be called as a program.
*/
public static void main(String args[]) {
// Since this is a static function, we can only access static data.
// Therefore, create an instance of this class; run constructor:
new RTsemBuffer();
}
/**
* Constructor which in this example acts as the main program.
*/
public RTsemBuffer () {
Buffer buff = new Buffer();
Producer p = new Producer(buff);
Consumer c = new Consumer(buff);
c.start();
p.start();
System.out.println("\n\n"+"RTsemBuffer: Threads are running ...");
try {
p.join();
// Give consumer 10s to complete its work, then stop it.
Thread.sleep(10000);
c.interrupt(); // Tell consumer to stop.
c.join(); // Wait until really stopped.
}
catch (InterruptedException e) {/* Continue termination...*/};
System.out.println("\n"+"RTsemBuffer: Execution completed!");
}
} // RTsemBuffer
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="/ljrt/ljrt.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>exercise 3</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
#Tue Aug 21 13:59:03 CEST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
package buffer;
import se.lth.cs.realtime.RTError;
class Buffer {
int available; // Number of lines that are available.
final int size=8; // The max number of buffered lines.
String[] buffData; // The actual buffer.
int nextToPut; // Writers index.
int nextToGet; // Readers index.
Buffer() {
buffData = new String[size];
}
synchronized void putLine(String inp) {
try {
while (available==size) wait();
} catch (InterruptedException exc) {
throw new RTError("Buffer.putLine interrupted: "+exc);
};
buffData[nextToPut] = new String(inp);
if (++nextToPut >= size) nextToPut = 0;
available++;
notifyAll(); // Only notify() could wake up another producer.
}
synchronized String getLine() {
// Write the code that implements this method ...
return null;
}
}
package buffer;
/**
* The Consumer.
*/
class Consumer extends Thread {
Buffer theBuffer;
Consumer(Buffer b) {
super();
theBuffer = b;
}
public void run() {
try {
sleep(10000); // 10s until work starts.
while (true) {
System.out.println(theBuffer.getLine());
}
}
catch (Exception e) {/* Let thread terminate. */};
} // run
} // Consumer
package buffer;
/**
* The producer.
*/
class Producer extends Thread {
Buffer theBuffer;
Producer(Buffer b) {
super(); // Construct the actual thread object.
theBuffer = b;
}
public void run() {
String producedData = "";
try {
while (true) {
if (producedData.length()>75) break;
producedData = new String("Hi! "+producedData);
sleep(1000); // It takes a second to obtain data.
theBuffer.putLine(producedData);
}
}
catch (Exception e) {
// Just let thread terminate (i.e., return from run).
}
} // run
} // Producer
package buffer;
/**
* Simple producer/consumer example using semaphores.
* The complete example, including all classes, is put in one outer class.
* That lets us keep it all in one file. (Not good for larger programs.)
*/
public class RTsemBuffer {
/**
* Static stuff which permits the class to be called as a program.
*/
public static void main(String args[]) {
// Since this is a static function, we can only access static data.
// Therefore, create an instance of this class; run constructor:
new RTsemBuffer();
}
/**
* Constructor which in this example acts as the main program.
*/
public RTsemBuffer () {
Buffer buff = new Buffer();
Producer p = new Producer(buff);
Consumer c = new Consumer(buff);
c.start();
p.start();
System.out.println("\n\n"+"RTsemBuffer: Threads are running ...");
try {
p.join();
// Give consumer 10s to complete its work, then stop it.
Thread.sleep(10000);
c.interrupt(); // Tell consumer to stop.
c.join(); // Wait until really stopped.
}
catch (InterruptedException e) {/* Continue termination...*/};
System.out.println("\n"+"RTsemBuffer: Execution completed!");
}
} // RTsemBuffer
package queue;
class ClerkHandler extends Thread {
YourMonitor mon;
int id;
ClerkHandler(YourMonitor sharedData, int id) { mon=sharedData; this.id=id; }
public void run() {
while (true) {
HW.waitClerkButton(id);
mon.clerkFree(id);
}
}
}
package queue;
class CustomerHandler extends Thread {
YourMonitor mon;
CustomerHandler(YourMonitor sharedData) { mon = sharedData; }
public void run() {
while (true) {
HW.waitCustomerButton();
int qNum = mon.customerArrived();
HW.printTicket(qNum);
}
}
}
package queue;
class DispData {
int ticket;
int counter;
}
package queue;
class DisplayHandler extends Thread {
YourMonitor mon;
DispData disp;
DisplayHandler(YourMonitor sharedData) { mon = sharedData; }
public void run() {
while (true) {
try {
disp = mon.getDisplayData();
HW.display(disp.ticket, disp.counter);
sleep(10000);
} catch (InterruptedException e) { break; }
}
}
}
package queue;
public class HW {
static void waitClerkButton(int id) {
long sleep = (long)(Math.random() * 60000);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static void waitCustomerButton() {
long sleep = (long)(Math.random() * 20000);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static void printTicket(int qNum) {
System.out.println("TICKET: customer got ticket "+qNum);
}
static void display(int ticket, int counter) {
System.out.println("DISPLAY: counter "+counter+" now handling ticket "+ticket);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment