操作系统——进程控制

实验目的:

进程是计算机操作系统的重要的核心概念,合理的管理进程是操作系统最核心功能之一,通过编写相应的进程程序,加强学生对进程概念的理解,包括进程的组成(PCB结构),进程的并发执行和操作系统进行进程管理的相关原语。

实验器材:

CodeBlocks

实验内容:

运用C语言编程,使用顺序表模拟内存中的进程队列,实现进程的管理。进程的管理主要包括进程的创建、进程的查询、进程阻塞、进程换出以及进程的撤销等操作。

实验步骤:

#include<iostream>

#include<malloc.h>

#include<assert.h>

#include<stdio.h>

#include<stdlib.h>

using namespace std;

PCB结构体:

enum Process_State

{

    Pro_State_blocked = 1,

    Pro_State_running = 2,

};

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;

}

实验结果(附数据和图表):

实验结果分析及结论:

进程是程序以及数据在处理机上运行的过程,它是系统资源分配和调度的一个独立单位。面对进程的查寻和换出等功能实现,只是对就绪队列进行检索,并将其相关信息输出来。    

实验心得体会和建议:  

在没有实验之前,我对进程的了解只限于字面意思,但是通过此次实验我对其有了更深刻的理解,创建一个进程,首先应该为它分配内存空间,然后将进程的基本输入到进程控制块PCB中,同时将创建好的进程插入到进程就绪队列中。                      

  • 27
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值