contiki系统process代码分析(一)
总结:
分析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