【无标题】

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TIME_SLICE 2
 
// 进程状态:就绪 等待 阻塞 完成
enum process_type{
    process_type_wait = 'W',
    process_type_run = 'R',
    process_type_block = 'B',
    process_type_finish = 'F'
};

typedef struct PCB{ 
    char name;
    int Max[100]; 
    int Need[100];
    int Allocate[100]; 
	int run_time; 
	char state;
	PCB *next; 
}PCB; 
 
char Name[100]={0};
int Available[100];
int Request[100];
int Work[100];  
bool Finish[100];
char Security[100];
int M = 100;
int N = 100;
bool flag = true; 

void init(PCB *list);
void create_process(PCB *list);
void print(PCB *list);
void test(PCB *list);
void Retest(PCB *list);
bool bank(PCB *list);
bool safe(PCB *list);
void round_robin(PCB *list);
 
int main(){	
	PCB *list = (PCB *)malloc(sizeof(PCB));
	if(list == NULL){
		printf("动态分配内存失败!");
	}
    list->next = NULL;
	srand((int)time(NULL));
	init(list);
	printf("\n");
	print(list);
	if(!safe(list)) {
		exit(0);
	}
	printf("\n"); 
	round_robin(list);
	return 0;
}
 
void init(PCB *list){
    int m,n;
	printf("系统可用资源种类数为:");
	scanf("%d",&n);
    getchar();
	N = n;
	char name;
	int number; 
	for(int i = 1;i <= n;i++){
		printf("\t资源%d的名称",i);
		
		scanf("%c",&name);
		Name[i] = name;
		printf("\t资源%c的初始个数为",name);	
		scanf("%d",&number);
        getchar();
		Available[i] = number;
	}
	printf("\n请输入进程的数量:");	
	scanf("%d",&m);
	M = m;
	create_process(list);
}
 
void create_process(PCB *list){
	int temp[100] = {0};
	int number = 1; 
	while(number <= M){
		PCB *p = (PCB *)malloc(sizeof(PCB));
		if(p == NULL){
			printf("动态分配内存失败!");
		}
		printf("\t请输入第%d进程的进程名:",number); 
		getchar();
		scanf("%c",&p->name);
		bool flag;
		do{
			flag = false; 
			for(int i = 1;i <= N;i++){ 
				p->Max[i] = rand()%10;
				if(p->Max[i] > Available[i]){ 
					flag = true;
					break;	
				}
				p->Need[i] = p->Max[i];
				p->Allocate[i] = 0;		
			} 				
		}while(flag);
		p->run_time = 0; 
		p->state = process_type_wait;
		p->next = NULL;
		if(list->next == NULL){
			list->next = p;
		}else{
			PCB *move = list->next;
			while(move->next != NULL){
				move = move->next;
			}
			move->next = p;	 	
		}
		number++;
	}
	printf("\n");
} 

void print(PCB *list){
	printf("\n系统目前可用的资源(Available):\n");
	for(int i = 1;i <= N;i++){
		printf("%c  ",Name[i]);
	}
	printf("\n");
	for( i = 1;i <= N;i++){
		printf("%d  ",Available[i]);
	}
	printf("\n\n");
	printf("系统当前的资源分配情况及进程运行情况如下:\n");
	printf("          Max        Allocate        Need\n");
	printf("进程名  ");
	for( i = 1;i <= 3;i++){
		for(int j = 1;j <= N;j++){
			printf("%c  ",Name[j]);
		}
		printf("     "); 
	}
	printf("已运行时间   进程状态\n");
	PCB *p = list->next;
	while(p != NULL){
		printf("  %c\t",p->name);
	    for(int j = 1;j <= N;j++)
			printf("%d  ",p->Max[j]);
		printf("     "); 
		for( j = 1;j <= N;j++)
			printf("%d  ",p->Allocate[j]);
		printf("     ");
		for( j = 1;j <= N;j++)
			printf("%d  ",p->Need[j]);
		printf("        ");
		printf("  %d\t  ",p->run_time); 
		printf("%c\n",p->state); 
		p = p->next;
	}
}
 
void test(PCB *list){ 
	PCB *p = list->next;
	for(int i = 1;i <= N;i++){
		Available[i] -= Request[i];
		p->Allocate[i] += Request[i];
		p->Need[i] -= Request[i];
	}
}

void Retest(PCB *list){
	PCB *p = list->next;
	for(int i = 1;i <= N;i++){
		Available[i] += Request[i];
		p->Allocate[i] -= Request[i];
		p->Need[i] += Request[i];
	}
}
 
bool bank(PCB *list){
    PCB *p = list->next;
	for(int i = 1;i <= N;++i){
		Request[i] = rand()%(p->Need[i]+1);
	}
	int n = 0;
	for( i = 1;i <= N;++i){
		if(Request[i] == 0){
			n++;
		}
	}
	if(n == N){
		flag = false;
		return false;
	}
	printf("\n%c进程请求 —— ",p->name);
	for( i = 1;i <= N;i++){
		printf("资源%c:",Name[i]);
		printf("%d   ",Request[i]); 
	}
	printf("\n\n"); 
	int num = 1;
    for( i = 1;i <= N;++i){
		if(Request[i] > p->Need[i]){ 
			printf("%c进程申请的资源大于它需要的资源\n",p->name);
			printf("分配不合理,不予分配!\n");
			return false;
		}else{
            if(Request[i] > Available[i]){                         
				printf("%c进程申请的资源大于系统现可利用的资源\n",p->name);
				printf("系统尚无足够资源,不予分配!\n");
				return false;
			}
		}
	}
	test(list); 
	if(!safe(list)){
		Retest(list);
		return false;
	}
	return true;
}
 
bool safe(PCB *list){
	int m = 0;
	PCB *move = list->next;
	while(move != NULL && move->state != process_type_finish){
		move = move->next;
		++m;
	}
	for(int i = 1;i <= N;i++){
		Work[i] = Available[i];
	}
    for( i = 1;i <= M;i++){
    	if(i <= m){
    		Finish[i] = false;
		}else{
			Finish[i] = true;
		}
	} 
	int pnum = 1,apply;
	int num = 1;
	PCB *p = list->next; 
	while(p != NULL && pnum <= m){
		apply = 0;
		for(int i = 1;i <= N;i++){
			if(Finish[pnum] == false && p->Need[i] <= Work[i]){   
				apply++;
				if(apply == N){  
					for(int x = 1;x <= N;x++){
				        Work[x] += p->Allocate[x];
					} 
					Finish[pnum] = true;
					Security[num++] = p->name;
					pnum = 0; 
					p = list; 
				}
			}
		}
		pnum++; 
		p = p->next;
	}
	for( i = 1;i <= M;i++){
		if(Finish[i] == false){
			printf("系统不安全!\n");
			return false;
		}
	}
    printf("系统是安全的!\n");
    printf("存在一个安全序列:");
	for( i = 1;i <= m;i++){
		printf(" %c ",Security[i]);
		if(i < m){
			printf("->");
		} 
	}
	printf("\n");
	return true;
}
 
void round_robin(PCB *list){
    PCB *index = list->next;
    while (index != NULL && index->state != process_type_finish) {
    	flag = true; 
		if(bank(list)){
			index->run_time += TIME_SLICE;
	        int num = 0;
	        for(int i = 1;i <= N;++i){
	        	if(index->Max[i] == index->Allocate[i]){
        			num++;
				}	
			}
	        if (num == N) {
	            index->state = process_type_finish;
	            for(int i = 1;i <= N;i++){
					Available[i] += index->Allocate[i];
				}
	            printf("%c进程完成\n",index->name);
	        }else{
	            index->state = process_type_wait;
	        }
	    	if (index->state == process_type_finish){
		        list->next = index->next;
		        PCB *p = list;
		        while (p->next != NULL) {
		            p = p->next;
		        }
		        p->next = index;
				index->next = NULL;
			} else {
		        list->next = index->next;
		        PCB *p = list;
		        while (p->next != NULL && p->next->state != process_type_finish) {
		            p = p->next;
		        }
		        if (p->next == NULL) {
		            p->next = index;
		            p->next->next = NULL;
		        }else{
		            index->next = p->next;
		            p->next = index;
		        }
			}	
		}else{
			index->state = process_type_block;
	        list->next = index->next;
	        PCB *p = list;
	        while (p->next != NULL && p->next->state != process_type_finish) {
	            p = p->next;
	        }
	        if (p->next == NULL) {
	            p->next = index;
	            p->next->next = NULL;
	        }else{
	            index->next = p->next;
	            p->next = index;
	        }
		}
		if(list->next->state != process_type_finish){
			list->next->state = process_type_run;        	
		}
		if(flag){
	        print(list);	        
			printf("\n");	
		}
		index = list->next;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值