操作系统实验

实验一 进程状态转换模拟

流程图

在这里插入图片描述

代码
#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <sys/stat.h>
#include  <fcntl.h>
#include  <error.h>
#include  <wait.h>
#include  <unistd.h>

#define MAXSIZE 50
#define ERROR -99
#define MSIZE 100

typedef struct
{
   
    char name;    //进程名字
    int msize;    //占用内存大小
    int time;    //运行时间
    int priority;   //进程优先级
}process;

typedef process QElemType;

typedef struct     //队列
{
   
    QElemType data[MAXSIZE];
    int front;
    int  rear;
    int size;
}Queue;

Queue *createQ()    //创建队列
{
   
    Queue *q = (Queue*)malloc(sizeof(Queue));
    q->front = -1;
    q->rear = -1;
    q->size = 0;
    return q;
}

void addQ(Queue *q,QElemType e)    //入队列
{
   
    if(((q->rear+1)%MAXSIZE)==q->front)
    {
   
        printf("队列已满\n");
        return;
    }
    q->rear++;
    q->size++;
    q->data[q->rear] = e;
    int n=q->front+1;
    for(int i=n;i<=q->rear;i++)       //按照队列中进程的优先级顺序调整
    {
   
        for(int j =n;j<=q->rear-1-i;j++)
        {
   
            if((q->data[j]).priority > (q->data[j+1]).priority)     //保证优先级低的在后面
            {
   
                process tmp;
                tmp=q->data[j];
                q->data[j]=q->data[j+1];
                q->data[j+1]=tmp;
            }
        }
    }
}


QElemType deleteQ(Queue *q)   //出队列(队头出)
{
   
    q->front++;
    q->size--;
    return q->data[q->front];
}


int isempty(Queue *q)    //判断一个队列是否为空
{
   
    if(q->size==0)
        return 1;
    else
        return 0;
}

void deleteq(Queue *q,QElemType a)  //删除队列中的某元素
{
   
    int flag=0;
    if(isempty(q)==1)
        printf("此队列为空!\n");
    else
    {
   
        for(int i=0;i<=q->rear;i++)        //队列不为空时在其中循环查找,找到就删除该进程
        {
   
            if(q->data[i].name==a.name)
            {
   
                for(int j=i;j<q->rear;j++)
                {
   
                    q->data[j]=q->data[j+1];
                }
                q->rear--;
                q->size--;
                flag=1;
                break;
            }
        }
        if(flag==0)
            printf("队列中没有该进程!\n");
    }

}

void printQ(Queue *q)   //打印队列中的元素
{
   
    if(isempty(q))
    {
   
        printf("空");
    }
    else{
   
        int n=q->front+1;
        for(int i=n;i<=q->rear;i++)
        {
   
            printf("%c  ",(q->data[i]).name);

        }
    }
}

void Create(Queue *newq,Queue *ready,Queue *run,Queue *blocked,int &allsize)     //新建一个进程
{
   
    process p;
    char n;
    printf("请输入所创建进程名字(字母):");
    scanf("%c",&p.name);
    printf("请输入进程占用内存大小(整数):");
    scanf("%d",&p.msize);
    printf("请输入进程运行时间(整数):");
    scanf("%d",&p.time);
    printf("请输入进程优先级(整数):");
    scanf("%d",&p.priority);
    allsize=allsize+p.msize;
    if(allsize >MSIZE)     //超过内存限制需要在新建队列中等待
    {
   
        printf("\n");
        printf("该进程需要等待之后才能提交\n");
        printf("\n");
        addQ(newq,p);
        allsize=allsize-p.msize;
    }
    else
    {
   
        addQ(ready,p);
    }
    printf("转换后状态为:\n");
    printf("Running:   ");
    printQ(run);
    printf("\n");
    printf("Ready:     ");
    printQ(ready);
    printf("\n");
    printf("Blocked:   ");
    printQ(blocked);
    printf("\n");
    printf("New:       ");
    printQ(newq);
    printf("\n");
    printf("\n");
}


void Timeout(Queue *newq,Queue *ready,Queue *run,Queue *blocked)          //时间片超时
{
   
    process x;
    if (isempty(run)!=1)   //运行队列不为空
    {
   
       x = deleteQ(run);       //将运行队列中的进程移到就绪队列中
       addQ(ready, x);
       printf("进程%c 超时 Running->Ready\n",x.name);
    }
    else   //运行为空
    {
   
        printf("\n");
        printf("当前无运行进程!\n");
        printf("\n");
    }
    printf("转换后状态为:\n");
    printf("Running:   ");
    printQ(run);
    printf("\n");
    printf("Ready:     ");
    printQ(ready);
    printf("\n");
    printf("Blocked:   ");
    printQ(blocked);
    printf("\n");
    printf("New:       ");
    printQ(newq);
    printf("\n");
    printf("\n");
}

void EventWait(Queue *newq,Queue *ready,Queue *run,Queue *blocked)          //进程阻塞
{
   
   process x;
   if (isempty(run)!=1)   //运行队列不为空
   {
   
      x = deleteQ(run);
      addQ(blocked, x);
      printf("进程%c 被阻塞 Running->Blocked\n",x.name);
   }
   else    //运行为空
   {
   
       printf("\n");
       printf("当前无运行进程!\n");
       printf("\n");
   }
   printf("转换后状态为:\n");
   printf("Running:   ");
   printQ(run);
   printf("\n");
   printf("Ready:     ");
   printQ(ready);
   printf("\n");
   printf("Blocked:   ");
   printQ(blocked);
   printf("\n");
   printf("New:       ");
   printQ(newq);
   printf("\n");
   printf("\n");
}

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1.目的: 自行编制模拟程序,通过形象化的状态显示,深入理解进程的概念、进程之间的状态转换及其所带来的PCB内容 、组织的变化,理解进程与其PCB间的一一对应关系。 2. 内容及要求: 1) 设计并实现一个模拟进程状态转换及其相应PCB内容、组织结构变化的程序。 2) 独立编写、调试程序进程的数目、进程的状态模型(三状态、五状态、七状态或其它)以及PCB的组织形式可自行选择。 3) 合理设计与进程PCB相对应的数据结构。PCB的内容要涵盖进程的基本信息、控制信息、资源需求及现场信息。 4) 设计出可视性较好的界面,应能反映出进程状态的变化引起的对应PCB内容、组织结构的变化。 5) 代码书写要规范,要适当地加入注释。 6) 认真进行预习,完成预习报告。 7) 实验完成后,要认真总结,完成实验报告。 3.使用的数据结构及说明: 在本实验中,主要用到的数据结构是PCB的结构,其中PCB的数据结构如下: struct PCB { int P_Id; //PCB的ID号 char P_Name[10]; //PCB的名称 char P_State[10]; //PCB状态 int P_Runtime; //PCB的所需要的运行时间 int P_Requiry; //PCB所需要的资源要求 struct PCB * next ; //PCB块的下一个指针 } ; 其中,P_Id,和P_Name用来标示一个进程,而P_State用来标示进程的五种状态:Create_state,Ready_state,Block_state,Run_state,Exit_state。P_Runtime标示要完成一个进程所需要的时间。P_Requiry标示一个进程的执行所需要的其他条件,当其他的条件满足,则P_Requiry置1,否则置0。Struct PCB * next 用来指向同一队列中的下一个PCB块。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

代码真的好难打

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

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

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

打赏作者

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

抵扣说明:

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

余额充值