【操作系统实验7】线程竞争范围及线程调度

一、实验目标

  1. 了解和掌握 Linux 系统的线程竞争范围
  2. 了解和掌握 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, &param);
        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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值