Thread-Safe FileStreams

Lex Spoon lex at cc.gatech.edu
Mon Apr 6 19:09:07 UTC 1998


Mark Guzdial writes:
 > At 7:15 PM +0200 4/6/98, Andreas Raab wrote:
 > >You're doing two bad things at the same time ;-) Not only you access the
 > >same file from different processes, you even more *share* the variable
 > >where the file pointer is stored. If you wouldn't do this, you would not
 > >run out of file handles. If you would use aFile1, aFile2, and aFile3 in
 > >each process everything would be fine (except from the primitiveFailed
 > >of course).
 > 
 > You're right, of course.  When I fix the shared file variable as you
 > describe, I don't lose file handles anymore. <sigh>  It's pretty clear that
 > we're running out of them in the PWS, but it's been hard to reliably
 > replicate the error.
 > 
 > But the general question still stands: What's the right level to address
 > thread-safe file access?


$.02:

In Unix at least, it is okay to open the same file multiple times
within the same process.  Each open gives a completely independent
file handle.  Closing one handle doesn't affect other handles (test
program below, tried on Linux 2.0.33).


Lex





#include <fcntl.h>
#include <unistd.h>

#define M1 "hello from 1\n"
#define M2 "hello from 2\n"


int main() {
     int f1, f2;

     f1 = open("/dev/stdout", O_WRONLY);
     f2 = open("/dev/stdout", O_WRONLY);

     write(f1, M1, strlen(M1));
     close(f1);

     write(f2, M2, strlen(M2));
     close (f2);

     return 0;
}





More information about the Squeak-dev mailing list