pthread_setschedparam

PTHREAD_SETSCHEDPARAM(3)   Linux Programmer's Manual  PTHREAD_SETSCHEDPARAM(3)



åå
       pthread_setschedparam, pthread_getschedparam - ã¹ã¬ããã®
       ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨ãã©ã¡ã¼ã¿ãè¨å®/åå¾ãã

æ¸å¼
       #include <pthread.h>

       pthread_setschedparam(pthread_t thread, int policy,
                             const struct sched_param *param);
       pthread_getschedparam(pthread_t thread, int *policy,
                             struct sched_param *param);

       -pthread ã§ã³ã³ãã¤ã«ãã¦ãªã³ã¯ããã

説æ
       pthread_setschedparam() é¢æ°ã¯ãã¹ã¬ãã thread ã®
       ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨ã¹ã±ã¸ã¥ã¼ãªã³ã°ãã©ã¡ã¼ã¿ãè¨å®ããã

       policy 㯠thread ã®æ°ããã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ãæå®ããã policy
       ã«æå®ã§ããå¤ã¨ãã®æå³ã¯ sched_setscheduler(2) 㧠説æããã¦ããã

       param ãæãæ§é ä½ã¯ thread ã®æ°ããã¹ã±ã¸ã¥ã¼ãªã³ã°ãã©ã¡ã¼ã¿ã
       æå®ãããã¹ã±ã¸ã¥ã¼ãªã³ã°ãã©ã¡ã¼ã¿ã¯ä»¥ä¸ã®æ§é ä½ã§ç®¡çãããã

           struct sched_param {
               int sched_priority;     /* Scheduling priority */
           };

       è¦ã¦åããéãããµãã¼ãããã¦ããã¹ã±ã¸ã¥ã¼ãªã³ã°ãã©ã¡ã¼ã¿ã¯ä¸ã¤ã ã
       ã§ãããåã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã§è¨±å¯ãããã¹ã±ã¸ã¥ã¼ãªã³ã°åªå度ã®
       詳細ã«ã¤ãã¦ã¯ãsched_setscheduler(2) ãåç§ã®ãã¨ã

       pthread_getschedparam() é¢æ°ã¯ãã¹ã¬ãã thread ã®
       ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨ãã©ã¡ã¼ã¿ãã ãããã policy 㨠param
       ãæããããã¡ã«å¥ãã¦è¿ãã è¿ãããåªå度ã®å¤ã¯ãæãæè¿å®è¡ãã thread ã«å½±é¿ãä¸ãã
       pthread_setschedparam(), pthread_setschedprio, pthread_create ã§è¨‐
       å®ãããå¤ã¨ãªãã è¿ãããåªå度ã¯ãåªå度ã®ç¶æ¿ãåªå度ã®ä¸éãè¨å®ããé¢æ° (ä¾ãã°
       pthread_mutexattr_setprioceiling(3) ã pthread_mutexattr_setprotocol(3)
       ãåç§) ã®å¼ã³åºãã®çµæ è¡ãããä¸æçãªåªå度ã®èª¿æ´ã®å½±é¿ãåããªãã

è¿ãå¤
       æåããã¨ããããã®é¢æ°ã¯ 0 ãè¿ãã ã¨ã©ã¼ã®å ´åã0 以å¤ã®ã¨ã©ã¼çªå·ãè¿ãã
       pthread_setschedparam() ã失æããå ´åã thread ã®
       ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨ãã©ã¡ã¼ã¿ã¯å¤æ´ãããªãã

ã¨ã©ã¼
       ãããã®é¢æ°ã¯ã©ã¡ãã以ä¸ã®ã¨ã©ã¼ã§å¤±æããå ´åãããã

       ESRCH  ID ã thread ã®ã¹ã¬ãããè¦ã¤ãããªãã£ãã

       pthread_setschedparam() ã¯ããã«ä»¥ä¸ã®ã¨ã©ã¼ã§å¤±æããå ´åãããã

       EINVAL policy ãèªèã§ããªãããªã·ã¼ã§ãããã param ã policy
              ã§ã¯æå³ãæããªãå¤ã§ããã

       EPERM  å¼ã³åºãå´ããæå®ãããã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ããã©ã¡ã¼ã¿ãè¨å®ãã
              ã®ã«å¿è¦ãªç¹æ¨©ãæããªãã

       POSIX.1-2001 ã§ã¯ã pthread_setschedparam() ã«é¢ã㦠ã¨ã©ã¼ ENOTSUP
       ("ãµãã¼ãããã¦ããªãå¤ãã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã ãã©ã¡ã¼ã¿ã«è¨å®ãããã¨ãã")
       ã追å ã§è¦å®ããã¦ããã

æºæ
       POSIX.1-2001.

注æ
       ã¹ã¬ããã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ãåªå度ãå¤æ´ããããã«å¿è¦ãªè¨±å¯ã
       å¤æ´ããå ´åã®å½±é¿ãããã³åã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã§èªããããåªå度
       ã®ç¯å²ã®è©³ç´°ã«ã¤ãã¦ã¯ã sched_setscheduler(2) ãåç§ã

ä¾
       以ä¸ã®ããã°ã©ã 㯠pthread_setschedparam() 㨠pthread_getschedparam()
       ãã¹ã±ã¸ã¥ã¼ãªã³ã°ã«é¢é£ãã pthreads ã®
       ä»ã®ãããããªé¢æ°ã®ä½¿ç¨ä¾ã示ããã®ã§ããã

       以ä¸ã®å®è¡ä¾ã§ã¯ãã¡ã¤ã³ã¹ã¬ããã¯ãèªåã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã åªå度
       10 ã® SCHED_FIFO ãè¨å®ããã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã
       ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼å±æ§ SCHED_RR ã¨ã¹ã±ã¸ã¥ã¼ãªã³ã°åªå度 å±æ§ 20
       ã§åæåããã 次ã«ããã®ããã°ã©ã 㯠(pthread_attr_setinheritsched(3) ã使ã£ã¦)
       ãã®ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã® inherit scheduler å±æ§ã«
       PTHREAD_EXPLICIT_SCHED ãè¨å®ãããPTHREAD_EXPLICIT_SCHED ã¯ã
       ãã®ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã使ã£ã¦ä½æãããã¹ã¬ããã¯ã¹ã¬ããå±æ§
       ãªãã¸ã§ã¯ãããã¹ã±ã¸ã¥ã¼ãªã³ã°å±æ§ãåå¾ãã¦ä½¿ããã¨ãæå³ããã
       ããããããã®ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã使ã£ã¦ã¹ã¬ãããä½æãã
       ä½æããã¹ã¬ããã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨åªå度ã表示ããã

           $ su      # Need privilege to set real-time scheduling policies
           Password:
           # ./a.out -mf10 -ar20 -i e
           Scheduler settings of main thread
               policy=SCHED_FIFO, priority=10

           Scheduler settings in 'attr'
               policy=SCHED_RR, priority=20
               inheritsched is EXPLICIT

           Scheduler attributes of new thread
               policy=SCHED_RR, priority=20

       ä¸è¨ã®åºåã§ã¯ãã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨åªå度ãã¹ã¬ããå±æ§
       ãªãã¸ã§ã¯ãã§æå®ãããå¤ããåããã¦ãããã¨ãåããã

       次ã®å®è¡ä¾ã¯åã®ãã®ã¨åãã ãã inherit scheduler å±æ§ã
       PTHREAD_INHERIT_SCHED ã«è¨å®ãããç¹ãç°ãªãã PTHREAD_INHERIT_SCHED
       ã¯ããã®ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãã使ã£ã¦ä½æ
       ãããã¹ã¬ããã¯ãã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãããã¹ã±ã¸ã¥ã¼ãªã³ã°å±æ§ã
       ç¡è¦ãã代ããã«å¼ã³åºããã¹ã¬ããããã¹ã±ã¸ã¥ã¼ãªã³ã°å±æ§ãåå¾ãã ãã¨ãæå³ããã

           # ./a.out -mf10 -ar20 -i i
           Scheduler settings of main thread
               policy=SCHED_FIFO, priority=10

           Scheduler settings in 'attr'
               policy=SCHED_RR, priority=20
               inheritsched is INHERIT

           Scheduler attributes of new thread
               policy=SCHED_FIFO, priority=10

       ä¸è¨ã®åºåã§ã¯ãã¹ã±ã¸ã¥ã¼ãªã³ã°ããªã·ã¼ã¨åªå度ãã
       ã¹ã¬ããå±æ§ãªãã¸ã§ã¯ãããã§ã¯ãªãã ã¹ã¬ãããä½æããã¹ã¬ããããåããã¦ãããã¨ãåããã

       ãªãã -i i ãçç¥ããå ´åã§ãã PTHREAD_INHERIT_SCHED ã inherit scheduler
       å±æ§ã®ããã©ã«ãå¤ãªã®ã§ã åºåã¯åãã«ãªãã

   ããã°ã©ã ã®ã½ã¼ã¹

       /* pthreads_sched_test.c */

       #include <pthread.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <errno.h>

       #define handle_error_en(en, msg) \
               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

       static void
       usage(char *prog_name, char *msg)
       {
           if (msg != NULL)
               fputs(msg, stderr);

           fprintf(stderr, "Usage: %s [options]\n", prog_name);
           fprintf(stderr, "Options are:\n");
       #define fpe(msg) fprintf(stderr, "\t%s", msg);          /* Shorter */
           fpe("-a<policy><prio> Set scheduling policy and priority in\n");
           fpe("                 thread attributes object\n");
           fpe("                 <policy> can be\n");
           fpe("                     f  SCHED_FIFO\n");
           fpe("                     r  SCHED_RR\n");
           fpe("                     o  SCHED_OTHER\n");
           fpe("-A               Use default thread attributes object\n");
           fpe("-i {e|s}         Set inherit scheduler attribute to\n");
           fpe("                 'explicit' or 'inherit'\n");
           fpe("-m<policy><prio> Set scheduling policy and priority on\n");
           fpe("                 main thread before pthread_create() call\n");
           exit(EXIT_FAILURE);
       }

       static int
       get_policy(char p, int *policy)
       {
           switch (p) {
           case 'f': *policy = SCHED_FIFO;     return 1;
           case 'r': *policy = SCHED_RR;       return 1;
           case 'o': *policy = SCHED_OTHER;    return 1;
           default:  return 0;
           }
       }

       static void
       display_sched_attr(int policy, struct sched_param *param)
       {
           printf("    policy=%s, priority=%d\n",
                   (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
                   (policy == SCHED_RR)    ? "SCHED_RR" :
                   (policy == SCHED_OTHER) ? "SCHED_OTHER" :
                   "???",
                   param->sched_priority);
       }

       static void
       display_thread_sched_attr(char *msg)
       {
           int policy, s;
           struct sched_param param;

           s = pthread_getschedparam(pthread_self(), &policy, &param);
           if (s != 0)
               handle_error_en(s, "pthread_getschedparam");

           printf("%s\n", msg);
           display_sched_attr(policy, &param);
       }

       static void *
       thread_start(void *arg)
       {
           display_thread_sched_attr("Scheduler attributes of new thread");

           return NULL;
       }

       int
       main(int argc, char *argv[])
       {
           int s, opt, inheritsched, use_null_attrib, policy;
           pthread_t thread;
           pthread_attr_t attr;
           pthread_attr_t *attrp;
           char *attr_sched_str, *main_sched_str, *inheritsched_str;
           struct sched_param param;

           /* Process command-line options */

           use_null_attrib = 0;
           attr_sched_str = NULL;
           main_sched_str = NULL;
           inheritsched_str = NULL;

           while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) {
               switch (opt) {
               case 'a': attr_sched_str = optarg;      break;
               case 'A': use_null_attrib = 1;          break;
               case 'i': inheritsched_str = optarg;    break;
               case 'm': main_sched_str = optarg;      break;
               default:  usage(argv[0], "Unrecognized option\n");
               }
           }

           if (use_null_attrib &&
                   (inheritsched_str != NULL || attr_sched_str != NULL))
               usage(argv[0], "Can't specify -A with -i or -a\n");

           /* Optionally set scheduling attributes of main thread,
              and display the attributes */

           if (main_sched_str != NULL) {
               if (!get_policy(main_sched_str[0], &policy))
                   usage(argv[0], "Bad policy for main thread (-s)\n");
               param.sched_priority = strtol(&main_sched_str[1], NULL, 0);

               s = pthread_setschedparam(pthread_self(), policy, &param);
               if (s != 0)
                   handle_error_en(s, "pthread_setschedparam");
           }

           display_thread_sched_attr("Scheduler settings of main thread");
           printf("\n");

           /* Initialize thread attributes object according to options */

           attrp = NULL;

           if (!use_null_attrib) {
               s = pthread_attr_init(&attr);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_init");
               attrp = &attr;
           }

           if (inheritsched_str != NULL) {
               if (inheritsched_str[0] == 'e')
                   inheritsched = PTHREAD_EXPLICIT_SCHED;
               else if (inheritsched_str[0] == 'i')
                   inheritsched = PTHREAD_INHERIT_SCHED;
               else
                   usage(argv[0], "Value for -i must be 'e' or 'i'\n");

               s = pthread_attr_setinheritsched(&attr, inheritsched);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setinheritsched");
           }

           if (attr_sched_str != NULL) {
               if (!get_policy(attr_sched_str[0], &policy))
                   usage(argv[0],
                           "Bad policy for 'attr' (-a)\n");
               param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);

               s = pthread_attr_setschedpolicy(&attr, policy);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setschedpolicy");
               s = pthread_attr_setschedparam(&attr, &param);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_setschedparam");
           }

           /* If we initialized a thread attributes object, display
              the scheduling attributes that were set in the object */

           if (attrp != NULL) {
               s = pthread_attr_getschedparam(&attr, &param);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_getschedparam");
               s = pthread_attr_getschedpolicy(&attr, &policy);
               if (s != 0)
                   handle_error_en(s, "pthread_attr_getschedpolicy");

               printf("Scheduler settings in 'attr'\n");
               display_sched_attr(policy, &param);

               s = pthread_attr_getinheritsched(&attr, &inheritsched);
               printf("    inheritsched is %s\n",
                       (inheritsched == PTHREAD_INHERIT_SCHED)  ? "INHERIT" :
                       (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
                       "???");
               printf("\n");
           }

           /* Create a thread that will display its scheduling attributes */

           s = pthread_create(&thread, attrp, &thread_start, NULL);
           if (s != 0)
               handle_error_en(s, "pthread_create");

           /* Destroy unneeded thread attributes object */

           s = pthread_attr_destroy(&attr);
           if (s != 0)
               handle_error_en(s, "pthread_attr_destroy");

           s = pthread_join(thread, NULL);
           if (s != 0)
               handle_error_en(s, "pthread_join");

           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       getrlimit(2), sched_get_priority_min(2), sched_setscheduler(2),
       pthread_attr_init(3), pthread_attr_setinheritsched(3),
       pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3),
       pthread_create(3), pthread_self(3), pthread_setschedprio(3),
       pthreads(7)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2008-11-17          PTHREAD_SETSCHEDPARAM(3)