OS作业调度C语言实现

SJF、FCFS、FPF算法:

#include "stdio.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#define getjcb(type) (type*)malloc(sizeof(type))



int n = 0, time = 0; float eti, ewi;
struct jcb {
	char name[10]; /* 作业名 */
	char state; /* 作业状态 */
	int pn; /*优先级*/
	int ts; /* 提交时间 */
	int tb; /* 开始运行时间 */
	int tc; /* 完成时间 */
	float ti; /* 周转时间 */
	float wi; /* 带权周转时间 */
	int ntime; /* 作业所需运行时间 */
	char resource[10]; /* 所需资源 */
	struct jcb *link; /* 结构体指针 */
} *p, *q, *head = NULL;
typedef struct jcb JCB;
void initial() {
	int i;
	printf("\nInput jcb num\n");
	scanf("%d", &n);
	printf("Input\nname\tpn\tts\tntime\tresource\n");
	for (i = 0; i < n; i++) {
		p = getjcb(JCB);
		scanf("%s\t%d\t%d\t%d\t%s", &p->name,&p->pn, &p->ts, &p->ntime, &p->resource);
		p->state = 'W';
		p->link = NULL;
		if (head == NULL) head = q = p;
		else {
			q->link = p;
			q = p;
		}
	}
}
void print(JCB *pr) {
	JCB *p=(JCB*)malloc(sizeof(*pr));
	printf("\ntime=%d", time);
	printf("\nname\tstate\tpn\tts\tntime\tsource\ttb\ttc\tti\twi\n");
	printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\t%d\n",
		pr->name, pr->state, pr->ts, pr->ntime, pr->resource, pr->tb, pr->tc, pr->ti, pr->wi,p->pn );
	p = head;
	do {
		if (p->state == 'W')
			printf("%s\t%c\t%d\t%d\t%s\t%d\n",
				p->name, p->state, p->ts, p->ntime, p->resource, p->pn);
		p = p->link;
	} while (p != NULL);
	p = head;
	do {
		if (p->state == 'F')
			printf("%s\t%c\t%d\t%d\t%s\t%d\t%d\t%4.2f\t%4.2f\t%d\n",
				pr->name, pr->state, pr->ts, pr->ntime, pr->resource, pr->tb, pr->tc, pr->ti, pr->wi, p->pn);
		p = p->link;
	} while (p != NULL);
}
void running(JCB *p) {                                             //计算
	p->tb = time; p->state = 'R';
	p->tc = p->tb + p->ntime;
	p->ti = (float)(p->tc - p->ts);
	p->wi = (float)(p->ti / p->ntime);
	eti += p->ti;
	ewi += p->wi;
	print(p);
	time += p->ntime;
	p->state = 'F';
	printf("\n%s has been finished!\npress any key to continue...\n", p->name);
	_getch();
}
void last() {
	eti /= n; ewi /= n;
	printf("\neti=%7.3f\tewi=%7.3f\n", eti, ewi);
}
void sjf() {                                                    
	JCB *min;
	int i, iden;
	for (i = 0; i < n; i++) {
		p = min = head; iden = 1;
		do {
			if (p->state == 'W'&&p->ts <= time)                  //查找短时间
				if (iden) {
					min = p; iden = 0;
				}
				else if (p->ntime < min->ntime) min = p;
			p = p->link;
		} while (p != NULL);
		if (iden) {
			i--; printf("\ntime=%d:\tno JCB submib...wait...", time); time++;
			if (time > 100) { printf("\nruntime is too long...error"); _getch(); }
		}
		else {
			running(min);
		}
	}
}
void fcfs() {
	JCB *min;
	int i, iden;
	for (i = 0; i < n; i++) {
		p = min = head; iden = 1;
		do {
			if (p->state == 'W'&&p->ts <= time)
				if (iden) {
					min = p; iden = 0;
				}
				else if (p->ts < min->ts) min = p;
			p = p->link;
		} while (p != NULL);
		if (iden) {
			i--; printf("\ntime=%d:\tno JCB submib...wait...", time); time++;
			if (time > 100) { printf("\nruntime is too long...error"); _getch(); }
		}
		else {
			running(min);
		}
	}
}
void fpf() {
	JCB *min;
	int i, iden;
	for (i = 0; i < n; i++) {
		p = min = head; iden = 1;
		do {
			if (p->state == 'W'&&p->ts <= time)                  //查找短时间
				if (iden) {
					min = p; iden = 0;
				}
				else if (p->pn <min->pn)
					min = p;
			p = p->link;
		} while (p != NULL);
		if (iden) {
			i--; printf("\ntime=%d:\tno JCB submib...wait...", time); time++;
			if (time > 100) { printf("\nruntime is too long...error"); _getch(); }
		}
		else {
			running(min);
		}
	}
}

void runjcb(int m) {
	printf("\n\nstart running jcb use algorithm %d.", m);
	switch (m) {
	case 1:fcfs(); break;
	case 2:sjf(); break;
	case 3:fpf(); break;
	default:printf("\nrunjcb error...\n"); exit(1);
	}
}
void start() {
	int m;
	char str[100] = "\nselect algorithm\n1.FCFS\n2.SJF\n3.FPF\n";
	printf("%s", str);
	m = _getch() - 48;
	initial();
	if (1 <= m && m <= 3) runjcb(m);
	else {
		printf("\nselect error!try again...\n");
		start();
	}
	last();
}
int main() {
	start();
	printf("\nfinished!");
	_getch();
	return 0;
}
 

计算结果:

FCFS:

SJF:

FPF:

 

第三个FPF实现有问题,带权周转时间wi不能正确计算,烦请大佬看看吧

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值