简体   繁体   中英

Different threads being scheduled with different policies

In POSIX api for thread scheduling, we can have different threads with different scheduling policies.

Example:

pthread_attr_setschedpolicy (&attr, SCHED_FIFO); // set policy first in first out
pthread_create(&tid1,&attr,runner,NULL);  // create thread with first in first out
pthread_attr_setschedpolicy (&attr, SCHED_RR);  // set policy round robin
pthread_create(&tid2,&attr,runner,NULL);  // create thread with round robin

All threads should follow a common scheduling policy in my opinion ( please correct me if I am wrong), then how does the above code work?

No, threads don't need to have a common scheduling policy. The scheduling policy controls how the thread moves within the list of runnable threads at its static priority level, so this behaviour can be controlled at the individual thread level. When picking a process to run the scheduler will look for the highest static priority that has runnable threads and choose the thread that is head of the list for that static priority level.

For SCHED_FIFO , once a thread reaches the head of the list for a given priority it will stay there until it blocks or yields. For SCHED_RR , a runnable thread that has exceeded its maximum time quantum will be moved to the end of the list for its static priority.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM