Skip to content

Instantly share code, notes, and snippets.

@Pacifist117
Created May 27, 2014 19:36
Show Gist options
  • Save Pacifist117/89e286111a1ff5d65361 to your computer and use it in GitHub Desktop.
Save Pacifist117/89e286111a1ff5d65361 to your computer and use it in GitHub Desktop.
#include <sched.h>
#include <linux/sched.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <inttypes.h>
#include <sched.h>
#include <time.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct sched_attr {
uint32_t size;
uint32_t sched_policy;
uint64_t sched_flags;
/* SCHED_NORMAL, SCHED_BATCH */
int32_t sched_nice;
/* SCHED_FIFO, SCHED_RR */
uint32_t sched_priority;
/* SCHED_DEADLINE */
uint64_t sched_runtime;
uint64_t sched_deadline;
uint64_t sched_period;
};
#ifndef SCHED_DEADLINE
#define SCHED_DEADLINE 6
#endif
static int
sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags)
{
return syscall(__NR_sched_setattr, pid, attr, flags);
}
static int
sched_getattr(pid_t pid, const struct sched_attr *attr, unsigned int size, unsigned int flags)
{
return syscall(__NR_sched_getattr, pid, attr, flags);
}
int main(int argc, char * argv[]) {
if ( argc != 2 ) {
printf("usage: sched_dead_test <pid>\n");
return 0;
}
struct sched_param param;
struct sched_attr attr;
int ret = 0;
pid_t pid = (pid_t)(atoi(argv[1]));
ret = sched_getparam(pid,&param);
printf("running sched_dead_test for %ld\n",pid);
if (ret)
printf("1ret=%d (%s)\n",ret,strerror(errno));
ret = sched_getattr(pid,&attr,sizeof(struct sched_attr),0);
if (ret)
printf("2ret=%d (%s)\n",ret,strerror(errno));
ret = 0;
attr.sched_policy = SCHED_DEADLINE;
attr.sched_runtime = 5000000; //5ms
attr.sched_deadline = 50000000; //50ms
attr.sched_period = 50000000; //50ms
//sched_setparam((pid_t)(atoi(argv[0])),&param);
ret = sched_setattr((pid_t)(atoi(argv[1])), &attr, 0);
if (ret)
printf("3ret=%d (%s)\n",ret,strerror(errno));
ret = 0;
//int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param);
ret = sched_setscheduler((pid_t)(atoi(argv[1])),SCHED_DEADLINE,&param);
if (ret)
printf("4ret=%d (%s)\n",ret,strerror(errno));
//int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment