操作系统实验-进程控制(C++)

实验目的

能够模拟操作系统内核对进程的控制和管理:包括进程的创建和撤销、进程状态的切换和简单的内存空间管理。

实验内容

  1. 能够模拟进程的创建和撤销过程
  2. 对进程的状态进行全面的控制
  3. 按先进先出的方式管理就绪和阻塞队列,按队列输出进程状态
  4. 完成可变分区的分配和回收
  5. 界面清晰友好
  6. 实验结束后撰写实验报告

数据结构

两条链表,构成两个对列,running_list和blocked_list队列;

running_list存放当前正在运行的线程,默认头结点是正在运行的线程,其余后继结点是就绪状态的进程;

blocked_list存放当先处于阻塞状态中的线程;

算法设计

创建:判断内存是否已满,判断是否重名,均满足条件后,输入该进程其他信息,并将其加入就绪队列

展示:展示运行进程和就绪进程

阻塞:判断是否是就绪队列中的运行进程,满足条件,将该运行进程插入到阻塞队列中

唤醒:判断该进程是否在阻塞队列中,满足条件,将其插入到就绪队列中,等待运行

终止:判断该进程是否在运行进程中,满足条件,找到并释放

实验流程图

实验代码

#include<iostream>
#include<malloc.h>
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
//进程状态
enum Process_State
{
    Pro_State_blocked = 1,
    Pro_State_running = 2,
};
//PCB结构体
typedef struct PCB{
    int id;
    int size;
    char content[20];
    int state;
    struct PCB *next;
}PCB;
PCB *running_list = (PCB *)malloc(sizeof(PCB));
PCB *blocked_list = (PCB *)malloc(sizeof(PCB));
void create(PCB *running_list, PCB *blocked_list, int *size);
void show(PCB *running_list);
void change(PCB *running_list, PCB *blocked_list, int *size);
void kill(PCB *running_list, int *size);
void awake(PCB *running_list, int *blocked_list, int *size);
int isExist_running(PCB *running_list, int id);
int isExist_blocked(PCB *running_list, int id);
PCB *find(PCB *list, int id);
void create(PCB *running_list, PCB *blocked_list, int *size)
{
    //无效判断 测试用例大小为10000时 无法实现 留待改进
    PCB *pro = (PCB *)malloc(sizeof(PCB));
    assert(pro != NULL);
    int id;
    cout<<"请输入进程名,判断是否重复"<<endl;
    cin >> id;
    //cout<<"1111"<<endl;
    //判断进程是否重名,分两种,在运行和阻塞
    //为什么出错?
    //发现错误 main函数中阻塞队列出错
    if(isExist_running(running_list,id)){
        cout << "进程重名了,换个名字继续吧" << endl;
        return;
    }
    if(isExist_blocked(blocked_list,id)){
        cout << "进程重名啦,换个名字继续吧" << endl;
        return;
    }
    //没重名
    pro->id=id;
    //输入PCB的其他属性值
    cout<<"请输入进程的大小"<<endl;
    cin>>pro->size;
    if (pro->size >= 1024)
    {
        cout << "内存不足,换个小一点的程序吧" << endl;
        return;
    }
    cout<<"请输入进程的内容"<<endl;
    cin>>pro->content;
    pro->state=Pro_State_running;
    pro->next=NULL;
    PCB *npro=running_list;
    while(npro->next!=NULL){
        npro = npro->next;
    }
    npro->next= pro;
    *size=*size+1;
}
void show(PCB *running_list)
{
    PCB *npro = running_list->next;
    if (npro == NULL)
    {
        cout << "该时刻没有正在运行的进程" << endl;
        return;
    }
    while (npro != NULL)
    {
        cout << "进程名:" << npro->id << endl;
        cout << "进程大小:" << npro->size << endl;
        cout << "进程内容:" << npro->content << endl;
        npro = npro->next;
    }
}
void change(PCB *running_list, PCB *blocked_list, int *size)
{
    if (*size == 0)
    {
        cout << "没有可以阻塞的进程" << endl;
        return;
    }
    int id;
    cout << "请输入需要阻塞的进程名字:";
    cin >> id;
    //判断进程是不是在就绪队列中
    if (isExist_running(running_list, id))
    {
        //在,找到位置
        PCB *npro = find(running_list, id);
        //修改这个进程的状态
        npro->next->state = Pro_State_blocked;
        //放在就绪队列,findposition  insert move
        PCB *pro = blocked_list;
        while (pro->next != NULL)
        {
            pro = pro->next;
        }
        pro->next = npro->next;
        npro->next = npro->next->next;
        pro->next->next = NULL;
        *size = *size - 1;
        cout << "已经将进程换出到阻塞队列" << endl;
    }
    else
        cout << "改进程不存在或者已经在就绪队列中" << endl;
}
void kill(PCB *running_list, int *size)
{
    if (*size == 0)
    {
        cout << "没有可以杀死的进程" << endl;
        return;
    }
    int id;
    cout << "请输入要杀死的进程:" << endl;
    cin >> id;
    //判断是否存在 find reserve move delete
    if (isExist_running(running_list, id))
    {
        PCB *npro = find(running_list, id);
        PCB *location = npro->next;
        npro->next = npro->next->next;
        *size = *size - 1;
        free(location);
        cout << "已经杀死该进程" << endl;
    }
    else
        cout << "该进程不存在,或者已处于阻塞队列中" << endl;
}
void awake(PCB *running_list, PCB *blocked_list, int *size)
{
    PCB *npro = blocked_list;
    if (npro->next == NULL)
    {
        cout << "没有可以唤醒的进程" << endl;
        return;
    }
    int id;
    cout << "请输入要唤醒的进程名字:" << endl;
    cin >> id;
    //判断是否存在 find return change insert
    if (isExist_blocked(blocked_list, id))
    {
        npro = find(blocked_list, id);
        npro->next->state = Pro_State_running;
        PCB *pro = running_list;
        while (pro->next != NULL)
        {
            pro = pro->next;
        }
        pro->next = npro->next;
        npro->next = npro->next->next;
        pro->next->next = NULL;
        *size = *size + 1;
        cout << "已成功唤醒进程" << endl;
    }
    else
        cout <<"改进程不存在" << endl;
}
int isExist_running(PCB *running_list, int id)
{
    int result = 0;
    PCB *pro = running_list->next;
    while (pro != NULL)
    {
        if (pro->id == id)
        {
            result = 1;
            break;
        }
        pro = pro->next;
    }
    return result;
}
int isExist_blocked(PCB *blocked_list, int id)
{
    int result = 0;
    PCB *pro = blocked_list->next;
    while (pro != NULL)
    {
        if (pro->id=id)
        {
            result = 1;
            break;
        }
        pro = pro->next;
    }
    return result;
}
PCB *find(PCB *list, int id)
{
    PCB *pro = list;
    while (pro->next != NULL)
    {
        if (pro->next->id == id)
        {
            return pro;
        }

        pro=pro->next;
    }
    return NULL;
}

int main()
{
    //就绪队列
    running_list->next = NULL;
    //阻塞队列
    //找到错误点 =-
    blocked_list->next = NULL;
    int pro_size = 0;
    char choose;
    while (1)
    {
        cout << "欢迎来到我的进程控制系统" << endl;
        cout << "请输入序号以选择如下功能" << endl;
        cout << "1-创建新进程" << endl;
        cout << "2-展示当前进程" << endl;
        cout << "3-阻塞执行进程" << endl;
        cout << "4-唤醒阻塞进程" << endl;
        cout << "5-终止执行程序" << endl;
        cout << "6-退出程序" << endl;

        cin>>choose;
        switch (choose)
        {
        case '1':
            create(running_list, blocked_list, &pro_size);
            break;
        case '2':
            show(running_list);
            break;
        case '3':
            change(running_list, blocked_list, &pro_size);
            break;
        case '4':
            awake(running_list, blocked_list,&pro_size);
            break;
        case '5':
            kill(running_list, &pro_size);
            break;
        case '6':
            return 0;
        default:
            cout << "出错啦,请输入序号" << endl;
            break;
        }
    }
    return 0;
}

运行示例

 

实验小结

实验缺陷:判断内存容量和正在运行的线程总大小,存在缺陷,留待完善。//已改进

实验改进:判断内存容量,但没有输入第一个进程的大小,只给它初始化为0,所以会出现问题,判断语句要在进程大小输入后再判断,实验缺陷已改进。

实验心得:耐心,细心,遇到errors不要慌,一个一个改,总能改完。

  • 8
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
#include<iostream> #include <iomanip> using namespace std; typedef struct page { int num; int bl_num; int i; struct page *next; }p_list,*p_ptr; typedef struct memory { int data; struct memory *next; }m_list,*m_ptr; int wst[8][8]; int v,z,qy_f=0,qy_l=0,yh; void change_add(int bl_num,int me_s,int lo_ad) { int b,c; yh=lo_ad/me_s; b=lo_ad%me_s; c=bl_num*me_s+b; cout<<"页号和偏移量:"<<yh<<"---"<<b<<endl; cout<<"物理地址为:"<<hex<<c<<endl; } void init_page(p_ptr &l,m_ptr &k) { int m; m_ptr s,q; p_ptr r,p; k=new m_list; k->next=NULL; s=k; l=new p_list; l->next=NULL; r=l; for(m=0;m<v;m++) { p=new p_list; p->num=m; p->bl_num=-1; p->i=0; r->next=p; r=p; } r->next=NULL; for(m=0;m<z;m++) { q=new m_list; q->data=-1; s->next=q; s=q; } s->next=NULL; } void show_page(p_ptr l) { p_ptr r; r=l->next; cout<<"页号"<<" "<<"块号"<<" "<<"状态位"<<endl; while(r!=NULL) { cout<<" "<<r->num<<" "<<setw(2)<<r->bl_num<<" "<<r->i<<endl; r=r->next; } } void show_memory(m_ptr k) { m_ptr s; s=k->next; cout<<"主存"<<endl; while(s!=NULL) { cout<<s->data<<endl; s=s->next; } } void init_wst() { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { wst[i][j]=rand()%2; } } } void print_wst() { for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { cout<<wst[i][j]<<" "; } cout<<endl; } } int rand_bl() { int bl_nu; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(wst[i][j]==0) { wst[i][j]=1; bl_nu=8*i+j+1; return bl_nu; } } } return bl_nu; } int pdk(m_ptr k) { int i=0; m_ptr s; s=k->next; while(s!=NULL) { if(s->data==-1) { i++; s=s->next; } else { return i; } } return i; } int mzf(m_ptr k,int page_num) { int i=0; m_ptr s; s=k->next; while(s!=NULL) { if(s->data==page_num) { return 1; } else { s=s->next; } } return 0; } int FIFO(p_ptr &l,m_ptr &k,int page_num,int bl_nu) { int m; p_ptr r; m_ptr s,t,u; u=new m_list; s=k->next; r=l->next; while(r!=NULL) { if(r->num==page_num&&r->i!=0) { break; } if(r->num==page_num&&r->i==0) { r->i=1; r->bl_num=bl_nu; qy_f++; } r=r->next; } if(pdk(k)!=0&&pdk(k)==z) { while(s!=NULL) { if(s->data==page_num) { show_page(l); show_memory(k); return 0; } s=s->next; } s=k->next; for(m=0;m<z-1;m++) { s=s->next; } s->data=page_num; z--; show_page(l); show_memory(k); return 0; } if(pdk(k)==0) { if(mzf(k,page_num)==1) { show_page(l); show_memory(k); return 0; } if(mzf(k,page_num)==0) { while(s->next!=NULL) { t=s; s=s->next; } t->next=NULL; r=l->next; while(r!=NULL) { if(r->num==s->data) { r->bl_num=-1; r->i=0; } r=r->next; } delete s; u->data=page_num; u->next=k->next; k->next=u; show_page(l); show_memory(k); } } } /*int LRU(p_ptr &l,m_ptr &k,int page_num,int bl_nu) { int m; p_ptr r; m_ptr s,t,u; u=new m_list; s=k->next; r=l->next; while(r!=NULL) { if(r->num==page_num&&r->i!=0) { break; } if(r->num==page_num&&r->i==0) { r->i=1; r->bl_num=bl_nu; qy_l++; } r=r->next; } if(pdk(k)!=0&&pdk(k)==z) { while(s!=NULL) { if(s->data==page_num) { show_page(l); show_memory(k); return 0; } s=s->next; } s=k->next; for(m=0;m<z-1;m++) { s=s->next; } s->data=page_num; z--; show_page(l); show_memory(k); return 0; } if(pdk(k)==0) { if(mzf(k,page_num)==1) { while(s->next!=NULL) { t=s; if(s->data==page_num) { } } show_page(l); show_memory(k); return 0; } if(mzf(k,page_num)==0) { while(s->next!=NULL) { t=s; s=s->next; } t->next=NULL; r=l->next; while(r!=NULL) { if(r->num==s->data) { r->bl_num=-1; r->i=0; } r=r->next; } delete s; u->data=page_num; u->next=k->next; k->next=u; show_page(l); show_memory(k); } } }*/ void main() { int lo_ad,bl_nu,bl_sz,ra_bl; p_ptr page; m_ptr memory; cout<<"请输入页表长度:"<<endl; cin>>v; cout<<"请输入块数:"<<endl; cin>>z; cout<<"请输入块的长度(b):"<<endl; cin>>bl_sz; init_wst(); init_page(page,memory); show_page(page); show_memory(memory); while(lo_ad!=-1) { ra_bl=rand_bl(); cout<<"请输入逻辑地址:"<<endl; cin>>hex>>lo_ad; change_add(ra_bl,bl_sz,lo_ad); if(yh>v-1) { cout<<"error"<<endl; continue; } cout<<dec; cout<<"FIFO:"<<endl; FIFO(page,memory,yh,ra_bl); cout<<"缺页数:"<<qy_f<<endl; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二琳爱吃肉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值