-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
58 lines (50 loc) · 1.17 KB
/
benchmark.cpp
File metadata and controls
58 lines (50 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <unistd.h>
#include <sched.h>
#include <signal.h>
#include <cstdio>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
int main(int, char **argv)
{
sched_param param;
sched_getparam( 0, ¶m );
param.sched_priority = 2;
if ( 0 != sched_setscheduler( 0, SCHED_FIFO, ¶m ) ) {
perror( "1. fail" );
}
const pid_t pid = fork();
if ( pid == 0 ) {
// I'm the child
// make the child realtime prio, but less than the parent
sched_param param2;
sched_getparam( 0, ¶m2 );
param2.sched_priority = 1;
if ( 0 != sched_setscheduler( 0, SCHED_FIFO, ¶m2 ) ) {
perror( "2. fail" );
}
// drop privileges
if ( geteuid() != getuid() ) {
seteuid( getuid() );
if ( geteuid() != getuid() ) {
return -1;
}
argv[0] = strdup( "./CA" );
execv( "./CA", argv );
perror( "./CA" );
return -1;
}
}
timeval timeout;
for ( int i = 0; i < 600; ++i ) {
timeout.tv_sec = 1;
timeout.tv_usec = 0;
select( 0, 0, 0, 0, &timeout );
if ( pid == waitpid( pid, 0, WNOHANG ) ) {
return 0;
}
}
kill( pid, SIGKILL );
return 0;
}