Hi all,

My program have two threads (parent thread & child thread).
I want to set a high priority to my child thread when it is created. So I just follow the example given in the follow ()


**************************************************

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

#define BUMP_PRIO 1
static int thePriority = 0;

void showSchedParam(pthread_attr_t *a)
{
  int                 rc=0;
  struct sched_param  param;

  printf("Get scheduling parameters\n");
  rc = pthread_attr_getschedparam(a, &param);
  checkResults("pthread_attr_getschedparam()\n", rc);

  printf("The thread attributes object indicates priority: %d\n",
         param.sched_priority);
  thePriority = param.sched_priority;
  return;
}

int main(int argc, char **argv)
{
  pthread_t             thread;
  int                   rc=0;
  pthread_attr_t        pta;
  struct sched_param    param;

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

  printf("Create a thread attributes object\n");
  rc = pthread_attr_init(&pta);
  checkResults("pthread_attr_init()\n", rc);

  showSchedParam(&pta);

  memset(&param, 0, sizeof(param));
  if (thePriority + BUMP_PRIO <= PRIORITY_MAX_NP) {
    param.sched_priority = thePriority + BUMP_PRIO;
  }

  printf("Setting scheduling parameters\n");
  rc = pthread_attr_setschedparam(&pta, &param);
  checkResults("pthread_attr_setschedparam()\n", rc);

  showSchedParam(&pta);

  printf("Destroy thread attributes object\n");
  rc = pthread_attr_destroy(&pta);
  checkResults("pthread_attr_destroy()\n", rc);

  printf("Main completed\n");
  return 0;
}

**************************************************

but in the above example, there is no any new thread created. So I just change this as follow



**************************************************

pthread_t client_thread;
int client_rc;
int (*client_fp)(void *);	

struct sched_param param;
int rc;

pthread_attr_t attr;
pthread_mutex_init(&mutex, NULL);  
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

showSchedParam(&attr);
memset(&param, 0, sizeof(param));
param.sched_priority = 10;

rc = pthread_attr_setschedparam(&attr, &param);
showSchedParam(&attr);

    client_fp = calc_available_bandwidth;	
    client_rc = pthread_create(&client_thread, &attr, client_fp, (void *)dst_addres);
    if (client_rc)
    {
      fprintf(stderr, "ERROR; return code from pthread_create()[CLIENT] is %d\n", client_rc);
      exit(-1);
    }

showSchedParam(&attr);
pthread_attr_destroy(&attr);

**************************************************

But still I could not get the result that I am expected to have (High priority for child thread)

please can anyone tell me whether this is correct or not (setting a priority when create a new thread)

Also I would like to know the value should I set for "param.sched_priority " to have high priority....? (Positive values or negative values)

Dani AI

Generated

Short answer: your attr changes are being ignored. By default, new threads inherit the parent’s scheduler (SCHED_OTHER) and, on most POSIX systems, SCHED_OTHER ignores sched_priority. To make priority take effect you must: (1) opt out of inheritance, (2) pick a real-time policy, and (3) set a priority within that policy’s valid range. Also expect EPERM unless the process has the right privileges (e.g., CAP_SYS_NICE or an rtprio limit). @Ancient Dragon’s warning is spot on: real-time priorities can starve other work if you are not careful.

Here is the minimal sequence that actually applies a higher priority at create time and verifies it:

pthread_attr_t attr;
pthread_attr_init(&attr);

pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);   /* key */
pthread_attr_setschedpolicy(&attr, SCHED_RR);                  /* or SCHED_FIFO */

int min = sched_get_priority_min(SCHED_RR);
int max = sched_get_priority_max(SCHED_RR);

struct sched_param sp = { .sched_priority = max - 1 };         /* never hard-code */
pthread_attr_setschedparam(&attr, &sp);

pthread_t tid;
int rc = pthread_create(&tid, &attr, calc_available_bandwidth, (void*)dst_address);
pthread_attr_destroy(&attr);

/* Optional: confirm what the kernel applied */
int policy; struct sched_param cur;
pthread_getschedparam(tid, &policy, &cur);
printf("policy=%d, prio=%d\n", policy, cur.sched_priority);

Notes:

  • Valid priorities are non-negative and must be between sched_get_priority_min(policy) and sched_get_priority_max(policy). Negative values are invalid.
  • If you stay with SCHED_OTHER, sched_priority is effectively 0. To bias CPU share there, adjust niceness (nice()/setpriority()), but that is cooperative and weaker than real-time.
  • Keep RT priorities modest, add blocking points (I/O, sleep, condition waits), and consider using SCHED_RR with a sensible priority rather than max. ’s suggestion of equal priorities is often safer unless you truly need deterministic latency.

Recommended Answers

All 3 Replies

Increasing priority for a thread is not a good idea because it may halt all other threads, including the operating system itself.

So in which type of situations do we use "pthread_attr_setschedparam()" ......?

Please tell me what should I do to solve my problem....?

Try setting them with the same priority. Does this work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.