{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/119","repository_url":"https://api.github.com/repos/Microsoft/BashOnWindows","labels_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/119/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/119/comments","events_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/119/events","html_url":"https://github.com/Microsoft/BashOnWindows/issues/119","id":147336231,"number":119,"title":"sched_{get,set}affinity APIs broken","user":{"login":"tycho","id":29616,"avatar_url":"https://avatars.githubusercontent.com/u/29616?v=3","gravatar_id":"","url":"https://api.github.com/users/tycho","html_url":"https://github.com/tycho","followers_url":"https://api.github.com/users/tycho/followers","following_url":"https://api.github.com/users/tycho/following{/other_user}","gists_url":"https://api.github.com/users/tycho/gists{/gist_id}","starred_url":"https://api.github.com/users/tycho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tycho/subscriptions","organizations_url":"https://api.github.com/users/tycho/orgs","repos_url":"https://api.github.com/users/tycho/repos","events_url":"https://api.github.com/users/tycho/events{/privacy}","received_events_url":"https://api.github.com/users/tycho/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-11T07:16:01Z","updated_at":"2016-04-11T07:47:42Z","closed_at":null,"body":"Simple test case follows.\r\n\r\n```c\r\n#define _GNU_SOURCE\r\n#include <errno.h>\r\n#include <stdio.h>\r\n#include <stdint.h>\r\n#include <sched.h>\r\n\r\nint main(int argc, char **argv)\r\n{\r\n        cpu_set_t set;\r\n        uint32_t desired;\r\n\r\n        CPU_ZERO(&set);\r\n\r\n        if (sched_getaffinity(0, sizeof(set), &set))\r\n        {\r\n                printf(\"sched_getaffinity failed with %d\\n\", errno);\r\n        }\r\n        printf(\"current affinity: %08x\\n\", *(uint32_t *)&set);\r\n\r\n        desired = 1;\r\n        if (sched_setaffinity(0, sizeof(desired), (cpu_set_t *)&desired))\r\n        {\r\n                printf(\"sched_getaffinity failed with %d\\n\", errno);\r\n        }\r\n\r\n        if (sched_getaffinity(0, sizeof(set), &set))\r\n        {\r\n                printf(\"sched_getaffinity failed with %d\\n\", errno);\r\n        }\r\n        printf(\"current affinity: %08x\\n\", *(uint32_t *)&set);\r\n\r\n        return 0;\r\n}\r\n```\r\n\r\nYes, I recognize the above is abusing type-punning and such, but it's a repro from a real-world program that was breaking for me.\r\n\r\nBare metal Linux:\r\n```\r\n$ ./test\r\ncurrent affinity: 0000000f\r\ncurrent affinity: 00000001\r\n```\r\n\r\nBash on Windows:\r\n```\r\n$ ./test\r\ncurrent affinity: 0000000f\r\nsched_getaffinity failed with 22\r\ncurrent affinity: ffeb000f\r\n```"},"comment":{"url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/comments/208208997","html_url":"https://github.com/Microsoft/BashOnWindows/issues/119#issuecomment-208208997","issue_url":"https://api.github.com/repos/Microsoft/BashOnWindows/issues/119","id":208208997,"user":{"login":"tycho","id":29616,"avatar_url":"https://avatars.githubusercontent.com/u/29616?v=3","gravatar_id":"","url":"https://api.github.com/users/tycho","html_url":"https://github.com/tycho","followers_url":"https://api.github.com/users/tycho/followers","following_url":"https://api.github.com/users/tycho/following{/other_user}","gists_url":"https://api.github.com/users/tycho/gists{/gist_id}","starred_url":"https://api.github.com/users/tycho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tycho/subscriptions","organizations_url":"https://api.github.com/users/tycho/orgs","repos_url":"https://api.github.com/users/tycho/repos","events_url":"https://api.github.com/users/tycho/events{/privacy}","received_events_url":"https://api.github.com/users/tycho/received_events","type":"User","site_admin":false},"created_at":"2016-04-11T07:47:42Z","updated_at":"2016-04-11T07:47:42Z","body":"Ew, this is not nice:\r\n\r\n```\r\nsteven@localhost ~\r\n$ cat test.c\r\n#define _GNU_SOURCE\r\n#include <errno.h>\r\n#include <pthread.h>\r\n#include <stdio.h>\r\n#include <stdint.h>\r\n#include <sched.h>\r\n\r\nint main(int argc, char **argv)\r\n{\r\n        cpu_set_t set;\r\n\r\n        CPU_ZERO(&set);\r\n        CPU_SET(0, &set);\r\n\r\n        pthread_setaffinity_np(pthread_self(), sizeof(set), &set);\r\n\r\n        if (sched_getaffinity(0, sizeof(set), &set))\r\n        {\r\n                printf(\"sched_getaffinity failed with %d\\n\", errno);\r\n        }\r\n        printf(\"current affinity: %08x\\n\", *(uint32_t *)&set);\r\n\r\n        CPU_ZERO(&set);\r\n        CPU_SET(1, &set);\r\n\r\n        pthread_setaffinity_np(pthread_self(), sizeof(set), &set);\r\n\r\n        if (sched_getaffinity(0, sizeof(set), &set))\r\n        {\r\n                printf(\"sched_getaffinity failed with %d\\n\", errno);\r\n        }\r\n        printf(\"current affinity: %08x\\n\", *(uint32_t *)&set);\r\n\r\n        return 0;\r\n}\r\nsteven@localhost ~\r\n$ gcc -pthread -o test test.c\r\nsteven@localhost ~\r\n$ ./test\r\ncurrent affinity: 00000001\r\ncurrent affinity: 00000082\r\n```\r\nI'm able to successfully set the affinity mask with a proper cpu_set_t structure, but only *once*. If I change the affinity again, then sched_getaffinity gives a bogus result."}}