[Openmcl-devel] How to properly close the streams created by run-program?
StefanMandl at web.de
Wed Apr 19 16:53:07 EDT 2006
Hi out there,
I'm trying to roll my own little "pipe" command.
This is the source code:
(defparameter newline-string (format nil "~%"))
(defun read-all (str)
(do ((line (read-line str nil 'eof)
(read-line str nil 'eof)))
((eql line 'eof))
(setf res (cons (concatenate 'string line newline-string) res)))
(setf res (nreverse res))
(apply #'concatenate (cons 'string res))))
(defun pipe (prg1 args1 prg2 args2 input)
(with-input-from-string (inp input)
(let* ((p1 (run-program prg1 args1 :output :stream :input inp))
(p2 (run-program prg2 args2 :output :stream :input
(close (external-process-output-stream p1))
(when (external-process-error-stream p1)
(close (external-process-error-stream p1)))
(when (external-process-input-stream p1)
(close (external-process-input-stream p1)))
(let ((res (read-all (external-process-output-stream p2))))
(close (external-process-output-stream p2))
(when (external-process-error-stream p2)
(close (external-process-error-stream p2)))
(when (external-process-input-stream p2)
(close (external-process-input-stream p2)))
when I call it in the Terminal like this (actually discarding the output):
(dotimes (i 1000) (pipe "/bin/ls" '() "/bin/cat" '() ""))
> Error in process listener(1): Too many open files
> While executing: "Unknown"
> Type :POP to abort.
Type :? for other options.
Do you have any idea, which streams I forgot to close?
I'm running openmcl 1.0 on OS X 10.4.6
Thanks for any help!
More information about the Openmcl-devel