alarm against infinit loop

Discussion in 'C++' started by ankitks@yahoo.com, Dec 1, 2006.

  1. Guest

    Hi guys,
    is there any utility available as a protection against endless_loop()

    something like this:

    alarm.set(5); //set timeout for 5 sec

    endless_loop();

    alarm.reset(); //reset it to 0, as alarm not needed any more

    Thanks,
    , Dec 1, 2006
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi guys,
    > is there any utility available as a protection against endless_loop()
    >
    > something like this:
    >
    > alarm.set(5); //set timeout for 5 sec
    >
    > endless_loop();
    >
    > alarm.reset(); //reset it to 0, as alarm not needed any more
    >
    > Thanks,


    Perhaps there exists such a utility, but as it
    would not be standard C++, it would not be topical
    here. However, a simple way to implement this would
    involve instrumenting the loop itself. Before
    the loop begins, capture a time with std::clock(),
    and at one or more points inside the loop, capture
    the time again. The difference between it and the
    one obtained outside the loop would give elapsed time.
    Compare it with your desired 'time-out' value. Break
    the loop if elapsed time >= your time-out value.

    See standard library documentation for details on
    how to use 'std::clock()'.

    -Mike
    Mike Wahler, Dec 2, 2006
    #2
    1. Advertising

  3. Binary Guest

    On Dec 2, 7:13 am, wrote:
    > Hi guys,
    > is there any utility available as a protection against endless_loop()
    >
    > something like this:
    >
    > alarm.set(5); //set timeout for 5 sec
    >
    > endless_loop();
    >
    > alarm.reset(); //reset it to 0, as alarm not needed any more
    >


    pesudo code:

    int flag = 0;

    main(){
    signal(SIGALRM, alarm_handler)
    while (1) {
    if (flag == 1)
    break;
    }
    }

    void alarm_handler()
    {
    flag = 1;
    }

    Actually, alarm signal will break the blocked system call.

    > Thanks,
    Binary, Dec 2, 2006
    #3
  4. Ian Collins Guest

    wrote:
    > Hi guys,
    > is there any utility available as a protection against endless_loop()
    >

    Testing?

    Anything else would be either intrusive, or platform specific
    (signals/watchdog timers).

    --
    Ian Collins.
    Ian Collins, Dec 2, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. service

    alarm workaround

    service, Nov 6, 2003, in forum: Perl
    Replies:
    0
    Views:
    585
    service
    Nov 6, 2003
  2. MrPugh

    generating infinit doubles

    MrPugh, Jul 9, 2003, in forum: C++
    Replies:
    3
    Views:
    290
    Ron Natalie
    Jul 9, 2003
  3. na1paj

    HELP, INFINIT LOOP... simple LINKED LIST

    na1paj, Oct 23, 2003, in forum: C Programming
    Replies:
    13
    Views:
    581
    Sheldon Simms
    Oct 28, 2003
  4. Replies:
    1
    Views:
    219
  5. Isaac Won
    Replies:
    9
    Views:
    352
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page