總結:
分析process部分代碼,瞭解人物調度的基本內容,包括如下內容
1、任務的鏈表,以及任務的增加和刪除
2、contiki的程序控制塊PCB的簡介
3、contiki任務的三種狀態
- 任務的開始、添加、刪除:
(1)任務的頭鏈表
struct process *process_list = NULL;
struct process *process_current = NULL;
process_list,這個鏈表的指針用來指向當前任務的首地址
process_current,這個鏈表的指針用來指向當前正在執行的任務
(2)將任務添加進任務鏈表中
Void process_start(struct process *p, const char *arg)
{
struct process *q;
/* First make sure that we don't try to start a process that is
already running. */
for(q = process_list; q != p && q != NULL; q = q->next);
/* If we found the process on the process list, we bail out. */
if(q == p) {
return;
} 檢查任務鏈表和新添加的任務是否有重複
/* Put on the procs list.*/
p->next = process_list;
process_list = p; //插入鏈表頭
p->state = PROCESS_STATE_RUNNING;//設置程序運行的狀態
PT_INIT(&p->pt);//行號
PRINTF("process: starting '%s'\n", PROCESS_NAME_STRING(p));
/* Post a synchronous initialization event to the process. */
process_post_synch(p, PROCESS_EVENT_INIT, (process_data_t)arg);
跑一下當前加入的進程
}
(3)刪除任務
static void
exit_process(struct process *p, struct process *fromprocess)
{
register struct process *q;
struct process *old_current = process_current;
for(q = process_list; q != p && q != NULL; q = q->next);
if(q == NULL) {
return;
} //檢測要刪除的任務在任務鏈表中
if(process_is_running(p)) {
/* Process was running */
p->state = PROCESS_STATE_NONE;
/*
* Post a synchronous event to all processes to inform them that
* this process is about to exit. This will allow services to
* deallocate state associated with this process.
*/
//遍歷任務鏈表,給鏈表中非自己的任務發送我即將退出的情況,如下例子爲
* 例如,print_hello_process進程被刪除,則需要通知etimer進程把與該任務相關的定時器移除。因爲該事件綁定了進程的地址
for(q = process_list; q != NULL; q = q->next) {
if(p != q) {
call_process(q, PROCESS_EVENT_EXITED, (process_data_t)p);
}
}
if(p->thread != NULL && p != fromprocess) {
/* Post the exit event to the process that is about to exit. */
process_current = p;
p->thread(&p->pt, PROCESS_EVENT_EXIT, NULL);
}
}
if(p == process_list) {
process_list = process_list->next;
} else {//刪除任務
for(q = process_list; q != NULL; q = q->next) {
if(q->next == p) {
q->next = p->next;
break;
}
}
}
process_current = old_current;
}
2、程序控制塊PCB
struct process {
struct process *next; //指向下個任務
#if PROCESS_CONF_NO_PROCESS_NAMES
#define PROCESS_NAME_STRING(process) ""
#else
const char *name;
#define PROCESS_NAME_STRING(process) (process)->name //任務名
#endif
PT_THREAD((* thread)(struct pt *, process_event_t, process_data_t)); //調用創建的進程
struct pt pt; //保存行號
unsigned char state, needspoll; //進程狀態
};
3、程序運行的三種狀態
p->state = PROCESS_STATE_NONE;
p->state = PROCESS_STATE_RUNNING;
p->state = PROCESS_STATE_CALLED;
PROCESS_STATE_NONE是指進程不處於運行狀態
PROCESS_STATE_RUNNING就緒,等待被執行,未必有運行的權限
PROCESS_STATE_CALLED運行狀態 ,獲得實在的運行權限
飲水不忘挖井人:https://www.cnblogs.com/liu13526825661/p/6118393.html