吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1810|回复: 8
收起左侧

[C&C++ 转载] 操作系统进程调度实验,把老师代码完善了一下,实现了优先级调度和时间片轮转算法

[复制链接]
Atdwlrj 发表于 2022-12-9 19:15
操作系统进程调度实验,把老师代码完善了一下,实现了优先级调度和时间片轮转算法,这里如果有同样在做实验的小伙伴,可以借鉴一下

[C] 纯文本查看 复制代码
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)
#include <stdio.h>
#include <dos.h>
#include <assert.h>
#include <stdlib.h>
#include <conio.h>
#include <io.h>
#include <windows.h>
#define P_NUM 3
#define P_TIME 50
enum state {
	ready,
	execute,
	block,
	finish
};
struct pcb {
	char name[4];
	int priority;
	int cputime;
	int needtime;
	int count;
	int round;
	enum state process;
	struct pcb* next;
};
struct pcb* get_process();
struct pcb* get_process() {
	struct pcb* q = NULL;
	struct pcb* t = NULL;
	struct pcb* p = NULL;
	int i = 0;
	printf("input name and needtime,please input 3 processes\n");
	while (i < P_NUM) {
			q = (struct pcb*)malloc(sizeof(struct pcb));
			assert(q);
			scanf("%s", &(q->name));
			scanf("%d", &(q->needtime));
			q->cputime = 0;
			q->priority = P_TIME - q->needtime;
			q->process = ready;
			q->next = NULL;
			if (i == 0) {
				p = q;
				t = q;
			}
			else {
				t->next = q;
				t = q;
			}
			i++;
		}/*while*/
		return p;
	
}
void  display(struct pcb* p) {
	printf("name    cputime    needtime    priority    state\n");
	while (p) {
		printf("%s", p->name);
		printf("        ");
		printf("%d", p->cputime);
		printf("        ");
		printf("%d", p->needtime);
		printf("        ");
		printf("%d", p->priority);
		printf("        ");
		switch (p->process) {
		case ready:
			printf("ready\n");
			break;
		case execute:
			printf("execute\n");
			break;
		case block:
			printf("block\n");
			break;
		case finish:
			printf("finish\n");
			break;
		}
		p = p->next;
	}
}
int process_finish(struct pcb* q) {
	int bl = 1;
	while (bl && q) {
		bl = bl && q->needtime == 0;
		q = q->next;
	}
	return bl;
}
void cpuexe(struct pcb* q) {
	struct pcb* t = q;
	int max_priority = 0;
	while (q) {
		if (q->process != finish) {
			q->process = ready;
			if (q->needtime == 0) {
				q->process = finish;
			}
		}
		if (max_priority < q->priority && q->process != finish) {
			max_priority = q->priority;
			t = q;
		}
		q = q->next;
	}
	assert(t);
	if (t->needtime != 0) {
		t->priority -= 3;
		t->needtime--;
		t->process = execute;
		t->cputime++;
	}
}
void priority_cal() {
	struct pcb* p;
	int cpu = 0;
	p = get_process();
	while (!process_finish(p)) {
		cpu++;
		printf("cputime:%d\n", cpu);
		cpuexe(p);
		display(p);
		Sleep(5);
	}
	printf("All processes have finished,press any key to exit");
	getch();
}
void display_menu() {
	printf("CHOOSE THE ALGORITHM:\n");
	printf("1 PRIORITY\n");
	printf("2 ROUNDROBIN\n");
	printf("3 EXIT\n");
}
struct pcb* get_process_round() {
	struct pcb* q;
	struct pcb* t;
	struct pcb* p;
	int i = 0;
	printf("input name and time,please input 3 processes\n");
	while (i < P_NUM) {
		q = (struct pcb*)malloc(sizeof(struct pcb));
		assert(q);
		scanf("%s", &(q->name));
		scanf("%d", &(q->needtime));
		q->cputime = 0;
		q->round = 0;
		q->count = 0;
		q->process = ready;
		q->next = NULL;
		if (i == 0) {
			p = q;
			t = q;
		}
		else {
			t->next = q;
			t = q;
		}
		i++;
	}  /*while*/
	return p;
}
void cpu_round(struct pcb* q) {
	q->cputime += 2;
	q->needtime -= 2;
	if (q->needtime < 0) {
		q->needtime = 0;
	}
	q->count++;
	q->round++;
	q->process = execute;
}
struct pcb* get_next(struct pcb* k, struct pcb* head) {
	struct pcb* t;
	t = k;
	do {
		t = t->next;
	} while (t && t->process == finish);
	if (t == NULL) {
		t = head;
		while (t->next != k && t->process == finish) {
			t = t->next;
		}
	}
	return t;
}
void set_state(struct pcb* p) {
	while (p) {
		if (p->needtime == 0) {
			p->process = finish;
		}
		if (p->process == execute) {
			p->process = ready;
		}
		p = p->next;
	}
}
void display_round(struct pcb* p) {
	printf("NAME  CPUTIME  NEEDTIME  COUNT  ROUND  STATE\n");
	while (p) {
		printf("%s", p->name);
		printf("        ");
		printf("%d", p->cputime);
		printf("        ");
		printf("%d", p->needtime);
		printf("        ");
		printf("%d", p->count);
		printf("        ");
		printf("%d", p->round);
		printf("        ");
		switch (p->process) {
		case ready:
			printf("ready\n");
			break;
		case execute:
			printf("execute\n");
			break;
		case finish:
			printf("finish\n");
			break;
		}
		p = p->next;
	}
}
void round_cal() {
	struct pcb* p;
	struct pcb* r;
	int cpu = 0;
	p = get_process_round();

	r = p;
	while (!process_finish(p)) {
		cpu += 2;
		cpu_round(r);
		r = get_next(r, p);
		printf("cpu %d\n", cpu);
		display_round(p);
		set_state(p);
		Sleep(5);
	}
	printf("All processes have finished,press any key to exit");
	getch();
}
/* 主程序 */
void main() {
	int user_input;
	display_menu();
	scanf("%d", &user_input);
	switch (user_input) {
	case 1:priority_cal(); break;
	case 2:round_cal(); break;
	case 3:break;
	default:
		display_menu();
		scanf("%d", &user_input);
		break;
	}
}
捕获.JPG
捕获2.JPG

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
diwuc + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

心鱼鱼 发表于 2022-12-9 19:47
大佬真的超级厉害
likaiaixuexi 发表于 2022-12-9 20:00
我这小白啥时候成为您这么厉害的大佬,修改代码
onpure 发表于 2022-12-9 20:07
diwuc 发表于 2022-12-10 00:26
来学习一下
Lshandou 发表于 2022-12-10 08:58
支持,新人不懂,还得从头开始啊,环境的配置
19sl 发表于 2022-12-17 20:32
学习一下
libaogui2022 发表于 2022-12-25 21:28
感谢分享,学习下
libaogui2022 发表于 2023-2-26 22:07
谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 00:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表