CTM: Exercise 4-6

[ Posted by Urban Hafner Sun, 06 Nov 2005 16:39:50 GMT ]

The problem

Thread scheduling. Section 4.8.3.2 shows how to skip over already-calculated elements of a stream. If we use this technique to sum the elements of the integer stream in section 4.3.1, the result is much smaller than 11249925000, which is the sum of the integers in the stream. Why is it so much smaller? Explain this result in terms of thread scheduling.

My solution

What probably happens is that generating another element in the production thread is so fast that more than one ca be generated in one time slice. When the consumer thread restarts in the next slice it only takes the last element computed, so it loses some (or many) elements.

Tags , , , , , ,

CTM: Exercise 4-2

[ Posted by Urban Hafner Wed, 02 Nov 2005 16:12:33 GMT ]

The problem

Threads and garbage collection. This exercise examines how garbage collection behaves with threads and dataflow variables. Consider the following program:

proc {B _}
   {Wait _}
end

proc {A}
   Collectible={NewDictionary}
in
   {B Collectible}
end

After the call {A} is done, will Collectible become garbage? That is, will the memory occupied by Collectible be recovered? Give an answer by thinking about the semantics. Verify that the Mozart system behaves in this way.

My solution

At first I was a bit confused by what is meant with after the call is done and how _ behaves. But then I understood. Wait waits until its argument becomes determined. But when you use _ as an argument that can never happen because it creates a new unbound variable.

This I why I think Collectible can’t be garbage collected. It can’t be done because the call to A isn’t finished yet, because the call to B isn’t finished.

Tags , , , , , , ,

CTM: Exercise 4-1

[ Posted by Urban Hafner Wed, 02 Nov 2005 16:10:56 GMT ]

The problem

Thread semantics. Consider the following variation of the statement used in section 4.1.3 to illustrate thread semantics:

1
2
3
4
5
local B in
   thread B=true end
   thread B=false end
   if B then {Browse yes} end
end

For this exercise, do the following:

(a) Enumerate all possible executions of this statement.

(b) Some of these executions cause the program to terminate abnormally. Make a small change to the program to avoid these abnormal terminations.

My solution

(a) Line 4 has to wait until B becomes bound, so either line 2 or 3 has to execute first. The following combinations are possible:

  • 2,3,4
  • 2,4,3
  • 3,2,4
  • 3,4,2

(b) Actually all executions terminate abnormally because all of the eventually try to unify true and false. So what we have to check in the thread in line 2 and 3 is if B is already bound. This leads to the following program:

local B in
   thread if {IsDet B} then skip else B=true end end
   thread if {IsDet B} then skip else B=false end end
   if B then {Browse yes} end
end

Tags , , , , , ,