Reputation: 2051
I am using gcc c++ 4.7 on Debian 7. I want to set some priorities for my threads. Looks like I have to do it through pthread. I am getting confused by the scheduler policy and priority in pthread.
Q1:
I use sched_setscheduler
in my c++ code to set the thread scheduler to SCHED_RR
. Will all the threads in this process use this real time scheduler? Or I can have different scheduler policy in one process for different threads?
Q2:
Does the thread priority take effect only inside the process or across multiple process? E.g. I have two processes both using SCHED_RR
. One has a thread with priority 99, the other has a thread with priority 98. Does the former thread has a higher priority over the latter? What if the threads use different scheduler, how to compare their priority?
Q3:
What "default" number should I use for a scheduler's priority? E.g. I use the code below:
struct sched_param param;
param.sched_priority = default_priority;
sched_setscheduler(0, SCHED_RR, ¶m));
What value should I set to the default_priority
? I have some high priority thread, normal priority thread and some low priority thread in my program. Among 1-99, what number should I use for the priorities?
Q4:
Process priority and thread priority mixed. For example, I can use nice to set the process priority. If one process has lower process priority but in my code I set its thread to a high priority. Does this override the process priority setting?
Upvotes: 4
Views: 1502
Reputation: 2051
I googled around and read various documents. I think I can answer my own question here.
Pthread has a contention scope attribute. It could be PTHREAD_SCOPE_SYSTEM or PTHREAD_SCOPE_PROCESS. It does not require implementation on both of them. Linux only supportes PTHREAD_SCOPE_SYSTEM, which means all threads from all processes compete with each other. Also, in Linux, thread is essentially a lightweight process. The process scheduler does not treat process and thread differently in scheduling.
Answers. Q1: Threads in the same process can have different scheduling policies and priorities.
Q2: Threads compete across processes.
Q3: I can use some arbitrary numbers. Each priority, from 1 to 99, will have its own queue in scheduling.
Q4: The nice value is used in the Linux default SCHED_OTHER policy. When real time policy like SCHED_RR or SCHED_FIFO is used for a thread, the nice value has no effect. Since the min priority of SCHED_RR and SCHED_FIFO is 1 and SCHED_OTHER's priority can only be 0. So threads with real time policy always have scheduling preferences than non real time ones.
The answers apply to Linux only. Other OS like BSD, Solaris may have different pthread implementations.
Upvotes: 2