Loading ...
Sorry, an error occurred while loading the content.

child process (apparently) not returning error

Expand Messages
  • Santosh Sugur
    Hi Guys, I have recently started working with multiple (child ) processes. I have the following code which is not returning any error: Please see below where
    Message 1 of 2 , Dec 4, 2007
    • 0 Attachment
      Hi Guys,

      I have recently started working with multiple (child ) processes. I
      have the following code which is not returning any error: Please see
      below where in the subroutine spawn_proc() I wait on STDIN and delete
      a file that is about to be opened and expect an error. However i don't
      get any error and it just continues with the other files. Please let
      me know what am i doing wrongly? The intent is to be sure that all
      files are opened and truncated successfully and print a message even
      if a single file doesn't otherwiose print success.

      #!/usr/bin/perl

      use strict;
      use warnings;
      use Fcntl;
      use POSIX ":sys_wait_h";


      print "Opening (opendir) dir $data_loc \n";
      my $rc = opendir DIRHANDLE, "$data_loc";
      if (!$rc)
      {
      print "$! \n";
      exit;
      }

      my @allfiles = readdir DIRHANDLE;

      spawn_proc(@allfiles);

      closedir DIRHANDLE;
      my $kid;
      do {
      $kid = waitpid(-1,&WNOHANG);
      } until $kid == -1;

      print "All went fine \n";



      sub spawn_proc
      {
      my @allfiles = @_;
      foreach my $file (@allfiles)
      {
      my $pid;
      if($pid = fork)
      {
      print "current process pid: $pid\n";
      }
      elsif(defined $pid)
      {
      # Child process is here
      if (-f "$data_loc/$file")
      {
      $| = 1;
      print "Opening $data_loc/$file\n";

      <STDIN>; #### So that can delete one of the files it is
      about to open and expect an error.

      sysopen(FILE, "$data_loc/$file", O_RDWR) or die "Can't
      open $data_loc/$file: $!\nFAIL \n", exit (1);
      print "truncating $data_loc/$file\n";
      my $truncate_res = truncate "$data_loc/$file", 1024;
      if (!$truncate_res)
      {
      die "Couldn't truncate file $file,: $! \nFAIL \n", exit (1);
      }

      close FILE;

      }
      exit (0);
      }


      }


      }



      Thanks in advance,
      Santosh
    • Santosh Sugur
      I could get this working by removing the or in sysopen(FILE, $data_loc/$file , O_RDWR) or die Can t open $data_loc/$file: $! nFAIL n , exit (1); and
      Message 2 of 2 , Dec 5, 2007
      • 0 Attachment
        I could get this working by removing the 'or' in

        sysopen(FILE, "$data_loc/$file", O_RDWR) or die "Can't
        open $data_loc/$file: $!\nFAIL \n", exit (1);
        and replacing it explicitly with an if (! )

        if (!sysopen(FILE, "$data_loc/$file", O_RDWR))
        {
        print "Can't open $data_loc/$file: $!\n";
        $handle->shlock( );
        $fail = 1;
        $handle->shunlock( );
        exit (1);
        }

        Also to track failures in multiple processes I used the IPC::Shareable
        module from CPAN as explained in the Perl Cookbook (16.12.Sharing Variables
        in Different Processes).

        Regards,
        Santosh

        On Dec 4, 2007 10:10 PM, Santosh Sugur <santosh.sugur@...> wrote:

        > Hi Guys,
        >
        > I have recently started working with multiple (child ) processes. I
        > have the following code which is not returning any error: Please see
        > below where in the subroutine spawn_proc() I wait on STDIN and delete
        > a file that is about to be opened and expect an error. However i don't
        > get any error and it just continues with the other files. Please let
        > me know what am i doing wrongly? The intent is to be sure that all
        > files are opened and truncated successfully and print a message even
        > if a single file doesn't otherwiose print success.
        >
        > #!/usr/bin/perl
        >
        > use strict;
        > use warnings;
        > use Fcntl;
        > use POSIX ":sys_wait_h";
        >
        >
        > print "Opening (opendir) dir $data_loc \n";
        > my $rc = opendir DIRHANDLE, "$data_loc";
        > if (!$rc)
        > {
        > print "$! \n";
        > exit;
        > }
        >
        > my @allfiles = readdir DIRHANDLE;
        >
        > spawn_proc(@allfiles);
        >
        > closedir DIRHANDLE;
        > my $kid;
        > do {
        > $kid = waitpid(-1,&WNOHANG);
        > } until $kid == -1;
        >
        > print "All went fine \n";
        >
        >
        >
        > sub spawn_proc
        > {
        > my @allfiles = @_;
        > foreach my $file (@allfiles)
        > {
        > my $pid;
        > if($pid = fork)
        > {
        > print "current process pid: $pid\n";
        > }
        > elsif(defined $pid)
        > {
        > # Child process is here
        > if (-f "$data_loc/$file")
        > {
        > $| = 1;
        > print "Opening $data_loc/$file\n";
        >
        > <STDIN>; #### So that can delete one of the files it is
        > about to open and expect an error.
        >
        > sysopen(FILE, "$data_loc/$file", O_RDWR) or die "Can't
        > open $data_loc/$file: $!\nFAIL \n", exit (1);
        > print "truncating $data_loc/$file\n";
        > my $truncate_res = truncate "$data_loc/$file", 1024;
        > if (!$truncate_res)
        > {
        > die "Couldn't truncate file $file,: $! \nFAIL \n", exit (1);
        > }
        >
        > close FILE;
        >
        > }
        > exit (0);
        > }
        >
        >
        > }
        >
        >
        > }
        >
        >
        >
        > Thanks in advance,
        > Santosh
        >


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.