Periodic Task in Linux

我怕爱的太早我们不能终老 提交于 2019-12-23 17:00:40

问题


I want to write a C program that would periodically perform some task (say, print something on console).

I have implemented it using nanosleep as below. Every 500ms the function 'func' is called.

#include <stdio.h>
#include <time.h>

void func(void);

int main()
{
  struct timespec mytimespec;
  mytimespec.tv_sec = 0;
  mytimespec.tv_nsec = 500000000; /* 500 ms */

  while(1)
  {
    func(); 
    nanosleep(&mytimespec,NULL);
  }
  return 0;
}

void func(void)
{
  printf("This would be printed periodically\n");
}

Above is working correctly. However I have some doubts:-

  • Would it work accurately if there are multiple threads and one thread relies on nanosleep to do the periodic task?

  • Is there a way to spawn a periodic thread in linux? Or, to use some timer callback?


回答1:


You should read time(7) (and perhaps signal(7)...). You probably want some event loop (at least if your program is doing some input). That loop is based upon a multiplexing syscall like poll(2) (see also this and that answers). Many libraries provide event loops, notably libevent, libev, Gtk/Glib, Qt, ...

On Linux you could be also interested by timerfd_create(2) (in addition of other more traditional solutions).

And read Advanced Linux Programming




回答2:


timer_create will be the system call you are looking for.

it has a sigaction structure that specify what way the callback should be called.

you have to initialize the member it_interval in the timer_settime function's newvalue argument, so that the function that registered at sigaction structure is called every it_interval.

the function can be a signal handler, a thread that will be run when the timer is expired.

see timer_create manual page for more info on this.




回答3:


There are many ways depending on task:

1) You can use realtime clock (man rtc)

2) You can use cron (spawning processes any process by timer)

3) You can use select/pselect (it is good for small time intervals for periodic thread spawning)

/* Wait up to five seconds. */
           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(1, 0, 0, 0, &tv);

And if you are monitoring files/directories you can use inotify.




回答4:


Would it work accurately if there are multiple threads and one thread relies on nanosleep to do the periodic task?

Function nanosleep() won't return earlier than you've specified in its arguments (unless it is interrupted by signal handler or encounters an error). But it may return later (usually only a bit later).

Please, take a look on the following article: http://www.drdobbs.com/soft-real-time-programming-with-linux/184402031 - it seems to explain timers in a good way.

Is there a way to spawn a periodic thread in linux? Or, to use some timer callback?

Take a look on timers: http://man7.org/linux/man-pages/man2/timer_create.2.html



来源:https://stackoverflow.com/questions/19446969/periodic-task-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!