操作系统实验——进程调度算法

c语言实现:

使用C语言编程(注意运行环境是visual c++ 6.0,如果选择visual studio 则需要做一些改动,否者会报错) 。改动实例如下:

//即在scanf和strcpy末尾加上'_s'

strcpy_s(p[i].state,"R");

#include<stdio.h>
#include<string.h>
#define num 100
int timeslice;
struct process
{
	char name[50];
	int priority;
	int needtime;
	int usedtime;
	int order;
	char state[10];
};
void print(process p[], int len)
{
	for (int i = 0; i < len; i++)
	{
		p[i].order = i;
		printf("请输入进程%d名字:", i + 1);
		scanf("%s", &p[i].name);
		printf("请输入进程%d的优先级:", i + 1);
		scanf("%d", &p[i].priority);
		printf("请输入进程%d所需要的时间:", i + 1);
		scanf("%d", &p[i].needtime);
		printf("\n");
	}
	printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");

}
void sort(process p[], int len)
{
	int i, j;
	process p1;
	for (i = 0; i < len; i++)
	{
		for (j = i + 1; j < len; j++)
		{
			if (p[i].priority < p[j].priority || p[i].priority == p[j].priority && p[i].needtime < p[j].needtime)//先比较优先级大小,如果优先级大小相等则比较其所需执行时间的大小
			{
				p1 = p[i];
				p[i] = p[j];
				p[j] = p1;
			}
		}
	}

}
void sort1(process p[], int len)
{
	int i, a, b, sum = 0;
	int count;
	for (i = 0; i < len; i++)
		sum += p[i].needtime;
	a = sum % timeslice;
	if (a > 0)
		b = sum / timeslice + 1;
	else
		b = sum / timeslice;
	count = b + 1;
	sort(p, len);
	printf("进程名\t\t优先级\t\t到达时间\t需要时间\t已用时间\t进程状态\n");
	for (i = 0; i < len; i++)
	{
		p[i].usedtime = 0;
		if (i == 0)
			strcpy(p[i].state, "R");
		else
			strcpy(p[i].state, "W");
		printf("  %s\t\t  %d\t\t  %d\t\t  %d\t\t  %d\t\t  %s\n", p[i].name, p[i].priority, p[i].order, p[i].needtime, p[i].usedtime, p[i].state);
	}
	printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");
	do
	{
		printf("进程名\t\t优先级\t\t到达时间\t需要时间\t已用时间\t进程状态\n");
		--p[0].priority;
		p[0].usedtime += timeslice;
		if (p[0].usedtime > p[0].needtime)
			p[0].usedtime = p[0].needtime;
		sort(p, len);
		for (i = 0; i < len; i++)
		{
			if (i == 0)
			{
				if (p[i].usedtime == p[i].needtime)
				{
					strcpy(p[i].state, "F");
					p[i].priority = 0;
				}
				else if (p[i].usedtime < p[i].needtime && p[i].priority == 0)
				{
					strcpy(p[i].state, "R");
					++p[i].priority;
					++count;
				}
				else
					strcpy(p[i].state, "R");
			}
			else if (p[i].usedtime == p[i].needtime)
			{
				strcpy(p[i].state, "F");
					p[i].priority = 0;
			}
			else
					strcpy(p[i].state, "W");
					printf("  %s\t\t  %d\t\t  %d\t\t  %d\t\t  %d\t\t  %s\n", p[i].name, p[i].priority, p[i].order, p[i].needtime, p[i].usedtime, p[i].state);
		}
		printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");
		--count;

	} while (count > 0);
}
int main()
{
	process p[num] = { 0 };
	int n;
	printf("请输入时间片大小:");
	scanf("%d", &timeslice);
	printf("\n");
	printf("请输入所要调度进程的个数(2-100):");
	scanf("%d", &n);
	printf("\n");
	print(p, n);
	sort1(p, n);
	return 0;
}

c++和c语言结合实现

使用C语言和C++语言结合编程(运行环境在visual studio),两种语言结合较为灵活,可以根据所输入的长度,通过new操作动态分配所指定大小的内存。

#include<iostream>
using namespace std;
int timeslice;
struct process
{
	char name[50];
	int priority;
	int needtime;
	int usedtime;
	int order;
	char state[10];
};
void print(process p[], int len)
{
	for (int i = 0; i < len; i++)
	{
		p[i].order = i;
		printf("请输入进程%d名字:", i + 1);
		cin >> p[i].name;
		printf("请输入进程%d的优先级:", i + 1);
		cin >> p[i].priority;
		printf("请输入进程%d所需要的时间:", i + 1);
		cin >> p[i].needtime;
		cout << endl;
	}
	printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");
}
void sort(process p[], int len)
{
	int i, j;
	process p1;
	for (i = 0; i < len; i++)
	{
		for (j = i + 1; j < len; j++)
		{
			if (p[i].priority < p[j].priority || p[i].priority == p[j].priority && p[i].needtime < p[j].needtime)
			{
				p1 = p[i];
				p[i] = p[j];
				p[j] = p1;
			}
		}
	}
}
void sort1(process p[], int len)
{
	int i, a, b, sum = 0;
	int count;
	for (i = 0; i < len; i++)
		sum += p[i].needtime;
	a = sum % timeslice;
	if (a > 0)
		b = sum / timeslice + 1;
	else
		b = sum / timeslice;
	count = b + 1;
	sort(p, len);
	printf("进程名\t\t优先级\t\t到达时间\t需要时间\t已用时间\t进程状态\n");
	for (i = 0; i < len; i++)
	{
		p[i].usedtime = 0;
		if (i == 0)
			strcpy_s(p[i].state, "R");
		else
			strcpy_s(p[i].state, "W");
		printf("  %s\t\t  %d\t\t  %d\t\t  %d\t\t  %d\t\t  %s\n", p[i].name, p[i].priority, p[i].order, p[i].needtime, p[i].usedtime, p[i].state);
	}
	printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");
	do
	{
		printf("进程名\t\t优先级\t\t到达时间\t需要时间\t已用时间\t进程状态\n");
		--p[0].priority;
		p[0].usedtime += timeslice;
		if (p[0].usedtime > p[0].needtime)
			p[0].usedtime = p[0].needtime;
		sort(p, len);
		for (i = 0; i < len; i++)
		{
			if (i == 0)
			{
				if (p[i].usedtime == p[i].needtime)
				{
					strcpy_s(p[i].state, "F");
					p[i].priority = 0;
				}
				else if (p[i].usedtime < p[i].needtime && p[i].priority == 0)
				{
					strcpy_s(p[i].state, "R");
					++p[i].priority;
					++count;
				}
				else
					strcpy_s(p[i].state, "R");
			}
			else if (p[i].usedtime == p[i].needtime)
			{
				strcpy_s(p[i].state, "F");
				p[i].priority = 0;
			}
			else
				strcpy_s(p[i].state, "W");
			printf("  %s\t\t  %d\t\t  %d\t\t  %d\t\t  %d\t\t  %s\n", p[i].name, p[i].priority, p[i].order, p[i].needtime, p[i].usedtime, p[i].state);
		}
		printf("-*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\n");
		--count;

	} while (count > 0);
}
int main()
{
	int n;
	printf("请输入时间片大小:");
	cin >> timeslice;
	cout << endl;
	printf("请输入所要调度进程的个数(2-100):");
	cin >> n;
	process* p = new process[n];
	cout << endl;
	print(p, n);
	sort1(p, n);
	return 0;
}

运行结果:

若时间片为4

进程名

优先级

需要时间

P1

4

18

P2

8

12

P3

4

10

1.进程p2的优先级最高,最先分配到处理机,每分配到一次处理机,其优先级也随之减一且其一次执行的时间为一个时间片的大小即为4。直至其执行的时间等于其所需要的时间时停止占用处理机

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值