2026-02-06 03:34:50 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="demo-container">
|
|
|
|
|
<h4>进程内存隔离演示</h4>
|
|
|
|
|
|
|
|
|
|
<div class="controls">
|
2026-02-18 17:38:10 +08:00
|
|
|
<el-button
|
|
|
|
|
type="primary"
|
|
|
|
|
size="small"
|
|
|
|
|
:disabled="processes.length >= 4"
|
|
|
|
|
@click="addProcess"
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
创建进程
|
|
|
|
|
</el-button>
|
2026-02-18 17:38:10 +08:00
|
|
|
<el-button
|
|
|
|
|
type="danger"
|
|
|
|
|
size="small"
|
|
|
|
|
:disabled="processes.length === 0"
|
|
|
|
|
@click="killProcess"
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
结束进程
|
|
|
|
|
</el-button>
|
2026-02-18 17:38:10 +08:00
|
|
|
<el-button
|
|
|
|
|
size="small"
|
|
|
|
|
@click="simulateCrash"
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
模拟进程崩溃
|
|
|
|
|
</el-button>
|
2026-02-18 17:38:10 +08:00
|
|
|
<el-button
|
|
|
|
|
size="small"
|
|
|
|
|
@click="reset"
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
重置
|
|
|
|
|
</el-button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="memory-view">
|
2026-02-18 17:38:10 +08:00
|
|
|
<div class="memory-label">
|
|
|
|
|
系统内存
|
|
|
|
|
</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
<div class="memory-blocks">
|
|
|
|
|
<div
|
|
|
|
|
v-for="process in processes"
|
|
|
|
|
:key="process.id"
|
|
|
|
|
class="process-block"
|
|
|
|
|
:class="{ crashed: process.crashed, active: process.active }"
|
|
|
|
|
:style="{ width: process.size + '%', backgroundColor: process.color }"
|
|
|
|
|
>
|
|
|
|
|
<div class="process-header">
|
|
|
|
|
<span class="process-name">进程 {{ process.id }}</span>
|
|
|
|
|
<span class="process-pid">PID: {{ process.pid }}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="process-memory">
|
|
|
|
|
<div class="memory-section code">
|
|
|
|
|
<span class="section-label">代码段</span>
|
|
|
|
|
<span class="section-size">{{ process.codeSize }}MB</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="memory-section data">
|
|
|
|
|
<span class="section-label">数据段</span>
|
|
|
|
|
<span class="section-size">{{ process.dataSize }}MB</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="memory-section heap">
|
|
|
|
|
<span class="section-label">堆</span>
|
|
|
|
|
<span class="section-size">{{ process.heapSize }}MB</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="memory-section stack">
|
|
|
|
|
<span class="section-label">栈</span>
|
|
|
|
|
<span class="section-size">{{ process.stackSize }}MB</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-02-18 17:38:10 +08:00
|
|
|
<div
|
|
|
|
|
v-if="process.crashed"
|
|
|
|
|
class="crash-overlay"
|
|
|
|
|
>
|
2026-02-06 03:34:50 +08:00
|
|
|
<span class="crash-text">💥 已崩溃</span>
|
|
|
|
|
<span class="crash-info">不影响其他进程</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-02-18 17:38:10 +08:00
|
|
|
<div
|
|
|
|
|
v-if="showSharedMemory"
|
|
|
|
|
class="shared-memory"
|
|
|
|
|
>
|
|
|
|
|
<div class="shared-label">
|
|
|
|
|
共享内存区域 (IPC)
|
|
|
|
|
</div>
|
2026-02-06 03:34:50 +08:00
|
|
|
<div class="shared-content">
|
2026-02-18 17:38:10 +08:00
|
|
|
<div
|
|
|
|
|
v-for="process in processes"
|
|
|
|
|
:key="process.id"
|
|
|
|
|
class="shared-access"
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
class="access-indicator"
|
|
|
|
|
:style="{ backgroundColor: process.color }"
|
|
|
|
|
/>
|
2026-02-06 03:34:50 +08:00
|
|
|
<span>进程 {{ process.id }} 可以访问</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="info-panel">
|
|
|
|
|
<el-alert
|
|
|
|
|
:title="currentInfo.title"
|
|
|
|
|
:type="currentInfo.type"
|
|
|
|
|
:description="currentInfo.description"
|
|
|
|
|
show-icon
|
|
|
|
|
:closable="false"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
|
|
|
|
|
|
const processes = ref([])
|
|
|
|
|
const showSharedMemory = ref(false)
|
|
|
|
|
const colors = ['#409eff', '#67c23a', '#e6a23c', '#f56c6c']
|
|
|
|
|
let pidCounter = 1000
|
|
|
|
|
|
|
|
|
|
const currentInfo = computed(() => {
|
|
|
|
|
if (processes.value.length === 0) {
|
|
|
|
|
return {
|
|
|
|
|
title: '进程隔离',
|
|
|
|
|
type: 'info',
|
|
|
|
|
description: '每个进程拥有独立的虚拟地址空间,一个进程崩溃不会影响其他进程。点击"创建进程"开始演示。'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const crashed = processes.value.filter(p => p.crashed).length
|
|
|
|
|
if (crashed > 0) {
|
|
|
|
|
return {
|
|
|
|
|
title: '隔离性验证',
|
|
|
|
|
type: 'success',
|
|
|
|
|
description: `进程已崩溃但其他进程正常运行,证明进程间内存隔离有效。崩溃的进程会被操作系统回收资源。`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
title: '内存布局',
|
|
|
|
|
type: 'info',
|
|
|
|
|
description: `当前有 ${processes.value.length} 个进程在运行。每个进程的内存分为代码段、数据段、堆和栈,相互隔离不可访问。`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function killProcess() {
|
|
|
|
|
if (processes.value.length === 0) return
|
|
|
|
|
processes.value.pop()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function simulateCrash() {
|
|
|
|
|
if (processes.value.length === 0) return
|
|
|
|
|
|
|
|
|
|
// 随机让一个未崩溃的进程崩溃
|
|
|
|
|
const candidates = processes.value.filter(p => !p.crashed)
|
|
|
|
|
if (candidates.length > 0) {
|
|
|
|
|
const victim = candidates[Math.floor(Math.random() * candidates.length)]
|
|
|
|
|
victim.crashed = true
|
|
|
|
|
victim.active = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
|
processes.value = []
|
|
|
|
|
showSharedMemory.value = false
|
|
|
|
|
pidCounter = 1000
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.demo-container {
|
|
|
|
|
padding: 20px;
|
|
|
|
|
background: #f5f7fa;
|
2026-02-14 20:23:34 +08:00
|
|
|
border-radius: 6px;
|
2026-02-06 03:34:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h4 {
|
|
|
|
|
margin: 0 0 16px 0;
|
|
|
|
|
color: #303133;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.controls {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
margin-bottom: 16px;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.memory-view {
|
|
|
|
|
background: white;
|
2026-02-14 20:23:34 +08:00
|
|
|
border-radius: 6px;
|
2026-02-06 03:34:50 +08:00
|
|
|
padding: 16px;
|
|
|
|
|
margin-bottom: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.memory-label {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
color: #303133;
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.memory-blocks {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-block {
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
padding: 12px;
|
|
|
|
|
color: white;
|
|
|
|
|
transition: all 0.3s;
|
|
|
|
|
position: relative;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-block.crashed {
|
|
|
|
|
opacity: 0.5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-block.active {
|
|
|
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-header {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-name {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-pid {
|
|
|
|
|
opacity: 0.8;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.process-memory {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
font-size: 11px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.memory-section {
|
|
|
|
|
background: rgba(255, 255, 255, 0.2);
|
|
|
|
|
padding: 4px 8px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.section-label {
|
|
|
|
|
opacity: 0.7;
|
|
|
|
|
font-size: 10px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.section-size {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.crash-overlay {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
background: rgba(0, 0, 0, 0.7);
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.crash-text {
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.crash-info {
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
opacity: 0.8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.shared-memory {
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
padding: 12px;
|
|
|
|
|
background: #f4f4f5;
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
border: 2px dashed #c0c4cc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.shared-label {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
color: #606266;
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.shared-content {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.shared-access {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 4px;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
color: #606266;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.access-indicator {
|
|
|
|
|
width: 8px;
|
|
|
|
|
height: 8px;
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.info-panel {
|
|
|
|
|
margin-top: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@media (max-width: 768px) {
|
|
|
|
|
.process-memory {
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cpu-cores {
|
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|