Demo entry 6338274

PSA

   

Submitted by anonymous on Dec 11, 2016 at 09:37
Language: C++. Code size: 3.1 kB.

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<time.h>  //用来生成随机数种子

float time1 = 0;	//记录已运行的时间

					//进程PCB结构体  
struct PROCESS
{
	char name[10];
	int level;
	char state;
	float running_time = 0;
	float finish_time = 0;
	struct PROCESS *next;
};
typedef struct PROCESS Process;


Process *waiting, *running, *p, *q;	//创建两个链表储存等待队列和执行队列,p、q为辅助指针


int main()
{
	void creat(int num);		//创建进程
	void show();				//显示进程信息
	void getProcess();			//获取下一次进程

	int num;
	//链表实例化
	running = (Process *)malloc(sizeof(Process));
	running->next = NULL;
	waiting = (Process *)malloc(sizeof(Process));
	waiting->next = NULL;

	printf("\t****************************************\n");
	printf("\t*           优先级调度算法             *\n");
	printf("\t****************************************\n\n");

	printf("请输入进程数量:");
	scanf("%d", &num);

	creat(num);
	//每敲一次键盘进程执行一次
	while (getchar()) {

		getProcess();	//执行进程
		if (running->next == NULL&&waiting->next == NULL)
		{
			printf("\n进程运行完毕,按任意键退出\n");
			getchar();
			break;
		}
		show();
	}



}

void creat(int num)
{
	void find();	//找到进程优先级应该在的位置
	int i = 1;

	while (i<(num + 1))
	{
		p = waiting;	//指定指针用于交换
		q = (Process *)malloc(sizeof(Process));
		q->next = NULL;

		printf("\n请输入进程 %d 的进程名:", i);
		scanf("%s", &q->name);
		printf("请输入进程 %d 的优先级:", i);
		scanf("%d", &q->level);
		printf("请输入进程 %d 的运行时间:", i);
		scanf("%f", &q->running_time);
		q->finish_time = 0;
		q->state = 'w';	//设置进程状态
		find();
		q->next = p->next;	//将所输入进程插入到相应位置
		p->next = q;
		p = p->next;
		i++;
	}
}

void find()
{
	while (p->next != NULL)
	{
		if (p->next->level < q->level)
		{
			return;
		}
		else
		{
			p = p->next;
		}
	}
}

void show()
{
	p = running->next;
	q = waiting->next;
	if (p != NULL)
	{
		printf("\n当前运行的进程为: %s\n", p->name);
		printf("进程名  状态  优先级  所需时间  已执行时间\n");
		printf("   %s\t %c\t %d\t %.2f\t  %.2f\n", p->name, p->state, p->level, p->running_time, p->finish_time);
	}
	else printf("\n当前没有进程运行。\n");
	if (q == NULL)
	{
		printf("\n当前没有就绪队列。\n");
	}
	else
	{
		printf("\n当前的就绪队列为:\n");
		printf("进程名  状态  优先级  所需时间  已等待时间\n");
		while (q != NULL)
		{
			printf("   %s\t %c\t %d\t%.2f\t  %.2f\n", q->name, q->state, q->level, q->running_time, q->finish_time);
			q = q->next;
		}
	}

}

void getProcess()
{

	q = waiting->next;

	if (running->next != NULL)
	{
		time1 = time1 + running->next->running_time;	//记录总的已运行时间
		free(running->next);	//释放资源
		running->next = NULL;
	}

	if (q != NULL)
	{
		running->next = q;	//将等待队列的第一个进程放入运行队列
		p = running->next;
		p->state = 'R';	//设置状态
		srand(time(0));	//设置种子,随机生成进程已运行时间
		p->finish_time = (float)(rand() % ((int)p->running_time * 100)) / 100;
		waiting->next = q->next;	//将等待队列往后移
		p->next = NULL;
	}
	else
	{
		waiting->next = NULL;
	}
	q = waiting->next;
	p = running->next;
	if (running->next != NULL)
	{
		while (q)
		{
			q->finish_time = time1 + p->finish_time;	//设置等待队列的已等待时间
			q = q->next;
		}
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).