summaryrefslogtreecommitdiff
path: root/examples/APG/Reactor/Timers.cpp
blob: c603b86bef4829d257ea74f5b32973b85c2fae04 (plain)
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
// $Id$

#include "ace/config-all.h"
#include "ace/OS_main.h"

#if !defined (ACE_LACKS_FORK)

#include "ace/streams.h"

#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>

typedef void (*timerTask_t)(void);

// Listing 1 code/ch07
pid_t timerTask (int initialDelay,
                 int interval,
                 timerTask_t task)
{
  if (initialDelay < 1 && interval < 1)
    return -1;

  pid_t pid = fork ();

  if (pid < 0)
    return -1;

  if (pid > 0)
    return pid;

  if (initialDelay > 0)
    sleep (initialDelay);

  if (interval < 1)
    return 0;

  while (1)
    {
      (*task) ();
      sleep (interval);
    }

  ACE_NOTREACHED (return 0);
}
// Listing 1

// Listing 2 code/ch07
void foo ()
{
  time_t now = time (0);
  cerr << "The time is " << ctime (&now) << endl;
}
// Listing 2

void programMainLoop (void)
{
  sleep (30);
}

// Listing 3 code/ch07
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  pid_t timerId = timerTask (3, 5, foo);
  programMainLoop ();
  kill (timerId, SIGINT);
  return 0;
}
// Listing 3

#else
#include <stdio.h>

int ACE_TMAIN (int, ACE_TCHAR *[])
{
  puts ("This very unportable example requires fork().\n");
  return 0;
}

#endif /* ACE_LACKS_FORK */