[PATCH] How to write a portable "run_on" program?

From: Rusty Russell (rusty@rustcorp.com.au)
Date: Thu May 08 2003 - 00:14:40 EST


Hi all,

        Currently you need to do the following in userspace to set
your affinity portably:

#include <limits.h>

#define BITS_PER_LONG (sizeof(long)*CHAR_BIT)

int set_cpu(int cpu)
{
        size_t size = sizeof(unsigned long);
        unsigned long *bitmask = NULL;
        int ret;

        do {
                size *= 2;
                bitmask = realloc(bitmask, size);
                memset(bitmask, 0, size);
                bitmask[cpu / BITS_PER_LONG] = (1 << (cpu % BITS_PER_LONG);
                ret = sched_setaffinity(getpid(), size, bitmask);
        } while (ret < 0 && errno = -EINVAL);
        return ret;
}

I know Linus thinks this is natural and fine[1], but for the rest of
us, how about the following patch, which allows:

int set_cpu(int cpu)
{
        size_t size = sched_getaffinity(getpid(), 0, NULL);
        unsigned char bitmask[size];

        memset(bitmask, 0, size);
        bitmask[cpu/8] = (1 << (cpu%8));
        return sched_setaffinity(getpid(), size, bitmask);
}

Thoughts?
Rusty.

[1] http://www.ussg.iu.edu/hypermail/linux/kernel/0206.2/0551.html

--
  Anyone who quotes me in their sig is an idiot. -- Rusty Russell.

diff -urpN --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.69-bk2/kernel/sched.c working-2.5.69-bk2-getaffinity/kernel/sched.c --- linux-2.5.69-bk2/kernel/sched.c 2003-05-05 12:37:13.000000000 +1000 +++ working-2.5.69-bk2-getaffinity/kernel/sched.c 2003-05-08 15:11:30.000000000 +1000 @@ -1949,6 +1949,10 @@ asmlinkage long sys_sched_getaffinity(pi task_t *p; real_len = sizeof(mask); + + /* This lets the user query the bitmask size. */ + if (!len) + return real_len; if (len < real_len) return -EINVAL; - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Thu May 15 2003 - 22:00:26 EST