Answer of exercise 12

Of course the parent must wait on its child. However, before that the input of the pipe on which the child reads must be closed by the parent, otherwise the child will wait indefinitely for new integers from the parent. This leads to a deadlock (closing the channel empties the buffer before closing the corresponding descriptor, therefore no data is lost). Concretely, the line 10 of the sieve function needs to be replaced by:

let output = out_channel_of_descr fd_out in generate len output; close_out output; ignore(waitpid [] p);;

Accordingly, we enclose the lines 1216 of the filter function (represented by ... below) with the following lines:

try ... with End_of_file -> close_out output; ignore (waitpid [] p)
* * *