C++实现操作系统进程调度模拟及开发建议
### 🍭准备工作 要编译和运行这个 C++ 程序,你需要安装一个 C++ 编译器,例如 `g++`。在大多数 Linux 发行版中,可以使用以下命令安装: ```bash sudo apt-get install g++ ``` 在 macOS 上,可以使用 Homebrew 安装: ```bash brew install gcc ``` 在 Windows 上,可以安装 MinGW 或 Visual Studio 来获得 C++ 编译环境。 ### 📝代码实现 ```cpp #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; // 定义进程结构体 struct Process { int id; int arrivalTime; int executionTime; int priority; int remainingTime; int endTime; Process(int _id, int _arrivalTime, int _executionTime, int _priority) : id(_id), arrivalTime(_arrivalTime), executionTime(_executionTime), priority(_priority), remainingTime(_executionTime), endTime(0) {} // 重载小于运算符,用于优先队列 bool operator<(const Process& other) const { if (priority != other.priority) { return priority < other.priority; } return arrivalTime > other.arrivalTime; } }; // 模拟进程调度 void processScheduling(vector<Process>& processes) { int currentTime = 0; int index = 0; priority_queue<Process> waitingQueue; Process* currentProcess = nullptr; while (index < processes.size() ||!waitingQueue.empty() || currentProcess) { // 将到达的进程加入等待队列 while (index < processes.size() && processes[index].arrivalTime <= currentTime) { waitingQueue.push(processes[index]); index++; } // 如果当前进程为空,从等待队列中取出一个进程 if (!currentProcess &&!waitingQueue.empty()) { currentProcess = new Process(waitingQueue.top()); waitingQueue.pop(); } // 如果有更高优先级的进程,切换进程 if (!waitingQueue.empty() && waitingQueue.top().priority > currentProcess->priority) { waitingQueue.push(*currentProcess); delete currentProcess; currentProcess = new Process(waitingQueue.top()); waitingQueue.pop(); } // 执行当前进程 if (currentProcess) { currentProcess->remainingTime--; currentTime++; if (currentProcess->remainingTime == 0) { currentProcess->endTime = currentTime; delete currentProcess; currentProcess = nullptr; } } else { // 如果没有进程在执行,时间推进到下一个进程到达的时间 if (index < processes.size()) { currentTime = processes[index].arrivalTime; } } } // 按结束时间排序并输出结果 sort(processes.begin(), processes.end(), [](const Process& a, const Process& b) { return a.endTime < b.endTime; }); for (const auto& process : processes) { cout << process.id << " " << process.endTime << endl; } } int main() { vector<Process> processes; int id, arrivalTime, executionTime, priority; while (cin >> id >> arrivalTime >> executionTime >> priority) { processes.emplace_back(id, arrivalTime, executionTime, priority); } processScheduling(processes); return 0; } ``` ### 💡代码解释 1. **`Process` 结构体**:用于存储进程的相关信息,包括进程号、到达时间、执行时间、优先级、剩余执行时间和结束时间。重载了小于运算符,用于优先队列的排序。 2. **`processScheduling` 函数**:模拟进程调度的核心函数。使用一个优先队列 `waitingQueue` 来存储等待的进程,使用 `currentProcess` 指针来表示当前正在执行的进程。在每一个时间点,将到达的进程加入等待队列,检查是否有更高优先级的进程需要切换,执行当前进程,直到所有进程都执行完毕。 3. **`main` 函数**:读取输入数据,将每个进程添加到 `processes` 向量中,调用 `processScheduling` 函数进行调度,并输出结果。 ### 🚀进阶使用指南 - **性能优化**:可以使用更高效的数据结构来存储和管理进程,例如红黑树。 - **错误处理**:在读取输入数据时,可以添加输入验证,确保输入的合法性。 - **扩展功能**:可以添加更多的调度算法,例如先来先服务(FCFS)、短作业优先(SJF)等。 - **安全建议**:在动态分配内存时,要确保及时释放内存,避免内存泄漏。 - **常见陷阱规避**:注意优先队列的排序规则,确保优先级高的进程先被执行。 ### 📚相关技术栈推荐和学习资源 - **相关技术栈**:C++ 标准库中的 `vector`、`queue`、`algorithm` 等。 - **学习资源**: - C++ 官方文档:https://en.cppreference.com/w/ - 《C++ Primer》:经典的 C++ 学习书籍。 ### 💡开发建议 - **模块化设计**:将不同的功能封装成独立的函数,提高代码的可读性和可维护性。 - **注释和文档**:添加必要的注释,解释代码的功能和逻辑,方便自己和他人理解代码。 - **测试用例**:编写测试用例,对代码进行全面的测试,确保代码的正确性。 ######[AI写代码神器 | 1321点数解答 | 2026-07-26 14:46:29]
- C语言实现:根据图书单价与数量计算应付总费用 (字节豆包 | 291点数解答 | 2024-11-04 22:11:11)378
- C++编程:图书购买费用计算及输入输出处理要点(字节豆包 | 347点数解答 | 2025-11-03 14:32:27)113
- 计算多本计算机图书总价:Python、Java、C++ 三种语言实现(字节豆包 | 1013点数解答 | 2025-12-07 17:30:24)136
- Python 编程:根据图书单价和数量精准计算应付总费用(字节豆包 | 344点数解答 | 2025-12-14 13:10:04)81
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)538
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)456
- Python 实现 DES 加密算法编程实验:从原理到单步调试全解析(字节豆包 | 4096点数解答 | 2025-11-09 22:06:30)128
- C++实现操作系统进程调度模拟及开发建议(字节豆包 | 1321点数解答 | 2026-07-26 14:46:29)7
- C++实现操作系统进程调度模拟程序及开发建议(字节豆包 | 1206点数解答 | 2026-07-26 14:49:00)8
- C++实现计算字符矩阵中十字图形个数(字节豆包 | 333点数解答 | 2026-03-14 19:46:38)101
- C++实现矩阵中十字图形个数的计算(字节豆包 | 847点数解答 | 2026-03-14 20:19:15)112
- C++计算字符矩阵中十字图形个数的实现与开发建议(字节豆包 | 720点数解答 | 2026-04-10 19:42:04)88