auto commit

This commit is contained in:
CyC2018 2018-06-30 18:49:48 +08:00
parent 327a02eb72
commit 34a463b688
2 changed files with 2 additions and 301 deletions

View File

@ -187,8 +187,6 @@ GNU 计划,译为革奴计划,它的目标是创建一套完全自由的操
## HDD
[Decoding UCS Invicta Part 1](https://blogs.cisco.com/datacenter/decoding-ucs-invicta-part-1)
Hard Disk Drives(HDD) 俗称硬盘,具有以下结构:
- 盘面Platter一个硬盘有多个盘面
@ -200,6 +198,8 @@ Hard Disk Drives(HDD) 俗称硬盘,具有以下结构:
<div align="center"> <img src="../pics//014fbc4d-d873-4a12-b160-867ddaed9807.jpg" width=""/> </div><br>
[Decoding UCS Invicta Part 1](https://blogs.cisco.com/datacenter/decoding-ucs-invicta-part-1)
## 磁盘接口
### 1. IDE

View File

@ -9,7 +9,6 @@
* [进程与线程](#进程与线程)
* [进程状态的切换](#进程状态的切换)
* [进程调度算法](#进程调度算法)
* [进程调度算法实现](#进程调度算法实现)
* [进程同步](#进程同步)
* [经典同步问题](#经典同步问题)
* [进程通信](#进程通信)
@ -240,304 +239,6 @@ Linux 的系统调用主要有以下这些:
分为硬实时和软实时,前者必须满足绝对的截止时间,后者可以容忍一定的超时。
## 进程调度算法实现
以下只是假象系统上的调度算法实现。源代码:[Scheduling](https://github.com/CyC2018/Algorithm/tree/master/Process-Scheduling/src)
### 1. FCFS
首先创建进程数据结构:
```java
public class Process {
private String name;
private long totalTime;
private long remainTime;
private long comeInTime;
public Process(String name, long totalTime, long comeInTime) {
this.name = name;
this.totalTime = totalTime;
this.remainTime = totalTime;
this.comeInTime = comeInTime;
}
public void run(long runTime) {
System.out.println("process " + name + " is running...");
System.out.println("come in time : " + comeInTime);
System.out.println("total time : " + totalTime);
System.out.println("remain time : " + remainTime);
System.out.println();
remainTime -= runTime;
try {
Thread.sleep(runTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public long getTotalTime() {
return totalTime;
}
public long getRemainTime() {
return remainTime;
}
public long getComeInTime() {
return comeInTime;
}
}
```
接着创建一个进程等待队列数据结构:
```java
public interface ProcessQueue {
void add(Process process);
Process get();
boolean isEmpty();
}
```
FCFS 算法使用普通的先进先出队列即可实现该进程等待队列:
```java
public class FCFSProcessQueue implements ProcessQueue {
private Queue<Process> queue = new LinkedList<>();
@Override
public void add(Process process) {
queue.add(process);
}
@Override
public Process get() {
return queue.poll();
}
@Override
public boolean isEmpty() {
return queue.isEmpty();
}
}
```
接下来是调度器的实现,把它设计成可独立运行的 Thread它对进程等待队列进行轮询如果有进程的话就从队列中取出一个任务来执行。批处理系统中调度器调度一个进程都直接让进程执行完毕也就是给进程运行时间为进程的总时间。
```java
public class Scheduler extends Thread {
protected ProcessQueue processQueue;
public Scheduler(ProcessQueue processQueue) {
this.processQueue = processQueue;
}
}
```
```java
public class BatchScheduler extends Scheduler {
public BatchScheduler(ProcessQueue processQueue) {
super(processQueue);
}
@Override
public void run() {
while (true) {
if (!processQueue.isEmpty()) {
Process process = processQueue.get();
process.run(process.getTotalTime());
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
使用一个模拟器来模拟实际场景下进程的不定时到达,每 1s 到达一个进程,并且进程的运行时间再 0s \~ 3s 之间,使得多个进程会发生竞争关系。
```java
public class ProcessComeEmulator extends Thread {
private ProcessQueue processQueue;
public ProcessComeEmulator(ProcessQueue processQueue) {
this.processQueue = processQueue;
}
@Override
public void run() {
int processId = 0;
while (true) {
System.out.println("process " + processId + " is coming...");
System.out.println();
Process process = new Process((processId++) + "", getRandomTime(), System.currentTimeMillis());
processQueue.add(process);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private long getRandomTime() {
return (long) (Math.random() * 3000);
}
}
```
最后创建一个客户端来运行整个调度程序:
```java
public class FCFSClient {
public static void main(String[] args) {
ProcessQueue processQueue = new FCFSProcessQueue();
ProcessComeEmulator processComeEmulator = new ProcessComeEmulator(processQueue);
processComeEmulator.start();
Scheduler scheduler = new FCFSScheduler(processQueue);
scheduler.start();
}
}
```
执行结果如下:
```html
process 0 is coming...
process 0 is running...
come in time : 1528964425691
total time : 1807
remain time : 1807
process 1 is coming...
process 1 is running...
come in time : 1528964426691
total time : 2311
remain time : 2311
process 2 is coming...
process 3 is coming...
process 4 is coming...
process 2 is running...
come in time : 1528964427692
total time : 2651
remain time : 2651
process 5 is coming...
process 6 is coming...
process 3 is running...
come in time : 1528964428692
total time : 137
remain time : 137
process 4 is running...
come in time : 1528964429692
total time : 2513
remain time : 2513
```
### 2. SJF
与 FCFS 不同的是SJF 的进程等待队列需要使用优先级队列来实现:
```java
public class SJFProcessQueue implements ProcessQueue {
private PriorityQueue<Process> processesQueue = new PriorityQueue<>(
(o1, o2) -> (int) (o1.getTotalTime() - o2.getTotalTime()));
@Override
public void add(Process process) {
processesQueue.add(process);
}
@Override
public Process get() {
return processesQueue.poll();
}
@Override
public boolean isEmpty() {
return processesQueue.isEmpty();
}
}
```
运行客户端:
```java
public class SJFClient {
public static void main(String[] args) {
ProcessQueue processQueue = new SJFProcessQueue();
ProcessComeEmulator processComeEmulator = new ProcessComeEmulator(processQueue);
processComeEmulator.start();
Scheduler scheduler = new BatchScheduler(processQueue);
scheduler.start();
}
}
```
```java
process 0 is coming...
process 0 is running...
come in time : 1528964250005
total time : 2496
remain time : 2496
process 1 is coming...
process 2 is coming...
process 1 is running...
come in time : 1528964251006
total time : 903
remain time : 903
process 3 is coming...
process 2 is running...
come in time : 1528964252006
total time : 1641
remain time : 1641
process 4 is coming...
process 5 is coming...
process 4 is running...
come in time : 1528964254007
total time : 243
remain time : 243
process 5 is running...
come in time : 1528964255007
total time : 646
remain time : 646
process 3 is running...
come in time : 1528964253006
total time : 2772
remain time : 2772
```
## 进程同步
### 1. 临界区