![](/img/trans.png)
[英]GNU Make to build files that are outdated AND newer than a certain timestamp
[英]Build against newer linux headers than libc is built using
我想使用從Linux 3.14開始可用的新SCHED_DEADLINE
調度策略編寫程序。
我從一個簡單的程序開始嘗試使用sched_setattr
函數。
#include <sched.h>
int main(void)
{
// struct sched_attr attr;
// attr.size = sizeof(struct sched_attr);
// attr.sched_policy = SCHED_DEADLINE;
sched_setattr(0, (void*)0, 0);
return 0;
}
但是,在編譯時出現以下錯誤:
$gcc dead.c
dead.c: In function ‘main’:
dead.c:8:2: warning: implicit declaration of function ‘sched_setattr’ [-Wimplicit-function-declaration]
sched_setattr(0, (void*)0, 0);
^~~~~~~~~~~~~
/tmp/ccGxWxZE.o: In function `main':
dead.c:(.text+0x19): undefined reference to `sched_setattr'
collect2: error: ld returned 1 exit status
我的系統正在運行Ubuntu 16.10 Yakkety,內核為4.8.0-59-generic。 包含的sched.h
文件可在/usr/include/sched.h
找到,並由軟件包libc6-dev
。 此頭文件不包含函數sched_setattr
和我嘗試使用的朋友。
但是我安裝的內核(和內核頭文件)附帶了一個sched.h
頭文件,其中包含我需要的定義。 它位於我的系統上的/usr/src/linux-headers-4.8.0-58/include/linux/sched.h
。
因此,我天真的認為讓我們僅針對較新的linux頭文件而不是libc6-dev提供的頭文件進行構建。 我的程序只能在此或更新的內核上運行,但這很好。
我將第一行修改為: #include <linux/sched.h>
並執行:
gcc -I/usr/src/linux-headers-$(uname -r)/include -I/usr/src/linux-headers-$(unam -r)/arch/x86/include dead.c
現在我得到錯誤和警告的頁面一頁。 這似乎不是要走的路。
用比libc提供的新的Linux標頭構建用戶空間程序的正確方法是什么?
然后,我該如何構建上面的程序?
sched_setattr()
是一個系統調用,似乎沒有一對一的libc包裝器。 您可以自己進行包裝,如下所示:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <linux/sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
struct sched_attr {
uint32_t size; /* Size of this structure */
uint32_t sched_policy; /* Policy (SCHED_*) */
uint64_t sched_flags; /* Flags */
int32_t sched_nice; /* Nice value (SCHED_OTHER, SCHED_BATCH) */
uint32_t sched_priority; /* Static priority (SCHED_FIFO, SCHED_RR) */
/* Remaining fields are for SCHED_DEADLINE */
uint64_t sched_runtime;
uint64_t sched_deadline;
uint64_t sched_period;
};
static int sched_setattr (pid_t pid, const struct sched_attr *attr, unsigned int flags)
{
return syscall (SYS_sched_setattr, pid, attr, flags);
}
int main (int argc, char *argv[])
{
struct sched_attr attr;
int res;
memset (&attr, 0, sizeof (struct sched_attr));
attr.size = sizeof (struct sched_attr);
res = sched_setattr (getpid (), &attr, 0);
if (res < 0) {
perror ("sched_setattr");
return 1;
}
return 0;
}
在嘗試嘗試包含獲取struct sched_attr
的定義所需的內核頭文件並閱讀谷歌搜索“用戶空間中的內核頭”發現的注釋時,查看報告的錯誤,我真的不建議嘗試為此添加內核頭文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.