標籤:turned 進程 tail http lin star signal lib 操作
轉自:http://blog.csdn.net/cyberlabs/article/details/6920138
使用makecontext實現使用者線程 現代Unix系統都在ucontext.h中提供用於環境切換的函數,這些函數有getcontext, setcontext,swapcontext 和makecontext。其中,getcontext用於儲存當前上下文,setcontext用於切換上下文,swapcontext會儲存當前上下文並切換到另一個上下文,makecontext建立一個新的上下文。實現使用者線程的過程是:我們首先調用getcontext獲得當前上下文,然後修改ucontext_t指定新的上下文。同樣的,我們需要開闢棧空間,但是這次實現的線程庫要涉及棧生長的方向。然後我們調用makecontext切換上下文,並指定使用者線程中要執行的函數。 在這種實現中還有一個挑戰,即一個線程必須可以主動讓出CPU給其它線程。swapcontext函數可以完成這個任務,圖4展示了一個這種實現的範例程式,child線程和parent線程不斷切換以達到多線程的效果。在libfiber-uc.c檔案中可以看到完整的實現。#include #include #include // 64kB stack#define FIBER_STACK 1024*64ucontext_t child, parent;// The child thread will execute this functionvoid threadFunction(){printf( "Child fiber yielding to parent" );swapcontext( &child, &parent );printf( "Child thread exiting\n" );swapcontext( &child, &parent );}int main(){// Get the current execution contextgetcontext( &child );// Modify the context to a new stackchild.uc_link = 0;child.uc_stack.ss_sp = malloc( FIBER_STACK );child.uc_stack.ss_size = FIBER_STACK;child.uc_stack.ss_flags = 0; if ( child.uc_stack.ss_sp == 0 ){perror( "malloc: Could not allocate stack" );exit( 1 );}// Create the new contextprintf( "Creating child fiber\n" );makecontext( &child, &threadFunction, 0 );// Execute the child contextprintf( "Switching to child fiber\n" );swapcontext( &parent, &child );printf( "Switching to child fiber again\n" );swapcontext( &parent, &child );// Free the stackfree( child.uc_stack.ss_sp );printf( "Child fiber returned and stack freed\n" );return 0;}圖4用makecontext實現線程使用者級線程的搶佔 搶佔實現的一個最重要的因素就是定時觸發的計時器中斷,它的存在使得我們能夠中斷當前程式的執行,非同步對進程的時間片消耗情況進行統計,並在必要的時候(可能是時間片耗盡,也可能是一個高優先順序的程式就緒)從當前進程調度到其它進程去執行。 對於使用者空間程式來說,與核心空間的中斷相對應的就是訊號,它和中斷一樣都是非同步觸發,並能引起執行流的跳轉。所以要想實現使用者級線程的搶佔,我們可以藉助定時器訊號(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 10typedef int uthread_t;typedef void uthread_attr_t;uthread_t current = 0;#define uthread_self() currentstruct 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;} 似乎該有的都有了,也許真的可以在使用者空間實現一個虛擬作業系統環境玩呢...
使用makecontext實現使用者線程【轉】