[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

setting a timeout

Posted on 1998-09-09
6
Medium Priority
?
234 Views
Last Modified: 2010-03-05
Here is a copy of a admin at my hosting copany "I am thinking because it involves uploading the program is just not
executing but yet hanging around and keeping httpd childs open until we kill
them off.  Could you set a timeout or somethiong on this script?"  How would I do this?  Here is the script.  Thanks for any suggestions/help.
#!/usr/local/bin/perl
    use CGI;
      require '/home/dank/public_html/cgi-local/chat/locksubs.pl';

    $cgi = new CGI;

print "Content-type: text/html\n\n";


    if ($cgi->param('file') eq '') {
      &no_pic;
}
      $username = $cgi->param('USERNAME');
      $pics_dir = '/home/dank/public_html/cgi-local/pics';
    $filename = "/home/dank/public_html/cgi-local/pics/$username.gif";
    open(OUT,"> $filename");
    while($bytesread = read($cgi->param('file'), $buffer, 1024)) {
        print OUT $buffer;
    }
    close(OUT);
      &lock("waiting-count.db","$pics_dir",'3');
      open(COUNT,"<$pics_dir/waiting-count.db");
      $count = <COUNT>;
      close(COUNT);
      $count++;
      open(COUNT,">$pics_dir/waiting-count.db");
      print COUNT "$count";
      close(COUNT);
      &unlock("waiting-count.db","$pics_dir");      
      &lock("waiting.db","$pics_dir",'3');
      open(WAIT,">>$pics_dir/waiting.db");
      print WAIT "$username--$count\n";
      close(WAIT);
      &unlock("waiting.db","$pics_dir");

print "done. username = $username\n";

sub no_pic {
      print "sorry please give a pic to process.\n";
      exit;
}
0
Comment
Question by:idadan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204782
What is &lock
0
 

Author Comment

by:idadan
ID: 1204783
A file locking routine, from the cgi-perl cookbook (matt wright)...
0
 

Accepted Solution

by:
arhnold earned 300 total points
ID: 1204784
Hello,
      One method I use when I want to have a timeout on some part of a script is to use SIGALRM, with the signal handler set to a subroutine to handle the case.  Here is an example hacked from a program of mine...

..yada yada yada...
$timeout = 60;
$SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
alarm($timeout)  # Starts the alarm timer ticking down
   # Main reading loop.
   while ($go_on) {
      &Go_Do_Something(); # This has $timeout seconds to return
                          # else the ALRM will pop  
      # We returned in time, restart the timer.              
      alarm($timeout);
      ... yada yada yada...


## In this case here is what the signal handler would do...
sub Wait_For_Timeout {
   #####################################
   # Signal Handler for Wait_For (ALRM).
   #####################################
   &dprint("\n\t** Script Timeout.  Moving on...\n");
   $go_on=0;  # Breaks the while() loop above
}

      Hope this helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:idadan
ID: 1204785
Ok... in this line
    $SIG{'ALRM'} = 'Wait_For_Timeout'; # A subroutine
is that legal? It looks funny to me.. and also shouldn't you define $go_on as something before calling it or won't it just read that as 0?  I need this code to work as is, if there is anything wrong, could you just look it over and check it?  thanks.
0
 

Expert Comment

by:arhnold
ID: 1204786
Yes this is legal.  If you have the Programming Perl v2 book look at p340-1 for an example.  The only difference is that they use
$SIG{'x'} = \&foo, but its the same thing in the end.  

      The $go_on variable was defined a couple hundred lines previous in the script.  It was declared as a 'my' and was visible to all code in this file.

      You could keep your code pretty much the same.  Write a new subroutine to cleanup the locks on a timeout and print an error message.  Then wrap the area of the code you wish to guard with the timer stuff. e.g

$SIG{'ALRM'} = \&My_Timeout;

alarm(60);
&lock("waiting.db","$pics_dir",'3');
   open(WAIT,">>$pics_dir/waiting.db");
   print WAIT "$username--$count\n";
   close(WAIT);
   &unlock("waiting.db","$pics_dir");
## We completed this part in under 60 seconds, cancel the timer
alarm(0);

$SIG{'ALRM'} = 'DEFAULT'; # Return to normal response
0
 

Author Comment

by:idadan
ID: 1204787
Thanks for the help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question