chiark / gitweb /
found implementation of SIGXCPU etc.
authorian <ian>
Sat, 17 May 2008 14:59:24 +0000 (14:59 +0000)
committerian <ian>
Sat, 17 May 2008 14:59:24 +0000 (14:59 +0000)
hostside/rtprio.c [new file with mode: 0644]

diff --git a/hostside/rtprio.c b/hostside/rtprio.c
new file mode 100644 (file)
index 0000000..f8f5e3b
--- /dev/null
@@ -0,0 +1,56 @@
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+#include <assert.h>
+#include <string.h>
+#define MAXCREDIT 5
+#define MAXNTH 2
+
+static time_t now, last;
+static struct rlimit rlcpu;
+static struct sigaction saxcpu;
+
+static void gnow(void) { time(&now); }
+
+static void makepending(void) {
+  int r;
+  last= now;
+  r= sigaction(SIGXCPU, &saxcpu, 0); assert(!r);
+  r= setrlimit(RLIMIT_CPU, &rlcpu); assert(!r);
+}
+
+static void exceeded(void) {
+  const char m[]= "cpu limit exceeded\n";
+  write(2,m,sizeof(m)-1);
+  raise(SIGXCPU);
+}
+
+static void xcpuhandler(int ignored) {
+  int credit;
+  gnow();
+  credit= (now - last)/MAXNTH;
+  if (credit <= 0) exceeded();
+  if (credit > MAXCREDIT) credit= MAXCREDIT;
+  rlcpu.rlim_cur += credit;
+  makepending();
+}
+
+int main(void) {
+  int r;
+
+  memset(&saxcpu,0,sizeof(saxcpu));
+  saxcpu.sa_handler= xcpuhandler;
+  sigemptyset(&saxcpu.sa_mask);
+  saxcpu.sa_flags= SA_RESETHAND | SA_RESTART | SA_NODEFER;
+  
+  r= getrlimit(RLIMIT_CPU, &rlcpu); assert(!r);
+
+  gnow();
+  rlcpu.rlim_cur= MAXCREDIT;
+  makepending();
+
+  for (;;);
+}