SJF算法 c语言

编程小菜鸟发布的第二个自己编写的程序,有什么不对的地方欢迎各位大佬指正!

#include <malloc.h>
#include <stdio.h>

typedef struct table
{
	char name[10];//进程名
	int id;//进程id
	int atime;//到达时间
	int stime;//开始时间
	int runtime;//运行时间
	int ftime;//完成时间
	float total;//周转时间
	float weight;//带权周转时间
	int starttime;
	int finishtime;
}PCB;

typedef struct _Node
{
	struct _Node* next;
	PCB pcb;
}node;

struct node* creat(int count)
{
	node* head = malloc(sizeof(node));
	head->next = NULL;
	node* move = head;

	for (int i = 0; i < count; i++)
	{
		node* fresh = malloc(sizeof(node));
		fresh->next = NULL;
		move->next = fresh;

		printf("请输入第%d个进程的id、到达时间、运行时间:\n", i + 1);
		scanf("%d%s%d%d", &fresh->pcb.id, fresh->pcb.name, &fresh->pcb.atime, &fresh->pcb.runtime);

		move = fresh;
	}
	return head;
}

void fcfs(node* head) {
	for (node* turn = head->next; turn->next != NULL; turn = turn->next)
	{
		for (node* move = head->next; move->next != NULL; move = move->next)
		{
			if (move->pcb.atime > move->next->pcb.atime)
			{
				PCB temp = move->pcb;
				move->pcb = move->next->pcb;
				move->next->pcb = temp;
			}

		}

	}
}

void middle(node* head) {
	for (node* turn = head->next; turn->next != NULL; turn = turn->next)
	{
		for (node* move = head->next; move->next != NULL; move = move->next)
		{
			if (move->pcb.atime == move->next->pcb.atime && move->pcb.runtime > move->next->pcb.runtime) {
				PCB temp = move->pcb;
				move->pcb = move->next->pcb;
				move->next->pcb = temp;
			}

		}
	}
}

void sjf(node* head) {
	node* temp = head->next;
	node* move = head->next->next;
	temp->pcb.starttime = temp->pcb.atime;
	temp->pcb.finishtime = temp->pcb.starttime + temp->pcb.runtime;
	int num = temp->pcb.finishtime;
	while (move->next != NULL) {
		temp = temp->next;
		move = move->next;
		node* tap = move;
		while (move != NULL) {
			if (temp->pcb.atime <= num && move->pcb.atime <= num && temp->pcb.runtime > move->pcb.runtime) {
				PCB map = temp->pcb;
				temp->pcb = move->pcb;
				move->pcb = map;
				move = move->next;
			}
			else {
				break;
			}
		}
		if (temp->pcb.atime < num) {
			num = num + temp->pcb.runtime;

		}
		else {
			num = temp->pcb.atime + temp->pcb.runtime;

		}
		move = tap;
	}
}


void running(node* head) {
	node* move = head->next;
	while (move != NULL) {
		printf("%s程序正在运行.....\n", move->pcb.name);
		move = move->next;
	}
}

void s_f_t_w_time(node* head) {
	node* move = head->next->next;
	node* first = head->next;
	first->pcb.total = first->pcb.runtime;
	first->pcb.stime = first->pcb.atime;
	first->pcb.ftime = first->pcb.stime + first->pcb.runtime;
	first->pcb.weight = first->pcb.total / first->pcb.runtime;
	printf("%d %4s %7d %11d %10d %10d %16f %10f\n", first->pcb.id, first->pcb.name, first->pcb.atime, first->pcb.runtime, first->pcb.stime, first->pcb.ftime, first->pcb.total, first->pcb.weight);
	while (move != NULL) {
		if (first->next->pcb.atime <= first->pcb.ftime) {
			first->next->pcb.stime = first->pcb.ftime;
			first->next->pcb.ftime = first->next->pcb.stime + first->next->pcb.runtime;
			first->next->pcb.total = first->next->pcb.stime + first->next->pcb.runtime - first->next->pcb.atime;
			first->next->pcb.weight = first->next->pcb.total / first->next->pcb.runtime;
		}
		else {
			first->next->pcb.stime = first->next->pcb.atime;
			first->next->pcb.ftime = first->next->pcb.stime + first->next->pcb.runtime;
			first->next->pcb.total = first->next->pcb.stime + first->next->pcb.runtime - first->next->pcb.atime;
			first->next->pcb.weight = first->next->pcb.total / first->next->pcb.runtime;
		}
		printf("%d %4s %7d %11d %10d %10d %16f %10f\n", first->next->pcb.id, first->next->pcb.name, first->next->pcb.atime, first->next->pcb.runtime, first->next->pcb.stime, first->next->pcb.ftime, first->next->pcb.total, first->next->pcb.weight);
		first = first->next;
		move = move->next;
	}
}

int main(void) {
	node* p;
	printf("请输入进程数量:\n");
	int count;
	scanf("%d", &count);
	p = creat(count);
	fcfs(p);
	middle(p);
	sjf(p);
	running(p);
	printf(" id    进程名   到达时间   运行时间   开始时间   结束时间   周转时间   带权周转时间\n");
	s_f_t_w_time(p);

}

输入及运行结果如下:

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是C++语言实现RR算法SJF算法代码: RR算法实现: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; struct Process { int pid; // 进程ID int burst_time; // 执行时间 int remain_time; // 剩余时间 int arrival_time; // 到达时间 int waiting_time; // 等待时间 int turnaround_time; // 周转时间 }; void RR(vector<Process> processes, int quantum) { queue<Process> ready_queue; int total_time = 0; int n = processes.size(); int completed = 0; int current_time = 0; for (int i = 0; i < n; i++) { total_time += processes[i].burst_time; processes[i].remain_time = processes[i].burst_time; } while (completed < n) { for (int i = 0; i < n; i++) { if (processes[i].arrival_time <= current_time && processes[i].remain_time > 0) { ready_queue.push(processes[i]); } } if (ready_queue.empty()) { current_time++; continue; } Process current_process = ready_queue.front(); ready_queue.pop(); int execution_time = min(quantum, current_process.remain_time); current_time += execution_time; current_process.remain_time -= execution_time; if (current_process.remain_time <= 0) { completed++; current_process.turnaround_time = current_time - current_process.arrival_time; current_process.waiting_time = current_process.turnaround_time - current_process.burst_time; cout << "Process " << current_process.pid << " completed." << endl; cout << "Turnaround time: " << current_process.turnaround_time << endl; cout << "Waiting time: " << current_process.waiting_time << endl; } else { ready_queue.push(current_process); } } } int main() { vector<Process> processes = { {1, 10, 0, 0, 0}, {2, 5, 0, 1, 0}, {3, 8, 0, 2, 0}, {4, 3, 0, 3, 0}, {5, 7, 0, 4, 0} }; int quantum = 2; RR(processes, quantum); return 0; } ``` SJF算法实现: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Process { int pid; // 进程ID int burst_time; // 执行时间 int arrival_time; // 到达时间 int waiting_time; // 等待时间 int turnaround_time; // 周转时间 }; bool compare(const Process& a, const Process& b) { return a.burst_time < b.burst_time; } void SJF(vector<Process> processes) { sort(processes.begin(), processes.end(), compare); int n = processes.size(); int current_time = 0; for (int i = 0; i < n; i++) { current_time += processes[i].burst_time; processes[i].turnaround_time = current_time - processes[i].arrival_time; processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time; cout << "Process " << processes[i].pid << " completed." << endl; cout << "Turnaround time: " << processes[i].turnaround_time << endl; cout << "Waiting time: " << processes[i].waiting_time << endl; } } int main() { vector<Process> processes = { {1, 10, 0, 0, 0}, {2, 5, 1, 0, 0}, {3, 8, 2, 0, 0}, {4, 3, 3, 0, 0}, {5, 7, 4, 0, 0} }; SJF(processes); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一串平凡的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值