fcfs算法 c语言

该代码示例展示了如何用C语言实现FCFS进程调度算法,包括创建进程队列、按到达时间排序、模拟进程运行以及计算周转时间和带权周转时间。程序首先读取用户输入的进程信息,然后按照到达时间对进程进行调度并输出运行状态和相关时间指标。
摘要由CSDN通过智能技术生成
#include <malloc.h>
#include <stdio.h>

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

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

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

	printf("请输入进程数量:\n");
	int count;
	scanf("%d", &count);
	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 running(node* head) {
	node* move = head->next;//创建move指针控制循环次数
	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;//创建move指针控制循环次数
	node* first = head->next;//将首结点的地址数据传入first指针中
	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;
	p = creat();
	fcfs(p);
	printf("\n");
	running(p);
	printf("\n");
	printf(" id    进程名   到达时间   运行时间   开始时间   结束时间   周转时间   带权周转时间\n");
	s_f_t_w_time(p);
	return 0;
}

编程小菜鸟,欢迎各位大佬指正错误或者不规范的地方!

运行结果在这下面:

以下是C语言实现FCFS代码的示例: ```c #include <stdio.h> #include <stdlib.h> struct Process { int pid; // 进程ID int arrival_time; // 到达时间 int burst_time; // 执行时间 int waiting_time; // 等待时间 int turnaround_time; // 周转时间 }; void calculate_times(struct Process *processes, int num_processes) { int current_time = 0; // 当前时间 for (int i = 0; i < num_processes; i++) { if (processes[i].arrival_time > current_time) { current_time = processes[i].arrival_time; // 执行该进程前需要等待 } processes[i].waiting_time = current_time - processes[i].arrival_time; processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time; current_time += processes[i].burst_time; } } void print_table(struct Process *processes, int num_processes) { printf("%-10s%-16s%-16s%-18s%-20s\n", "Process ID", "Arrival Time", "Burst Time", "Waiting Time", "Turnaround Time"); for (int i = 0; i < num_processes; i++) { printf("%-10d%-16d%-16d%-18d%-20d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time); } printf("\n"); } int main() { int num_processes; printf("Enter the number of processes: "); scanf("%d", &num_processes); struct Process *processes = malloc(num_processes * sizeof(struct Process)); for (int i = 0; i < num_processes; i++) { printf("Enter arrival time and burst time for process %d: ", i + 1); scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time); processes[i].pid = i + 1; } calculate_times(processes, num_processes); print_table(processes, num_processes); free(processes); return 0; } ``` 这段代码可以计算多个进程的到达时间、执行时间、等待时间和周转时间,并输出成表格形式。其中,我们假设输入时可以保证进程按到达时间递增的顺序输入。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一串平凡的代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值