Phase 1 Tutorial - COMPUTER SCIENCE

advertisement
Nachos
Getting the Source Code



-See CVS quickstart on COSC3407 website. On
windows use putty:
In the computer lab:
-> start -> Math and computer science -> internet
applications -> putty
or open dos window: p: -> cd putty -> putty
type nachos.cs.laurentian.ca in host name box. you get a warning message the 1st time -you can
ignore it (click yes). Sign on.
follow instructions from cvs quickstart. -Need group
number -these are listed on the course website under
project groups.
Moving files






can use ftp to move files to and from your pc.
ftp nachos.cs.laurentian.ca You will be
prompted to enter a user name and password.
Once signed in you can use cd to change
directories, dir or ls to list the directory, pwd to
show the current directory.
Use get filename to move a file from the current
directory on the remote system to the current
directory on the local system.
Use put filename to move a file from the current
directory on the local system to the current
directory on the remote system.
Can use mget *.* to get an entire directory.
Nachos Source Code




The code for nachos is in the directory
groupx/nachos (x is your group number).
All the code that will be changed in
project 1 is in the threads subdirectory
(groupx/nachos/threads).
You may also want to look at the machine
subdirectory (if you are ambitious). This
implements the nachos virtual machine.
There are also some sample design
documents on the course web site.
Project 1:

Go to project phases -> project1 on
the course website to get a
description of the project. Read this
section very carefully.
Task 1:





The task requires you to modify the KThread.java
class. If you look at the source code you will see that
the join method has not been implemented. The task is
to implement this method.
You also have to make modifications to at least one
other method.
For example, suppose 2 threads t1 and t2 have been
created.
If thread t1 has the line t2.join(), this means that
thread t1 will stop processing until thread t2 has
completed.
A thread can be in 1 of 4 states:
new, ready, running, blocked or finished.
To avoid busy waiting, the waiting thread should be in
the blocked state (look at the sleep method). You then
need some way to "wake up" the blocked thread (t1)
when thread t2 finishes.
Creating threads





To create and run a thread:
final KThread KThreadA, KThreadB, KThreadC;
…
KThreadD = new KThread(new Runnable(){
public void run()
{
System.out.println("THREAD D STARTED");
for(int i = 0; i < 100000000; i++)
{}
KThreadE.join();
System.out.println("THREAD D FINISHED");
}
});
To run:
KThreadD.fork();
Described in Guide to Nachos on course website under link Nachos for
Java Walkthrough.
Task2

There is a file Condition2.java in the
threads directory. This program has the
methods you need to implement – it’s
just a shell. Look at Condition.java which
implements the condition variables using
semaphores. The task is to implement
condition variables without
using
semaphores. To do this task you should
understand both condition variables and
semaphores. Look at the code in
Semaphore.java.
Task2 (Condition Variables)



Condition variable: Has an associated lock to
make sure only one process is active in the critical
region at a time. A thread must acquire the lock
to enter the critical region.
The condition variable allows a thread to wait for
a condition to become true while in the critical
region. If the condition is not true the thread can
"sleep" in the critical region and allow another
process to enter. This thread may change the
condition to true and wake a thread that is
currently sleeping on the condition. The awakened
thread is moved to the ready queue (mesa
semantics).
This means it does not run right away. The
awakened thread will try to required the lock and
proceed. It must check the condition again to
make sure it is still true.
Task 2 -Methods




void sleep()
This method atomically released the lock and puts itself
to sleep. On waking it needs to require the lock.
void wake()
This method wake one thread (if there is one) that are
sleeping on the condition.
void wakeAll()
Wakes all threads sleeping on this condition.
**note** All methods require that the thread hold the
lock.
Task 3



Complete the alarm class
The task is to implement the waitUntil(int
x) method in Alarm.java
We want to make the calling thread to
block -i.e. move to the wait queue. The
thread should be woken when the interval
specified on the call is over (within 500
ticks). The timerInterrupt() method in this
class is called every 500 ticks.To get the
current time:
long curTime =Machine.timer().getTime();
Task 4:




Implement the Communicator class.
2 methods to implement, void speak(int word) and int
listen(). The message (word) is passed from the
speaker to exactly one listener. If there are no listeners
the speaker must wait for one. For example
if 3 speaker threads are run first, they will all wait until
listeners arrive. If then 3 listener threads arrive then
each will pick up a different word from each of the
three speakers. As soon as their word is transferred the
speaker and listener exit the system.
Conversely, if listeners arrive before speakers, they
must also wait.
The documentation suggests that this can be done with
one condition variable. Note that there should never be
listeners and speakers waiting at the same time.
Task 5

Implement ReactWater. Somewhat similar to
Communicator. In this case 2 hydrogen, and one
oxygen. Each thread will either be a hydrogen
thread (call hReady()) or an oxygen thread (call
oReady()). As soon as 2 hydrogen threads and one
oxygen thread are ready, makeWater()
can be called and those threads can exit. For
example, if 3 oxygen threads are created
and 4 hydrogen, 2 water molecules can be created,
and 1 oxygen molecule should be left
waiting. No thread should be part of 2 different
water molecules.
Download