gulden has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting a "Segmentation Fault (core dumped) ", caused by "opendir" function, when running the folowing code:
#!/opt/coolstack/bin/perl use strict; use threads ('yield', 'stack_size' => 64*4096, 'exit' => 'threads_only', 'stringify'); #--------------------------------------------------------------- my $path = "/tmp"; my $time = time; #my @files = qw(detail-1min-19-06-22); my @files = &get_files($path); my @thrs_loaders; foreach my $file (@files){ my ($thr) = threads->create(\&load, $file); push @thrs_loaders, $thr; } $_->join for @thrs_loaders; exit; #--------------------------------------------------------------------- sub get_files{ my $dir = shift; opendir(DIR, $dir) or die "Cant open dir $!"; # HERE IS THE CAUSE close(DIR); return "detail-1min-19-06-22"; } #--------------------------------------------------------------------- sub load{ my $file = shift; print "Processing $file\n"; }

I've tested in:

$ perl -Mthreads -e 'print $threads::VERSION' 1.71 $ perl -v This is perl, v5.8.8 built for sun4-solaris-thread-multi
Any tips?

Replies are listed 'Best First'.
Re: Threads + opendir = Segmentation Fault (core dumped)
by BrowserUk (Patriarch) on Jun 24, 2009 at 17:01 UTC

    Try this

    #! perl use strict; use threads ( 'yield', 'stack_size' => 64*4096, 'exit' => 'threads_only', 'stringify' ); my $path = "/tmp"; my @files = &get_files($path); my @thrs_loaders; foreach my $file (@files){ push @thrs_loaders, threads->create(\&load, $file); } $_->join for @thrs_loaders; <>; exit; sub get_files{ my $dir = shift; opendir(my $dh, $dir) or die "Cant open dir $!"; # HERE IS THE CAUS +E close( $dh); return "detail-1min-19-06-22"; } sub load{ my $file = shift; print "Processing $file\n"; }

    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.
      so why "$dh" works but "DH" didn't?

        Good question.

        Probably because there is a latent bug in the cloning of bare word directory handles--but that's speculation.

        With investigation it might be something that could be fixed for the next major release of Perl, but it really doesn't make any sense to not use lexical handles with threads.


        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.
      Nice tip, it solved my problem!!

      I didn't understand the cause of the problem, but now it works :)

Re: Threads + opendir = Segmentation Fault (core dumped)
by jwkrahn (Abbot) on Jun 24, 2009 at 17:32 UTC

    As well as what BrowserUk said, you should be using closedir to close a directory handle opened by opendir instead of close which works on filehandles instead.