您的位置:首页 > 其它

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

2015-07-10 14:35 471 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: