0

I have created a thread in my Linux application, using pthread_create(). I would like to let this thread run at very very low priority as there are some real time threads running in the same application. The following is the code I have in the thread function itself:

        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

        /* Trying to set lowest priority possible for this thread */
        param.sched_priority = 0;
        ret = pthread_setschedparam(pthread_self(), SCHED_IDLE, &param);
        if (!ret)
                printf("Successfully set sched policy of thread\n");

I want to confirm if the above code is okay or not. Does it guarantee that my thread will not be given higher priority when compared to the other real time threads. Please suggest if any changes are required. FYI, the code runs on an embedded platform.

  • "*Does it guarantee that my thread will not be given higher priority when compared to the other real time threads.*" so you do not want to have this low prio thread be run at all? – alk Sep 18 '18 at 09:46
  • Ofcourse I want it to be run – OpenSourceEnthusiast Sep 18 '18 at 09:48
  • Your program's threads are not the only threads that will be running on a Linux host. If you want a thread in your program to have a higher priority relative to _all_ of the threads that might be running on the platform, then you will have to boost its priority. It won't be enough to just lower the priorities of the few threads that your program controls. – Ohm's Lawman Sep 18 '18 at 13:30

2 Answers2

0

I assume you have used Default Linux time-sharing scheduling. as you have added param.sched_priority = 0;

for that, you can check Here

which states that

   SCHED_OTHER can be used at only static priority 0 (i.e., threads
   under real-time policies always have priority over SCHED_OTHER pro‐
   cesses).  SCHED_OTHER is the standard Linux time-sharing scheduler
   that is intended for all threads that do not require the special
   real-time mechanisms.
   The thread to run is chosen from the static priority 0 list based on
   a dynamic priority that is determined only inside this list.  The
   dynamic priority is based on the nice value (see below) and is
   increased for each time quantum the thread is ready to run, but
   denied to run by the scheduler.  This ensures fair progress among all
   SCHED_OTHER threads.
ixnisarg
  • 117
  • 2
  • 8
  • I want to use SCHED_IDLE Setting priority=0 does it conflict i.e by setting both SCHED_IDLE and priority=0 I am eventually giving this process a higher priority? – OpenSourceEnthusiast Sep 18 '18 at 09:58
0

From manual page of pthread_create

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

you can create your own attribute and set its properties, including priority as you did.

param.sched_priority = 0;

And from the manual page of pthread_setschedparam

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

The pthread_getschedparam() and pthread_setschedparam() allow the scheduling policy and scheduling parameters of individual threads within a multi-threaded process to be retrieved and set. For SCHED_FIFO and SCHED_RR, the only required member of the sched_param structure is the priority sched_priority. For SCHED_OTHER, the affected scheduling parameters are implementation-dependent.

If the pthread_setschedparam() function fails, no scheduling parameters will be changed for the target thread.

So above it clearly stated that you can set & retrieve the scheduling parameters for RR and FIFO scheduling policy. Also in case of SCHED_OTHER its implementation-dependent.

After running the thread which changes the priority, do run the command and check. For e.g run the below command

ps -T -l [PID]

and check the priority with respect to PID.

Also read @paxdiablo answer here about linux threads scheduling priorities.

Achal
  • 11,821
  • 2
  • 15
  • 37