![](/img/trans.png)
[英]Set the process priority and the affinity of an irq process from C/C++
[英]How to set IRQ priority in Linux from C/C++?
現在,我在一個實時Linux的使用設定一個IRQ優先chrt
(對於給定irqPid):
/usr/bin/chrt -f -p 95 irqPid
有沒有一種方法/某些功能可以從C / C ++執行此操作(除了運行system()
來執行上述命令)?
您可能需要sched_setattr 。 使用strace可以很容易地看到syscall chrt在使用什么。
$ sleep 10 &
[2] 3590
$ sudo strace -- /usr/bin/chrt -f -p 95 $! 2>&1 | grep "=95"
sched_setattr(3590, {size=48, sched_policy=SCHED_FIFO, sched_flags=0, sched_nice=0, sched_priority=95, sched_runtime=0, sched_deadline=0, sched_period=0}, 0) = 0
在當前進程中執行此操作的一些代碼如下所示:
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.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, char**) {
int result;
struct sched_attr attr;
memset(&attr, 0, sizeof(attr));
attr.size = sizeof(sched_attr);
attr.sched_priority = 95;
attr.sched_policy = SCHED_FIFO;
result = sched_setattr(getpid(), &attr, 0);
if (result == -1) {
perror("Error calling sched_setattr.");
}
}
在編譯文件上再次使用strace:
$ sudo strace ./a.out 2>&1 | grep "=95"
sched_setattr(4889, {size=48, sched_policy=SCHED_FIFO, sched_flags=0, sched_nice=0, sched_priority=95, sched_runtime=0, sched_deadline=0, sched_period=0}, 0) = 0
由於某種原因,我在系統的glibc或sched.h中找不到系統調用,因此我必須包括結構和系統調用定義。 我對此並不太了解,因此該功能可能級別比所需級別低,而更高級別的調用可以更輕松地完成您想要的操作,但這恰恰反映了chrt正在做什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.