关闭

操作系统时间片轮转法进程CPU调度

641人阅读 评论(0) 收藏 举报
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct{
	char name[10];
	int arrtime;
	int worktime;
}DataType;

typedef struct node{
	DataType data;
	struct node *next;
}ListNode;

typedef ListNode *LinkList;

LinkList head;
void create_insert_LinkList(int flag1)
{
	ListNode *p1, *p2, *p;
	p = (ListNode*)malloc(sizeof(ListNode));
	head = p;
	p->next = NULL;
	while (flag1 > 0)
	{
		p = (ListNode*)malloc(sizeof(ListNode));
		cout << "进程名:";
		cin >> p->data.arrtime;
		cout << "运行时间:";
		cin >> p->data.worktime;
		cout << "***********" << endl;
		p->next = NULL;
		p1 = head;
		p2 = p1->next;
		while (p2->next != NULL&&p2->data.arrtime < p1->data.arrtime)
		{
			p1 = p2;
			p2 = p2->next;
		}
		p1->next = p;
		p->next = p2;
		flag1 = flag1 - 1;
	}
}

void pcb_LinkList(int flag2)
{
	LinkList H;
	ListNode *rear, *q, *p;
	int RR, rr, time, m, n;
	p = (ListNode*)malloc(sizeof(ListNode));
	p = NULL;
	H = p;
	cout << "定义时间片大小:";
	cin >> RR;
	cout << "****************";
	rr = RR;
	H = head->next;
	head->next = head->next->next;
	rear = H;
	rear->next = NULL;
	time = H->data.arrtime;
	while (flag2 != 0)
	{
		n = 0;
		while (rr != 0)
		{
			rr = rr - 1;
			time = time + 1;
			if (head->next != NULL)
			{
				if (head->next->data.arrtime <= time)
				{
					if (H = NULL)
					{
						H = head->next;
						head->next = head->next->next;
						rear = H;
						rear->next = NULL;
					}
					else
					{
						rear->next = head->next;
						head->next = head->next->next;
						rear = rear->next;
						rear->next = NULL;
					}
				}
			}
			if (H != NULL)
			{
				H->data.worktime = H->data.worktime - 1;
				m = 1;
				n = n + 1;
				if (H->data.worktime == 0)
				{
					cout << "在第" << time - n << "s" << endl;
					cout << "进程" << H->data.name << "运行" << n << "s" << "状态:C" << "完成时间:" << time << endl;
					cout << endl;
					H = H->next;
					flag2 = flag2 - 1;
					m = 0;
					n = 0;
				}
			}
		}
		if (m == 1)
		{
			cout << "在第" << time - n << "s" << endl;
			cout << "进程" << H->data.name << "运行" << n << "s" << "状态:R" << endl;
			cout << endl;
		}
		if (H == NULL)
		{
			if (head->next != NULL)
			{
				time = head->next->data.arrtime;
			}
		}
		else
		{
			if (H->next != NULL&&m == 1 && n == RR)
			{
				q = H;
				H = H->next;
				rear->next = q;
				rear = rear->next;
				rear->next = NULL;
			}
		}
		rr = RR;
	}
}
int main()
{
	cout << "时间片轮转法模拟" << endl;
	cout << endl;
	int flag;
	cout << "输入进程数目:";
	cin >> flag;
	cout << "***************" << endl;
	create_insert_LinkList(flag);
	pcb_LinkList(flag);
	cout << "运行结束" << endl;
	return 0;
}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:101373次
    • 积分:2774
    • 等级:
    • 排名:第12961名
    • 原创:181篇
    • 转载:5篇
    • 译文:1篇
    • 评论:23条
    博客专栏
    最新评论