-
深入浅出
PINTOS
又名
零基础学
PINTOS
——
哈哈重在恶搞。温帅哥出品。
<
/p>
忙活了一个周末,废寝忘食地啃特那保姆写的《现代操作作系统》,
pintos
的第一个实验才
仅仅算是初见端倪。。。斯坦福
阿斯坦福,你把这个
pintos
搞的这么难,坑爹阿!能独立
完成这个实验的孩子你们伤不起阿!
言归正传,把我知道的一点点
pintos
实验
project1
的做法全都抖落出来,分享一下。已经
会的大牛不要嫌我嘚瑟阿。。。
一、在
pintos
源码中找到最基本的概念:
本次实验至少需要阅读
thread.h
,
tread.c
,
interrup.h
,
time.c
这四个文件。
< br>
pintos
在
thread
.h
中定义了一个结构体
struct thread
,
这个结构体就存方了有关进程的基本
信息。<
/p>
struct thread
{
tid_t tid;
/* Thread identifier. */
enum thread_status status;
/* Thread state. */
char name[16];
/* Name (for debugging purposes). */
uint8_t *stack;
/* Saved stack pointer. */
int priority;
/* Priority. */
struct list_elem allelem;
/* List element for all threads list.
*/
/*
Shared between thread.c and synch.c. */
struct list_elem elem;
/* List element. */
#ifdef USERPROG
/* Owned by userprog/process.c. */
uint32_t *pagedir;
/* Page directory. */
#endif
/* Owned by
thread.c. */
unsigned
magic;
/* Detects
stack overflow. */
};
大家不要被这么庞大的结构体所吓倒,
其实他说的事情很简单,
无非是这个线程的几个基本
信息。
值得注意的是
enum thread_status
这个枚举
类型的变量,他的意思就是这个线程现
在所处的状态。
enum thread_status
{
THREAD_RUNNING,
/*
Running thread. */
THREAD_READY,
/* Not
running but ready to run. */
THREAD_BLOCKED,
/*
Waiting for an event to trigger. */
THREAD_DYING
/* About
to be destroyed. */
};
还有一个最最重要的概念是中断。
所谓中断其实分两种,
一种是
IO
设备
向
CPU
发出的中断
的信息,另一种是
CPU
决定切换到另一个进程时(轮换时间片)发出的指令。我
们现在处
理第二种。
pintos
的中
断在
interrupt.h
和
int
errupt.c
之中。其中这个枚举类型
intr_lver
l
会
在后面被反复提到:
enum intr_level
{
INTR_OFF,
/*
Interrupts disabled. */
INTR_ON
/*
Interrupts enabled. */
};
其实这个
intr_level<
/p>
表达的意思更简单,就是有两个单词,
intr_off
表示关中断,
on
表示开
中断。大家都知道,执行原子级别操作的时候,中断必须是关着的。
最后还要说以下,
pintos<
/p>
是以
ticks
作为基本时间单位的,每
秒有
TIMER_FREQ
个
tick
s
:
/* Number of
timer interrupts per second. */
#define
TIMER_FREQ 100 //
系统默认这个宏为
100
还有一点,
pintos
默认每一个<
/p>
ticks
调用一次时间中断。换句话说,每一个线程最多可以占
据
CPU
一个
ticks
的时长,之后就必须放手。
二、掌握
thread
的基本操作(函数):
以下函数在
thread.c
中都可以找到。
1.
thread_current()
获取当前当前
的线程的指针。
2.
thread_
foreach(thread_action_func *func, void *aux)
遍历当前
ready queue
中的
所有线
程,
并且对于每一个线程执行一次
func
操作。
注意到这里的
fun
c
是一个任意给定函数的指
针,参数
a
ux
则是你想要传给这个函数的参数。实际上
pintos
p>
没有多么高深,所有
ready
的线程被保
存在一个链表中。
这个函数做得不过是遍历了一遍链表而已。
注
意这个函数只能
在中断关闭的时候调用。
3.
thread_block()
和
thread_unblock(thread *t
)
。
这
是一对儿函数,区别在于第一个函数的
作用是把当前占用
cpu
的线程阻塞掉(就是放到
waiting
里面),而第二个函数作用是将已
经被阻塞掉的进程
t
唤醒到
ready
队列中。
< br>
4.
timer_interrupt
(struct intr_frame *args UNUSED)
这个函数在
p>
timer.c
中,
pintos
在每次
时间中断时(即每一个时间单位(
tic
ks
))调用一次这个函数。
5.
intr_disable ()
这
个函数在
interrupt.c
中,作用是返回关中断,然后
返回中断关闭前的状
态。(其实说白了状态不就是
INTR_O
FF,INTR_ON
这两种么。
)
三、
代码分析与
timer_sleep()
函数的重新设计:
p>
timer_sleep
的作用是让此线
程等待
ticks
单位时长,然后再执行。函数原型:
void
timer_sleep
(int64_t ticks)
//
参数的意思是你想要等待的时间长度
{
int64_t start
= timer_ticks ();
//
记录开始时的系统时间
ASSERT (intr_get_level ()
== INTR_ON);
-
-
-
-
-
-
-
-
-
上一篇:感悟曼德拉名言警句中英文大全
下一篇:关于和谐的句子