Search code examples
affinitytaskaffinitysetthreadaffinitymasktaskset

Check value of PF_NO_SETAFFINITY


Is it possible to tell whether a process/thread has the PF_NO_SETAFFINITY flag set? I'm running taskset on a series of process ids and some are throwing errors of the following form:

taskset: failed to set pid 30's affinity: Invalid argument

I believe this is because some processes have PF_NO_SETAFFINITY set (see Answer).

Thank you!


Solution

  • Yes - look at /proc/PID/stat's 'flag' field

    <linux/sched.h
    
    #define PF_NO_SETAFFINITY 0x04000000        /* Userland is not allowed to meddle with cpus_allowed */
    

    Look here for details on using /proc:

    Example:

    ps -eaf
    www-data 30084 19962  0 07:09 ?        00:00:00 /usr/sbin/apache2 -k start
    ...
    
    cat /proc/30084/stat
    30084 (apache2) S 19962 19962 19962 0 -1 4194624 554 0 3 0 0 0 0 0 20 0 1 0 298837672 509616128 5510 18446744073709551615 1 1 0 0 0 0 0 16781312 201346799 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    

    The flags are 4194624


    Q: Do you mind specifying how you'd write a simple script that outputs true/false based on whether you're allowed to set affinity?

    A: I don't feel comfortable providing this without the opportunity to test, but you can try something like this...

    flags=$(cut -f 9 -d ' ' /proc/30084/stat)
    echo $(($flags & 0x40000000))