File: TestTimeout.c

package info (click to toggle)
cmake 4.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 152,344 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (72 lines) | stat: -rw-r--r-- 1,407 bytes parent folder | download | duplicates (5)
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
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OpenBSD__)
/* NOLINTNEXTLINE(bugprone-reserved-identifier) */
#  define _XOPEN_SOURCE 600
#endif

#if defined(_WIN32)
#  include <windows.h>
#else
#  include <sched.h>
#  include <unistd.h>
#endif

#include <stdio.h>

#ifdef SIGNAL
#  include <errno.h>
#  include <signal.h>
#  include <string.h>

static unsigned int signal_count;
static void signal_handler(int signum)
{
  (void)signum;
  ++signal_count;
}
#endif

int main(void)
{
#ifdef FORK
  pid_t pid = fork();
  if (pid != 0) {
    return 0;
  }
#endif

#ifdef SIGNAL
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = signal_handler;
  while ((sigaction(SIGUSR1, &sa, NULL) < 0) && (errno == EINTR))
    ;
#endif

#if defined(_WIN32)
  Sleep((TIMEOUT + 4) * 1000);
#elif defined(SIGNAL_IGNORE)
#  if defined(__CYGWIN__) || defined(__sun__) || defined(__GNU__)
#    define ERRNO_IS_EINTR (errno == EINTR || errno == 0)
#  else
#    define ERRNO_IS_EINTR (errno == EINTR)
#  endif
  {
    unsigned int timeLeft = (TIMEOUT + 4 + SIGNAL_IGNORE);
    while ((timeLeft = sleep(timeLeft), timeLeft > 0 && ERRNO_IS_EINTR)) {
      printf("EINTR: timeLeft=%u\n", timeLeft);
      fflush(stdout);
    }
  }
#else
  sleep((TIMEOUT + 4));
#endif

#ifdef SIGNAL
  if (signal_count > 0) {
    printf("SIGUSR1: count=%u\n", signal_count);
    fflush(stdout);
  }
#endif

  return 0;
}