1.目的和要求
通过实验理解进程的概念,进程的组成(PCB结构),进程的并发执行和操作系统进行进程管理的相关原语(主要是进程的创建、执行、撤消)。
2.实验内容
用C/C++语言编程模拟进程管理,至少要有:创建新的进程;查看运行进程;换出某个进程;杀死运行进程以及进程之间通信等功能。
3.设计思路
Process类包括以下字段:进程名,进程优先数,轮转时间片,进程所占用的CPU时间,进程的状态;和创建进程,显示进程,取出进程名,取出状态等方法。
在类外设计了7个函数来分别实现各自的功能
- CreateNewprocess(headptr,counter,capacity)创建新的进程函数
- ViewProcess(headptr,counter) 查看运行进程函数
- SwapOutProcess(headptr,counter) 换出新的进程函数
- KillProcess(headptr,counter) 杀死运行进程函数
- SuspendProcess(headptr,counter) 挂起运行进程函数
- WakeupProcess(headptr,counter) 唤醒挂起进函数
- ApplyForNewMemory(headptr,capacity,counter))申请新的内存函数
在主函数int main()中用process类指针headptr指向new的process对象数组,同时还包括以下内容:
- 运用了对象指针链表作为主要的数据结构。
- counter,capacity分别作为进程个数和进程容量的全局变量作为参数传入子函数模块中。
- 宏定义增加空间常量increment为5。
- 运用#include头文件中的time_t Time; time(&Time);操作实现时间实时显示。
- 利用do-while循环与switch选择实现了菜单式界面
- 利用system(“cls”);函数实现清屏操作,增加界面演示效果。
4.源代码
// F:\debug\c++debug\OperatingSystem\ProcessManagement.cpp
// author:1045263694@qq.com
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
# define increment 5
using namespace std;
//以下是进程类的定义
class process
{
friend void SuspendProcess(process *headptr,int counter); //友元函数
friend void WakeupProcess(process *headptr,int counter); //友元函数
protected:
int pid; //进程名
int priority; //进程优先级
int cputime; //进程使用CPU时间
int status; //进程状态
int Rotationtimeslice; //轮转时间片
int large; //进程大小
public:
process(){
} //默认构造函数
process(process &p)
{
p.pid=pid;
//p.priority=priority;
//p.cputime=cputime;
//p.large=large;
p.status=status;
}
virtual ~process(){
}
void display()
{
cout<<"进程名:"<<pid<<endl//<<"优先级:"<<priority<<endl<<"CPU时间:"<<cputime<<endl<<"大小:"<<large<<endl
<<"状态:"<<status<<endl;
}
void setall(int &counter)
{
cout<<"0-正常,1-挂起,2-唤醒" <<endl;
cout<<"请输入新的进程名:"; cin>>pid;
//cout<<"请输入新的进程优先级:"; cin>>priority;
//cout<<"请输入新进程大小:"; cin>>large;
//cout<<"请输入新进程使用CPU时长:"; cin>>cputime;
cout<<"请输入新进程状态:"; cin>>status; //0-正常,1-挂起,2-唤醒
counter++;
}
int Getpid()
{
return pid;
}
int Getstatus()
{
return status;
}
};//process
void CreateNewprocess(process *headptr,