操作系统
操作系统
系统调用(API接口)
内核,用户
中断服务程序一定是操作系统编写的
内核分类:
微内核
聚内核
内核中再分(面向对象内核)
混合结构:Macos
虚拟机结构
系统的引导过程:BIOS——>BOOT
room
进程管理:
进程控制块PCB:结构体
进程绝对隔离,线程完全共享
进程效率低,线程效率高
CPU调度:
CPU-IO突发:
进程分类:
CPU绑定的进程,IO绑定的进程
调度:
进程运行-》等待
等待-》就绪
就绪-》运行
退出
抢占式:非自愿的(主流),通用操作系统
非抢占式:自愿的,嵌入式
分配:
恢复状态(切换上下文),切换到用户状态,找到合适的位置
分配消耗时间由硬件决定
调度的评价指标:
CPU的利用率,吞吐率,周转时间(进程的所有时间),等待时间,响应时间。
CPU的利用率,吞吐率:评价整个系统
周转时间(进程的所有时间),等待时间,响应时间:针对进程,一般采取平均值
优先级调度:多级队列:同一个队列中的优先级相同
常用的数据结构:多级反馈队列——>现在的操作系统常用的处理方式
反馈的实现:基本的策略:所有线程先放在最高优先级,当CPU用的较多的时候优先级下降。动态判断,当从等待状态变为准备状态时,优先级就会上升一点。一般IO绑定的再高优先级。
多核CPU:每一个核心同时只能运行一个进程
特点:负载均衡
核心需均衡:进程迁移,刷新cpu cache
进程的同步:当同时访问共享的数据时,有可能破坏数据。
生产者-消费者问题
进程的切换是随机的,即在一条CPU指令执行完毕之后都会有可能进行中断响应(进程切换)。
解决办法要求:
竞争条件——>临界区:一段代码对共享的数据进行了访问,那这段代码就是临界区
解决的办法:对临界区的保护,在任意时刻只有一个进程可以访问临界区。
策略:锁
怎么给临界区加锁:
必须符合三大定律:互斥条件、进度的条件、有限等待(公平性)
终极解决办法:硬件可以控制中断——>关中断(要求时间间隔短),原子硬件指令。
常见两种指令:
Test memory word and set value:testAndSet
Swap countents of tow memory words
加锁,性能下降
信号量:一个整数,P(),V()操作。wait,signal(原子操作)。
计数信号量:特殊:二制信号量(互斥锁)
硬件支持:开关中断 或 TestAndSet
实现中的问题:忙等——>自旋。解决的办法:将其放入等待态。信号量维护了一个等待队列,当有资源时唤醒。
Block,wakeup
经典进程通信问题:
生产者-消费者问题:缓冲区,定义三个信号量:互斥信号量(保护缓冲区),空余缓冲区量,缓冲区中有多少个满的
读者-写者问题:一些进程属于读者,读不会破坏数据。一些进程属于写者,会破坏数据。同一时刻只能有一个写者操作,且不能对读者有影响(只能在没有读者的情况下写)。解决:三个信号量——>读写者互斥信号量、
存储:文件系统、数据库(事务解决了读者写者问题)。策略:更改了指针的指向,回收旧内存
哲学家就餐问题:——>死锁
管程:一种更高阶的对临界区进行保护的方法&#x