mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] How to write a portable "run_on" program?
@ 2003-05-08  5:14 Rusty Russell
  2003-05-08  5:31 ` Linus Torvalds
  0 siblings, 1 reply; 4+ messages in thread
From: Rusty Russell @ 2003-05-08  5:14 UTC (permalink / raw)
  To: torvalds, rml, mingo; +Cc: linux-kernel

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;
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-05-08  6:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-08  5:14 [PATCH] How to write a portable "run_on" program? Rusty Russell
2003-05-08  5:31 ` Linus Torvalds
2003-05-08  7:00   ` Rusty Russell
2003-05-08  7:00   ` Rusty Russell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome