整个代码的流程也不复杂,大家可以运行、单步调试一把,试试看。


#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>

#define UINT32 unsigned   int
#define STACK_LENGTH      512
#define THREAD_MAX_NUMBER 10

typedef struct _TASK_INFO
{
    UINT32 id;
    UINT32* stack;
    UINT32 size;
    UINT32 context;
    UINT32 priority;
    UINT32 time_slice;
    void (*func)();

}TASK_INFO;

static struct itimerval oldtv;
UINT32 old   = 0;
UINT32 count = 0;
UINT32 task_stack[THREAD_MAX_NUMBER][STACK_LENGTH] = {0};
TASK_INFO gAllTask[THREAD_MAX_NUMBER] = {0};
UINT32 current_thread_id = 0;

void set_timer()
{
        struct itimerval itv;
        itv.it_interval.tv_sec = 1;
        itv.it_interval.tv_usec = 0;
        itv.it_value.tv_sec = 1;
        itv.it_value.tv_usec = 0;
        setitimer(ITIMER_REAL, &itv, &oldtv);
}

void swap(UINT32* prev, UINT32* next)
{
    __asm("push %%eax "
          "push %%ebx "
          "push %%ecx "
          "push %%edx "
          "push %%esi "
          "push %%edi "
          "push %%ebp "
          "push %%esp "
          "lea 0x8(%%ebp), %%eax "
          "mov (%%eax), %%eax "
          "mov %%esp, (%%eax) "

          "lea 0xc(%%ebp), %%eax "
          "mov (%%eax), %%eax "
          "mov (%%eax), %%esp "
          "pop %%esp "
          "pop %%ebp "
          "pop %%edi "
          "pop %%esi "
          "pop %%edx "
          "pop %%ecx "
          "pop %%ebx "
          "pop %%eax "
          ::);
}

void hello()
{
        int temp = 0;

        while(1) {
            printf("id = %d, temp = %d, count = %d in thread! ",current_thread_id,  temp ++, count ++);
            swap(&gAllTask[current_thread_id].context, &old);

            printf("id = %d, temp = %d, count = %d in thread! ",current_thread_id,  temp ++, count ++);
            swap(&gAllTask[current_thread_id].context, &old);
        }
}

int find_next_thread()
{
    int index;

    for(index = THREAD_MAX_NUMBER -1; index >=0; index --)
    {
        if(0 != gAllTask[index].time_slice)
            break;
    }

    return index;     
}

void reset_time_slice ()
{
    int index;

    for(index = 0; index < THREAD_MAX_NUMBER; index++)
        gAllTask[index].time_slice = gAllTask[index].priority + 1;
}

void task_init(int index)
{
        UINT32 unit = gAllTask[index].size;
        UINT32* pData = gAllTask[index].stack;

        memset((void*)pData,(int) 0, unit * sizeof(UINT32));
        pData[unit -1] = (UINT32) gAllTask[index].func;
        pData[unit -2] = 0;
        pData[unit -3] = 0;
        pData[unit -4] = 0;
        pData[unit -5] = 0;
        pData[unit -6] = 0;
        pData[unit -7] = 0;
        pData[unit -8] = 0;
        pData[unit -9] = 0;
        pData[unit -10] = (UINT32) &pData[unit - 9];
        gAllTask[index].context = (UINT32) &pData[unit -10];
}

void signal_handler(int m)
{
        int index;

start:
        index = find_next_thread();
        if(-1 == index)
        {
            reset_time_slice();
            goto start;
        }

        gAllTask[index].time_slice --;
        current_thread_id = index;
        swap(&old, &gAllTask[current_thread_id].context);
}

void set_all_task()
{
        int index;
        memset(gAllTask, 0, sizeof(gAllTask));

        for(index = 0; index < THREAD_MAX_NUMBER; index ++)
        {
            gAllTask[index].id = index;
            gAllTask[index].stack = task_stack[index];
            gAllTask[index].size = STACK_LENGTH;
            gAllTask[index].context = 0;
            gAllTask[index].func = hello;
            gAllTask[index].priority = index;
            gAllTask[index].time_slice = index + 1;

            task_init(index);
        }
}

int main()
{
        char val;

        set_all_task();
        set_timer();
        signal(SIGALRM, signal_handler);

        while(1)
        {
            scanf("%c", &val);
        }

        exit(0);
        return 1;
}