Rendezvous: Difference between revisions

Line 791:
And caught the goose soon,
**** writer task "mg" terminated: out of ink ****
</pre>
 
=={{header|Julia}}==
<lang julia>using Distributed
 
mutable struct Printer
inputpath::Channel{String}
errorpath::Channel{String}
inkremaining::Int32
reserve::Printer
name::String
function Printer(ch1, ch2, ink, name)
this = new()
this.inputpath = ch1
this.errorpath = ch2
this.inkremaining = ink
this.name = name
this.reserve = this
this
end
end
 
function lineprintertask(printer)
while true
line = take!(printer.inputpath)
linesprinted = 0
if(printer.inkremaining < 1)
if(printer.reserve == printer)
put!(printer.errorpath, "Error: printer $(printer.name) out of ink")
else
put!(printer.reserve.inputpath, line)
end
else
println(line)
printer.inkremaining -= 1
end
end
end
 
function schedulework(poems)
printerclose(printer) = (close(printer.inputpath); close(printer.errorpath))
reserveprinter = Printer(Channel{String}(1), Channel{String}(10), 5, "Reserve")
mainprinter = Printer(Channel{String}(1), Channel{String}(10), 5, "Main")
mainprinter.reserve = reserveprinter
@async(lineprintertask(mainprinter))
@async(lineprintertask(reserveprinter))
printers = [mainprinter, reserveprinter]
activeprinter = 1
for poem in poems, line in poem
activeprinter = (activeprinter % length(printers)) + 1
put!(printers[activeprinter].inputpath, line)
for prn in printers
while isready(prn.errorpath)
println(take!(prn.errorpath))
end
end
end
for p in printers
printerclose(p)
end
end
 
const humptydumpty = ["Humpty Dumpty sat on a wall.",
"Humpty Dumpty had a great fall.",
"All the king's horses and all the king's men,",
"Couldn't put Humpty together again."]
 
const oldmothergoose = ["Old Mother Goose,",
"When she wanted to wander,",
"Would ride through the air,",
"On a very fine gander.",
"Jack's mother came in,",
"And caught the goose soon,",
"And mounting its back,",
"Flew up to the moon."]
 
schedulework([humptydumpty, oldmothergoose])
</lang>{{output}}<pre>
Humpty Dumpty had a great fall.
Couldn't put Humpty together again.
Humpty Dumpty sat on a wall.
All the king's horses and all the king's men,
Old Mother Goose,
Would ride through the air,
Jack's mother came in,
When she wanted to wander,
On a very fine gander.
And caught the goose soon,
Error: printer Reserve out of ink
Error: printer Reserve out of ink
</pre>
 
4,102

edits