[comp.lang.icon] open

day@grand.UUCP (01/06/88)

Consider the following /bin/sh script:

echo finished | (sleep 15 ; cat)

Compare that with the following icon program:

procedure
main (args)
    local output

    if not (output := open ("sleep 15 ; cat",  "wp")) then
	stop ("trmsg: can't open a pipe for writing")
    write (output, "finished")
    return
end

I believe they should be functionally equivalent.
They're not.  The shell script waits for the cat,
while the icon version exits immediately after
writing to the pipe, without waiting.

I think this is an obvious bug.
If you don't agree, I will be happy to explain more fully.

 --dave

nowlin@ihuxy.UUCP (01/08/88)

> Consider the following /bin/sh script:
> 
> echo finished | (sleep 15 ; cat)
> 
> Compare that with the following icon program:
> 
> procedure
> main (args)
>     local output
> 
>     if not (output := open ("sleep 15 ; cat",  "wp")) then
> 	stop ("trmsg: can't open a pipe for writing")
>     write (output, "finished")
>     return
> end
> 
> I believe they should be functionally equivalent.
> They're not.  The shell script waits for the cat,
> while the icon version exits immediately after
> writing to the pipe, without waiting.
> 
> I think this is an obvious bug.
> If you don't agree, I will be happy to explain more fully.
> 
>  --dave

I don't see why this is an Icon bug.  The real culprit is the program.  If
you'll add a close(output) to your program then the program will wait for
the process attached to the pipe to complete and you'll see the behavior
you expected.

	procedure
	main (args)
		local output

		if not (output := open ("sleep 15 ; cat","wp")) then
			stop ("trmsg: can't open a pipe for writing")
		write (output, "finished")

		close(output)  #  IMPORTANT LINE

		return
	end

If you open the pipe, write to it, and then terminate your Icon program
without waiting for the pipe to complete (sort of pull the rug out from
under it) the output to the pipe stays in the input buffer of the spawned
commands and when the sleep completes the cat will pick it up.  Learn to
program symmetrically so that files or pipes that are opened are also
closed and you will run into far fewer problems.

Jerry Nowlin
(...!ihnp4!ihuxy!nowlin)

day@grand.UUCP (Dave Yost) (01/12/88)

Thanks.  I'm relieved to know that if I explicitly do the close,
as I agree I should have, it works properly.  Still, the book
does say that open files are closed automatically upon program
termination, so my program should have acted as if I had done
the close.  I have already mentioned this to the Icon Group.

 --dave yost