Thread in Linux

  1. If I create a highest priority thread, is that thread will run immediately ?

I think so.. The next thread should be you……

 

OK, first , let's understand that if you want to specify a priority to a thread ,it means that it is a real time thread. And of course , the real time thread has higher priority than any non-real-time thread.

If the system has no real time thread , and you create a real time thread , it will be scheduled immediately AND it will not stop until he is blocked.

If the system already has real time thread , but new thread priority is higher, and also the new thread will be scheduled immediately. 

 

  1. An interesting example :

Int main()

{

  1. Create normal  thread 1
  2. Create normal thread 2
  3. Create a real time thread 1  with priority 10
  4. Create a real time thread 2  with priority 20 // 20 is high than 10

 

}

 

Which thread will run first? Will the real time thread 2 has a chance to run??

Answer: real time thread 1 will run first

  real time thread 2 has no chance to run , because real time thread 1 will preemptive all the normal thread and that includes the main() thread!! It means real-time thread2 even has not a chance to be created!!!!!!

Real-time thread will preemptive all the non real-time thread. Be careful!! Make sure your normal thread has the chance to run!!

 

 

  1. A SCHED_FIFO process runs until either it is blocked by an I/O  request,  it  is  pre emptived by a higher priority process, or it calls sched_yield .

 

  1. If have 3 thread with priority of 3, 4 ,5, what is the run sequence?

Thread 5 will always run only if he was blocked ; then thread 4 will get the cpu , only when both 5 and 4 are block then thread 3 will get the cpu.

  1. Does preemptive means the highest priority thread will get the CPU whenever it is ready?

Yes. And in Linux , all scheduling is preemptive no matter it is real time thread or non real-time thread. The only difference is that real-time thread has higher priority. (from 1 to 99 for realtime thread, 0 is for non-real-time thread)

  1. Real thread in Linux? Means this thread is special than others?

Yes. That is the thread you created with FIFO ROUND_RB.

pthread_attr_t thread_attr;

struct sched_param sch_param;

pthread_attr_init(&thread_attr);

 

pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO);

sch_param.sched_priority = 50;

pthread_attr_setschedparam(&thread_attr, &sch_param);

 

if(pthread_create(&g_display_int_thread_id, &thread_attr, display_init_thread, NULL))

{

assert(!"Error in create InitSvpClient thread");

}

 

  1. SCHED_RR & SCHED_FIFO mode , are they just the setting for thread with equal priority?

NO. If you create a thread with the scheduler attribute set to SCHED_RR or SCHED_FIFO mode, they are real-time thread. SCHED_RR is a variation of the SCHED_FIFO . The only difference is that for thread with equal priority , in _RR policy will share the CPU between them , but for _FIFO mode, it is run as FIFO…. That mean only when one give up the cpu the other will get it.

  1. What is the default thread priority in Linux?

  No priority. Default thread is non real time thread and the thread priority is default to 0. As all the normal thread has the same priority , so the scheduling police is the standard time sharing policy.

man sched_setscheduler , you will understand what is the scheduler policy for non real time thread.

  1. What is the difference in thread scheduler between kernel 2.4 - 2.6
    1. Pre-emptive point in the kernel  http://www.rtcmagazine.com/articles/print_article/100132
      1. Before, when it is in the kernel mode (through a system call), it is not preemptive. Preemptive happens only when return back to user mode. It hurts the responsiveness sometimes.
    2. O(1) time for determine next process to run. http://www.ibm.com/developerworks/linux/library/l-scheduler/
      1. First , basically it is priority-base preemptive scheduler algorithm.
      2. We have total  140 priorities , 0-99 is for real time thread which is created through SCHED_FIFO or SCHED_RR. 100-139 is for normal thread.
      3. Scheduler always pick the head task in the highest priority task queue. 

To make this process more efficient, a bitmap is used to define when tasks are on a given priority list. Therefore, on most architectures, a find-first-bit-set instruction is used to find the highest priority bit set in one of five 32-bit words

  1. Dynamic task prioritization
    1. This is not something new and this only apply for the normal thread (not use thread).
    2. The theory:

To prevent tasks from hogging the CPU and thus starving other tasks that need CPU access, the Linux 2.6 scheduler can dynamically alter a task's priority. It does so by penalizing tasks that are bound to a CPU and rewarding tasks that are I/O bound. I/O-bound tasks commonly use the CPU to set up an I/O and then sleep awaiting the completion of the I/O. This type of behavior gives other tasks access to the CPU.

 

  1. In which case high priority thread starvation other one? How to prevent it?
    1. A
      1. Preemptive
      2. You are highest priority thread and you never has a break
    2. How to prevent it ?

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值