10/15/2009
Lecture 14
Part I
Example Exam
&
Course Summary
Example Exam Questions
Roller-Coaster
Specification
• A new roller-coaster is
to be built at Liseberg,
• And they want you to
write a program to
simulate it
• The ride begins when either
◦ The train is full of people, or
◦ When there is at least one passenger on the train,
and two minutes have passed since the last
passenger boarded.
boarded
◦ In order to find bugs
before it opens
• When the train is running, no new passengers
can board the train, and no passengers on the
train can leave.
PPHT09 – Example Exam
3
The Problem – Part 1
PPHT09 – Example Exam
4
The Problem – Part 2
• Assume that the train has K seats
• With the help of processes that communicate
using message passing, implement two
operations
• Operation void ride()
◦ Called by passengers who wish to ride the RC
◦ The calling process is blocked until the ride is over
and the train has returned to the station
• Operation void
id rideOver()
id
()
◦ op void ride(), and
◦ op void rideOver()
◦ Called by some operator process when the train
returns
◦ This operation releases all passengers from the
train and then allows new waiting passengers to
enter the train
• You are not required to write
◦ Any main program,
◦ Passenger processes, or
◦ An operator process
PPHT09 – Example Exam
5
PPHT09 – Example Exam
6
1
10/15/2009
Solution?
Exam Grading
• Make groups of 2
• Take out a red pen/pencil
• Take out pen and paper
• Exchange your solution with the group next to
you
• You have 10 minutes to solve it
• Carefully read their solution and see if you can
understand it
• Starting Now
PPHT09 – Example Exam
7
Solution – Timer
PPHT09 – Example Exam
8
Solution – One Possibility
public class Liseberg1 {
• We need a timer for timeouts
◦ From exercises: simple timeout timer
public op void ride();
public op void rideOver();
p
private
int K;
;
private op void boardTrain();
private op void leaveTrain();
public op void timer(cap void() alarm,
alarm
int ms) {
JR.nap(ms);
alarm();
}
public void ride() {
call boardTrain();
receive leaveTrain();
}
//continues
PPHT09 – Example Exam
9
Solution – One Possibility
Wait using
receive.
PPHT09 – Example Exam
10
Solution – One Possibility
private process control {
int onboard; boolean timedOut;
while(true) {
onboard = 0;
timedOut = false;
while (onboard < K && !timedOut) {
op void timeout();
if (onboard > 0)
send timer(timeout, 2000);
inni void boardTrain() {
onboard++; }
[]
void timeout() {
timedOut = true; }
}
//continuation
receive rideOver();
for (int i=0; i<onboard; i++)
call leaveTrain();
}
}
• The code on the course web-page has all the
other processes to test the solution and
experiment with it
//continues
PPHT09 – Example Exam
Model separate
train boarding
and leaving.
11
PPHT09 – Example Exam
12
2
10/15/2009
Solution – Another Possibility
Solution – Another Possibility
private process control {
boolean timedOut;
while(true) {
timedOut = false;
while (riding.length()<K && !timedOut) {
op void timeout();
if (
(riding.length()
idi
l
th() > 0)
send timer(timeout, 2000);
inni void ride() {
forward riding(); }
[]
void timeout() {
timedOut = true; }
}
• Use forward statement together with internal
operation
◦ Private operation riding()
public
bli class
l
Li
Liseberg2
b
2 {
public op void ride();
public op void rideOver();
private int K;
private op void riding();
//continues
//continues
PPHT09 – Example Exam
13
Solution – Another Possibility
PPHT09 – Example Exam
14
1-to-N synchronous channel
• From exam October 2003
//continuation
receive rideOver();
while (riding.length() > 0)
receive riding();
◦ See web site for precise formulation
• Implement a class with operations
◦ op void syncSend(int x)
◦ op int syncReceive()
}
}
• Such that
◦ Sender blocks until all N receivers are ready to
receive,
◦ Receiver blocks until sender and all the other N-1
receivers are ready
• Again, the code on the course web-page has
all the other processes to test the solution
and experiment with it
PPHT09 – Example Exam
15
Solution?
PPHT09 – Example Exam
16
Solution – One Possibility
• Back to groups
• Use forward statement together with internal
operation
◦ Private operation receiveQ()
• 10 minutes
public
bli class
l
O
OnetoN2
t N2 {
• Grading
public op void syncSend(int x);
public op int syncReceive();
private op int receiveQ();
private int N;
//continues
PPHT09 – Example Exam
17
PPHT09 – Example Exam
18
3
10/15/2009
Solution – One Possibility
Solution – Another Possibility
private process server {
while (true)
inni void syncSend(int x)
st receiveQ.length() >= N {
for(int i=0; i<N; i++)
inni int receiveQ() {
return x;
}
}
[] int syncReceive() {
forward receiveQ();
}
}
• Can we avoid the forwarding?
◦ Yes
• I would accept this shorter solution as correct
◦ And No
• JR is
i not executing
i this
hi solution
l i as expected…
d
public class OnetoN1 {
public op void syncSend(int x);
public op int syncReceive();
private int N;
//continues
PPHT09 – Example Exam
19
PPHT09 – Example Exam
20
Solution – Another Possibility
• Correct on paper but not in the real JR
private process server {
while (true)
inni void syncSend(int x)
st syncReceive.length() >= N {
for(int i=0; i<N; i++)
inni int syncReceive() {
return x;
}
}
}
PPHT09 – Example Exam
Part II
Course Summary
21
Course Summary
General Concepts
• What have we covered?
◦
◦
◦
◦
General concepts
Synchronisation Mechanisms
Synchronisation Problems
Concurrency Patterns
PPHT09 – Course Summary
23
PPHT09 – Course Summary
24
4
10/15/2009
Synchronisation Mechanisms
Busy Loops
Message Passing
Semaphores
• Used in low-level programming
◦ OS, libraries
◦ Embedded systems
Linda
Blocking
• Clever programming with shared variables
Monitors
◦ Peterson’s
P t
’ algorithm
l ith
• Hardware support
Transactional Memory
◦ Complex atomic instructions
• Non-blocking concurrency (lock/wait-free&TM)
Busy
Lock/Wait-Free:
– Complex instructions
Spin Locks:
– Peterson’s algorithm
– Complex instructions
PPHT09 – Course Summary
◦ Fine grained concurrency
◦ Even more complex atomic instructions
25
Semaphores
PPHT09 – Course Summary
26
Monitors
• P and V operations for atomically modifying a
non-negative integer variable
• P blocks when variable is 0
• Two common uses
• Mutual exclusion guaranteed
◦ Access only through operations
• Condition variables for conditional
synchronization
◦ wait for blocking
◦ Mutual exclusion: binary semaphore initialised
with one (1) permit = lock
◦ Conditional synchronisation: general counting
semaphore
• Releases monitor
• Different policies for who continues after signal
◦ signal for waking blocked processes
• Problem: care needed with nested calls
• Problem: unstructured
PPHT09 – Course Summary
27
Message Passing
PPHT09 – Course Summary
28
Transactional Memory
• Synchronous
• Lock-free programming made easy
◦ Channels
◦ Rendezvous
◦ Frees us from problems with locks
• Deadlock
• Convoying
• Compositionality
• Asynchronous
◦ Ch
Channels
l – JR
◦ Mail-box – Erlang
◦ Drawback: fairness
• Selective receive
• Uses the notion of transactions
◦ Input statement – JR
◦ Receive statement – Erlang
◦ A group of instructions executed atomically or not
at all
• Tuple Spaces
PPHT09 – Course Summary
29
PPHT09 – Course Summary
30
5
10/15/2009
Synchronisation Problems
Concurrency Patterns
• Locks
• Synchronisation primitive related patterns
• Problems
◦
◦
◦
◦
◦
◦
Shared update
Dining Philosophers
Producer-Consumer
Readers-Writers
Resource Allocation
…
◦ Passing the Baton
◦ Passing the Condition
•
•
•
•
•
•
• Solutions
◦ Standard/simple
◦ Fair variants
PPHT09 – Course Summary
Barrier
B
i SSynchronisation
h i ti
Client-Server
Pipelines
Replicated Workers
Timeouts, Alarms
…
31
What Does This Buy You?
PPHT09 – Course Summary
32
What Does This Buy You?
• Good coverage of synchronisation
mechanisms used in current (and near future)
• Coverage of classic concurrent problems and
solutions
◦ Programming languages
• Java, JR (Cω)
• Erlang
El
• Some experience in solving synchronisation
problems using a concurrent programming
language
◦ Libraries
• Pthreads
◦ Middleware
• First peak into the world of formal verification
• Javaspaces
◦ Operating Systems
• Singularity?
PPHT09 – Course Summary
33
What Next?
PPHT09 – Course Summary
34
What Next?
• Distributed systems
• Other related areas
◦ Failure; non-centralised techniques
◦ Operating systems
◦ Databases
◦ Programming Languages
• Real-Time Systems
◦ Hard deadlines, scheduling, real time kernels,
embedded systems
• Advanced concurrent programming?
• Software Engineering using Formal Methods
◦ Complain to your program
◦ Do a bachelor’s or master’s thesis with us
◦ Use model checking to verify correctness of
concurrent programs
PPHT09 – Course Summary
• We have plenty of interesting projects possible
• You can come with your own projects
35
PPHT09 – Course Summary
36
6
10/15/2009
Assignments
Exam
• Trainspotting
• Only English or English-X dictionary allowed
• Exam date and time: TDA381/DIT390
◦ 91% passed, 4% to mark
◦ Parallell programmering
◦ Concurrent Programming
◦ From Chalmers Student Portal:
• The Pub
◦ Only 24% have passed so far!
◦ Common problems
◦ Final dead-line is variable
• 2009-10-21 at 14:00 – 18:00
• Väg och Vatten
• Tuplespace
◦ Exam is anonymous
◦ 28% pass on first attempt
◦ but 76% still unmarked
• Remember to write course code
PPHT09 – Course Summary
37
PPHT09 – Course Summary
Exam Review
Exam Tips
• Monday, 2009-11-16 in 5453
• Read all the questions first
• Do your favorites first
• Plan your time
38
◦ Don’t spend too long on one question
• Don’t get hung up by syntax
• State your assumptions
◦ Especially if unsure about syntax
• Don’t use busy waiting with blocking
synchronisation primitives
◦ The simplest way to fail the exam
PPHT09 – Course Summary
39
More Exam Tips
PPHT09 – Course Summary
40
Course Completion
• Common errors
• To pass the course with full points
◦ Assumption that there will not be any questions
about X
◦ You must have passed the assignments, and
◦ You must have passed the exam
• Where X = monitors / whatever you didn’t learn in the
exercises assignments
exercises,
assignments, etc
etc.
• Assignments deadline is strict
◦ We will not mark any solution handed-in after the
deadline
◦ If you fail the assignments you can hand-in during
some other run of the course
◦ Modification of the question
• For example: If the question says to use asynchronous
message passing then don’t use semaphores!
◦ Busy waiting when using blocking primitives
• The simplest way to fail the exam
PPHT09 – Course Summary
41
PPHT09 – Course Summary
42
7
10/15/2009
Course Questionnaire
The End
• Anonymous Chalmers common questionnaire
• Concurrent programming is a very active area
of computer science
• Chalmers research competence
◦ With some course specific questions
◦ http://kursutv.portal.chalmers.se/
• Concurrent Programming TDA381/DIT390
◦
◦
◦
◦
◦ Also,
Also direct link from the course home page
◦ Reasonably short; takes little time
• Your feedback is very valuable!
Programming language technology
Concurrent and distributed systems
Formal methods
Hardware design
• For those interested more in the subject
◦ Various bachelor or master theses possible
◦ Many research opportunities (PhD?)
PPHT09 – Course Summary
43
PPHT09 – Course Summary
44
8
© Copyright 2026 Paperzz