author | Florian Pose <fp@igh-essen.com> |
Thu, 09 Oct 2008 13:24:17 +0000 | |
changeset 1254 | c19d273a9e76 |
parent 1249 | d64ed42e0d11 |
child 1255 | 38b7e05b20c1 |
permissions | -rw-r--r-- |
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
1 |
/***************************************************************************** |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
2 |
* |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
3 |
* $Id$ |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
4 |
* |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
5 |
****************************************************************************/ |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
6 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
7 |
#include <errno.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
8 |
#include <signal.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
9 |
#include <stdio.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
10 |
#include <string.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
11 |
#include <sys/resource.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
12 |
#include <sys/time.h> |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
13 |
#include <sys/types.h> |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
14 |
#include <unistd.h> |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
15 |
|
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
16 |
#include "ecrt.h" |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
17 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
18 |
#define PRIORITY 1 |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
19 |
|
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
20 |
/****************************************************************************/ |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
21 |
|
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
22 |
static unsigned int sig_alarms = 0; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
23 |
static unsigned int user_alarms = 0; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
24 |
|
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
25 |
/****************************************************************************/ |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
26 |
|
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
27 |
void signal_handler(int signum) { |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
28 |
switch (signum) { |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
29 |
case SIGALRM: |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
30 |
sig_alarms++; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
31 |
break; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
32 |
} |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
33 |
} |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
34 |
|
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
35 |
/****************************************************************************/ |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
36 |
|
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
37 |
int main(int argc, char **argv) |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
38 |
{ |
1244
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
39 |
ec_master_t *master; |
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
40 |
ec_domain_t *domain; |
1246
4042bda8c980
Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents:
1244
diff
changeset
|
41 |
ec_slave_config_t *sc; |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
42 |
struct sigaction sa; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
43 |
struct itimerval tv; |
1244
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
44 |
|
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
45 |
master = ecrt_request_master(0); |
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
46 |
if (!master) |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
47 |
return -1; |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
48 |
|
1244
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
49 |
domain = ecrt_master_create_domain(master); |
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
50 |
if (!domain) |
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
51 |
return -1; |
0b70040d3daa
No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
1242
diff
changeset
|
52 |
|
1246
4042bda8c980
Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents:
1244
diff
changeset
|
53 |
sc = ecrt_master_slave_config(master, 0, 0, 0x00000002, 0x044C2C52); |
4042bda8c980
Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents:
1244
diff
changeset
|
54 |
if (!sc) |
4042bda8c980
Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents:
1244
diff
changeset
|
55 |
return -1; |
4042bda8c980
Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents:
1244
diff
changeset
|
56 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
57 |
printf("Activating master...\n"); |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
58 |
if (ecrt_master_activate(master)) |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
59 |
return -1; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
60 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
61 |
#if PRIORITY |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
62 |
pid_t pid = getpid(); |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
63 |
if (setpriority(PRIO_PROCESS, pid, -19)) |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
64 |
fprintf(stderr, "Warning: Failed to set priority: %s\n", |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
65 |
strerror(errno)); |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
66 |
#endif |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
67 |
|
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
68 |
sa.sa_handler = signal_handler; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
69 |
sigemptyset(&sa.sa_mask); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
70 |
sa.sa_flags = 0; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
71 |
if (sigaction(SIGALRM, &sa, 0)) { |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
72 |
fprintf(stderr, "Failed to install signal handler!\n"); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
73 |
return -1; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
74 |
} |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
75 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
76 |
printf("Starting timer...\n"); |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
77 |
tv.it_interval.tv_sec = 0; |
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
78 |
tv.it_interval.tv_usec = 100000; |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
79 |
tv.it_value.tv_sec = 0; |
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
80 |
tv.it_value.tv_usec = 1000; |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
81 |
if (setitimer(ITIMER_REAL, &tv, NULL)) { |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
82 |
fprintf(stderr, "Failed to start timer: %s\n", strerror(errno)); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
83 |
return 1; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
84 |
} |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
85 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
86 |
printf("Started.\n"); |
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
87 |
while (1) { |
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
88 |
sleep(1); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
89 |
|
1249
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
90 |
#if 1 |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
91 |
struct timeval t; |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
92 |
gettimeofday(&t, NULL); |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
93 |
printf("%u.%06u\n", t.tv_sec, t.tv_usec); |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
94 |
#endif |
d64ed42e0d11
Userspace example with priority.
Florian Pose <fp@igh-essen.com>
parents:
1247
diff
changeset
|
95 |
|
1247
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
96 |
while (sig_alarms != user_alarms) { |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
97 |
ecrt_master_receive(master); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
98 |
ecrt_master_send(master); |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
99 |
|
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
100 |
user_alarms++; |
5f1f1a3e6636
Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents:
1246
diff
changeset
|
101 |
} |
1242
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
102 |
} |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
103 |
|
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
104 |
return 0; |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
105 |
} |
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
106 |
|
632a6b91f8e4
First version of userspace library with requesting/releasing.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
107 |
/****************************************************************************/ |