infinite loops -- waiting for some event

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default infinite loops -- waiting for some event

    Hello,
    what is the best way of solving cases where one has to wait
    (forever, until killed or instructed otherwise) for some event to
    occur? E.g., wait for data, monitoring a file? In fact, that task is
    not that difficult, but takes over the CPU if the infinite loop
    lacks sleep(). I've always thought using this method was somehow
    inferior but then found out that even utilities like tail(1) (on
    Linux) use it. Or on another occasion, if one uses curses (UNIX
    again), which will most likely include an infinite loop, with the
    nodelay(...) option turned on. Obviously, there must be a solution to
    such cases: with nodelay() turned off, the infinite loop doesn't need
    sleep(). Is a daemon, thus a system-dependant solution, the best way
    to go or is it unconnected with such a task?

    What is the solution behind these methods?

    Thanks,

    andrej
    --
    echo ${girl_name} > /etc/dumpdates
    Andrej Hocevar Guest

  2. Similar Questions and Discussions

    1. Getting Error: Event Type 'flash.event:event' is unavailable ?????
      Hi, I am not using Cairngorm or anything, but trying to get an app built first without it then look into it. I am getting this error however...
    2. Mysql infinite loop?!
      this query doesnt work: SELECT d.cID,d.cLName,d.cFName , k.kid FROM cname d, cu_key e, rep_key f , `key` k WHERE (d.cLName LIKE '%') and (((e.cID...
    3. Infinite recursion detected... How do I prevent that?
      I have a rule similar to this: CREATE RULE rule_branch_delete AS ON DELETE TO tree DO DELETE FROM tree WHERE ancestor_id IS NOT NULL AND...
    4. Weird Infinite loop
      Hello, I have this piece of code, and since i put it on a new webserver, it has caused an infinite loop. As you can see, n begins at 1, and B_x...
    5. infinite menu help
      Hello folks. I'm having a few difficulties with a horizontally scrolling infinite menu on a flash site I am building. My problem is that only...
  3. #2

    Default Re: infinite loops -- waiting for some event

    In article <slrnbh8irl.r2.drejcica@sonet.utopija.linux>,
    Andrej Hocevar <drejcica@volja.net> wrote:
    >Hello,
    >what is the best way of solving cases where one has to wait
    >(forever, until killed or instructed otherwise) for some event to
    >occur? E.g., wait for data, monitoring a file? In fact, that task is
    >not that difficult, but takes over the CPU if the infinite loop
    >lacks sleep(). I've always thought using this method was somehow
    >inferior but then found out that even utilities like tail(1) (on
    >Linux) use it. Or on another occasion, if one uses curses (UNIX
    >again), which will most likely include an infinite loop, with the
    >nodelay(...) option turned on. Obviously, there must be a solution to
    >such cases: with nodelay() turned off, the infinite loop doesn't need
    >sleep(). Is a daemon, thus a system-dependant solution, the best way
    >to go or is it unconnected with such a task?
    If the event you're waiting for is data from a device, you normally block
    in read() or select() to wait for data to be available.

    If you're monitoring an ordinary file, there's usually no alternative to an
    infinite loop, and you should have a sleep() in it to keep from
    monopolizing the system.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  4. #3

    Default Re: infinite loops -- waiting for some event




    Andrej Hocevar wrote:
    > Hello,
    > what is the best way of solving cases where one has to wait
    > (forever, until killed or instructed otherwise) for some event to
    > occur? E.g., wait for data, monitoring a file? In fact, that task is
    > not that difficult, but takes over the CPU if the infinite loop
    > lacks sleep(). I've always thought using this method was somehow
    > inferior but then found out that even utilities like tail(1) (on
    > Linux) use it. Or on another occasion, if one uses curses (UNIX
    > again), which will most likely include an infinite loop, with the
    > nodelay(...) option turned on. Obviously, there must be a solution to
    > such cases: with nodelay() turned off, the infinite loop doesn't need
    > sleep(). Is a daemon, thus a system-dependant solution, the best way
    > to go or is it unconnected with such a task?
    >
    select() and or poll() are the most common ways to do what you describe
    on unix.

    select and poll suffer scalability issues when you start dealing with
    around several thousand or more file descritors and there are newer
    techniques like /dev/kqueue or signals (SIGIO) and most recently and
    probably the best technique yet is epoll on linux.

    The problem you describe with curses I have solved with select and poll
    but you need to be careful wrt buffering, i.e. data may not be available
    from the kernel but may be sitting in an internal buffer and waiting on
    I/O at that point would be bad because you'd rather finish processing
    the remaining data in the buffer.


    Gianni Mariani Guest

  5. #4

    Default Re: infinite loops -- waiting for some event

    >>> Andrej Hocevar wrote:

    AH> what is the best way of solving cases where one has to wait
    AH> (forever, until killed or instructed otherwise) for some event to
    AH> occur? E.g., wait for data, monitoring a file? In fact, that task is
    AH> not that difficult, but takes over the CPU if the infinite loop
    AH> lacks sleep(). I've always thought using this method was somehow
    AH> inferior but then found out that even utilities like tail(1) (on
    AH> Linux) use it. Or on another occasion, if one uses curses (UNIX
    AH> again), which will most likely include an infinite loop, with the
    AH> nodelay(...) option turned on. Obviously, there must be a solution to
    AH> such cases: with nodelay() turned off, the infinite loop doesn't need
    AH> sleep(). Is a daemon, thus a system-dependant solution, the best way
    AH> to go or is it unconnected with such a task?

    You can choose a event library from existing ones or write your own
    implementation. They allow waiting for multiple events, organizing timer
    lists, etc.


    -netch-
    Valentin Nechayev Guest

  6. #5

    Default Re: infinite loops -- waiting for some event

    Andrej Hocevar wrote:
    > Hello,
    > what is the best way of solving cases where one has to wait
    > (forever, until killed or instructed otherwise) for some event to
    > occur? E.g., wait for data, monitoring a file? In fact, that task is
    > not that difficult, but takes over the CPU if the infinite loop
    > lacks sleep(). I've always thought using this method was somehow
    > inferior but then found out that even utilities like tail(1) (on
    > Linux) use it. Or on another occasion, if one uses curses (UNIX
    > again), which will most likely include an infinite loop, with the
    > nodelay(...) option turned on. Obviously, there must be a solution to
    > such cases: with nodelay() turned off, the infinite loop doesn't need
    > sleep(). Is a daemon, thus a system-dependant solution, the best way
    > to go or is it unconnected with such a task?
    While unrelated I have to point out that the only real solutions to infinite
    loops must be done in kernel-land. Any feature that "blocks" is either
    driven itself by an infinite loop or is a kernel feature. Of course, these
    things are trivial in kernel space. For instance, most filesystem layers
    implement some kind of queue for files being monitored and since they are
    the ultimate authority on a files state they only need to notify these
    monitors when the file changes.

    --
    Nate Hill <vugdeox@freeshell.org>
    Nate Hill Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139