操作系统 知识点总结

第一章 概论

  1. 单处理中,进程之间叫并发不是并行
  2. 用户通过命令接口(批处理和交互式)和系统调用来使用计算机
  3. 缓存全部由os管
  4. 分时time-sharing
  5. 有批处理、分时和实时os
  6. 中断使得io和cpu可以并行
  7. 多道程序cpu利用率高,吞吐量大,io利用率高
  8. 资源利用率不是实时os目标
  9. 优先级加非抢占式调度,可以改善系统响应时间
  10. 分时系统期待快速响应用户
  11. 定义原语的方式是关闭中断,让其所有动作不可分割的完成之后再打开
  12. 命令解释在用户态进行
  13. 中断处理和子程序调用都会压栈来保护现场,中断一定会保存程序状态字寄存器(PSWR)
  14. 关中断指令不能在用户态
  15. 内部异常处理后,不一定能回到异常指针,可能跳过
  16. 异常也称内中断
  17. 外部中断时,os需要保存通用寄存器内容
  18. 并发和共享是现代os基础

第二章 进程管理

  1. 调度是分配给资源。现有资源调度,后有进程切换
  2. 进程通信,pv是低级操作,高级有:
    1. 共享存储
    2. 消息传递(直接和间接)
    3. 管道通信(半双工)。管道大小可以设置,管道在空或者满的时候,会读或写阻塞。只允许一边写入,一边读出
  3. 进程是为了多道程序,线程是为了提高并发性能。有就绪阻塞运行三种状态。就是轻量级进程
  4. 在支持线程的os中,线程是独立调度的基本单位,进程是拥有资源的基本单位。
  5. 有了线程,则调度的时候可能发生进程切换也可能不发生,所以平均开销变小了。
  6. 设备分配是io系统,不新进程。
  7. P操作会导致阻塞态
  8. Cpu抢占,进程变为就绪态
  9. 不能调度与切换:1)处理中断时,2)临界区,3)源自操作
  10. Preemptive
  11. 周转时间是完成减去提交,带权周转是周转除以实际运行时间
  12. 等待是周转减运行
  13. 响应是提交到首次响应
  14. 高响应比优先调度:响应比R=(等待+要求服务时间)/要求服务时间。它综合考虑了等待时间和执行时间;满足短作业优先且不会饥饿
  15. 为了提高利用率,io越长应该越先

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VXFqf2xp-1583500272226)(file:///C:/Users/Kfor/AppData/Local/Temp/msohtmlclip1/01/clip_image018.png)]

  1. 时间片用完,执行态变就绪态
  2. 临界区也可以调度。比如打印机
  3. 时间片轮转,不会饥饿
  4. 硬件实现同步,可以中断屏蔽或者硬件指令。满足互斥但不有限等待
boolean TestAndSet(boolean *target)  {
    boolean rv = *target;
    *target = true;
    return rv; 
}

// boolean lock = false 对于第i个进程:
do {
    while (TestAndSet(&lock)) ;
    Critical Section
    lock = false;
    Remainder Section
}while(1);
  1. 使用信号量完成前驱关系。只需要画出前驱图,然后每个分支设置一个信号量即可。Semaphore。注意,两个wait在一起,同步wait在互斥wait前
  2. 管程。每次只能有一个进程进入管程。在x.wait(),x是条件变量,则其阻塞该进程并插入x的阻塞队列中
  3. Cs
full = 0, empty = n, mutex = 1
// producer

while (true) {

  // produce an item
  wait (empty);
  wait (mutex);

  // add the item to the buffer
  signal (mutex);
  signal (full);

}

// consumer

while (true) {
    wait (full);
    wait (mutex);

    //remove an item from buffer
    signal (mutex);
    signal (empty);

    // consume the removed item
}
  1. Read and write
mutex = 1, wrt = 1
//readers
readcount = 0
wait(mutex);
readcount++; 
if (readcount == 1)      
wait(rt);
signal(mutex);
//reading is performed

wait(mutex);
readcount--;
if (readcount == 0)
    signal(wrt);
signal(mutex);

//writers
wait(wrt);

//writing is performed
signal(wrt);
  1. 哲学家
semaphore chopstick[5] = 1。

对于第i哲学家:

do {
    wait(chopstick[i]);
    wait(chopstick[(i+1) % 5]);

    eat;

    signal(chopstick[i]);
    signal(chopstick[(i+1) % 5]);

    think;

} while (1); 
  1. 死锁。预防保守,是要破坏四个必要条件(互斥、不剥夺、hold and wait、循环等待),宁可资源闲置。避免是找安全序列。检测是通过剥夺等来检测并解除死锁
  2. 避免:银行家算法
Banker’s Algorithm(Deadlock Avoidance)

Resource-Request Algorithm

1. If Requesti £ Needi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.

2. If Requesti £ Available, go to step 3.  Otherwise Pi must wait, since resources are not available.

3. Pretend to allocate requested resources to Pi by modifying the state as follows:

​       Available = Available - Requesti

​       Allocationi = Allocationi + Requesti

​       Needi = Needi – Requesti

If safe Þ the resources are allocated to Pi 

If unsafe Þ Pi must wait, and the old resource-allocation state is restored

Safety Algorithm

1.Let Work and Finish be vectors of length m and n, respectively. Initialize:
(a) Work = Available
(b) Finish [i] = false for i = 0, 1, …, n- 1.

2.Find an index i such that both:
(a)Finish[i] == false
(b)Requesti £ Work

If no such i exists, go to step 4. 

3.Work = Work + Allocationi
Finish[i] = true
go to step 2.

4.If Finish [i] == true for all i, then the system is in a safe state.
  1. 资源分配图,无环一定没有死锁
  2. 死锁避免不会闲置用户申请资源顺序

第三章 内存管理

  1. 在连接阶段得到逻辑地址

  2. 进程中的块称为页,内存中的称为页帧(frame)外存称为块。

  3. 分页没有外部碎片

  4. 地址分为页号加偏移,偏移12位为4K

  5. 页表用于页号到物理块号的转换

  6. 页表寄存器PTR,存放页表的始址和页表长度

  7. 物理地址E=b(A对L整除)*L+W(A对L取余)

  8. 页表项是用来找到页在内存中的位置。内存以字节为单位。如果也存在内存中,就需要两次访问,找页表和找地址。于是加快第一次,tlb快表加速

  9. 当然,这会导致页表非常大。每个进程都要有一个页表。所以要分级。是的顶级页表最多只有一个页面。

  10. 二级页表例子。4KB的页,4B一个页表项,则一个页能容纳1k个,占用地址10位。偏移12位,所以余下10位。格式为一级页号、二级页号、页内偏移。

  11. 多级页表可以减少页表所占的连续内存空间

  12. 分段,段号,段长,在主存的始址

  1. 可以段页式管理。注意,一个进程可能有多个页表,但只有一个段表
  1. 虚拟内存实际上建立了“内存-外存”量级储存器结构,实现高速缓存。即,页表不一定真实在内存中
  2. 请求调页。需要在原有的分页上,添加状态位、访问字段、修改位、外存地址
  3. 如果不在内存中,就缺页中断。阻塞。(调页时候在唤醒)。内部中断
  4. 如果不够调入,就要页面置换。有opt,fifo,lru,clock(也叫nru)等算法。只有fifo会随着物理块数增大而出现更多页故障(belady)。
  5. Clock:有个使用位,首次装入为1,被访问到也为1,每当需要替换一页,就扫描缓冲区,找到为0的,并把1都置位0。
  6. 改进clock:添加一个修改位。U和m组合,淘汰顺序为00,01,10,11
  7. 不需要吧所有也都读入主存,所以分配给每个进程几个frame,就是进程驻留集。通常有固定分配局部、可变分配全局、可变分配局部。
  8. 抖动是因为物理页帧少。撤销进程。
  9. 增大tlb可以加快虚实地址转换
  10. Lru可以从后往前数,没有用的。

第四章 文件管理

  1. 打开文件时,要用计数器记录多少进程打开该文件。为0才从打开文件表中删除,最后释放fcb。

  2. 每个打开文件都要维护:文件指针,文件打开计数,文件磁盘位置,访问权限

  3. 目录结构:单,两(master fiile dir+user file directory),树,无环图

  4. 目录实现靠线性链表或者hash表

  5. 文件分配,连续、链接、索引

  1. 磁盘存取时间
  1. 磁盘管理

第五章 I/O管理

实验

实验1

  1. tar xvf linux-4.6.tar解压
  2. xz -d patch-4.6.xz | patch -p1补丁
  3. ln -s /usr/src/linux-4.6/ l
  4. make mrproper清.o
  5. make bzImage -jN编译内核
  6. make modules -jN编译内核模块
  7. make modules_install安装模块
  8. make install安装内核
  9. mkinitramfs 4.6.0 -o /boot/initrd.img-4.6.0并update-grub2修改grub。内核bzImage,initrd.img等都放到/boot

QA

  1. .config文件的作用为:内核编译时,主makefile文件调用它来进行内核编译配置。Make menuconfig时,也会自动更新这个值。
  2. Makefile和Kconfig文件的作用:makefile用于集成编译;Kconfig为分布在各目录下的文件,构成了一个分布式的内核配置数据库。每个Kconfig分别描述了所属目录源文件相关的内核配置菜单。在内核配置make menuconfig时,从Kconfig中读出配置菜单,用户配置完后保存到.config(在顶层目录下生成)
  3. System.map-4.6.0和initrd.img-4.6.0的作用:system.map是一个符号表,因为内核直接使用地址来引用变量,所以需要一个变量和地址的映射表。Initrd.img是内核镜像文件,保存了驱动模块等信息。
  4. 有pthread_create(&thread, &attr, &dataArray); pthread_join(thread,NULL); pthread_mutex_lock(mutex); pthread_mutex_unlock(mutex);pthread_mutex_t mutex; pthread_cond_t cond;pthread_cond_signal(cond); pthread_cond_wait(cond, mutex);
  5. gcc -o out in.c -lpthread
  6. 用for(p=init_task; (p=p->next_task!=&init_tak;)或者list_for_each(pos,&(initTask->tasks)来遍历进程;然后其中的p有comm,pid,real_parent, nr_dirtied,state等,state的TASK_RUNNING来判断状态
  7. 要加module_init()来初始化
  8. Make之后insmod .ko dmesg即可
  9. Pwd显示当前工作目录

实验2

  1. Syscall_64.tbl里面修改系统调用号
  2. 在sched.h下,有task_struct进程pcb。有state,stack,usage,flags,ptrace
  3. 在kernel/sys.c中添加我的系统调用
  4. 之后用户态syscall(号),再dmesg
  5. 修改内核法,内核模块法(拦截系统调用,修改其服务)

实验3

  1. Linux/fs/ext2
  2. 全局匹配替换 :%s/ext2/myext2/g
  3. Cat /proc/filesystems | grep myext2
  4. 操作为make; insmod .ko; dd if=/dev/zero of=myfs bs=1M count=1; /sbin/mkfs.ext2 myfs; mount -t myext2 -o loop ./myfs /mnt; mount; umount /mnt; rmmod myext2
  5. 其namei.c下有各个功能的实现;read和write操作在file.c下

操作

  1. Chmod 777 file

文件保护和访问权限:

mode of access: read, write, execute

three classes of users RWX

a) owner access 7 1 1 1
b) group access 6 1 1 0

c) public access 1 0 0 1

  1. Chown user file
  2. Tar -c是打包-x是拆包,-f后接文件名,-v显示全过程
  3. Grep “root” /etc/password
  4. Find . -name “*.h” -print
  5. Ps -a 显示所有进程
  6. Top用来实时显示process的动态
  7. Cat连接文件并打印到标准输出上cat /dev/null > /etc/test.txt
  8. Touch file创建文件
  9. Echo 内容>文件
  10. Echo 内容>>文件(追加)

其他

  1. 在创建Linux分区时,至少要创建的两个分区是: Swap/root
  2. 哪一个只是在内核运行时存在的 : proc
  3. Linux的内核受严格保护,与进程的用户态代码几乎隔绝。若想从用户态进入内核态,可以通过 : int 0x80
  4. 第一个扇区是mbr,读取前512字节。最后两个是55aa
  5. 缺页寄存器CR2
  6. 系统调用号EAX
  7. ESP(Extended Stack Pointer)为扩展栈指针寄存器,是指针寄存器的一种,用于存放函数栈顶指针。与之对应的是EBP(Extended Base Pointer),扩展基址指针寄存器,也被称为帧指针寄存器,用于存放函数栈底指针。
  8. Ds数据段寄存器
  9. Cs代码段寄存器
  10. Rt是实时的意思rt_priority
  11. 在do_fork()函数中首先创建一个task_struct结构体指针,再对传递给do_fork的flag参数进行处理和检查,看当前进程是否设置了跟踪标记ptrace。然后进入了copy_process函数,实现将父进程的寄存器以及所有进程执行环境的相关部分复制给子进程。在copy_process()函数中:首先对一些标志进行合法性检查,检查完之后调用dup_task_struct函数来为新进程创建一个内核栈、thread_info和task_struct,这里完全copy父进程的内容。
  12. 在32位Linux系统中,每个进程都有一个4G的虚拟地址空间,其中3G为进程独有的用户空间,1G为共享的内核空间。
  13. 0到16M是dma用的,16M到896是内核直接使用的,之后是不可以的
  14. Inode索引节点,包含文件的元信息。Size,uid,gid(group id),access,blocks...
  15. Vfs的一个目录项为dentry
  16. Task_struct (comm是文件名

VFS并不是一种实际的文件系统。ext2/ext4等物理文件系统是存在于外存空间的,而VFS仅存在于内存。

通用文件模型由四种数据对象组成:

超级块对象 superblock :存储已安装文件系统的信息,通常对应磁盘文件系统的文件系统超级块或控制块。 是文件系统中描述整体组织和结构的信息体。Linux中对于每种已安装的文件系统,在内存中都有与其对应的超级块

索引节点对象 inode object :存储某个文件的信息。通常对应磁盘文件系统的文件控制块。物理文件系统的inode在外存中并且是长期存在的, VFS的inode 对象在内存中,它仅在需要时才建立,不再需要时撤消(存在于中)

目录项对象dentry object :dentry对象主要是描述一个目录项,是路径的组成部分。

文件对象 file object:存储一个打开文件和一个进程的关联信息。只要文件一直打开,这个对象就一直存在于内存。

进程文件管理

fs_struct结构记录着进程所在文件系统根目录和当前目录,

files_struct结构包含着进程的打开文件表。

题目

  1. 注意缺页时候的块变化问题
  1. The sequential file is good for sequential storage devices, however not fit for disks.是错的
  2. For the same file, different physical organization structures are OK to be defined for different storage media with different file names.正确
  3. Threads that belong to the same process share one set of registers and the same stack错误。堆是共享的栈和寄存器都是独享的
  4. The cache technology is to solve the speed mismatch between CPU and I/O devices, but is not dedicated for that
  5. Consider a file system on a disk that has both logical and physical block size of 512 bytes. Assume that the information about each file is already in memory. For each of the three allocation strategies (contiguous, linked, indexed), answer these questions:

(1). How is the logical-to-physical address mapping accomplished in this system? (For the indexed allocation, assume that a file is always less than 512 blocks long.)

(2). If we are currently at logical block 10 (the last block accessed was 20) and want to access logical block 5, how many physical blocks must be read from the disk?

  1. If a file is accessed in direct access and the file length is not fixed, then it is suitable to use indexed file structure.
  2. 即Simultaneous Peripheral Operations On-Line的缩写,它是关于慢速字符设备如何与计算机主机交换信息一种技术,通常称为“假脱机技术”
  3. The main purpose to introduce buffer technology is to improve the I/O efficiency.是错的
  4. If a file system use two-level index allocation method(block size is 2KB, index address of each block occupies 4 bytes), what is the maximum size of a file which the file system can manage? 512MB
  5. An I/O channel is a special processor

你可能感兴趣的:(操作系统 知识点总结)