Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -3,13 +3,13 @@
|
|||
INT count := 0, errno;
|
||||
BOOL input complete := FALSE;
|
||||
SEMA output throttle = LEVEL 0, input throttle = LEVEL 1;
|
||||
<EFBFBD>
|
||||
|
||||
FILE input txt;
|
||||
errno := open(input txt, "input.txt", stand in channel);
|
||||
<EFBFBD>
|
||||
|
||||
PROC call back done = (REF FILE f) BOOL: ( input complete := TRUE );
|
||||
on logical file end(input txt, call back done);
|
||||
<EFBFBD>
|
||||
|
||||
PAR (
|
||||
WHILE
|
||||
DOWN input throttle;
|
||||
|
|
|
|||
|
|
@ -1,32 +1,20 @@
|
|||
import tools.threads, std.stdio, std.stream, std.thread;
|
||||
import std.algorithm, std.concurrency, std.stdio;
|
||||
|
||||
void main() {
|
||||
// line or EOF
|
||||
struct InputLine {
|
||||
string data;
|
||||
bool eof;
|
||||
static InputLine opCall(string s) { InputLine res; res.data = s; return res; }
|
||||
static InputLine EOF() { InputLine res; res.eof = true; return res; }
|
||||
}
|
||||
auto LineCh = new MessageChannel!(InputLine),
|
||||
ResultCh = new MessageChannel!(int);
|
||||
auto printer = new Thread({
|
||||
int count;
|
||||
while (true) {
|
||||
auto line = LineCh.get();
|
||||
if (line.eof) break;
|
||||
count ++;
|
||||
writefln(count, ": ", line.data);
|
||||
}
|
||||
ResultCh.put(count);
|
||||
return 0;
|
||||
});
|
||||
printer.start;
|
||||
auto file = new File("input.txt");
|
||||
while (!file.eof()) {
|
||||
auto line = file.readLine();
|
||||
LineCh.put(InputLine(line));
|
||||
}
|
||||
LineCh.put(InputLine.EOF());
|
||||
writefln("Count: ", ResultCh.get());
|
||||
auto printer = spawn(&printTask, thisTid);
|
||||
auto f = File("input.txt","r");
|
||||
foreach (string line; lines(f))
|
||||
send(printer, line);
|
||||
send(printer, true); //EOF
|
||||
auto n = receiveOnly!(int)();
|
||||
stdout.writefln("\n%d lines printed.", n);
|
||||
}
|
||||
|
||||
void printTask(Tid reader) {
|
||||
int n = 0;
|
||||
for (bool eof = false; !eof;)
|
||||
receive(
|
||||
(string line) {stdout.write(line); n++;},
|
||||
(bool) {send(reader, n); eof = true;}
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,24 @@
|
|||
-module(cc).
|
||||
-export([start/0, reader/2]).
|
||||
|
||||
-export([start/0]).
|
||||
|
||||
start() ->
|
||||
Pid = spawn(cc,reader,[self(), 0]),
|
||||
case file:open("input.txt", read) of
|
||||
{error, Any} -> io:fwrite("Error ~p~n",[Any]);
|
||||
{ok, Io} ->
|
||||
process(Io, Pid),
|
||||
file:close(Io)
|
||||
end,
|
||||
ok.
|
||||
My_pid = erlang:self(),
|
||||
Pid = erlang:spawn( fun() -> reader(My_pid, 0) end ),
|
||||
{ok, IO } = file:open( "input.txt", [read] ),
|
||||
process( io:get_line(IO, ""), IO, Pid ),
|
||||
file:close( IO ).
|
||||
|
||||
process(Io, Pid) ->
|
||||
case io:get_line(Io,"") of
|
||||
eof ->
|
||||
Pid ! count,
|
||||
wait();
|
||||
Any ->
|
||||
Pid ! Any,
|
||||
process(Io, Pid)
|
||||
end.
|
||||
|
||||
wait() ->
|
||||
|
||||
process( eof, _IO, Pid ) ->
|
||||
Pid ! count,
|
||||
receive
|
||||
I -> io:fwrite("Count:~p~n", [I])
|
||||
end.
|
||||
end;
|
||||
process( Any, IO, Pid ) ->
|
||||
Pid ! Any,
|
||||
process( io:get_line(IO, ""), IO, Pid ).
|
||||
|
||||
reader(Pid, C) ->
|
||||
receive
|
||||
|
|
|
|||
|
|
@ -1,28 +1,19 @@
|
|||
procedure main()
|
||||
local prod, cons
|
||||
prod := create producer("input.txt")
|
||||
cons := create consumer(prod)
|
||||
@cons
|
||||
procedure main(A)
|
||||
fName := A[1]|"index.txt"
|
||||
p := thread produce(fName)
|
||||
c := thread consume(p)
|
||||
every wait(p | c)
|
||||
end
|
||||
|
||||
procedure producer(fname)
|
||||
local f
|
||||
f := open(fname) | stop("Unable to open ", fname)
|
||||
# send what we read [read(f)] to the consumer (&source)
|
||||
while read(f) @ &source
|
||||
# send it 'null' which we use as a signal to request count
|
||||
write("count = ", &null @ &source)
|
||||
procedure produce(fName)
|
||||
every !open(fName)@>> # drop messages in p's outbox (blocking whenever box is full)
|
||||
@>> # Signal consumer that p is done
|
||||
write("count is ",<<@) # block until message in p's inbox
|
||||
end
|
||||
|
||||
procedure consumer(p)
|
||||
local value, i
|
||||
i := 1
|
||||
value := @p
|
||||
while \value do {
|
||||
write("=> ",value)
|
||||
value := @ &source
|
||||
i := i + 1
|
||||
}
|
||||
# send producer our count
|
||||
i @ &source
|
||||
procedure consume(p)
|
||||
i := 0
|
||||
while write(\<<@p) do (i+:=1) # loop until empty message in p's outbox
|
||||
# (blocking until each message arrives)
|
||||
i@>>p # put row count into p's inbox
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
:- object(team).
|
||||
|
||||
:- threaded.
|
||||
|
||||
:- public(start/0).
|
||||
start :-
|
||||
threaded((
|
||||
reader,
|
||||
writer(0)
|
||||
)).
|
||||
|
||||
reader :-
|
||||
open('input.txt', read, Stream),
|
||||
repeat,
|
||||
read_term(Stream, Term, []),
|
||||
threaded_notify(term(Term)),
|
||||
Term == end_of_file,
|
||||
!,
|
||||
close(Stream),
|
||||
threaded_wait(lines(Lines)),
|
||||
write('Number of lines: '), write(Lines), nl.
|
||||
|
||||
writer(N0) :-
|
||||
threaded_wait(term(Term)),
|
||||
( Term == end_of_file ->
|
||||
threaded_notify(lines(N0))
|
||||
; N is N0 + 1,
|
||||
write(Term), nl,
|
||||
writer(N)
|
||||
).
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
| ?- ?- team::start.
|
||||
a(0)
|
||||
a(1)
|
||||
a(2)
|
||||
a(3)
|
||||
a(4)
|
||||
a(5)
|
||||
a(6)
|
||||
a(7)
|
||||
a(8)
|
||||
a(9)
|
||||
Number of lines: 10
|
||||
true.
|
||||
|
|
@ -5,12 +5,13 @@ def reader():
|
|||
print('Printed %d lines.' % count)
|
||||
|
||||
r = reader()
|
||||
|
||||
# printer
|
||||
count = 0
|
||||
while True:
|
||||
line = next(r)
|
||||
if not line:
|
||||
for count, line in enumerate(r):
|
||||
if line is None:
|
||||
break
|
||||
print(line)
|
||||
count += 1
|
||||
r.send(count)
|
||||
try:
|
||||
r.send(count)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue