software interrupt - Linux

This is a discussion on software interrupt - Linux ; Hi, I would like to write a timer application where i can call a function every X milliseconds. How far I am: I wrote an application that checks the time since the last call and returns 1 when it's "ready" ...

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 42

Thread: software interrupt

  1. software interrupt

    Hi,

    I would like to write a timer application where i can call a function every
    X milliseconds. How far I am: I wrote an application that checks the time
    since the last call and returns 1 when it's "ready" again so i can issue a
    command like:

    TimerStart(5); //every 5 seconds
    while (1){
    if (CheckTimer(5))
    cout << "Great, 5 milliseconds have passed" << endl;
    }

    This works great but I would like to write something without the while(1)
    loop where i need to check more often than every 5 milliseconds. I would
    like something that would jump into a specific function every 5 ms. How can
    i do something like this? This must work, I remember Delphi 3.0 on Windows
    had such functionality a long time ago. I'm just not sure how i can write
    it myself and of course for Linux, not Windows.

    Thanks!
    Ron
    --
    chEErs roN

  2. Re: software interrupt

    On Mar 29, 6:21 pm, Ron Eggler wrote:
    > Hi,
    >
    > I would like to write a timer application where i can call a function every
    > X milliseconds. How far I am: I wrote an application that checks the time
    > since the last call and returns 1 when it's "ready" again so i can issue a
    > command like:
    >
    > TimerStart(5); //every 5 seconds
    > while (1){
    > if (CheckTimer(5))
    > cout << "Great, 5 milliseconds have passed" << endl;
    >
    > }
    >
    > This works great but I would like to write something without the while(1)
    > loop where i need to check more often than every 5 milliseconds. I would
    > like something that would jump into a specific function every 5 ms. How can
    > i do something like this? This must work, I remember Delphi 3.0 on Windows
    > had such functionality a long time ago. I'm just not sure how i can write
    > it myself and of course for Linux, not Windows.
    >
    > Thanks!
    > Ron
    > --
    > chEErs roN


    1) The process must run as 'root' and must change scheduler classes.
    (man 'sched_setscheduler'.

    2) Use 'nanosleep' to wait.

    Be very careful. Errors in a process with a special scheduler class
    can hang the system. You must complete what you are doing *very*
    quickly and add appropriate safeties to handle the case where you
    can't finish the job before it becomes time to do the next job.
    Otherwise, the system will be able to do nothing else.

    DS

  3. Re: software interrupt

    On Mar 29, 6:21 pm, Ron Eggler wrote:
    > Hi,
    >
    > I would like to write a timer application where i can call a function every
    > X milliseconds. How far I am: I wrote an application that checks the time
    > since the last call and returns 1 when it's "ready" again so i can issue a
    > command like:
    >
    > TimerStart(5); //every 5 seconds
    > while (1){
    > if (CheckTimer(5))
    > cout << "Great, 5 milliseconds have passed" << endl;
    >
    > }
    >
    > This works great but I would like to write something without the while(1)
    > loop where i need to check more often than every 5 milliseconds. I would
    > like something that would jump into a specific function every 5 ms. How can
    > i do something like this? This must work, I remember Delphi 3.0 on Windows
    > had such functionality a long time ago. I'm just not sure how i can write
    > it myself and of course for Linux, not Windows.
    >
    > Thanks!
    > Ron
    > --
    > chEErs roN


    1) The process must run as 'root' and must change scheduler classes.
    (man 'sched_setscheduler'.

    2) Use 'nanosleep' to wait.

    Be very careful. Errors in a process with a special scheduler class
    can hang the system. You must complete what you are doing *very*
    quickly and add appropriate safeties to handle the case where you
    can't finish the job before it becomes time to do the next job.
    Otherwise, the system will be able to do nothing else.

    DS

  4. Re: software interrupt

    David Schwartz wrote:

    > 1) The process must run as 'root' and must change scheduler classes.
    > (man 'sched_setscheduler'.


    I can't really see how i should use sched_setscheduler() to accomplish what
    i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    be able to change scheduling and prority of a given PID.

    > 2) Use 'nanosleep' to wait.


    To wait for what?

    > Be very careful. Errors in a process with a special scheduler class
    > can hang the system. You must complete what you are doing *very*
    > quickly and add appropriate safeties to handle the case where you
    > can't finish the job before it becomes time to do the next job.
    > Otherwise, the system will be able to do nothing else.


    My task would be to look in a directory for a file and if there is one, open
    a socket on a specific port and send the file out. Depending on the file
    size and on the connection, this could take a some time. I would wanna
    issue the timertask every 1 Minute only tho....
    Any ideas/slutions are greatly appreciated!

    --
    chEErs roN

  5. Re: software interrupt

    David Schwartz wrote:

    > 1) The process must run as 'root' and must change scheduler classes.
    > (man 'sched_setscheduler'.


    I can't really see how i should use sched_setscheduler() to accomplish what
    i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    be able to change scheduling and prority of a given PID.

    > 2) Use 'nanosleep' to wait.


    To wait for what?

    > Be very careful. Errors in a process with a special scheduler class
    > can hang the system. You must complete what you are doing *very*
    > quickly and add appropriate safeties to handle the case where you
    > can't finish the job before it becomes time to do the next job.
    > Otherwise, the system will be able to do nothing else.


    My task would be to look in a directory for a file and if there is one, open
    a socket on a specific port and send the file out. Depending on the file
    size and on the connection, this could take a some time. I would wanna
    issue the timertask every 1 Minute only tho....
    Any ideas/slutions are greatly appreciated!

    --
    chEErs roN

  6. Re: software interrupt

    Ron Eggler wrote:
    > I would like to write a timer application where i can call a function
    > every X milliseconds.


    No problem, only that this is limited by the scheduling granularity, which
    ranges from 1ms to 20ms (I think) for Linux. If what you want is feasible
    also depends on whether a delay between the deadline and the according code
    being called is fatal, i.e. what your realtime requirements are.

    > How far I am: I wrote an application that checks the
    > time since the last call and returns 1 when it's "ready" again so i can
    > issue a command like:
    >
    > TimerStart(5); //every 5 seconds
    > while (1){
    > if (CheckTimer(5))
    > cout << "Great, 5 milliseconds have passed" << endl;
    > }
    >
    > This works great but I would like to write something without the while(1)
    > loop [...]


    Use a thread, e.g. boost::thread for a C++ program.

    > [...]i need to check more often than every 5 milliseconds.


    In that case you have a hard realtime requirement, so you should get
    acquainted to realtime programming. David already gave you a few pointers
    how to tweak the scheduler.

    One note though: both realtime programming and multithreaded programming
    require a lot of understanding and good planning and thorough understanding
    upfront. I guess you'll be up for a tough ride.


    Uli


  7. Re: software interrupt

    Ron Eggler writes:
    > I would like to write a timer application where i can call a function every
    > X milliseconds. How far I am: I wrote an application that checks the time
    > since the last call and returns 1 when it's "ready" again so i can issue a
    > command like:
    >
    > TimerStart(5); //every 5 seconds
    > while (1){
    > if (CheckTimer(5))
    > cout << "Great, 5 milliseconds have passed" << endl;
    > }
    >
    > This works great but I would like to write something without the while(1)
    > loop where i need to check more often than every 5 milliseconds. I would
    > like something that would jump into a specific function every 5 ms. How can
    > i do something like this?


    You could use an interval timer (setitimer/ getitimer).

  8. Re: software interrupt

    Ron Eggler writes:
    > I would like to write a timer application where i can call a function every
    > X milliseconds. How far I am: I wrote an application that checks the time
    > since the last call and returns 1 when it's "ready" again so i can issue a
    > command like:
    >
    > TimerStart(5); //every 5 seconds
    > while (1){
    > if (CheckTimer(5))
    > cout << "Great, 5 milliseconds have passed" << endl;
    > }
    >
    > This works great but I would like to write something without the while(1)
    > loop where i need to check more often than every 5 milliseconds. I would
    > like something that would jump into a specific function every 5 ms. How can
    > i do something like this?


    You could use an interval timer (setitimer/ getitimer).

  9. Re: software interrupt

    David Schwartz writes:

    > On Mar 29, 6:21 pm, Ron Eggler wrote:
    >> Hi,
    >>
    >> I would like to write a timer application where i can call a function every
    >> X milliseconds. How far I am: I wrote an application that checks the time
    >> since the last call and returns 1 when it's "ready" again so i can issue a
    >> command like:
    >>
    >> TimerStart(5); //every 5 seconds
    >> while (1){
    >> if (CheckTimer(5))
    >> cout << "Great, 5 milliseconds have passed" << endl;
    >>
    >> }
    >>
    >> This works great but I would like to write something without the while(1)
    >> loop where i need to check more often than every 5 milliseconds. I would
    >> like something that would jump into a specific function every 5 ms. How can
    >> i do something like this? This must work, I remember Delphi 3.0 on Windows
    >> had such functionality a long time ago. I'm just not sure how i can write
    >> it myself and of course for Linux, not Windows.

    >
    > 1) The process must run as 'root' and must change scheduler classes.
    > (man 'sched_setscheduler'.
    >
    > 2) Use 'nanosleep' to wait.
    >
    > Be very careful. Errors in a process with a special scheduler class
    > can hang the system. You must complete what you are doing *very*
    > quickly and add appropriate safeties to handle the case where you
    > can't finish the job before it becomes time to do the next job.
    > Otherwise, the system will be able to do nothing else.


    On Linux a runaway real-time priority process can be tamed by pressing
    SysRq+N.

    --
    Måns Rullgård
    mans@mansr.com

  10. Re: software interrupt

    David Schwartz writes:

    > On Mar 29, 6:21 pm, Ron Eggler wrote:
    >> Hi,
    >>
    >> I would like to write a timer application where i can call a function every
    >> X milliseconds. How far I am: I wrote an application that checks the time
    >> since the last call and returns 1 when it's "ready" again so i can issue a
    >> command like:
    >>
    >> TimerStart(5); //every 5 seconds
    >> while (1){
    >> if (CheckTimer(5))
    >> cout << "Great, 5 milliseconds have passed" << endl;
    >>
    >> }
    >>
    >> This works great but I would like to write something without the while(1)
    >> loop where i need to check more often than every 5 milliseconds. I would
    >> like something that would jump into a specific function every 5 ms. How can
    >> i do something like this? This must work, I remember Delphi 3.0 on Windows
    >> had such functionality a long time ago. I'm just not sure how i can write
    >> it myself and of course for Linux, not Windows.

    >
    > 1) The process must run as 'root' and must change scheduler classes.
    > (man 'sched_setscheduler'.
    >
    > 2) Use 'nanosleep' to wait.
    >
    > Be very careful. Errors in a process with a special scheduler class
    > can hang the system. You must complete what you are doing *very*
    > quickly and add appropriate safeties to handle the case where you
    > can't finish the job before it becomes time to do the next job.
    > Otherwise, the system will be able to do nothing else.


    On Linux a runaway real-time priority process can be tamed by pressing
    SysRq+N.

    --
    Måns Rullgård
    mans@mansr.com

  11. Re: software interrupt

    Måns Rullgård wrote:

    > On Linux a runaway real-time priority process can be tamed by
    > pressing SysRq+N.


    Only with ALT, and only if "magic sysrq hacks" are compiled in.

    Regards,


    Björn

    Fup2 comp.os.linux.development.apps

    --
    BOFH excuse #10:

    hardware stress fractures


  12. Re: software interrupt

    Måns Rullgård wrote:

    > On Linux a runaway real-time priority process can be tamed by
    > pressing SysRq+N.


    Only with ALT, and only if "magic sysrq hacks" are compiled in.

    Regards,


    Björn

    Fup2 comp.os.linux.development.apps

    --
    BOFH excuse #10:

    hardware stress fractures


  13. Re: software interrupt

    On Mar 30, 4:08 am, Ron Eggler wrote:
    > David Schwartz wrote:
    > > 1) The process must run as 'root' and must change scheduler classes.
    > > (man 'sched_setscheduler'.

    >
    > I can't really see how i should use sched_setscheduler() to accomplish what
    > i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    > be able to change scheduling and prority of a given PID.
    >
    > > 2) Use 'nanosleep' to wait.

    >
    > To wait for what?



    I think David is suggesting that you perform a loop, like this:
    ....
    struct timespec ts;

    while (1) {
    ts.tv_sec = 0;
    ts.tv_nsec = 5000000;
    nanosleep (&ts, NULL); // delay 5ms
    checkfilefunction (blablabla...);
    }

    But to guarantee low latencies, around 5ms, you would need to set the
    priority
    high, to real-time scheduling, which prevents other processes from
    running.
    But if you don't need that, don't use it.

    > My task would be to look in a directory for a file and if there is one, open
    > a socket on a specific port and send the file out. Depending on the file
    > size and on the connection, this could take a some time. I would wanna
    > issue the timertask every 1 Minute only tho....
    > Any ideas/slutions are greatly appreciated!


    After reading this, it is not clear to me what is supposed to happen
    every 5ms and
    and what happens every 1 minute.

    -Mike

  14. Re: software interrupt

    On Mar 30, 4:08 am, Ron Eggler wrote:
    > David Schwartz wrote:
    > > 1) The process must run as 'root' and must change scheduler classes.
    > > (man 'sched_setscheduler'.

    >
    > I can't really see how i should use sched_setscheduler() to accomplish what
    > i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    > be able to change scheduling and prority of a given PID.
    >
    > > 2) Use 'nanosleep' to wait.

    >
    > To wait for what?



    I think David is suggesting that you perform a loop, like this:
    ....
    struct timespec ts;

    while (1) {
    ts.tv_sec = 0;
    ts.tv_nsec = 5000000;
    nanosleep (&ts, NULL); // delay 5ms
    checkfilefunction (blablabla...);
    }

    But to guarantee low latencies, around 5ms, you would need to set the
    priority
    high, to real-time scheduling, which prevents other processes from
    running.
    But if you don't need that, don't use it.

    > My task would be to look in a directory for a file and if there is one, open
    > a socket on a specific port and send the file out. Depending on the file
    > size and on the connection, this could take a some time. I would wanna
    > issue the timertask every 1 Minute only tho....
    > Any ideas/slutions are greatly appreciated!


    After reading this, it is not clear to me what is supposed to happen
    every 5ms and
    and what happens every 1 minute.

    -Mike

  15. Re: software interrupt

    On Mar 29, 9:08 pm, Ron Eggler wrote:

    > I can't really see how i should use sched_setscheduler() to accomplish
    > what
    > i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    > be able to change scheduling and prority of a given PID.


    What good would it do to sleep for 5 milliseconds if you're using a
    scheduler that gives another process a 20 millisecond timeslice?

    > > 2) Use 'nanosleep' to wait.

    >
    > To wait for what?


    To wait for 5 milliseconds (or whatever is left of it) to pass.

    DS

  16. Re: software interrupt

    On Mar 29, 9:08 pm, Ron Eggler wrote:

    > I can't really see how i should use sched_setscheduler() to accomplish
    > what
    > i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    > be able to change scheduling and prority of a given PID.


    What good would it do to sleep for 5 milliseconds if you're using a
    scheduler that gives another process a 20 millisecond timeslice?

    > > 2) Use 'nanosleep' to wait.

    >
    > To wait for what?


    To wait for 5 milliseconds (or whatever is left of it) to pass.

    DS

  17. Re: software interrupt

    In comp.os.linux.development.system Ron Eggler wrote:
    | David Schwartz wrote:
    |
    |> 1) The process must run as 'root' and must change scheduler classes.
    |> (man 'sched_setscheduler'.
    |
    | I can't really see how i should use sched_setscheduler() to accomplish what
    | i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    | be able to change scheduling and prority of a given PID.
    |
    |> 2) Use 'nanosleep' to wait.
    |
    | To wait for what?
    |
    |> Be very careful. Errors in a process with a special scheduler class
    |> can hang the system. You must complete what you are doing *very*
    |> quickly and add appropriate safeties to handle the case where you
    |> can't finish the job before it becomes time to do the next job.
    |> Otherwise, the system will be able to do nothing else.
    |
    | My task would be to look in a directory for a file and if there is one, open
    | a socket on a specific port and send the file out. Depending on the file
    | size and on the connection, this could take a some time. I would wanna
    | issue the timertask every 1 Minute only tho....
    | Any ideas/slutions are greatly appreciated!

    It is unclear from your posts if your program needs to do something every
    5 milliseconds or every minute. Could you be more specific?

    The mentioned method using setitimer and handling a SIGALRM signal seems
    most appropriate to me. You might be able to do all the work needed to
    be done inside the signal handler. Or if that work needs to be done in
    the main loop, the signal handler could simply do nothing, having let this
    wake up some other function your program is waiting in, such as sigsuspend().

    One thing to consider is whether your requirement can accept a gradual
    slippage of time or not. If you have it wait exactly 5 milliseconds, it
    will in fact be slightly longer than that due to the time running or even
    idling outside of that wait. I have some programs that need to have zero
    accumulated time error. I achieve that by calculating the difference
    between the intended real time to wait to, and the current time. It does
    have jitter, but no accumulated error. So as long as the system is able
    to keep dispatching my process every time, it would execute 1728000 such
    cycles in a 24 hour period if the interval is to be 50 milliseconds. A
    program not accounting for the time slip would execute slightly fewer
    such cycles.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2008-03-30-2320@ipal.net |
    |------------------------------------/-------------------------------------|

  18. Re: software interrupt

    In comp.os.linux.development.system Ron Eggler wrote:
    | David Schwartz wrote:
    |
    |> 1) The process must run as 'root' and must change scheduler classes.
    |> (man 'sched_setscheduler'.
    |
    | I can't really see how i should use sched_setscheduler() to accomplish what
    | i'm looking for. What i found by googling "ched_setscheduler" is that i'd
    | be able to change scheduling and prority of a given PID.
    |
    |> 2) Use 'nanosleep' to wait.
    |
    | To wait for what?
    |
    |> Be very careful. Errors in a process with a special scheduler class
    |> can hang the system. You must complete what you are doing *very*
    |> quickly and add appropriate safeties to handle the case where you
    |> can't finish the job before it becomes time to do the next job.
    |> Otherwise, the system will be able to do nothing else.
    |
    | My task would be to look in a directory for a file and if there is one, open
    | a socket on a specific port and send the file out. Depending on the file
    | size and on the connection, this could take a some time. I would wanna
    | issue the timertask every 1 Minute only tho....
    | Any ideas/slutions are greatly appreciated!

    It is unclear from your posts if your program needs to do something every
    5 milliseconds or every minute. Could you be more specific?

    The mentioned method using setitimer and handling a SIGALRM signal seems
    most appropriate to me. You might be able to do all the work needed to
    be done inside the signal handler. Or if that work needs to be done in
    the main loop, the signal handler could simply do nothing, having let this
    wake up some other function your program is waiting in, such as sigsuspend().

    One thing to consider is whether your requirement can accept a gradual
    slippage of time or not. If you have it wait exactly 5 milliseconds, it
    will in fact be slightly longer than that due to the time running or even
    idling outside of that wait. I have some programs that need to have zero
    accumulated time error. I achieve that by calculating the difference
    between the intended real time to wait to, and the current time. It does
    have jitter, but no accumulated error. So as long as the system is able
    to keep dispatching my process every time, it would execute 1728000 such
    cycles in a 24 hour period if the interval is to be 50 milliseconds. A
    program not accounting for the time slip would execute slightly fewer
    such cycles.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2008-03-30-2320@ipal.net |
    |------------------------------------/-------------------------------------|

  19. Re: software interrupt

    phil-news-nospam@ipal.net wrote:

    >
    > It is unclear from your posts if your program needs to do something every
    > 5 milliseconds or every minute. Could you be more specific?
    >
    > The mentioned method using setitimer and handling a SIGALRM signal seems
    > most appropriate to me. You might be able to do all the work needed to
    > be done inside the signal handler. Or if that work needs to be done in
    > the main loop, the signal handler could simply do nothing, having let this
    > wake up some other function your program is waiting in, such as sigsuspend().
    >


    Had a similar problem. But SIGALRM and setitimer don't have option for
    _user data_. Sometimes its important to get the context. Linux kernel
    typically only delivers the signal that something happened. Sometimes
    its important to have the context also to know what to do. Currently I
    do a quick hack, which is start a separate thread that waits for nano
    seconds and one waking up again performs its task. While creating a
    thread you can pass it a parameter which can be your context. Any other
    way of doing this?

    Regards
    --Himanshu

  20. Re: software interrupt

    phil-news-nospam@ipal.net wrote:

    >
    > It is unclear from your posts if your program needs to do something every
    > 5 milliseconds or every minute. Could you be more specific?
    >
    > The mentioned method using setitimer and handling a SIGALRM signal seems
    > most appropriate to me. You might be able to do all the work needed to
    > be done inside the signal handler. Or if that work needs to be done in
    > the main loop, the signal handler could simply do nothing, having let this
    > wake up some other function your program is waiting in, such as sigsuspend().
    >


    Had a similar problem. But SIGALRM and setitimer don't have option for
    _user data_. Sometimes its important to get the context. Linux kernel
    typically only delivers the signal that something happened. Sometimes
    its important to have the context also to know what to do. Currently I
    do a quick hack, which is start a separate thread that waits for nano
    seconds and one waking up again performs its task. While creating a
    thread you can pass it a parameter which can be your context. Any other
    way of doing this?

    Regards
    --Himanshu

+ Reply to Thread
Page 1 of 3 1 2 3 LastLast