多机调度程序

输入

输入机器数量M,作业数量n程序中定义,每一个作业的运行时间随机生成。

输出

每个作业的运行开始时间结束时间使用的机器编号的列表
   mmsp.h文件:
#define N 7
struct JOB {
    int jobid;
    int runningtime;
    int starttime;
    int machineid;
    int order;
};
struct JOB job[N];
struct MACHINE {
    int jobid;
    int totaltime;
};
    quicksort.h文件:
#include "mmsp.h"
void quicksort(struct JOB job[], int low, int high);
int split(struct JOB job[], int low, int high)
void quicksort(struct JOB a[], int low, int high)
{
  int middle; 
  if (low >= high) return;
  middle = split(a, low, high);
  quicksort(a, low, middle - 1);
  quicksort(a, middle + 1, high);
}
int split(struct JOB a[], int low, int high)
{
  int part_element = a[a[low].order].runningtime;
  int temp = a[low].order;
 
  for (;;) {
    while (low < high && part_element >= a[a[high].order].runningtime)
      high--;
    if (low >= high) break;
    a[low++].order = a[high].order;
 
    while (low < high && a[a[low].order].runningtime >= part_element)
      low++;
    if (low >= high) break;
    a[high--].order = a[low].order;
  }
 
  a[high].order = temp;
  return high;
}
main.cpp文件:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "quicksort.h"
void getData(struct JOB job[], int n);
void result_output(struct JOB job[], int n)
void mmsp(struct JOB job[], int n, int m);

int main(void)
{
	int m;

	time_t t;
	srand((unsigned)time(&t));  /* 设置随机数起始值 */
	getData(job, N); /* 获得数据放入数组a中 */

	printf("Unordered datas: \n");
	result_output(job, N);

	quicksort(job, 0, N - 1);

	printf("In sorted order: \n");
	result_output(job, N);

	printf("\nM=");
	scanf_s("%d", &m);

	mmsp(job, N, m);//

	printf("\nresult: \n");
	result_output(job, N);

	system("pause");
	return 0;
}
void mmsp(struct JOB job[], int n, int m)//
{
	MACHINE *machine = 0;  //定义MACHINE型结构体数组
	machine = (struct MACHINE*)malloc(sizeof(struct MACHINE)*m); //为动态结构体数组分配内存空间
	
	int i;
	for (i = 0; i<n; i++) {
		if (i<m) {
			job[job[i].order].machineid = i;
			job[job[i].order].starttime = 0;

			machine[i].jobid = job[job[i].order].jobid;
			machine[i].totaltime = job[job[i].order].runningtime;
		}
		else {
			int j, p = 0;
			int starttime = machine[0].totaltime;
			for (j = 1; j<m; j++)
				if (machine[j].totaltime < starttime)
					p = j;

			job[job[i].order].machineid = p;
			job[job[i].order].starttime = machine[p].totaltime;


			machine[p].jobid = job[job[i].order].jobid;
			machine[p].totaltime += job[job[i].order].runningtime;
		}
	}
}
void getData(struct JOB job[], int n)
{
	int i;
	for (i = 0; i < n; i++) {
		job[i].jobid = i;
		job[i].runningtime = 1 + rand() % 30; /* 获得1-30之间的整数值 */

		job[i].machineid = 0;
		job[i].starttime = 0;

		job[i].order = i;
	}
}
void result_output(struct JOB job[], int n)
{
	int i;

	printf("jobid runningtime machineid starttime order\n");
	for (i = 0; i < n; i++)
		printf("%5d %11d %9d %9d %5d\n", job[i].jobid, job[i].runningtime, job[i].machineid, job[i].starttime, job[i].order);
	printf("\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值