pthread_setschedparam()--Set Target Thread Scheduling Parameters


  Syntax:
 #include <pthread.h>
 #include <sched.h>
 int pthread_setschedparam(pthread_t thread, int policy,
 			   const struct sched_param *param);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: No

The pthread_setschedparam() function sets the scheduling parameters of the target thread. The supported IBM® i scheduling policy is SCHED_OTHER. An attempt to set the policy to a value other than this cause the EINVAL error. The sched_priority field of the param parameter must range from PRIORITY_MIN to PRIORITY_MAX or the ENOTSUP error occurs.

All reserved fields in the scheduling parameters structure must be binary 0 or the EINVAL error occurs.

Note: Do not use pthread_setschedparam() to set the priority of a thread if you also use another mechanism (outside of the pthread APIs) to set the priority of a thread. If you do, pthread_getschedparam() returns only that information that was set by the pthread interfaces (pthread_setschedparam() or modification of the thread attribute using pthread_attr_setschedparam()).


Authorities and Locks

None.


Parameters

thread
(Input) Pthread handle of the target thread

policy
(Input) Scheduling policy (must be SCHED_OTHER)

param
(Input) Scheduling parameters

Return Value

0
pthread_setschedparam() was successful.

value
pthread_setschedparam() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_setschedparam() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.

[ENOTSUP]

The value specified for the priority argument is not supported.


Related Information


Example

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#define _MULTI_THREADED
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include "check.h"

#define  BUMP_PRIO     1
int      thePriority = 0;

int showSchedParam(pthread_t thread)
{
  struct sched_param   param;
  int                  policy;
  int                  rc;

  printf("Get scheduling parameters\n");
  rc = pthread_getschedparam(thread, &policy, &param);
  checkResults("pthread_getschedparam()\n", rc);

  printf("The thread scheduling parameters indicate:\n"
         "priority = %d\n", param.sched_priority);
  return param.sched_priority;
}

void *threadfunc(void *parm)
{
  int           rc;

  printf("Inside secondary thread\n");
  thePriority = showSchedParam(pthread_self());
  sleep(5);  /* Sleep is not a very robust way to serialize threads */
  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  struct sched_param    param;
  int                   policy = SCHED_OTHER;
  int                   theChangedPriority=0;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create thread using default attributes\n");
  rc = pthread_create(&thread, NULL, threadfunc, NULL);
  checkResults("pthread_create()\n", rc);

  sleep(2);  /* Sleep is not a very robust way to serialize threads */

  memset(&param, 0, sizeof(param));
  /* Bump the priority of the thread a small amount */
  if (thePriority - BUMP_PRIO >= PRIORITY_MIN_NP) {
      param.sched_priority = thePriority - BUMP_PRIO;
  }

  printf("Set scheduling parameters, prio=%d\n",
         param.sched_priority);
  rc = pthread_setschedparam(thread, policy, &param);
  checkResults("pthread_setschedparam()\n", rc);

  /* Let the thread fill in its own last priority */
  theChangedPriority = showSchedParam(thread);
 
  if (thePriority == theChangedPriority ||
      param.sched_priority != theChangedPriority) {
      printf("The thread did not get priority set correctly, "
             "first=%d last=%d expected=%d\n",
             thePriority, theChangedPriority, param.sched_priority);
      exit(1);
  }
  
  sleep(5);  /* Sleep is not a very robust way to serialize threads */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPSSP0
Create thread using default attributes
Inside secondary thread
Get scheduling parameters
The thread scheduling parameters indicate:
priority = 0
Set scheduling parameters, prio=-1
Get scheduling parameters
The thread scheduling parameters indicate:
priority = -1
Main completed


API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]