Using ioprio_set in c++

2020-07-20 05:35发布

问题:

I am trying to use ioprio_set to give a calling thread a higher priority for the IO scheduler. This is done within a c++ program. I want the call to look like this:

ioprio_set(IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IO_PRIO_CLASS_BE,0));

The man page says ioprio_set does not have Glibc wrapper, so they should be called using syscall. I tried the following:

syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IO_PRIO_CLASS_BE,0));

The problem is that the macros IOPRIO_WHO_PROCESS, IOPRIO_PRIO_VALUE and IO_PRIO_CLASS_BE cannot be found, and I don't know how to replace them by int values.

Thanks for any advice!

回答1:

Use this Linux header below. adding the following settings in Application.mk. APP_LDFLAGS :=-Wl,--allow-shlib-undefined



回答2:

Not sure if there's a better way than copying the definitions from include/linux/ioprio.h

#include <sys/syscalls.h>

#define IOPRIO_CLASS_SHIFT  (13)
#define IOPRIO_PRIO_VALUE(class, data)  (((class) << IOPRIO_CLASS_SHIFT) | data)

enum {
    IOPRIO_WHO_PROCESS = 1,
    IOPRIO_WHO_PGRP,
    IOPRIO_WHO_USER,
};

int main() {
    syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(1, 0))
}