一、实验目标
- 了解和掌握 Linux 系统的线程竞争范围
- 了解和掌握 Linux 系统提供的线程调度算法
二、实验内容
1.思路
创建线程,用pthread_attr_getscope()方法来确认是系统竞争范围还是线程竞争范围,再用pthread_attr_setscope()方法设置为系统竞争范围。
查看当前进程的调度算法,然后修改调度算法,最后还原调度算法,并分步打印。
2.代码
//thread_scope.c
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *runner(void *param);
int main (int argc, char * argv[]){
int i, scope;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
if (pthread_attr_getscope(&attr, &scope) != 0)
fprintf(stderr, "unable to ge scheduling scope\n");
else {
if (scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS\n");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr, "Illegal scope value.\n");
}
pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
if (pthread_attr_getscope(&attr, &scope) != 0)
fprintf(stderr, "unable to ge scheduling scope\n");
else {
if (scope == PTHREAD_SCOPE_PROCESS)
printf("PTHREAD_SCOPE_PROCESS\n");
else if (scope == PTHREAD_SCOPE_SYSTEM)
printf("PTHREAD_SCOPE_SYSTEM\n");
else
fprintf(stderr, "Illegal scope value.\n");
}
for (i = 0; i < NUM_THREADS; i++ )
pthread_create(&tid[i], &attr, runner, NULL);
for (i = 0; i < NUM_THREADS; i++ )
pthread_join(tid[i], NULL);
}
void * runner(void * param)
{
pthread_exit(0);
}
//thread_sched.c
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
static int get_thread_policy(pthread_attr_t attr)
{
int policy;
pthread_attr_getschedpolicy(&attr, &policy);
switch(policy)
{
case SCHED_FIFO:
printf(" policy = SCHED_FIFO\n");
break;
case SCHED_RR:
printf(" policy = SCHED_RR\n");
break;
case SCHED_OTHER:
printf(" policy = SCHED_OTHER\n");
break;
default:
printf(" policy = UNKOWN\n");
break;
}
return policy;
}
static void set_thread_policy(pthread_attr_t attr, int policy)
{
pthread_attr_setschedpolicy(&attr, policy);
get_thread_policy(attr);
}
static void show_thread_priority(pthread_attr_t attr, int policy)
{
int priority = sched_get_priority_max(policy);
printf(" max priority = %d, ", priority);
priority = sched_get_priority_min(policy);
printf(" min priority = %d\n", priority);
}
static int get_thread_priority(pthread_attr_t attr)
{
struct sched_param param;
pthread_attr_getschedparam(&attr, ¶m);
printf(" priority = %d\n", param.sched_priority);
return param.sched_priority;
}
int main(void)
{
pthread_attr_t attr;
struct sched_param sched;
pthread_attr_init(&attr);
printf("- Show Current Policy:");
int policy = get_thread_policy(attr);
printf("- Show current configuration of priority:");
show_thread_priority(attr, policy);
printf("- Show SCHED_FIFO of priority:");
show_thread_priority(attr, SCHED_FIFO);
printf("- Show SCHED_RR of priority:");
show_thread_priority(attr, SCHED_RR);
printf("- Show priority of current thread:");
int priority = get_thread_priority(attr);
printf("\n -SET THREAD POLICY\n");
printf("- SET SCHED_FIFO policy:");
set_thread_policy(attr, SCHED_FIFO);
printf("- SET SCHED_RR policy:");
set_thread_policy(attr, SCHED_RR);
printf("- Restore current policy:");
set_thread_policy(attr, policy);
pthread_attr_destroy(&attr);
return 0;
}
3.过程及运行结果展示
编译运行thread_scope.c 程序,确认Linux 系统提供的线程竞争范围,利用代码加以分析
编译运行 thread_sched.c 程序,确认 Linux 系统提供的线程调度算法,利用代码加以分析代码
三、实验结论
1.创建线程,用pthread_attr_getscope()方法来确认是系统竞争范围还是线程竞争范围,再用pthread_attr_setscope()方法设置为系统竞争范围。
2.Ubuntu系统提供的是系统竞争范围线程调度。
3…Local Scheduling: 线程库调度用户级线程到一个有效的LWP【也就是用户线程映射内核线程】
– 被称为进程竞争范围VIM
– 根据优先级完成,一般是由程序员给定
– 竞争发生在相同进程的线程之间
4…Global Scheduling: 系统将内核线程调度到有效的物理处理器上,被称为系统竞争范围
– 竞争发生在系统的所有线程之间(Window XP)
5. 系统为用户创建的调度算法为SCHED_OTHER