1 /* vi: set sw=4 ts=4: */
3 * taskset - retrieve or set a processes's CPU affinity
4 * Copyright (c) 2006 Bernhard Fischer
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #include <getopt.h> /* optind */
14 int taskset_main(int argc, char** argv)
16 cpu_set_t mask, new_mask;
19 const char *state = "current\0new";
20 char *p_opt = NULL, *aff = NULL;
22 ul = bb_getopt_ulflags(argc, argv, "+p:", &p_opt);
23 #define TASKSET_OPT_p (1)
25 if (ul & TASKSET_OPT_p) {
26 if (argc == optind+1) { /* -p <aff> <pid> */
30 argv += optind; /* me -p <arg> */
31 pid = bb_xgetularg10_bnd(p_opt, 1, ULONG_MAX); /* -p <pid> */
33 aff = *++argv; /* <aff> <cmd...> */
35 /* to_cpuset(bb_xgetularg_bnd(aff, 16, 1, ULONG_MAX), &new_mask); */
37 unsigned long l = bb_xgetularg_bnd(aff, 16, 1, ULONG_MAX);
40 while (i < CPU_SETSIZE && l >= (1<<i)) {
42 CPU_SET(i, &new_mask);
47 if (ul & TASKSET_OPT_p) {
49 if (sched_getaffinity(pid, sizeof (mask), &mask) < 0)
50 bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 'g', pid);
51 bb_printf("pid %d's %s affinity mask: %x\n", /* %x .. perhaps _FANCY */
53 if (!*argv) /* no new affinity given or we did print already, done. */
57 if (sched_setaffinity(pid, sizeof (new_mask), &new_mask))
58 bb_perror_msg_and_die("Failed to %cet pid %d's affinity", 's', pid);
59 if (ul & TASKSET_OPT_p) {
66 bb_perror_msg_and_die("%s", *argv);