Manual Pages for Linux CentOS command on man pthread_getschedparam
MyWebUniversity

Manual Pages for Linux CentOS command on man pthread_getschedparam

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

NAME

pthreadsetschedparam, pthreadgetschedparam - set/get scheduling pol‐ icy and parameters of a thread SYNOPSIS

#include pthreadsetschedparam(pthreadt thread, int policy, const struct schedparam *param); pthreadgetschedparam(pthreadt thread, int *policy, struct schedparam *param);

Compile and link with -pthread. DESCRIPTION The pthreadsetschedparam() function sets the scheduling policy and parameters of the thread thread. policy specifies the new scheduling policy for thread. The supported values for policy, and their semantics, are described in schedsetscheduler(2). The structure pointed to by param specifies the new scheduling parame‐ ters for thread. Scheduling parameters are maintained in the following structure: struct schedparam { int schedpriority; /* Scheduling priority */ }; As can be seen, only one scheduling parameter is supported. For details of the permitted ranges for scheduling priorities in each scheduling policy, see schedsetscheduler(2). The pthreadgetschedparam() function returns the scheduling policy and parameters of the thread thread, in the buffers pointed to by policy and param, respectively. The returned priority value is that set by the most recent pthreadsetschedparam(), pthreadsetschedprio(3), or pthreadcreate(3) call that affected thread. The returned priority does not reflect any temporary priority adjustments as a result of calls to any priority inheritance or priority ceiling functions (see, for example, pthreadmutexattrsetprioceiling(3) and pthreadmutex‐ attrsetprotocol(3)). RETURN VALUE On success, these functions return 0; on error, they return a nonzero error number. If pthreadsetschedparam() fails, the scheduling policy and parameters of thread are not changed. ERRORS Both of these functions can fail with the following error: ESRCH No thread with the ID thread could be found. pthreadsetschedparam() may additionally fail with the following errors: EINVAL policy is not a recognized policy, or param does not make sense for the policy. EPERM The caller does not have appropriate privileges to set the spec‐ ified scheduling policy and parameters.

POSIX.1-2001 also documents an ENOTSUP ("attempt was made to set the policy or scheduling parameters to an unsupported value") error for pthreadsetschedparam(). ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). ┌─────────────────────────┬───────────────┬─────────┐ │Interface │ Attribute │ Value │ ├─────────────────────────┼───────────────┼─────────┤

│pthreadsetschedparam(), │ Thread safety │ MT-Safe │ │pthreadgetschedparam() │ │ │ └─────────────────────────┴───────────────┴─────────┘ CONFORMING TO

POSIX.1-2001. NOTES For a description of the permissions required to, and the effect of, changing a thread's scheduling policy and priority, and details of the permitted ranges for priorities in each scheduling policy, see schedsetscheduler(2). EXAMPLE The program below demonstrates the use of pthreadsetschedparam() and pthreadgetschedparam(), as well as the use of a number of other sched‐

uling-related pthreads functions. In the following run, the main thread sets its scheduling policy to SCHEDFIFO with a priority of 10, and initializes a thread attributes object with a scheduling policy attribute of SCHEDRR and a scheduling priority attribute of 20. The program then sets (using pthreadattrsetinheritsched(3)) the inherit scheduler attribute of the thread attributes object to PTHREADEXPLICITSCHED, meaning that threads created using this attributes object should take their schedul‐ ing attributes from the thread attributes object. The program then creates a thread using the thread attributes object, and that thread displays its scheduling policy and priority.

$ su # Need privilege to set real-time scheduling policies Password:

# ./a.out -mf10 -ar20 -i e Scheduler settings of main thread policy=SCHEDFIFO, priority=10 Scheduler settings in 'attr' policy=SCHEDRR, priority=20 inheritsched is EXPLICIT Scheduler attributes of new thread policy=SCHEDRR, priority=20 In the above output, one can see that the scheduling policy and prior‐ ity were taken from the values specified in the thread attributes object. The next run is the same as the previous, except that the inherit scheduler attribute is set to PTHREADINHERITSCHED, meaning that threads created using the thread attributes object should ignore the scheduling attributes specified in the attributes object and instead take their scheduling attributes from the creating thread.

# ./a.out -mf10 -ar20 -i i Scheduler settings of main thread policy=SCHEDFIFO, priority=10 Scheduler settings in 'attr' policy=SCHEDRR, priority=20 inheritsched is INHERIT Scheduler attributes of new thread policy=SCHEDFIFO, priority=10 In the above output, one can see that the scheduling policy and prior‐ ity were taken from the creating thread, rather than the thread attributes object.

Note that if we had omitted the -i i option, the output would have been the same, since PTHREADINHERITSCHED is the default for the inherit scheduler attribute. Program source /* pthreadsschedtest.c */

#include

#include

#include

#include

#include

#define handleerroren(en, msg) \ do { errno = en; perror(msg); exit(EXITFAILURE); } while (0) static void usage(char *progname, char *msg) { if (msg != NULL) fputs(msg, stderr);

fprintf(stderr, "Usage: %s [options]\n", progname); fprintf(stderr, "Options are:\n");

#define fpe(msg) fprintf(stderr, "\t%s", msg); /* Shorter */

fpe("-a Set scheduling policy and priority in\n"); fpe(" thread attributes object\n"); fpe(" can be\n"); fpe(" f SCHEDFIFO\n"); fpe(" r SCHEDRR\n"); fpe(" o SCHEDOTHER\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 Set scheduling policy and priority on\n"); fpe(" main thread before pthreadcreate() call\n"); exit(EXITFAILURE); } static int getpolicy(char p, int *policy) { switch (p) { case 'f': *policy = SCHEDFIFO; return 1; case 'r': *policy = SCHEDRR; return 1; case 'o': *policy = SCHEDOTHER; return 1; default: return 0; } } static void displayschedattr(int policy, struct schedparam *param) {

printf(" policy=%s, priority=%d\n", (policy == SCHEDFIFO) ? "SCHEDFIFO" : (policy == SCHEDRR) ? "SCHEDRR" : (policy == SCHEDOTHER) ? "SCHEDOTHER" : "???",

param->schedpriority); } static void displaythreadschedattr(char *msg) { int policy, s; struct schedparam param; s = pthreadgetschedparam(pthreadself(), &policy, ¶m); if (s != 0) handleerroren(s, "pthreadgetschedparam");

printf("%s\n", msg); displayschedattr(policy, ¶m); } static void * threadstart(void *arg) { displaythreadschedattr("Scheduler attributes of new thread"); return NULL; } int main(int argc, char *argv[]) { int s, opt, inheritsched, usenullattrib, policy; pthreadt thread; pthreadattrt attr; pthreadattrt *attrp; char *attrschedstr, *mainschedstr, *inheritschedstr; struct schedparam param;

/* Process command-line options */ usenullattrib = 0; attrschedstr = NULL; mainschedstr = NULL; inheritschedstr = NULL;

while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) { switch (opt) { case 'a': attrschedstr = optarg; break; case 'A': usenullattrib = 1; break; case 'i': inheritschedstr = optarg; break; case 'm': mainschedstr = optarg; break; default: usage(argv[0], "Unrecognized option\n"); } } if (usenullattrib && (inheritschedstr != NULL || attrschedstr != 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 (mainschedstr != NULL) { if (!getpolicy(mainschedstr[0], &policy))

usage(argv[0], "Bad policy for main thread (-s)\n"); param.schedpriority = strtol(&mainschedstr[1], NULL, 0); s = pthreadsetschedparam(pthreadself(), policy, ¶m); if (s != 0) handleerroren(s, "pthreadsetschedparam"); } displaythreadschedattr("Scheduler settings of main thread"); printf("\n"); /* Initialize thread attributes object according to options */ attrp = NULL; if (!usenullattrib) { s = pthreadattrinit(&attr); if (s != 0) handleerroren(s, "pthreadattrinit"); attrp = &attr; } if (inheritschedstr != NULL) { if (inheritschedstr[0] == 'e') inheritsched = PTHREADEXPLICITSCHED; else if (inheritschedstr[0] == 'i') inheritsched = PTHREADINHERITSCHED; else

usage(argv[0], "Value for -i must be 'e' or 'i'\n"); s = pthreadattrsetinheritsched(&attr, inheritsched); if (s != 0) handleerroren(s, "pthreadattrsetinheritsched"); } if (attrschedstr != NULL) { if (!getpolicy(attrschedstr[0], &policy)) usage(argv[0],

"Bad policy for 'attr' (-a)\n"); param.schedpriority = strtol(&attrschedstr[1], NULL, 0); s = pthreadattrsetschedpolicy(&attr, policy); if (s != 0) handleerroren(s, "pthreadattrsetschedpolicy"); s = pthreadattrsetschedparam(&attr, ¶m); if (s != 0) handleerroren(s, "pthreadattrsetschedparam"); } /* If we initialized a thread attributes object, display the scheduling attributes that were set in the object */ if (attrp != NULL) { s = pthreadattrgetschedparam(&attr, ¶m); if (s != 0) handleerroren(s, "pthreadattrgetschedparam"); s = pthreadattrgetschedpolicy(&attr, &policy); if (s != 0) handleerroren(s, "pthreadattrgetschedpolicy"); printf("Scheduler settings in 'attr'\n"); displayschedattr(policy, ¶m); s = pthreadattrgetinheritsched(&attr, &inheritsched);

printf(" inheritsched is %s\n", (inheritsched == PTHREADINHERITSCHED) ? "INHERIT" : (inheritsched == PTHREADEXPLICITSCHED) ? "EXPLICIT" : "???"); printf("\n"); } /* Create a thread that will display its scheduling attributes */ s = pthreadcreate(&thread, attrp, &threadstart, NULL); if (s != 0) handleerroren(s, "pthreadcreate"); /* Destroy unneeded thread attributes object */ s = pthreadattrdestroy(&attr); if (s != 0) handleerroren(s, "pthreadattrdestroy"); s = pthreadjoin(thread, NULL); if (s != 0) handleerroren(s, "pthreadjoin"); exit(EXITSUCCESS); } SEE ALSO getrlimit(2), schedgetprioritymin(2), schedsetscheduler(2), pthreadattrinit(3), pthreadattrsetinheritsched(3), pthreadattrsetschedparam(3), pthreadattrsetschedpolicy(3), pthreadcreate(3), pthreadself(3), pthreadsetschedprio(3), pthreads(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2008-11-17 PTHREADSETSCHEDPARAM(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™