Page 19 of 21
Article Index
Page 395
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <linux/sched.h>
#include <pthread.h>
#include <stdint.h>
struct sched_attr
{
uint32_t size;
uint32_t sched_policy;
uint64_t sched_flags;
int32_t sched_nice;
uint32_t sched_priority;
uint64_t sched_runtime;
uint64_t sched_deadline;
uint64_t sched_period;
};
int sched_setattr(pid_t pid, const struct sched_attr *attr,
unsigned int flags)
{
return syscall(__NR_sched_setattr, pid, attr, flags);
}
void *threadA(void *p)
{
struct sched_attr attr = {
.size = sizeof(attr),
.sched_policy = SCHED_DEADLINE,
.sched_runtime = 10 * 1000 * 1000,
.sched_period = 2 * 1000 * 1000 * 1000,
.sched_deadline = 11 * 1000 * 1000};
sched_setattr(0, &attr, 0);
for (;;)
{
printf("sensor\n");
fflush(0);
sched_yield();
};
}
int main(int argc, char **argv)
{
pthread_t pthreadA;
pthread_create(&pthreadA, NULL, threadA, NULL);
pthread_exit(0);
return (EXIT_SUCCESS);
}
Page 399
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
volatile int j;
volatile int i;
void *threadA(void *p)
{
for (i = 0;; i++)
{
};
}
void *threadB(void *p)
{
for (j = 0;; j++)
{
};
}
int main(int argc, char **argv)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_t pthreadA;
pthread_create(&pthreadA, NULL, threadA, NULL);
pthread_setaffinity_np(pthreadA, sizeof(cpu_set_t), &cpuset);
CPU_ZERO(&cpuset);
CPU_SET(2, &cpuset);
pthread_t pthreadB;
pthread_create(&pthreadB, NULL, threadB, NULL);
pthread_setaffinity_np(pthreadB, sizeof(cpu_set_t), &cpuset);
sleep(5);
printf("%d %d", i, j);
return (EXIT_SUCCESS);
}