C++ 如何在Linux中从C/C++;?
现在,我正在使用C++ 如何在Linux中从C/C++;?,c++,c,linux,system-calls,interrupt,C++,C,Linux,System Calls,Interrupt,现在,我正在使用chrt(对于给定的irqPid)在实时Linux中设置IRQ优先级: 是否有一种方法/某些函数可以从C/C++执行此操作(除了运行system()以执行上面的命令之外)?您可能需要。使用它很容易看到syscall chrt正在使用什么 $ sleep 10 & [2] 3590 $ sudo strace -- /usr/bin/chrt -f -p 95 $! 2>&1 | grep "=95" sched_setattr(3590, {size=48,
chrt
(对于给定的irqPid)在实时Linux中设置IRQ优先级:
是否有一种方法/某些函数可以从C/C++执行此操作(除了运行system()
以执行上面的命令之外)?您可能需要。使用它很容易看到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.");
}
}
由于某种原因,我无法在glibc中找到系统调用,也无法在系统的sched.h中找到结构,因此我必须包含结构和系统调用定义。我对此不太了解,所以这个函数的级别可能比需要的低,而更高级别的调用可以更轻松地完成您想要做的事情,但这恰恰反映了chrt正在做的事情。正如您所说的linux,我很确定一定有chrt源代码,所以您只需从中复制即可(还要感谢写这本书的人)。
#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.");
}
}
$ 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