[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

RE: script timer



      Hi Binand,

      I have seen the man pages ;-) The sleep() command may be implemented
      using SIGALRM, which I admit, I was unaware of, but the recommendation
      is always to use SIGALRM, when you do this kind of jobs. This is
because
      when the sleep() is called, the kernel always gives the process which
called
      sleep() a slice of its time. I may be wrong with the newer
implementation of
      sleep() as u suggest, but I will sure check that out.

      When I said SIG_ALARM, I really (I mean it), I meant SIGALRM. This
term
      will be easily understood by the non-techies also. Try saying SIGCHLD
to a
      newbie - a far better option is to say SIG_CHILD.

      About the system() call, yes it spawns a new process, pipes command to
a 
      new shell, but you don't have to worry about forking a new process
each time.

      I am writing from my office, where I do not use C, but the code which
I meant
      should be like,

      #include <stdio.h>
      #include <stdlib.h>
      #include <unistd.h>
      #include <signal.h>

      void sig_handler(int sgno)
      {
              system("my_executable");
              signal(SIGALRM, sig_handler);
              alarm(5);
      }

      int main(int argc, char **argv)
      {
               int pid;
               if ((pid=fork())==-1) {
                         perror("fork");
                         exit(1);
               }

               if (!pid) {
                      sig_handler(SIGALRM);
                      while(1)
                           ; /* And they lived happily ever after */
               } else {
                       fprintf(stderr, "Started daemon with pid: %d\n",
pid);
                       exit(0);
               }
      }

      I admit, I haven't compiled this, but rest assured this will **compile
and run**.

      1. The process will set the next timer, only after the system call
succeeds. If
          this is not acceptable, happy forking().

      And lastly Binand, **no offence**.

      Suvendra

> -----Original Message-----
> From:	Binand Raj S. [SMTP:binand@xxxxxxxxxxxxxxxxxxxxx]
> Sent:	Monday, August 09, 1999 4:29 PM
> To:	linux-india@xxxxxxxxx
> Subject:	Re: script timer
> 
> Chakrabarti, Suvendra (CTS) forced the electrons to say:
> >      Hi Binand,
> > 
> >      1. sleep(5) is not a very good option. This command will keep the
> > system resources
> >          busy. A better alternative will be trapping the signal
> SIG_ALARM.
> 
> Hmm... Have you seen the sleep(3) man page? It says (not in so many words,
> I admit) that sleep is implemented in exactly the same way (using SIGALRM,
> assuming that is the signal you meant). I think Stevens' book on Unix
> system programming also has an implementation of sleep this way.
> 
> I admit that sleep() has a bunch of problems associated, mainly setjmp.h
> functions, and SIGALRM.  Maybe a better way to do this will be using
> the select(2) system call to avoid all the problems of sleep.
> 
> >      2. Trying to fork() every 5 seconds and spawning a new process !!!
> That
> > is
> >          not decent in any sense. A simple system("cmd_line") should
> have
> >          sufficed in this case, assuming the program does not take more
> than
> >          5 seconds to execute.
> 
> system (command) also does the same thing! And it has got an additional
> bunch
> of security related problems associated with it. execl() or execv() are
> the
> recommended ones by unix gurus.
> 
> Binand
> 
> -- 
> #include <stdio.h>                                   | Binand Raj S.
> char *p = "#include <stdio.h>%cchar *p = %c%s%c;     | This is a self-
> int main(){printf(p,10,34,p,34,10);return 0;}%c";    | printing program.
> int main(){printf(p,10,34,p,34,10);return 0;}        | Try it!!
> --------------------------------------------------------------------
> For more information on Linux in India visit http://www.linux-india.org/
> The Linux India mailing list does not accept postings in HTML format.
- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
The Linux India mailing list does not accept postings in HTML format.

------------------------------