Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2033465
  • 博文数量: 369
  • 博客积分: 10093
  • 博客等级: 上将
  • 技术积分: 4271
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-21 00:59
文章分类

全部博文(369)

文章存档

2013年(1)

2011年(2)

2010年(10)

2009年(16)

2008年(33)

2007年(146)

2006年(160)

2005年(1)

分类: C/C++

2007-12-28 23:35:00

抢占实现的一个最重要的因素就是定时触发的计时器中断,它的存在使得我们能够中断当前程序的执行,异步对进程的时间片消耗情况进行统计,并在必要的时候(可能是时间片耗尽,也可能是一个高优先级的程序就绪)从当前进程调度到其它进程去执行。

对于用户空间程序来说,与内核空间的中断相对应的就是信号,它和中断一样都是异步触发,并能引起执行流的跳转。所以要想实现用户级线程的抢占,我们可以借助定时器信号(SIGALRM),必要时在信号处理程序内部进行上下文的切换。

为了验证自己的想法,我在上篇文章提到的协同多线程的基础上加上了相关抢占代码,具体实现如下:

#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#include <sys/time.h>

#define STACK_SIZE 4096
#define UTHREAD_MAX_NUM 256
#define INIT_TICKS 10

typedef int uthread_t;
typedef void uthread_attr_t;
uthread_t current = 0;

#define uthread_self() current

struct uthread_struct
{
        int used;
        ucontext_t context;
        char stack[STACK_SIZE];
        void* (*func)(void *arg);
        void *arg;
        void *exit_status;
        int ticks;
};

static struct uthread_struct uthread_slots[UTHREAD_MAX_NUM];

void panic(void)
{
                fprintf(stderr, "Panic, bala bala...\n");
                exit(EXIT_FAILURE);
}

void idle_thread(void)
{
        int i;

        for (i = 1; i < UTHREAD_MAX_NUM; i ++)
                if (uthread_slots[i].used)
                        break;

        if (i == UTHREAD_MAX_NUM)
                panic();
        if (current != 0)
                uthread_slots[current].used = 0;
        current = i;
        swapcontext(&uthread_slots[0].context, &uthread_slots[current].context);
}

void uthread_context_init(int tid)
{
        getcontext(&uthread_slots[tid].context);
        uthread_slots[tid].context.uc_stack.ss_sp = uthread_slots[tid].stack;
        uthread_slots[tid].context.uc_stack.ss_size = sizeof(uthread_slots[tid].stack);
        uthread_slots[tid].context.uc_link = &uthread_slots[0].context;
}

void uthread_init(void)
{
        uthread_context_init(0);
        uthread_slots[0].used = 1;
        makecontext(&uthread_slots[0].context, idle_thread, 0);
}

void uthread_schedule(void);

void uthread_exit(void *exit_status)
{
        uthread_slots[current].exit_status = exit_status;
        uthread_slots[current].used = 0;
        uthread_schedule();
}

void uthread_helper(void)
{
        uthread_exit(uthread_slots[current].func(uthread_slots[current].arg));
}

int uthread_create(uthread_t *thread, const uthread_attr_t *attr,
                        void* (*start_routine)(void*), void *arg)
{
        static int last_used = 0;
        int i;

        for (i = (last_used + 1) % UTHREAD_MAX_NUM; i != last_used;
                        i = (i + 1) % UTHREAD_MAX_NUM)
                if (!uthread_slots[i].used)
                        break;
        if (i == last_used)
                return -1;
        last_used = i;

        if (thread != NULL)
                *thread = i;
        uthread_context_init(i);
        uthread_slots[i].used = 1;
        uthread_slots[i].func = start_routine;
        uthread_slots[i].arg = arg;
        uthread_slots[i].exit_status = 0;
        uthread_slots[i].ticks = uthread_slots[current].ticks / 2;
        uthread_slots[current].ticks -= uthread_slots[i].ticks;
        makecontext(&uthread_slots[i].context, uthread_helper, 0);

        return 0;
}

void uthread_schedule(void)
{
        int i, prev;

        for (i = (current + 1) % UTHREAD_MAX_NUM; i != current;
                        i = (i + 1) % UTHREAD_MAX_NUM)
                if (uthread_slots[i].used)
                        break;
        if (i == current)
                panic();

        prev = current;
        current = i;
        swapcontext(&uthread_slots[prev].context, &uthread_slots[current].context);
}

void* thread(void *arg)
{
        int i;

        for (i = 0; 1; i ++) {
                if (i % 1000 == 0)
                        printf("thread/%d(%s): i = %d\n", current, (char*)arg, i);
                uthread_create(NULL, NULL, thread, arg);
                if (i % 1000000 == 0)
                uthread_schedule();
        }
}

void sig_ticks_timer(int signo)
{
        if (--uthread_slots[current].ticks <= 0) {
                uthread_slots[current].ticks = INIT_TICKS;
                uthread_schedule();
        }
}

int main(int argc, char *argv[])
{
        uthread_t tid;
        struct itimerval ticks_timer;

        uthread_init();

        uthread_create(&tid, NULL, thread, "hw1");
        printf("tid is %d\n", tid);
        uthread_create(&tid, NULL, thread, "hw2");
        printf("tid is %d\n", tid);

        signal(SIGALRM, sig_ticks_timer);
        ticks_timer.it_interval.tv_sec = 0;
        ticks_timer.it_interval.tv_usec = 10000;
        ticks_timer.it_value.tv_sec = 0;
        ticks_timer.it_value.tv_usec = 10000;
        setitimer(ITIMER_REAL, &ticks_timer, NULL);

        while (1)
                idle_thread();

        return 0;
}


似乎该有的都有了,也许真的可以在用户空间实现一个虚拟的操作系统环境玩呢...
阅读(4103) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

wanyisjtu2015-09-20 12:31:28

想问一下楼主你的thread函数为什么一直在create新的thread,有什么意义么