You are on page 1of 6

K.

Gop 2015 COMP201P Computer Arch Answers

COMP201P 2015 Computer Arch - Example Answers

Computer Architecture and Hardware

1.a)

A0 = 0 B0 =1
A1 = 0 B1 =1
A2 = 1 B2 =0
A3 = 1 B3 =0
OP = 1

1.b)

The system uses 2’s complement to encode negative numbers. This can be inferred from the
2-input mutliplexers on each of the B inputs, which inverts the bits on logical high, and the
multiplexer on the carry in which is set to 1 if OP is set to logical high.
This implies a 2’s complement encoding of negative numbers as the procedure for negating
a number under 2’s complement is to invert it’s bits and add 1.

1.c)

(ā · b̄) + (a · b) + (a · b̄ · c)

1.d)

• 0x68AC

• 45.375

1
K.Gop 2015 COMP201P Computer Arch Answers

1.e)

2.a)

• As MIPS memory addresses are 32 bits long, a MIPS instruction can not contain an
entire address - as there would be no space for the opcode (instructions are 32 bits long).

• To work around this, MIPS supports a base and offset displacement addressing scheme,
whereby memory can be accessed by providing an unsigned 32-bit integral value for a
base address in a register, and a signed 16-bit signed integral value in the instruction as
an immediate value.

• Memory is retrieved from the base address offset (i.e with the offset added on) by the
signed immediate value.

2.b)

. text
. globl convert
convert :
add $3 , $0 , $0 % $3 = i
a d d i $4 , $0 , 20 % $4 = 5 ∗ 4
a d d i $8 , $0 , 5 % $8 = 5
a d d i $9 , $0 , 9 % $9 = 9

loop :
add $5 , $1 , $0 % $5 = addr from
add $6 , $2 , $0 % $6 = addr t o
add $5 , $5 , $3 % $5 = addr from + o f f s e t
add $6 , $6 , $3 % $6 = addr t o + o f f s e t
lw $7 , 0 ( $5 ) % $7 = mem[ addr from + o f f s e t ]
s u b i $7 , $7 , 32 % $7 = $7 − 32
mult $7 , $7 , $8 % $7 = 5 ∗ ( $7 − 3 2 )
d i v $7 , $7 , $9 % $7 = ( 5 / 9 ) ∗ ( $7 − 3 2 )
sw $7 , 0 ( $6 ) % mem[ addr t o + o f f s e t ] = $7
a d d i $3 , $3 , 4 % $3 = $3 + 4
bne $3 , $4 , l o o p % i f ( $3 == 2 0 ) break ;
halt

2
K.Gop 2015 COMP201P Computer Arch Answers

Networking and Concurrency

1.a)

• A concurrent system, defined under the concurrency abstraction, is a program that


consists of 2 or more abstract processes, executing sequences of atomic actions.

• An abstract process is a sequence of atomic actions which must be executed in


sequence (relative to each other).

• An atomic action is an operation that can not be subdivided any further, and from
the purpose of a thread observing its execution, either all of the action has completed,
or none of it.

• Under the concurrency abstraction, only one atomic action can be executed at a
time, so to simulate parallel execution, the atomic actions from multiple processes are
interleaved.

• The concurrency abstraction provides a simplified view of concurrent systems to


manage their complexity, by modeling concurrent systems as collections of abstract
processes, whose atomic actions are interleaved sequentially.

• Under the concurrency abstraction, a program is only correct if it meets its speci-
fication under all interleavings of the individual atomic actions which make up it’s
processes.

1.b)i)

Values of z are: 0, 1, 2, 3, 6

1.b)ii)

Values of z are: 0, 1, 2

2.a)

• Java provides synchronization facilities through the use of synchronized blocks and syn-
chronized methods.

• Each object has an intrinsic reentrant mutual exclusion lock. Before a thread executes a
synchronized method of an object, it must first acquire the object’s intrinsic lock. When
a thread completes execution of a synchronized block or method it releases the intrinsic
lock, allowing other threads to then access it.

3
K.Gop 2015 COMP201P Computer Arch Answers

• As only one thread can take out the intrinsic lock at a time, this system ensures that only
one thread can access an objects synchronized methods at a time - mutual exclusion.

• If another thread attempts to call a synchronized method on the object while the original
thread has yet to complete, it will first try and acquire the lock. However, when attempt-
ing to do so, as the lock is already taken, the thread’s execution will be suspended and
it will be placed into the BLOCKED state.

• Once the original thread releases the lock, any threads that were placed into the BLOCKED
state while trying to acquire the object’s intrinsic lock will be awoken and can try to ac-
quire the intrinsic lock.

• As intrinsic locks are reentrant, once a thread holds the intrinsic lock for an object, it
can reacquire the lock without blocking - however in doing so, it increases the number of
times it must release the lock before other threads can acquire it.

• This reentrant property means that a thread can call other synchronized methods of the
object without deadlocking on itself.

2.b)

• When a thread holds an object’s intrinsic lock (i.e when it is executing a synchronized
section of code), if a thread calls wait on the object, it will be placed into the WAITING
state and will cease execution releasing the lock.

• Then when another thread holds the object’s intrinsic lock and calls notify() or noti-
fyAll(), it will wake up threads that were waiting on the object.

• At this point, all the woken threads will attempt to reacquire the intrinsic lock, however
the notifying thread already has it, so the woken threads are placed into the BLOCKED
state.

• Once the notifying thread leaves the synchronized section and releases the lock, the other
threads will reattempt to acquire the lock.

• Calling notify() only wakes one sleeping thread, whereas calling notifyAll() wakes all
sleeping threads.

• If notify is used it could lead to a deadlock situation, for the thread that happens to wake
could be using conditional synchronization, and finding that it’s condition has not been
met, will return to sleep. At this point, even if there was a thread that could operate on
the object in it’s current state, no action will occur.

• Using notify() is only safe when all threads are waiting on the same condition - in which
case, if any one thread wakes up and checks the condition and finds it to be false, then
all the other threads, if awoken would also come to the same conclusion.

2.c)i)

4
K.Gop 2015 COMP201P Computer Arch Answers

public enum Lane {


LANE A, LANE B
}
public c l a s s B r i d g e {
private s t a t i c int MAX WEIGHT = 5 0 0 0 ;
private s t a t i c int MAX DIFF = 2 0 0 0 ;
private int laneA = 0 ;
private i n t o laneB = 0 ;

public synchronized void e n t e r ( Lane l , int weight )


throw I n t e r r u p t e d E x c e p t i o n {

switch ( l ) {
case LANE A :
while ( ( ( int ) Math . abs ( ( laneA + weight ) − laneB ) > MAX DIFF)
| | ( ( laneA + weight ) + laneB > MAX WEIGHT) )
wait ( ) ;
laneA += weight ;
notifyAll ();
break ;
case LANE B :
while ( ( ( int ) Math . abs ( ( laneB + weight ) − laneA ) > MAX DIFF)
| | ( ( laneB + weight ) + laneA > MAX WEIGHT) )
wait ( ) ;
laneB += weight ;
notifyAll ();
break ;
}
}

public synchronized void e x i t ( Lane l , int weight )


throw I n t e r r u p t e d E x c e p t i o n {
switch ( l ) {
case LANE A :
while ( ( int ) Math . abs ( ( laneA − weight ) − laneB ) > MAX DIFF)
wait ( ) ;
laneA −= weight ;
notifyAll ();
break ;
case LANE B :
while ( ( int ) Math . abs ( ( laneB − weight ) − laneA ) > MAX DIFF)
wait ( ) ;
laneB −= weight ;
notifyAll ();
break ;
}

5
K.Gop 2015 COMP201P Computer Arch Answers

2.c)ii)

public c l a s s Car {
private f i n a l int weight ;
private f i n a l B r i d g e b r i d g e ;

public Car ( B r i d g e b , int weight ) {


i f ( weight < 0 )
throw new I l l e g a l A r g u m e n t E x c e p t i o n ( ” N e g a t i v e c a r i s i m p o s s i b l e ” ) ;
t h i s . weight = weight ;
this . bridge = b ;
}

public void run ( ) {


long t i m e n o s e e = ( long ) Math . random ( ) ∗ 1000 ∗ 60 ∗ 1 ;
t i m e n o s e e += 1 ∗ 1000 ∗ 6 0 ;

try {
b . e n t e r ( Lane . LANE A, weight ) ;
Thread . s l e e p ( t i m e n o s e e ) ;
b . e x i t ( Lane . LANE A, weight ) ;
} catch ( I n t e r r u p t e d E x c e p t i o n e ) {
System . e r r . p r i n t l n ( ”Was i n t e r r u p t e d ” ) ;
e . printStackTrace ( ) ;
}

You might also like