Userspace example with priority.
authorFlorian Pose <fp@igh-essen.com>
Thu, 02 Oct 2008 15:38:35 +0000
changeset 1249 d64ed42e0d11
parent 1248 3cc16b60a571
child 1250 642048176899
Userspace example with priority.
examples/user/main.c
--- a/examples/user/main.c	Thu Oct 02 15:38:10 2008 +0000
+++ b/examples/user/main.c	Thu Oct 02 15:38:35 2008 +0000
@@ -4,15 +4,19 @@
  *
  ****************************************************************************/
 
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <sys/time.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
 
 #include "ecrt.h"
 
+#define PRIORITY 1
+
 /****************************************************************************/
 
 static unsigned int sig_alarms = 0;
@@ -50,9 +54,17 @@
     if (!sc)
         return -1;
 
+    printf("Activating master...\n");
     if (ecrt_master_activate(master))
         return -1;
 
+#if PRIORITY
+    pid_t pid = getpid();
+    if (setpriority(PRIO_PROCESS, pid, -19))
+        fprintf(stderr, "Warning: Failed to set priority: %s\n",
+                strerror(errno));
+#endif
+
     sa.sa_handler = signal_handler;
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = 0;
@@ -61,25 +73,27 @@
         return -1;
     }
 
+    printf("Starting timer...\n");
     tv.it_interval.tv_sec = 0;
-    tv.it_interval.tv_usec = 10000;
+    tv.it_interval.tv_usec = 100000;
     tv.it_value.tv_sec = 0;
-    tv.it_value.tv_usec = 1;
+    tv.it_value.tv_usec = 1000;
     if (setitimer(ITIMER_REAL, &tv, NULL)) {
         fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
         return 1;
     }
 
+    printf("Started.\n");
 	while (1) {
         sleep(1);
 
+#if 1
+        struct timeval t;
+        gettimeofday(&t, NULL);
+        printf("%u.%06u\n", t.tv_sec, t.tv_usec);
+#endif
+
         while (sig_alarms != user_alarms) {
-
-#if 0
-            struct timeval t;
-            gettimeofday(&t, NULL);
-            printf("%u %u\n", t.tv_sec, t.tv_usec);
-#endif
             ecrt_master_receive(master);
             ecrt_master_send(master);