模拟进程创建、终止、阻塞、唤醒原语

1、设计创建、终止、阻塞、唤醒原语功能函数。

2、设计主函数,采用菜单结构(参见后面给出的流程图)。

3、设计“显示队列”函数,目的能将就绪、阻塞队列中的进程信息 显示在屏幕上,以供随时查看各队列中进程的变化情况

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<windows.h>

typedef struct PCB {
    char p_name[10];//进程名
    int  p_runtime, p_grade, p_status;//运行时间,优先级(1:低 2:中 3:高),状态(1:阻塞 2:就绪 3:运行)
    struct PCB* next;//指针
}PCB;

void Menu()
{
    printf("——————系统主菜单——————\n");
    printf("——————1.进程创建——————\n");
    printf("——————2.进程调度——————\n");
    printf("——————3.进程阻塞——————\n");
    printf("——————4.进程唤醒——————\n");
    printf("——————5.进程显示——————\n");
    printf("——————0.返回上一层—————\n");
    printf("\t请输入您需要的功能(0-5):");
}

void fprior(PCB* ready, PCB* run)
{
    PCB* p, * q;
    int grade=1;
    ready->next = run->next;
    run->next = NULL;//删除run队列中的结点
    p = q=ready;
    while (p->next)
    {
        if (p->next->p_grade > grade)
        {
            q = p;
            grade = p->next->p_grade;
        }
        p = p->next;
    }//从ready队列中选择优先权最高的进程(假设为p进程)
    p=q->next;
    q->next = p->next;//将p进程从就绪队列删除
    run->next = p;//将p进程插入到run队列中
}

void ftimeschedule(PCB* ready, PCB* run)
{
    PCB* p= ready->next;
    PCB* q;
    int time = 1;//输入系统设定的时间片
    while (p)
    {
        p->p_status = 3;
        ready->next = p->next;
        run->next = p;
        p->p_runtime = p->p_runtime - time;
        printf("进程名:%s\t剩余时间:%d\n", p->p_name, p->p_runtime);
        if (p->p_runtime > 0)
        {
            q = ready;
            while (q->next) q = q->next;
            p->p_status = 2;
            q->next = p;
            run->next = NULL;
        }
        else
        {
            run->next = NULL;
            free(p);
            printf("Press any Key to continue...\n");
            getchar();
        }
        p = ready->next;
    }
}

void frun(PCB* ready, PCB* run,int method)
{
    int flag=1;
    while (flag == 1)
    {
        switch (method)
        {
        case 1: fprior(ready, run);break;      //fprior实现高优先权优先调用
        case 2: ftimeschedule(ready, run);break;      //ftimeschedule实现时间片轮转调度算法
        }
        printf("Do you want to run another one 1:yes   0:no: ");
        scanf("%d", &flag);
    }
}



void Insert(PCB* head, PCB* temp) //插入链表函数
{
    PCB* p;
    p = head;
    while (p->next)
        p = p->next;
    p->next = temp;
    temp->next = NULL;
}


void Create(PCB* block, PCB* ready, PCB* run,int method)//创建进程
{
    PCB* head;
    int check1, check2, check3;
    int flag = 1;
    head = ((PCB*)malloc(sizeof(PCB)));
    printf("——————进程创建——————\n");
    while (flag == 1) {
        printf("\n请输入进程名:");
        scanf("%s", head->p_name);
        getchar();
        check1 = Checkname(block, head);//检查进程名,相同则返回主页面,否则显示创建成功
        check2 = Checkname(run, head);
        check3 = Checkname(ready, head);
        if ((check1 || check2 || check3) == 0)
        {
            printf("EEROR:已有该进程名,创建失败,将返回主界面.\n");
            return;
        }
        printf("请输入进程优先级:(1:低 2:中 3:高)(输入数字)\n");
        scanf("%d", &head->p_grade);
        printf("请输入进程运行时间:(秒)\n");
        scanf("%d", &head->p_runtime);
        head->p_status = 2;//进程状态就绪
        Insert(ready, head);
        printf("创建成功!\n");
        if (run->next== NULL) 
            frun(ready, run,method);
        printf("Do you want to create anthoer one(1:yes 0:no):");
        scanf("%d", &flag);
    }
}

void Show(PCB* block, PCB* run, PCB* ready)
{
    PCB* p, * q;
    int b = 1;
    int re = 1;
    int ru = 1;
    p = block;
    if (p->next == NULL)
       printf("阻塞队列空!\n");
    else
    {
        q = p->next;
        printf("——————阻塞进程——————\n");
        while (q)
        {
            printf("%d.进程名:%s", b++, q->p_name);
            printf("优先级(1:低 2:中 3:高):%d", q->p_grade);
            printf("运行时间:%d(秒)\n", q->p_runtime);
            q = q->next;
        }
    }
    p = ready;
    if (p->next == NULL)
        printf("就绪队列空!\n");
    else
    {
        q = p->next;
        printf("——————就绪进程——————\n");
        while (q)
        {
           printf("%d.进程名:%s", re++, q->p_name);
           printf("\t优先级(1:低 2:中 3:高):%d", q->p_grade);
           printf("\t运行时间:%d(秒)\n", q->p_runtime);
           q = q->next;
        }
    }
    p = run;
    if (p->next == NULL)
        printf("运行队列空!\n");
    else
    {
        q = p->next;
        printf("——————运行进程——————\n");
        while (q)
        {
           printf("%d.进程名:%s", ru++, q->p_name);
           printf("\t优先级(1:低 2:中 3:高):%d", q->p_grade);
           printf("\t运行时间:%d(秒)\n", q->p_runtime);
           q = q->next;
        }
    }
}





void Delete(PCB* head, PCB* delete)//删除delete结点
{
    PCB* p = head;
    PCB* q = delete->next;
    while (p)
        if (p->next == delete)
        {
            p->next = q;
            free(delete);
            return;
        }
        else p = p->next;
}


int Checkname(PCB* temp, PCB* head)
{
    PCB* p = temp;
    while (p->next)
    {
        p = p->next;
        if (strcmp(p->p_name, head->p_name) == 0)
            return 0;
    }
    return 1;
}
void Block( PCB* block, PCB* ready,PCB *run,int method)//
{
    char name[10];
    PCB* p = block, * q;
    system("cls");
    printf("——————进程阻塞——————\n");
    printf("请输入你想要阻塞的进程名称:");
    scanf("%s", name);
    getchar();
    p = p->next;
    while (p)//看该进程是否已在阻塞队列中
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("EEROR:该进程已在阻塞队列中");
            system("pause");
            return;
        }
        p = p->next;
    }
    p = ready->next;
    while (p)
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("EEROR:该进程已在就绪队列中");
            system("pause");
            return;
        }
        p = p->next;
    }
    p = run->next;
    while (p)
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("该进程正在运行当中,将将其放入阻塞队列");
            system("pause");
            p->p_status = 1;
            q = block->next;
            while (q) q = q->next;
            q->next = p;
            run->next = NULL;
            printf("阻塞该进程成功!\n");
            system("pause");
            frun(run, ready,method);
            return;
        }
        p = p->next;
    }
    if (!p)
    {
        printf("系统中不存在该进程\n");
        system("pause");
        return;
    }
}

void Stop(PCB* block, PCB* ready, PCB* run,int method)
{
    char name[10];
    PCB* p;
    printf("——————进程终止——————\n");
    printf("请输入你想要终止的进程名称:");
    scanf_s("%s", name);
    getchar();
    p = block->next;
    while (p)
    {
        p = p->next;
        if (strcmp(p->p_name, name) == 0)
        {
            Delete(block, p);
            printf("进程终止成功!");
            return;
        }
    }
    p = ready->next;
    while (p)
    {
        p = p->next;
        if (strcmp(p->p_name, name) == 0)
        {
            Delete(ready, p);
            printf("进程终止成功!");
            frun(ready, run, method);
            return;
        }
    }
    p = run->next;
    while (p)
    {
        p = p->next;
        if (strcmp(p->p_name, name) == 0)
        {
            Delete(run, p,method);
            printf("进程终止成功!");
            return;
        }
    }
    if (!p) printf("不存在该进程\n");
    system("pause");
}

void Woke(PCB* block, PCB* ready, PCB* run,int method)
{
    char name[10];
    PCB* p, * q;
    printf("——————进程唤醒——————\n");
    printf("请输入你想要唤醒的进程名称:");
    scanf_s("%s", name);
    getchar();
    p = block;
    p = p->next;
    while (p)
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("进程处于阻塞状态,即将唤醒\n");
            system("pause");
            p->p_status = 2;
            q = ready->next;
            while (q) q = q->next;
            q->next = p;
            printf("唤醒成功\n");
            if (run->next == NULL) frun(run, ready, method);
            return;
        }
        p = p->next;
    }
    p = run;
    p = p->next;
    while (p)
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("进程处于运行状态");
            system("pause");
            return;
        }
        p = p->next;
    }
    p = ready;
    p = p->next;
    while (p)
    {
        if (strcmp(p->p_name, name) == 0)
        {
            printf("进程处于就绪状态");
            system("pause");
            return;
        }
        p = p->next;
    }
    if (!p) printf("不存在该进程\n");
    system("pause");
}

void fmenu(int method)
{
    int select;
    int flag = 1;
    PCB* ready, * run, * block;
    block = (PCB*)malloc(sizeof(PCB));
    run = (PCB*)malloc(sizeof(PCB));
    ready = (PCB*)malloc(sizeof(PCB));
    block->next = NULL;
    run->next = NULL;
    ready->next = NULL;
    while (flag == 1)
    {
        if (!ready || !run || !block)
        {
            printf("申请空间失败!");
            system("pause");
            exit(0);
        }
        Menu();
        scanf("%d", &select);
        switch (select)
        {
        case 1:Create(block, ready, run, method);break;
        case 2:frun(ready, run, method);break;
        case 3:Block(block, ready, run, method);break;
        case 4:Woke(block, ready, run, method);break;
        case 5:Show(block, ready, run);break;
        case 0:flag = 0;break;
        default:printf("input error!");break;
        }
    }
}




void main()
{int flag=1;
 int select;
 while(flag==1)
   {  printf("系统调度算法\n");
      printf("1--- 高优先权优先调度\n");
      printf("2--- 时间片轮转调度\n");
      printf("0--- 退出\n");
      printf("请输入选择:");
      scanf("%d", &select);
      switch(select)
         {case 1:
          case 2: fmenu(select);break;
          case 0:flag=0;break;
          default:printf("input error!");break;
       } 
    }
}

            

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值