Thread in Linux

来源:互联网 发布:淘宝禁售商品的是 编辑:程序博客网 时间:2024/05/16 14:44

  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 tospecify a priority to a thread ,it means that it is a real time thread. And ofcourse , the real time thread has higher priority than any non-real-timethread.

If the system has no real time thread , and youcreate a real time thread , it will be scheduled immediately AND it will notstop until he is blocked.

If the system already has real time thread , butnew thread priority is higher, and also the new thread will be scheduledimmediately. 

 

  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 timethread 2 has a chance to run??

Answer: real time thread 1 will run first

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

Real-time threadwill preemptive all the non real-time thread. Be careful!! Make sure yournormal thread has the chance to run!!

 

 

  1. A SCHED_FIFO process runs until either it is blocked by an I/O  request,  it  is  preemptivedby a higher priority process, or it callssched_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 3will 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-timethread. The only difference is that real-time thread has higher priority. (from1 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 FIFOROUND_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 schedulerattribute 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 forthread 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 cputhe other will get it.

  1. What is the default thread priority in Linux?

 Nopriority. Default thread is non real time thread and the thread priority isdefault to 0. As all the normal thread has the same priority , so thescheduling police is the standard time sharing policy.

man sched_setscheduler , you will understand what is the scheduler policy for non realtime 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 todefine when tasks are on a given priority list. Therefore, on mostarchitectures, a find-first-bit-setinstruction is used to find the highest priority bit set in one of five 32-bitwords

  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 othertasks that need CPU access, the Linux 2.6 scheduler can dynamically alter atask's priority. It does so by penalizing tasks that are bound to a CPU andrewarding tasks that are I/O bound. I/O-bound tasks commonly use the CPU to setup an I/O and then sleep awaiting the completion of the I/O. This type ofbehavior 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 ?