Thread-Safe FileStreams

Lex Spoon lex at cc.gatech.edu
Mon Apr 6 20:35:39 UTC 1998


Mark Guzdial writes:
 > >I've tried it from Squeak - the result is the same. However, "/dev/stdout"
 > >is not considered to be a "regular" file. If you do the same with a usual
 > >file handle, only the second write should show the effect. That is at
 > >least what Squeak is doing if I try
 > 
 > I tried Andreas' example under MacOS, and it worked just fine.  So, why
 > does the below generate primitive fail errors? -- two of them, one on each
 > close.
 > 
 > | aFile aFile1 aFile2 |
 > "Put something in the file"
 > aFile _ FileStream fileNamed: 'jack.test'.
 > 1 to: 100 do: [:i | aFile nextPutAll: (i printString); cr.].
 > aFile close.
 > 
 > "Dual read test"
 > [		Transcript show: 'Proc1: Open-'.
 > 		aFile1 _ FileStream fileNamed: 'jack.test'.
 > 		(Delay forMilliseconds: (100 to: 500) atRandom) wait.
 > 		aFile1 contentsOfEntireFile.
 > 		(Delay forMilliseconds: (100 to: 500) atRandom) wait.
 > 		Transcript show: 'Proc1: Close-'.
 > 		aFile1 close.] fork.
 > [		Transcript show: 'Proc2: Open-'.
 > 		aFile2 _ FileStream fileNamed: 'jack.test'.
 > 		(Delay forMilliseconds: (100 to: 500) atRandom) wait.
 > 		aFile2 contentsOfEntireFile.
 > 		(Delay forMilliseconds: (100 to: 500) atRandom) wait.
 > 		Transcript show: 'Proc2: Close-'.
 > 		aFile2 close.] fork.
 > 
 > 


This triggers primitive failed errors on Linux, too.  Debugging some,
it seems the reason is that contentsOfEntireFile actually closes the
file stream itself, and so aFile1 and aFile2 end up being closed
twice!

However, this doesn't leak file handles; I checked before and after
running this, and the Squeak process had exactly 6 file descriptors
open in each case:


    $ ps | grep Squeak
     7718  p1 S     3:39 Squeakh server.image
    13128  17 R N   2:31 Squeak -xshm -lazy -memory 7m lex.image
    13139  p6 D     0:00 grep Squeak

    $ ls /proc/13128/fd
    0@  1@  2@  3@  4@  5@  6@        



The hunt is still on!

Lex





More information about the Squeak-dev mailing list