Hello,
I'm experimenting about using external commands, which act as filters (i.e. read from standard input and write to standard output):
(define p (open-process (list path: "/bin/head")))
(define t (make-thread (lambda () (let loop () (define line (read-line p)) (if (eof-object? line) line (begin (display "[") (display line) (display "]\n") (thread-yield!) (loop)))))))
(thread-start! t)
(display "a\nb\nc\n" p) (display "d\ne\nf\ng\nh\ni\nj\nk\nl\n" p) (force-output p) (thread-join! t)
Here's the corresponding output:
[a] [b] [c] [d] [e] [f] [g] [h] [i] [j]
No problem so far. This case is easy, because the external command /bin/head terminates automatically after having filtered the first 10 lines.
But there are filters, which will never terminate automatically, e.g. /bin/cat. In that case I want the main thread stopping filtering.
How can that be done? By adding (close-port p) somewhere? How should the example look like, if /bin/head is replaced by /bin/cat, and the sending thread decides to terminate after having sent 10 lines. The output should be the same as from the original example, of course.
Regards Thomas