⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multi.c

📁 操作系统实验,模拟进程调度.第一次上传东西,若有不好的地方望大家指教.
💻 C
字号:
/////////////////////////////////进程多队列轮转法调度算法
//文档中有更详细的注释

#include "stdio.h"
#include <stdlib.h>
#include <conio.h>

# define getpcb(type) (type*)malloc(sizeof(type))
# define N 5//队列数

struct pcb{//进程结构体
    char state;//状态
    char name[10];//名字
    int total_time;//服务时间
    int run_time;//已运行时间
	int arrive_time;//到达时间
    int count;//所在队列级数
    struct pcb *next;
}*p,*ready[N]={NULL};

typedef struct pcb PCB;

sort()//进程按到达时间先后存储
{
    int flag=1,i;
    PCB *tm1,*tm2;
	if(p->count<N-1)
		i=p->count;
	else i=N;
    if(ready[i]==NULL)
    {
        ready[i]=p;
        ready[i]->next=NULL;
    }
	else if(p->arrive_time<ready[i]->arrive_time)
	{
		p->next=ready[i];
		ready[i]=p;
	}
    else
    {
        tm1=ready[i];
        tm2=ready[i]->next;
        while(tm2)
        {
			if(p->arrive_time<tm2->arrive_time)
			{
				p->next=tm2;
				tm1->next=p;
				flag=0;
				break;
			}
			tm1=tm1->next;
			tm2=tm2->next;            
        }
        if(flag)
        {
            tm1->next=p;
            p->next=NULL;
        }
    }
}

input()//进程信息的输入
{
    int i,num;
    printf("\n 请输入进程数:");
    scanf("%d",&num);
    for (i=0;i<num;i++)
    {
        printf("\n 进程号:No.%d:\n",i+1);
        p=getpcb(PCB);
        printf("\n 输入进程名:");
        scanf("%s",p->name);
		printf("\n 输入进程到达时间:");
		scanf("%d",&p->arrive_time);
        printf("\n 输入进程运行时间:");
        scanf("%d",&p->total_time);	
        p->run_time=0;
        p->state='w';
        p->next=NULL;
        p->count=0;
        sort();
    }
}

display(PCB *q)//显示所有进程的状态
{
    printf("\n name\t state\t arrive_time\t total_time\t run_time\n");
    printf(" %s\t %c\t %d\t\t %d \t\t%d\n",
		q->name,q->state,q->arrive_time,q->total_time,q->run_time);
}

destroy()//撤消进程 
{
    printf("\n 进程[%s]已完成.\n",p->name);
    free(p);
    p=NULL;
}

running()//进程运行
{
    p->count++;
    if(p->count<N)
        p->run_time=p->run_time+p->count;
    else
        p->run_time=p->run_time+N;
    if(p->run_time>=p->total_time)
        destroy();
    else
		p->state='w';
    if(p)
        sort();
}

check()//查看进程的状态
{
    int i;
    PCB *pr;
    if(p->count<N)
        printf("\n 当前执行的就绪队列分配的CPU时间片为:%d",p->count+1);
    else
        printf("\n 当前执行的就绪队列分配的CPU时间片为:%d",N);
    printf("\n 当前正在运行的进程是:%s",p->name);
    display(p);
    for(i=0;i<N-1;i++)
    {
		pr=ready[i];
        printf("\n第%d就绪队列状态为:\n",i+1);
        while(pr)
        {
            display(pr);
            pr=pr->next;
        }
    }
    printf("\n第%d就绪队列状态为:\n",N);
	pr=ready[N];
    while(pr)
    {
        display(pr);
        pr=pr->next;
    }
}

main()
{//主函数
    int h=0,i;
	system("cls");
	printf("\n模拟进程调度系统——多队列轮转法调度算法\n");
    input();
	for(i=0;i<=N;i++)
	{
		while(ready[i])
		{
			getchar();
			h++;
			printf("\n The execute number:%d \n",h);
			p=ready[i];
			ready[i]=p->next ;
			p->next=NULL;
			p->state='R';
			check();
			running();
			printf("\n 按任键继续......");
		}
	}
    printf("\n\n 所有进程已经完成.\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -