文章目录
Chapter0 Operating system interfaces
System call | Description |
---|---|
fork() | Create a process |
exit() | Terminate the current process |
wait() | Wait for a child process to exit |
kill(pid) | Terminate process pid |
getpid() | Return the current process’s pid |
sleep(n) | Sleep for n clock ticks |
exec(filename, *argv) | Load a file and execute it |
sbrk(n) | Grow process’s memory by n bytes |
open(filename, flags) | Open a file; the flags indicate read/write |
read(fd, buf, n) | Read n bytes from an open file into buf |
write(fd, buf, n) | Write n bytes to an open file |
close(fd) | Release open file fd |
dup(fd) | Duplicate fd |
pipe§ | Create a pipe and return fd’s in p |
chdir(dirname) | Change the current directory |
mkdir(dirname) | Create a new directory |
mknod(name, major, minor) | Create a device file |
fstat(fd) | Retrun info about an open file |
link(f1, f2) | Create another name (f2) for the file f1 |
unlink(filename) | Remove a file |
Chapter1 Operating system organization
multiplexing、isolation、interaction
Chapter2 Page tables
每个进程的页表同时包括用户内存和内核内存的映射,当用户通过中断或者系统调用转入内核时就不需要进行页表的转换了
Chapter3 Traps, interrupts, and drivers
控制从CPU转移到内核的3种情况:
- 系统调用:用户程序请求操作系统服务
- 异常:程序执行非法操作
- 中断:设备产生信号,等待操作系统处理
x86有4个保护等级,0(最高等级)到3(最低等级),大多数操作系统只使用了0和3,被称为kernel mode
和user mode
,正在执行的指令的保护等级存储在%cs
寄存器的CPL
中
interrupt handlers
在interrupt descriptor table (IDT)
中定义,IDT有256个entry,每一个包含%cs
和%eip
int
、iret
指令
Chapter4 Locking
spinlock
// Acquire the lock.
// Loops (spins) until the lock is acquired.
// Holding a lock for a long time may cause
// other CPUs to waste time spinning to acquire it.
void
acquire(struct spinlock *lk)
{
pushcli(); // disable interrupts to avoid deadlock.
if(holding(lk))
panic("acquire");
// The xchg is atomic.
while(xchg(&lk−>locked, 1) != 0)
;
// Tell the C compiler and the processor to not move loads or stores 1585 // past this point, to ensure that the critical section’s memory 1586 // references happen after the lock is acquired.
__sync_synchronize();
// Record info about lock acquisition for debugging.
lk−>cpu = mycpu();
getcallerpcs(&lk, lk−>pcs);
}
// Release the lock.
void
release(struct spinlock *lk)
{
if(!holding(lk))
panic("release");
lk−>pcs[0] = 0;
lk−>cpu = 0;
// Tell the C compiler and the processor to not move loads or stores 1660
// past this point, to ensure that all the stores in the critical 1661
// section are visible to other cores before the lock is released.
// Both the C compiler and the hardware may re−order loads and
// stores; __sync_synchronize() tells them both not to.
__sync_synchronize(); 1665
// Release the lock, equivalent to lk−>locked = 0.
// This code can’t use a C assignment, since it might
// not be atomic. A real OS would use C atomics here.
asm volatile("movl $0, %0" : "+m" (lk−>locked) : ); 1670
popcli();
}
sleeplock
持有锁的期间,释放处理器
recursivelock
函数持有该锁,任何该函数调用的函数都可以重新获取锁