最近第三次精读郑钢先生《操作系统真相还原》第一版第九章线程时,发现414页有一个问题。不知道大家有没有注意到。
void thread_create(struct task_struct* pthread, thread_func function, void* func_arg) {
pthread->self_kstack -= sizeof(struct intr_stack);
pthread->self_kstack -= sizeof(struct thread_stack);
(下面省略)
}
参考任务结构的定义
struct task_struct {
uint32_t* self_kstack;
(下面省略)
}
成员self_kstack的类型是无符号整形指针。如果这样直接做减法的话,是不是被减去的地址值扩大4倍,也就是每次都减去了中断栈和线程栈大小的4倍。
因此函数中画下划线那两句是不是应该改为
pthread->self_kstack = (uint32_t*)((int)(pthread->self_kstack) - sizeof(struct intr_stack));
pthread->self_kstack = (uint32_t*)((int)(pthread->self_kstack) - sizeof(struct thread_stack));