Use sched_setscheduler() to set priority in user example. stable-1.5
authorFlorian Pose
Tue, 06 Feb 2018 14:58:14 +0100
branchstable-1.5
changeset 2697 4f5ae7e0fbaa
parent 2696 2b072c95b34c
child 2698 9e65f782e8a1
Use sched_setscheduler() to set priority in user example.
examples/user/main.c
--- a/examples/user/main.c	Tue Jan 30 17:09:33 2018 +0100
+++ b/examples/user/main.c	Tue Feb 06 14:58:14 2018 +0100
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <time.h> /* clock_gettime() */
 #include <sys/mman.h> /* mlockall() */
+#include <sched.h> /* sched_setscheduler() */
 
 /****************************************************************************/
 
@@ -353,10 +354,12 @@
 
     /* Set priority */
 
-    pid_t pid = getpid();
-    if (setpriority(PRIO_PROCESS, pid, -19)) {
-        fprintf(stderr, "Warning: Failed to set priority: %s\n",
-                strerror(errno));
+    struct sched_param param = {};
+    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
+
+    printf("Using priority %i.", param.sched_priority);
+    if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
+        perror("sched_setscheduler failed");
     }
 
     /* Lock memory */