{"id":4676,"date":"2014-03-30T14:32:19","date_gmt":"2014-03-30T14:32:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-it-possible-to-check-if-and-how-often-a-program-was-dispatched-scheduled-in-c-under-linux-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:32:19","modified_gmt":"2014-03-30T14:32:19","slug":"is-it-possible-to-check-if-and-how-often-a-program-was-dispatched-scheduled-in-c-under-linux-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-it-possible-to-check-if-and-how-often-a-program-was-dispatched-scheduled-in-c-under-linux-collection-of-common-programming-errors\/","title":{"rendered":"Is it possible to check if (and how often) a program was dispatched\/scheduled in C under Linux-Collection of common programming errors"},"content":{"rendered":"<p>On Linux you can use getrusage function.<\/p>\n<pre><code>#include \n#include \n#include \n\nint times_dispatched(long *vol, long *invol) {\n    struct rusage usage;\n    int err;\n\n    if ((err = getrusage(RUSAGE_SELF, &amp;usage)) != 0) {\n        return err;\n    }\n\n    *vol   = usage.ru_nvcsw;\n    *invol = usage.ru_nivcsw;\n\n    return 0;\n}\n<\/code><\/pre>\n<p>Test application:<\/p>\n<pre><code>#include \n#include \n\n#define LOOPS 100000000\n\nstatic void loop(volatile unsigned int count) {\n    while(count--) { }\n}\n\nint main(void) {\n    long vol, invol;\n\n    loop(LOOPS);\n\n    if (times_dispatched(&amp;vol, &amp;invol) != 0) {\n        fprintf(stderr, \"Unable to get dispatch stats\");\n        exit(1);\n    }\n    printf(\"Context switches: %ld voluntarily, %ld involuntarily\\n\",\n        vol, invol);\n\n    return 0;\n}\n<\/code><\/pre>\n<p>Output from Ideone:<\/p>\n<pre><code>Context switches: 3 voluntarily, 283 involuntarily\n<\/code><\/pre>\n<p>P.S. I wonder why it shows non-zero voluntary switches, may be that is because of using Ideone&#8230; On my desktop it is always zero, as expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On Linux you can use getrusage function. #include #include #include int times_dispatched(long *vol, long *invol) { struct rusage usage; int err; if ((err = getrusage(RUSAGE_SELF, &amp;usage)) != 0) { return err; } *vol = usage.ru_nvcsw; *invol = usage.ru_nivcsw; return 0; } Test application: #include #include #define LOOPS 100000000 static void loop(volatile unsigned int count) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4676","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4676"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4676\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}