Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
34a463b6
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
34a463b6
编写于
6月 30, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
327a02eb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
2 addition
and
301 deletion
+2
-301
notes/Linux.md
notes/Linux.md
+2
-2
notes/计算机操作系统.md
notes/计算机操作系统.md
+0
-299
未找到文件。
notes/Linux.md
浏览文件 @
34a463b6
...
...
@@ -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
...
...
notes/计算机操作系统.md
浏览文件 @
34a463b6
...
...
@@ -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. 临界区
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录