红联Linux门户
Linux帮助

线程调度的问题

发布时间:2010-11-11 17:36:54来源:红联作者:wucongdonglai
对于新建线程的调度有点不太理解,例子程序如下:
#define _REENTRANT
#include
#include
#include
#include

void *thread_function(void *arg);
char message[] = "Hello World";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
pthread_attr_t thread_attr;

int max_priority;
int min_priority;
struct sched_param scheduling_value;

res = pthread_attr_init(&thread_attr);
res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
printf("Waiting 1 \n");
max_priority = sched_get_priority_max(SCHED_OTHER);
min_priority = sched_get_priority_min(SCHED_OTHER);
scheduling_value.sched_priority = max_priority;[color=seagreen](这里不管我设优先级为最高或者最低,或者默认,结果都一样)
//scheduling_value.sched_priority = min_priority;
res = pthread_attr_setschedparam(&thread_attr, &scheduling_value);
(void)pthread_attr_destroy(&thread_attr);
sleep(1);
printf("Waiting 2\n");
exit(EXIT_SUCCESS);
}[/color]
void *thread_function(void *arg) {
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(1);
printf("Waiting 3\n");
pthread_exit(NULL);
}

结果如下:
Waiting 1
thread_function is running. Argument was Hello World
Waiting 2
Waiting 3
Waiting 3
疑问有3:
1,为什么创建新线程之后,无论我设优先级是什么,都是原先的线程先运行
2,线程创建之后,和原先线程之间是怎么轮询的
3:为什么我这个结果输出为多一个
Waiting 3
麻烦大家帮忙看看
文章评论

共有 2 条评论

  1. reaperzero 于 2010-11-12 10:13:16发表:

    学习下,,,,

  2. deepwhite 于 2010-11-12 09:42:02发表:

    [i=s] 本帖最后由 deepwhite 于 2010-11-12 13:36 编辑 [/i]

    参考一下下面的两篇文章。
    http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
    http://www.icir.org/gregor/tools/pthread-scheduling.html

    最后有一点,用户态的线程和进程对内核来讲,在调度上是一样的,所以轮询之类的和普通的多进程之间没有什么差别。

    I will only discuss the ntpl (native posix thread library) implementation, which is the default pthread implementation used in current glibcs. The nptl implementation only uses a 1:1 thread model. The scheduler handles everythread as if it were a process. Therefor only the supported scope isPTHREAD_SCOPE_SYSTEM. The default scheduling policy is SCHED_OTHER, which is the default Linux scheduler. The nptl implementation can utilizemultiple CPUs.


    It is possible to assign a thread aSCHED_FIFO and SCHED_RR policy. Since the scheduler handles every thread as if it were a process, a thread with one of these policies will be handled like a process with realtime schduling priority (see [2]). I.e the threadwill content for the CPU with all other procesess and not with thethreads of the same process. This implies that a threadcould starve other processes! Essentially the pthread_set_schedparam()call maps to the sched_setscheduler() call. This operation requires root privileges, since it can have a vital impact on thewhole system.


    For SCHED_OTHER the allowed min and max priorities are 0. Therefor itis not possible to change the priority. SCHED_FF or SCHED_RR have an allowed range of 1 ... 99 (see sched_get_priority_max(2) andsched_get_priority_min(2)).