簡體   English   中英

設置主線程時,pthread_create返回EAGAIN sched_deadline調度策略

[英]pthread_create returns EAGAIN when the main thread is set sched_deadline scheduling policy

我想讓一個進程在新的Linux SCHED_DEADLINE調度策略下進行調度。 同時,此過程必須創建一些輔助線程來完成其他一些工作。 但是,當我成功調用sched_setattr (用於設置進程調度策略)后調用pthread_create時,我得到了一個EAGAIN 我知道在實時過程中創建線程可能有點奇怪。 可能會出現諸如“新線程的調度策略將是什么”之類的問題。

盡管如此,是否有辦法在SCHED_DEADLINE進程中創建新線程?

以及如何定義新線程的調度策略?

重現我的問題的代碼可以在以下位置找到

https://github.com/lookflying/pthread_deadline/

我認為您會發現新pthread的默認調度策略是PTHREAD_INHERIT_SCHED 要覆蓋此內容,您需要給pthread_attr_init()一個顯式的屬性集,並與pthread_attr_setschedpolicy()pthread_attr_setschedparam() ,然后將這些屬性應用於pthread_create()

您可以sched_getscheduler()sched_getparam()設置為SCHED_DEADLINE然后將其輸入pthread_attr,以備后用。

合並SCHED_DEADLINE時,內核社區在列表中明確討論了該主題,並決定不允許SCHED_DEADLINE任務創建其他任務(通過fork()pthread_create() )。

因此,目前尚無法實現這種行為。 您必須先創建任務, 然后再設置SCHED_DEADLINE優先級。

關於第二個問題,不幸的是,glibc尚未包裝sched_setattr()系統調用(盡管已有4年了)。 這是一些用於創建SCHED_DEADLINE任務的代碼:

#define _GNU_SOURCE
#include <linux/kernel.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <time.h>
#include <linux/types.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/types.h>

#define SCHED_DEADLINE  6

/* __NR_sched_setattr number */
#ifndef __NR_sched_setattr
#ifdef __x86_64__
#define __NR_sched_setattr      314
#endif

#ifdef __i386__
#define __NR_sched_setattr      351
#endif

#ifdef __arm__
#define __NR_sched_setattr      380
#endif

#ifdef __aarch64__
#define __NR_sched_setattr      274
#endif
#endif

/* __NR_sched_getattr number */
#ifndef __NR_sched_getattr
#ifdef __x86_64__
#define __NR_sched_getattr      315
#endif

#ifdef __i386__
#define __NR_sched_getattr      352
#endif

#ifdef __arm__
#define __NR_sched_getattr      381
#endif

#ifdef __aarch64__
#define __NR_sched_getattr      275
#endif
#endif

struct sched_attr {
    __u32 size;

    __u32 sched_policy;
    __u64 sched_flags;

    /* SCHED_NORMAL, SCHED_BATCH */
    __s32 sched_nice;

    /* SCHED_FIFO, SCHED_RR */
    __u32 sched_priority;

    /* SCHED_DEADLINE */
    __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);
}

然后,在任務代碼中:

struct sched_attr attr;
attr.size = sizeof(attr);
attr.sched_flags = 0;
attr.sched_nice = 0;
attr.sched_priority = 0;

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

if (sched_setattr(0, &attr, flags) < 0) {
       perror("sched_setattr()");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM