diff --git a/common/utils/utils.c b/common/utils/utils.c
index 05e5fdf3a73a49bfbb8827f28f3a185a10f3a683..f0f3ddf8a72a37e7090186c09eff057184b05715 100644
--- a/common/utils/utils.c
+++ b/common/utils/utils.c
@@ -2,6 +2,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <unistd.h>
+#include <sched.h>
+#include <errno.h>
 #include "utils.h"
 
 void *calloc_or_fail(size_t size) {
@@ -137,3 +140,32 @@ void *memcpy1(void *dst,const void *src,size_t n) {
   asm volatile("rep movsb" : "+D" (dst) : "c"(n), "S"(src) : "cc","memory");
   return(ret);
 }
+
+void set_priority(int priority)
+{
+  /* When running with a realtime scheduler, a buggy run-away process can take
+     down the host requiring a reboot to recover.  To avoid this scenario, we
+     use alarm(2) to set a maximum running time */
+  unsigned max_runtime_seconds = 10 * 60; /* default */
+  const char *env = getenv("MAX_RUNTIME_SECONDS");
+  if (env != NULL)
+  {
+    max_runtime_seconds = atoi(env);
+  }
+  unsigned was_alarm = alarm(max_runtime_seconds);
+  /* Normally, was_alarm should be 0 indicating there was no previous alarm set.
+     A non-zero value could indicate a mistake */
+  fprintf(stderr, "Set alarm for %u seconds, was %u (see $MAX_RUNTIME_SECONDS)\n",
+          max_runtime_seconds, was_alarm);
+
+  struct sched_param param =
+  {
+    .sched_priority = priority,
+  };
+  fprintf(stderr, "Calling sched_setscheduler(%d)\n", priority);
+  if (sched_setscheduler(0, SCHED_RR, &param) == -1)
+  {
+    fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno));
+    abort();
+  }
+}
diff --git a/common/utils/utils.h b/common/utils/utils.h
index 9295ab7b548cc3d050a43bcbd27634fddc353e6c..d4337e71a6b7162164f92b2bbb1f2a264500a2b1 100644
--- a/common/utils/utils.h
+++ b/common/utils/utils.h
@@ -19,6 +19,7 @@ int hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int siz
 
 void *memcpy1(void *dst,const void *src,size_t n);
 
+void set_priority(int priority);
 
 char *itoa(int i);
 
diff --git a/executables/nr-uesoftmodem.c b/executables/nr-uesoftmodem.c
index cef324db348ff7edffa3022088b111634658b84a..0e25d5115fc065c5d718151af5f4b6011c36ca8f 100644
--- a/executables/nr-uesoftmodem.c
+++ b/executables/nr-uesoftmodem.c
@@ -415,6 +415,13 @@ void *rrc_enb_process_msg(void *notUsed) {
 
 
 int main( int argc, char **argv ) {
+  set_priority(79);
+  if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
+  {
+    fprintf(stderr, "mlockall: %s\n", strerror(errno));
+    return EXIT_FAILURE;
+  }
+
   //uint8_t beta_ACK=0,beta_RI=0,beta_CQI=2;
   PHY_VARS_NR_UE *UE[MAX_NUM_CCs];
   start_background_system();
diff --git a/nfapi/oai_integration/nfapi.c b/nfapi/oai_integration/nfapi.c
index 3b14eaabd5c40d483835df8a905614c9b4822e4c..5e71c5e2a4458fcff77c9336bbdb36768dd16cb0 100644
--- a/nfapi/oai_integration/nfapi.c
+++ b/nfapi/oai_integration/nfapi.c
@@ -40,15 +40,9 @@ typedef struct {
 static nfapi_params_t nfapi_params = {0};
 
 void set_thread_priority(int priority) {
-  //printf("%s(priority:%d)\n", __FUNCTION__, priority);
-  pthread_attr_t ptAttr;
-  struct sched_param schedParam;
-  schedParam.__sched_priority = priority; //79;
-
-  if(sched_setscheduler(0, SCHED_RR, &schedParam) != 0) {
-    printf("Failed to set scheduler to SCHED_RR\n");
-  }
+  set_priority(priority);
 
+  pthread_attr_t ptAttr;
   if(pthread_attr_setschedpolicy(&ptAttr, SCHED_RR) != 0) {
     printf("Failed to set pthread sched policy SCHED_RR\n");
   }
diff --git a/nfapi/oai_integration/nfapi_pnf.c b/nfapi/oai_integration/nfapi_pnf.c
index 1cd33292a6d6dd09fce214c0108b6c921d777ac4..d1a154cf732eb263d229e0a50a7d0e119c89a0b1 100644
--- a/nfapi/oai_integration/nfapi_pnf.c
+++ b/nfapi/oai_integration/nfapi_pnf.c
@@ -218,14 +218,9 @@ void pnf_nfapi_trace(nfapi_trace_level_t nfapi_level, const char *message, ...)
 }
 
 void pnf_set_thread_priority(int priority) {
-  pthread_attr_t ptAttr;
-  struct sched_param schedParam;
-  schedParam.__sched_priority = priority;
-
-  if(sched_setscheduler(0, SCHED_RR, &schedParam) != 0) {
-    printf("failed to set SCHED_RR\n");
-  }
+  set_priority(priority);
 
+  pthread_attr_t ptAttr;
   if(pthread_attr_setschedpolicy(&ptAttr, SCHED_RR) != 0) {
     printf("failed to set pthread SCHED_RR %d\n", errno);
   }
diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c
index 2c84085d371c4b3558c4c76780cac5461ea05aa7..16c2f80c480bced446e61aac510be3b50411749f 100644
--- a/targets/RT/USER/lte-softmodem.c
+++ b/targets/RT/USER/lte-softmodem.c
@@ -524,15 +524,7 @@ static  void wait_nfapi_init(char *thread_name) {
 
 int main ( int argc, char **argv )
 {
-  struct sched_param param =
-  {
-    .sched_priority = 79
-  };
-  if (sched_setscheduler( 0, SCHED_RR, &param ) == -1 )
-  {
-    fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno));
-    return EXIT_FAILURE;
-  }
+  set_priority(79);
   if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
   {
     fprintf(stderr, "mlockall: %s\n", strerror(errno));
diff --git a/targets/RT/USER/lte-uesoftmodem.c b/targets/RT/USER/lte-uesoftmodem.c
index fb16af0fa1ea0692c3bd502a505a3f3213b60467..349362b3b2534e5c82203e41f60197d3ce8a9b84 100644
--- a/targets/RT/USER/lte-uesoftmodem.c
+++ b/targets/RT/USER/lte-uesoftmodem.c
@@ -547,15 +547,7 @@ AssertFatal(false,"");
 }
 
 int main( int argc, char **argv ) {
-  struct sched_param param =
-  {
-    .sched_priority = 79
-  };
-  if (sched_setscheduler( 0, SCHED_RR, &param ) == -1 )
-  {
-    fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno));
-    return EXIT_FAILURE;
-  }
+  set_priority(79);
   if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
   {
     fprintf(stderr, "mlockall: %s\n", strerror(errno));