Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^6: Threads: How to share a FileHandle to Write

by gulden (Monk)
on May 19, 2009 at 17:06 UTC ( #764973=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Threads: How to share a FileHandle to Write
in thread Threads: How to share a FileHandle to Write

I've tested in this PCs:

PC 1

-bash-3.00$ uname -a SunOS reia 5.10 Generic_137137-09 sun4v sparc SUNW,Sun-Blade-T6320 -bash-3.00$ perl -Mthreads -e 'print $threads::VERSION . "\n"'; perl +-Mthreads::shared -e 'print $threads::shared::VERSION . "\n"' ; perl +-v 1.71 1.27 This is perl, v5.8.8 built for sun4-solaris-thread-multi
PC 2
-bash-3.00$ uname -a SunOS deimos 5.10 Generic_138889-03 i86pc i386 i86pc -bash-3.00$ perl -Mthreads -e 'print $threads::VERSION . "\n"'; perl - +Mthreads::shared -e 'print $threads::shared::VERSION . "\n"' ; perl - +v 1.72 1.28 This is perl, v5.8.8 built for i86pc-solaris-thread-multi
PC 3
$ uname -a Linux communicator 2.6.27-11-generic #1 SMP Wed Apr 1 20:57:48 UTC 200 +9 i686 GNU/Linux $ perl -Mthreads -e 'print $threads::VERSION . "\n"'; perl -Mthreads:: +shared -e 'print $threads::shared::VERSION . "\n"' ; perl -v 1.67 1.14 This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
What do you mean by CRT?


Comment on Re^6: Threads: How to share a FileHandle to Write
Select or Download Code
Re^7: Threads: How to share a FileHandle to Write
by BrowserUk (Pope) on May 19, 2009 at 18:16 UTC
    What do you mean by CRT?

    CRT == C RunTime libraries.

    Presumably all the versions of perl you tested were built using GCC and its runtime libraries? If those libraries have a bug they could be affecting all the versions you've tried.

    Another, and possibly more likely candidate is that the PERLIO implementation for unix/POSIX platforms has a bug that doesn't manifest itself in the MSWIN branches of the source tree.

    Two things that you could try--if you have the inclination to try and track this down:

    1. Re-build your local perl setting USE_PERLIO = undef or whatever the equivalent configuration parameter is for your system(s).
    2. See if you can get someone who runs perl under cygwin (and/or Strawberry Perl) to see if they can reproduce the problems you are having.

    Beyond that, report the problem to the threads maintainer and see what if any reaction you get.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      2. See if you can get someone who runs perl under cygwin (and/or Strawberry Perl) to see if they can reproduce the problems you are having.

      Does not work too in cygwin

      $ perl -v This is perl, v5.10.0 built for cygwin-thread-multi-64int $ perl -Mthreads -e 'print $threads::VERSION' 1.69 $ perl -Mthreads::shared -e 'print $threads::shared::VERSION' 1.21

        Again, it looks like a genuine bug in POSIX builds of Perl or the underlying libraries.

        As I don't run any of the systems where the failure occurs, I cannot help you further. There is really nothing you can do about it other than report it to the package mainatainer and/or p5p (via perlbug).

        You might be able to work around the bug by:

        1. Passing the fileno of the open file handle to each thread explicitly;
        2. Use POSIXdup() to clone the file descriptor manually;
        3. Use POSIXwrite() to write to it.

        In theory, you can use 'dupping' syntax of open (open my $fh, '>&', $fno) to achieve this, but chances are that is essentially what is being done when an open filehandle is automatically cloned, and so it would likely suffer the same problems if you do it yourself.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        I'm replying here cos it's too much to do via /msg

        gulden says We can clone filehandles, but can u ensure tha they share buffers, locks and son on?

        On (native) Win32 & Win64 I'll give you a qualified yes. On the evidence of your posts, maybe not on other (POSIX) platforms.

        To the best ability to track this through, the difference lies somewhere below this function in sv.c:

        PerlIO * PerlIO_fdupopen(pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) { #if defined(PERL_MICRO) || defined(__SYMBIAN32__) return NULL; #else #ifdef PERL_IMPLICIT_SYS return PerlSIO_fdupopen(f); #else #ifdef WIN32 return win32_fdupopen(f); #else if (f) { const int fd = PerlLIO_dup(PerlIO_fileno(f)); if (fd >= 0) { char mode[8]; #ifdef DJGPP const int omode = djgpp_get_stream_mode(f); #else const int omode = fcntl(fd, F_GETFL); #endif PerlIO_intmode2str(omode,mode,NULL); /* the r+ is a hack */ return PerlIO_fdopen(fd, mode); } return NULL; } else { SETERRNO(EBADF, SS_IVCHAN); } #endif return NULL; #endif #endif }

        With the Win* versions complying with this description from the POSIX standard:

        The object referenced by the descriptor does not distinguish betw +een fildes and fildes2 in any way. Thus if fildes2 and fildes are du +plicate references to an open file, read(2), write(2) and lseek(2) calls +all move a single pointer into the file, and append mode, non-blocking I/O + and asynchronous I/O options are shared between the references. If a + sepa- rate pointer into the file is desired, a different object referen +ce to the file must be obtained by issuing an additional open(2) call. + The close-on-exec flag on the new file descriptor is unset.

        other (ironically POSIX) platforms not!

        I'm guessing (on the basis of little more than gut feel and my abortive attempts to trace this through perlio.c, that the problem lies with the perlio implementation of the dup2 semantics rather than the underlying OSs (which would have noticed this much earlier).

        If you compile your own Perl on one of those platforms, then you could try using stdio instead of perlio and confirm or not my suspicions. Alternatively, put together a simple testcase that demonstrates the problem (or use mine from above that works for me and fails for you), and raise a PerlBug.

        As for locking: you don;t need to be able to share the filehandle in order to use locking. You declare a simple shared scalar and perform your locking on that.

        Sorry that I cannot explain this better, nor help you with non-Win* platforms.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://764973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-08-30 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls