Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 05-02-2018, 11:51 AM   #1
LQ Newbie
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
sched_setaffinity() and sched_setattr()

Is there a way I can use deadline scheduling and at the same time set cpu affinity to a process? Below is my testing code:

#define _GNU_SOURCE
#include "include/my_sched.h"
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main() {
	struct sched_attr attr;
	int x = 0;
	int ret;
	unsigned int flags = 0;
	long int tid = gettid();

	printf("deadline thread started [%ld]\n", tid);

	/* Set scheduling properties */
	attr.size = sizeof(attr);
	attr.sched_flags = 0;
	attr.sched_nice = 0;
	attr.sched_priority = 0;

	/* This creates a 100ms/300ms reservation */
	attr.sched_policy = SCHED_DEADLINE;
	attr.sched_runtime = 100 * 1000 * 1000;
	attr.sched_period = attr.sched_deadline = 300 * 1000 * 1000;

	ret = sched_setattr(0, &attr, flags);
	if (ret != 0) {
		done = 0;

	/* Set CPU affinity */
	cpu_set_t  mask;
	CPU_SET(0, &mask);
	ret = sched_setaffinity(0, sizeof(mask), &mask);

	if (ret != 0) {
		done = 0;

	return 0;
Even if I compile the above program and run it in sudo, I got error
sched_setaffinity: Device or resource busy
If I swap the order of sched_setattr() and sched_setaffinity(), I got a different error:
sched_setattr: Operation not permitted
This happens even if I'm in sudo.

Is there any problem with my code? Why I cannot use sched_setaffinity() and sched_setattr() with deadline scheduling in the same program?

For anyone who is interested to compile and try the program, below is code for my_include.h:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <linux/unistd.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <pthread.h>

#define gettid() syscall(__NR_gettid)


/* XXX use the proper syscall numbers */
#ifdef __x86_64__
#define __NR_sched_setattr		314
#define __NR_sched_getattr		315

#ifdef __i386__
#define __NR_sched_setattr		351
#define __NR_sched_getattr		352

#ifdef __arm__
#define __NR_sched_setattr		380
#define __NR_sched_getattr		381

static volatile int done;

struct sched_attr {
	__u32 size;

	__u32 sched_policy;
	__u64 sched_flags;

	__s32 sched_nice;

	__u32 sched_priority;

	/* SCHED_DEADLINE (nsec) */
	__u64 sched_runtime;
	__u64 sched_deadline;
	__u64 sched_period;

int sched_setattr(pid_t pid,
		const struct sched_attr *attr,
		unsigned int flags)
	return syscall(__NR_sched_setattr, pid, attr, flags);

int sched_getattr(pid_t pid,
		struct sched_attr *attr,
		unsigned int size,
		unsigned int flags)
	return syscall(__NR_sched_getattr, pid, attr, size, flags);
This question has been asked in a different thread (, but there's no answer for it.

Last edited by PopWq; 05-02-2018 at 11:53 AM. Reason: add reference
Old 05-09-2018, 09:12 AM   #2
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,188

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
What kernel are you running?

Edit: I think you are running into this check in sched/core.c which says deadline tasks can't run on fewer CPUs:

	 * Since bandwidth control happens on root_domain basis,
	 * if admission test is enabled, we only admit -deadline
	 * tasks allowed to run on all the CPUs in the task's
	 * root_domain.

Last edited by smallpond; 05-09-2018 at 09:20 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Real time application programming - sched_setattr() fails if I set CPU affinity using sched_setaffinity(). barz_83_LQ Linux - Newbie 2 11-20-2017 10:11 AM
How immediate is sched_setaffinity dpoznan Linux - Software 0 03-19-2008 08:00 AM
difference between set_cpus_allowed() and sched_setaffinity() asurya Linux - General 1 12-01-2006 09:52 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 10:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration