text book:Abraham Silberschatz-Operating System Concepts (9th,2012_12)
useful pages:
https://solver.assistedcoding.eu/page_replacement page replacement calculator
https://boonsuen.com/process-scheduling-solver cpu scheduling calculator
https://solver.assistedcoding.eu/process_scheduling cpu scheduling calculator
syllabus
评分:
Important Dates
Mid-term: Week 10, March 24
Final: April 22 to May 10: to be arranged by Academic Registrar
Project: Week 12, April 8
Project Demo: Week 12, April 9 to 10
lab
lab1
csdoor.comp.polyu.edu.hk
apollo/apollo2
pico
:类似于vim的编辑器,pico xx,若没有则创建,若有则进入。
例,pico hello.c
ctrl+o保存;ctrl+x退出;ctrl+r只读
compile:cc hello.c -o hello
(这里可以加其他变量,例如-std=c99,因为有的情况下(比如在for里初始化变量)cc编译不允许)
PC(实际上是Windows)通过<LF><CR>换行,但Linux仅通过<LF>换行。因此会出现win下写的文件在linux下运行不了的情况,此情况下输入dos2unix hello.c
可以将文件(hello.c)的换行符转为linux版本。
下图:linux中常用指令
lab2
segmentation falt:检查是否reference调用
ps 看所有进程;ps -elf 查看进程更多信息
将程序后台运行:&或ctrlz将程序杀死再fh放到后台
i.e, ./lab2A first 50 &
signals
(不同的signal代表不同的中断,通常是外部中断)
lec
lec2 interrupts&system calls
os represent itself as one or more processes(a program in exe)
I/O processing
IO:
cpu访问除去能直接访问的register和memory(RAM)以外的所有东西都要用到IO
2 methods:
Synchronous:同步,user层等待io结束才继续执行,同一时间只允许一个IO执行
Asynchronous:异步,user层不管IO,继续执行,同一时间允许多个IO执行
当IO执行完毕,program会收到interrupt
device status table储存IO进度
Interrupt processing
interrupt分类:
maskable/non-maskable:non优先级比maskable高
hardware/software: hardware : ctrl+c ; software: n/0
program/IO/timer
一般program interrupt都是software,maskable(指可以被用户操作的)
Interrupt table:储存不同种类的interrupt:ISR
non maskable interrupt出现:通过isr处理interrupt,再通过指针回到程序继续处理
Usermode/kernelmode:
system call或API连接的两头
user:mode bit=1
kernel:mode bit=0
system call
A system call is a programming interface to the services provided by the OS.
system call table储存不同形式的system call
API,System call interface和os 三者关系
系统调用例图:将一个文件复制到另一个文件
application programming interface (API)
System Call Parameter Passing
1、直接用寄存器传递;很快,但如果参数个数比寄存器多会失败
2、寄存器存参数地址,参数被用block或table的形式存在内存里
3、直接将参数存在栈里,系统调用从栈中弹出的参数
6 categories of system call
process control, file manipulation, device manipulation, information maintenance, communications, and protection.
os structures
linux和unix都是只有两层(user和kernal)
lec3 Unix/Linux programming
basic L/U
创建文件夹:mkdir
删空文件夹:rmdir
看user directory:pwd
看别名:alias
文件权限:r read w write x execute(利用ls -l)
-rw-r-----代表,自己可以rw,classmates可以r,其他人没有权限
改path参数:
更改权限:chmod
chmod [-cfvR] [--help] [--version] mode file...
将file1设置为所有人可读:chmod ugo+r file1.txt
利用*或?搜索lab开头的文件:
ls lab ?
查看文件类型和其他信息:file filename
labfiles=lab*.c ;file $labfiles
搜索包含xx内容的文件:grep
grep malloc *.c
:查找所有用到了malloc的c文件
具体参数看这里
pipe操作:把n个output合在一起:ls | wc
:给出ls下所有文件的lines,words,和characters
shell
echo:打印
echo $$
打印当前pid
bash
:新开一个subshell(此时pid不一样了),输入exit退出
变量:声明:v=xxx
echo $v(打印xxx)
export:操作环境变量:
export PERSON=ME; bash;echo $PERSON ;PERSON =YOU; echo $PERSON; exit;echo $PERSON
输出:ME; YOU;ME
变量运算:
Array
声明:var=(val1 val2 ... valN)
调用:${var[i]}
注意必须带大括号,因为$var 也是变量(且打印出来的是array的第一个数)
$ echo element 3, ${fac[3]}, $fac[3]
输出:element 3, 24, 1[3]
修改:var[i]=xxx
quotation
单引:strong;双引:weak
$ echo ’I need $5.00’
I need $5.00
$ echo ”I need $5.00”
I need .00
Common Built-In Variables
HOME
Store the full pathname of the home directory: where you
will go to (home) when you just type cd.
** PATH**
Store a list of directories to be searched when running
programs or shell scripts, separated by “:”.
PS1
Store primary prompt string, with a default value of '\s-\v$ ',
meaning system-version then $.
PS2
Store secondary prompt string, with a default value of '> '.
It is used for continuation of input lines
PWD
Store current working directory set by cd command.
HOSTNAME
Store current machine name.
UID
Store internal Unix user id.
PPID
Store process id of parent.
HISTSIZE
Store number of lines of history to remember, default to the
value of 500.
history
Setting it will enable command history to be stored, useful
for future, default to on.
noclobber
Setting it will prevent overwriting of files when using I/O
redirection, default to off.
ignoreeof
Setting it will prevent accidental logging out with
(end of input), often used when entering data from
keyboard, default to off.
allexport
Setting it will automatically export all modified variables,
default to off.
To turn on/off, use set –o/+o variable
set –o noclobber # turn on noclobber feature
set +o history # turn off history storage
运算
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加(注意使用的是反引号 ` 而不是单引号 '):
条件:
creating shell scripts
分为textline comments和comments
comments:由#作为前标
第一行:#!
标志程序去编译以下代码,并将该符号后的路径作为编译器路径
(如果第一行是二进制的话计算机会把它当成编译完毕的程序处理)
bash的第一行:#!/bin/bash
修改file权限以便运行:chmod 700 file
or chmod u+x file
不要创建叫test的shell script
传递参数:
输入输出改方向
标准输入:键盘;标准输出:屏幕
<
改输入;>
改输出
循环
for
while
until
file test
exist status
$?
以查看程序状态(会返回0-255),0表示成功退出,其他数字则表示错误
lec4 process Management
process
is a program in execution
process=program counter+ registers and processor status+text(code)+data+heap for dynamic storage
process state
scheduler
long term:
select be in redy queue
admission of jobs
controls the degree of multi programming
make process maintain good mix of io and cpu bound
short term:
select be execute next
allocation of cpu
** IO bound process**
more io than computation
CPU bound process
more computation than io
context switch
Sequence of events to bring CPU from an executing
process to another is called context switching
(no useful work)
unix/linux process
creation
只有返回0才是正常的
parent create child
orphan: adopt by a new parent
process communication&sychronization
sychronization: ensure order
lec5 interprocess communication
interprocess communication
direct:
unix/linux unamed pipr programming
communication topoligy
unix/linux named pipe programming
named pipe: actually a file(no parent child 关系)
(可以写在一个程序里,或用一个程序调用另一个程序)
lec6 cpu scheduling
lec7 memory management
virtual->physical address done by hardware(mmu)(very fast)
user process only derl with logical address
contiguous allocation
allocate a single block of memory to hold a process
2 registers protect user process from step into others
The relocation register contains the value of smallest
physical address for the process.
The limit register contains the range (size) of logical address
space.
first fit allocate the first hole that is big enough.
worst fit allocate the largest hole.
best fit allocate the smallest hole that is big enough
non contiguous allocation
When memory is divided up into pages, it is no
longer necessary that consecutive pages be allocated
for a process. This is non-contiguous allocation.
paging
pysical -> frame(size: always power of 2)
logical-> pagees
logical addres= page number§ + page offset(d)
translation look aside buffer:
有buffer hit的情况data access cost1+c(访问内存+buffer)
buffer miss: 2+c