用c++STL实现进程管理
项目要求:
设计一个允许n个进程并发运行的进程管理模拟系统。该系统包括有简单的进程控制,其进程调度采用时间片轮转算法。每个进程用一个PCB表示,其内容根据具体情况设置。各进程之间有一定的同步关系(可选)。系统在运行过程中应能显示或打印各进程的状态及有关参数的变化情况,以便观察诸进程的运行过程及系统的管理过程。
时间片轮转法(Round-Robin,RR)主要用于分时系统中的进程调度。为了实现轮转调度,系统把所有就绪进程按先入先出的原则排成一个队列。新来的进程加到就绪队列末尾。每当执行进程调度时,进程调度程序总是选出就绪队列的队首进程,让它在CPU上运行一个时间片的时间。时间片是一个小的时间单位,通常为10~100ms数量级。当进程用完分给它的时间片后,系统的计时器发出时钟中断,调度程序便停止该进程的运行,把它放入就绪队列的末尾;然后,把CPU分给就绪队列的队首进程,同样也让它运行一个时间片,如此往复。
我们使用queue和priority_queue分别模拟就绪队列run和阻塞队列wait,每次CPU处理的即为就绪队列run的队头。
pcb进程块包含name-进程名;runtime-进程剩余时间;runedtime-占用CPU的总时间;killtime-单次占用CPU的时间;waitpoint-每次减时间片,当不大于0时将进程加入阻塞队列;waittime-等待时长;
项目代码:
#include<bits/stdc++.h>
using namespace std; struct PCB{
int name;
int runtime;
int runedtime;
int state;
int killtime;
int waitpoint;
int waittime;
}; struct cmp{
bool operator()(PCB a,PCB b){
return a.waittime>b.waittime;
}
}; int n=,timeslice=; queue<PCB> run; priority_queue<PCB,vector<PCB>,cmp> wait; void Init(){
srand((unsigned)time(NULL));
for(int i=;i<n;i++){ PCB temp;
temp.name=i;
temp.runtime=rand()%+;
temp.runedtime=;
temp.killtime=;
temp.waitpoint=rand()%+;
temp.waittime=rand()%+;
//cout<<temp.runtime<<" "<<temp.waitpoint<<" "<<temp.waittime<<endl;
run.push(temp);
cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
<<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl;
}
cout<<"以上为各进程初始参数"<<endl;
cout<<endl;
} void Run(){
while(run.size()>||wait.size()>){
if(run.size()){
PCB temp=run.front();
run.pop();
temp.waitpoint-=timeslice;
if(temp.waitpoint>){//不阻塞
temp.runtime-=timeslice;
if(temp.runtime<=){
temp.killtime=temp.runtime+timeslice;
temp.runedtime=temp.runedtime+temp.killtime;
temp.runtime=; cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
<<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl<<"进程"<<temp.name<<"已结束"<<endl;
}
else{
temp.runedtime+=timeslice;
temp.killtime=timeslice;
run.push(temp); cout<<"name "<<temp.name<<","<<"runtime="<<temp.runtime<<","<<"runedtime="
<<temp.runedtime<<","<<"killtime="<<temp.killtime<<","<<"waitpoint="<<
temp.waitpoint<<","<<"waittime="<<temp.waittime<<endl;
}
}
else{
cout<<"进程"<<temp.name<<"产生阻塞"<<endl;
temp.waitpoint=;
wait.push(temp);
}
}
while(wait.size()){
PCB temp;
temp=wait.top();
wait.pop();
temp.waittime-=timeslice;
if(temp.waittime>){
wait.push(temp);
break;
}
else{
cout<<"进程"<<temp.name<<"即将被唤醒"<<endl;
run.push(temp);
}
}
cout<<endl;
}
cout<<endl<<endl<<endl<<"***********END***********"<<endl;
cout<<"****Designed by Kiven****"<<endl;
} int main(){
ios::sync_with_stdio(false);
Init();
Run();
return ;
}
用c++STL实现进程管理的更多相关文章
- 《Linux内核设计与实现》读书笔记 第三章 进程管理
第三章进程管理 进程是Unix操作系统抽象概念中最基本的一种.我们拥有操作系统就是为了运行用户程序,因此,进程管理就是所有操作系统的心脏所在. 3.1进程 概念: 进程:处于执行期的程序.但不仅局限于 ...
- 进程管理三大扩展工具htop
三大进程管理监控工具 HTOP 介绍: Htop是一款运行于Linux系统监控与进程管理软件,htop提供所有进程的列表,并且使用彩色标识出处理器.swap和内存状态.用户一般可以在top无法提供详尽 ...
- Linux进程管理子系统分析【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51298732 Linux进程管理: 进程与程序: 程序:存放在磁盘上的一系列代码 ...
- Linux下取代top的进程管理工具 htop
一.htop 简介 This is htop, an interactive process viewer for Linux. It is a text-mode application (for ...
- Linux进程管理
一.进程管理简介 进程是正在执行的程序或命令,每一个进程都是一个运行实体,都有自己的地址空间,并占用一定的系统资源. 进程管理的作用: 1.判断服务器的健康状态 2.查看系统中的所有进程 3.杀死进程 ...
- C++ Windows进程管理
功能: 1.各个进程启动.挂起.恢复.停止等 2.监听进程的运行状态,进程退出(正常.非正常)时,通知用户 3.异步队列 4.线程安全 进程管理器类: #ifndef __ProcessManager ...
- 12个Linux进程管理命令介绍(转)
12个Linux进程管理命令介绍 [日期:2015-06-02] 来源:Linux中国 作者:Linux [字体:大 中 小] 执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的 ...
- 理解Docker容器的进程管理
摘要: Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per container)&qu ...
- Android内存进程管理机制
参考文章: http://www.apkbus.com/android-104940-1-1.htmlhttp://blog.sina.com.cn/s/blog_3e3fcadd0100yjo2.h ...
随机推荐
- 怎么查看自己的IP地址?
https://jingyan.baidu.com/article/63f2362816d56c0208ab3dd5.html 1.通过自己的电脑查看的是内部局域网的IP地址 2.通过网上查看的IP地 ...
- MaLoc: a practical magnetic fingerprinting approach to indoor localization using smartphones
https://www.indooratlas.com/ MaLoc: a practical magnetic fingerprinting approach to indoor localizat ...
- 列举你了解的Python较其他语言的优势
1.简单易学 2.开发速度快 3.拥有最成熟的程序包资源库(第三方库)
- T_CODE I18N
关于T-CODE I18N 最近由于看到很多人遇到SMARTFORMS不能拖拽字段的问题,这个的解决方案 I18N:解决SMARTFORMS的不能从Field name 那边直接把变量拖入右边编辑框 ...
- Pandas一些小技巧
Pandas有一些不频繁使用容易忘记的小技巧 1.将不同Dataframe写在一个Excel的不同Sheet,或添加到已有Excel的不同Sheet(同名Sheet会覆盖) from pandas i ...
- Linux下Mysql数据库忘记root
系统环境:Red Hat Enterprise Linux Server 6 1.停止mysqld服务 [root@Server huage]# service mysqld stop 2.以跳过授权 ...
- Java for LeetCode 137 Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- ubuntu下安装redis以及redis客户端在mac下的使用
ubuntu下安装redis http://blog.fens.me/linux-redis-install/ 此方式利用brew安装包去获取最新的rdm客户端 资源失效了 https://www.j ...
- POJ 1330 Nearest Common Ancestors 【最近公共祖先LCA算法+Tarjan离线算法】
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20715 Accept ...
- HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...