Читаем Linux программирование в примерах полностью

Таймеры никогда не срабатывают раньше заданного времени, вместо этого срабатывая спустя небольшой постоянный интервал времени, зависящий от разрешения системного таймера (в настоящее время 10 мс). После срабатывания будет сгенерирован сигнал, а таймер будет сброшен. Если таймер срабатывает, когда процесс выполняется (для таймера ITIMER_VIRT это всегда верно), сигнал будет доставлен немедленно после создания. В противном случае, доставка будет сдвинута на небольшой промежуток времени, зависящий от загрузки системы.

Из этих трех таймеров ITIMER_REAL кажется наиболее полезным. Следующая программа, ch14-timers.c, показывает, как читать данные с терминала, но с тайм-аутом, чтобы программа не зависала на бесконечное время, ожидая ввода:

1  /* ch14-timers.c --- демонстрация интервальных таймеров */

2

3  #include

4  #include

5  #include

6  #include

7

8  /* handler --- обрабатывает SIGALRM */

9

10 void handler(int signo)

11 {

12  static const char msg[] = "\n*** Timer expired, you lose ***\n";

13

14  assert(signo == SIGALRM);

15

16  write(2, msg, sizeof(msg) - 1);

17  exit(1);

18 }

19

20 /* main --- установить таймер, прочесть данные с тайм-аутом */

21

22 int main(void)

23 {

24  struct itimerval tval;

25  char string[BUFSIZ];

26

27  timerclear(&tval.it_interval); /* нулевой интервал означает не сбрасывать таймер */

28  timerclear(&tval.it_value);

29

30  tval.it_value.tv_sec = 10; /* тайм-аут 10 секунд */

31

32  (void)signal(SIGALRM, handler);

33

34

35  printf("You have ten seconds to enter\nyour name, rank, and serial number: ");

36  (void)setitimer(ITIMER_REAL, &tval, NULL);

37  if (fgets(string, sizeof string, stdin) != NULL) {

38   (void)setitimer(ITIMER_REAL, NULL, NULL); /* выключить таймер */

39   /* обработать оставшиеся данные, вывод диагностики для иллюстрации */

40   printf("I'm glad you are being cooperative.\n");

41  } else

42   printf("\nEOF, eh? We won't give up so easily'\n");

43

44  exit(0);

45 }

Строки 10–18 представляют обработчик сигнала для SIGALRM; вызов assert() гарантирует, что обработчик сигнала был установлен соответствующим образом. Тело обработчика выводит сообщение и выходит, но оно может делать что-нибудь более подходящее для крупномасштабной программы.

В функции main() строки 27–28 очищают два члена struct timeval структуры struct itimerval.tval. Затем строка 30 устанавливает тайм-аут в 10 секунд. Установка tval.it_interval в 0 означает, что нет повторяющегося сигнала; он срабатывает лишь однажды. Строка 32 устанавливает обработчик сигнала, а строка 34 выводит приглашение.

Строка 36 устанавливает таймер, а строки 37–42 выводят соответствующие сообщения, основываясь на действиях пользователя. Реальная программа выполняла бы в этот момент свою задачу. Важно здесь обратить внимание на строку 38, которая отменяет таймер, поскольку были введены действительные данные.

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных