[Openmcl-devel] How to properly close the streams created by run-program?

Stefan Mandl StefanMandl at web.de
Wed Apr 19 13:53:07 PDT 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)
   (let (res)
     (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 
(external-process-output-stream p1))))

       (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)))

       res))))
======================

when I call it in the Terminal like this (actually discarding the output):

(dotimes (i 1000) (pipe "/bin/ls" '() "/bin/cat" '() ""))

I get:
 > 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!

Stefan Mandl



More information about the Openmcl-devel mailing list