真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

OS實驗之進程調(diào)度-創(chuàng)新互聯(lián)

操作系統(tǒng)實驗之優(yōu)先級進程調(diào)度

自己寫的進程調(diào)度代碼,寫完還是很用成就感的,代碼里都有注釋,就不多說了,直接上完整代碼
運行環(huán)境:windows11,編譯器:dev-c++

創(chuàng)新互聯(lián)長期為上千多家客戶提供的網(wǎng)站建設(shè)服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為澄城企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都網(wǎng)站建設(shè),澄城網(wǎng)站改版等技術(shù)服務。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
#include#include#include#define BLOCKED -1
#define READY 0
#define RUNNING 1
#define FREE 0
#define BUSY 1

typedef struct rcb RCB;

typedef struct prs{	//記錄進程占用的資源 
	int rid;
	RCB *rcb;
	struct prs *next;
}PRS;

typedef struct pcb{	//進程控制塊 
	int pid;
	char name[10];
	short type;
	int prio;
	PRS *rsc;
	struct pcb *next;
}PCB;

typedef struct rcb{	//資源控制塊 
	int rid;
	int pid;
	short state;
	PCB *pHead;
	struct rcb *next;
}RCB;

PCB *ruHead;	//正在執(zhí)行的進程 
PCB *reHead;	//就緒進程
RCB *rescs;	//所有資源
int PID = 1;	//進程號計數(shù)值 


char* getstr(int len);	//解析命令
void create(char *pname, int prio);	//創(chuàng)建進程 
void request(int rid);	//申請資源
void release(int rid);	//釋放資源
void kill(int pid);	//刪除進程
void kill_rel(PCB *p);	//刪除進程時釋放資源
void inquire(char *name);	//查詢進程信息
void init();	//初始化
void running();	//輸出正在運行的進程
void re2ru();	//檢查就緒隊列,調(diào)整運行進程狀況
void Rel_resc(RCB *q, PCB *p);	//對于被釋放的資源,檢查有無阻塞進程并處理

int main(){
	init();
	getchar();
	char cmd[20], *s, para[10][10];
	int i, j, k;
	while(1){
		running();
		printf(">");
		s = getstr(sizeof(cmd));
		strcpy(cmd, s);
		free(s);
		i = 0;
		k = 0;
		do{
			s = (char*)malloc(10*sizeof(char));
			j = 0;
			for(; cmd[i] != ' ' && i< strlen(cmd); i++){
				*(s+j) = cmd[i];
				j++;
			}
			*(s+j) = '\0';
			strcpy(para[k], s);
			k++;
			i++;
			free(s);
		}while(i< strlen(cmd));
		if(strcmp(para[0], "cr") == 0) create(para[1], atoi(para[2]));
		else if(strcmp(para[0], "rq") == 0) request(atoi(para[1]));
		else if(strcmp(para[0], "rl") == 0) release(atoi(para[1]));
		else if(strcmp(para[0], "kl") == 0) kill(atoi(para[1]));
		else if(strcmp(para[0], "inq") == 0) inquire(para[1]);
		else if(strcmp(para[0], "exit") == 0){
			printf("退出進程!");
			break;
		}
		else printf("命令輸入錯誤,請重新輸入!\n");
	}
	return 1;
}

//解析命令 
char* getstr(int len){
	int i=0;
	char *p, c;
	p = (char*)malloc(len*sizeof(char));
	do{
		if(i == len){
			printf("命令過長\n");
			break;
		}
		c = getchar();
		if(c == '\n') continue;
		*(p+i)=c;
		i++;
	}while(c!='\n');
	if(itype = RUNNING;
	ruHead->next = NULL;
	ruHead->prio = 0;
	ruHead->pid = 0;
	memset(ruHead->name, 0, 10);
	reHead = (PCB*)malloc(sizeof(PCB));
	reHead->type = READY;
	reHead->next = NULL;
	reHead->prio = 0;
	reHead->pid = 0;
	//資源序列初始化 
	rescs = (RCB*)malloc(sizeof(RCB));
	rescs->pHead = NULL;
	rescs->rid = 0;
	rescs->pid = 0;
	rescs->state = FREE;
	rescs->next = NULL;
	printf("輸入需求資源數(shù)量:");
	scanf("%d", &rsum);
	for(i=0;irid = i+1;
		node->pid = 0;
		node->state = FREE;
		node->pHead = NULL;
		node->next = NULL;
		for(p1=rescs;p1->next!=NULL;p1=p1->next);
		p1->next = node;
	}
	printf("資源列表:\n");
	for(p=rescs->next;p!=NULL;p=p->next){
		printf("資源id:%d,", p->rid);
		if(p->state==FREE) printf("空閑\n");
		else printf("占用\n");
	}
	printf("初始化完畢\n");
}

//輸出正在運行的進程
void running(){
	PCB *p = ruHead->next;
	if(p==NULL) printf("*無運行進程\n");
	else printf("*進程%s正在運行\(zhòng)n", p->name); 
}

//創(chuàng)建一個進程
void create(char *pname, int prio){
	PCB *node, *p, *q;
	//創(chuàng)建進程PCB 
	node = (PCB*)malloc(sizeof(PCB));
	node->pid = PID++;
	node->prio = prio;
	strcpy(node->name, pname);
	node->type = READY;
	node->next = NULL;
	node->rsc = NULL;
	//把進程PCB按照優(yōu)先級插入就緒隊列 
	p = reHead->next;
	if(p==NULL) reHead->next = node;
	else{
		for(q=reHead;p!=NULL&&p->prio>=node->prio;p=p->next) q=p;
		q->next = node;
		node->next = p;
	}
	re2ru();
	printf(" 創(chuàng)建進程%s,優(yōu)先級為%d\n", pname, prio);
}

//申請資源
void request(int rid){
	PCB *p = ruHead->next;
	RCB *q = rescs->next;
	for(;q!=NULL&&q->rid!=rid;q = q->next);
	if(q==NULL){
		printf("沒有資源%d\n", rid);
		printf("資源列表:\n");
		for(q=rescs->next;q!=NULL;q=q->next){
			printf("資源id:%d,", q->rid);
			if(q->state==FREE) printf("空閑\n");
			else printf("進程%d占用\n", q->pid);
		}
	}
	else{
		//資源未被占用
		if(q->state == FREE){
			PRS *pr, *npr;
			pr = (PRS*)malloc(sizeof(PRS));
			pr->rcb = q;
			pr->rid = q->rid;
			pr->next = NULL;
			q->state = BUSY;
			q->pid = p->pid;
			npr = p->rsc;
			if(npr == NULL) p->rsc = pr;
			else{
				for(;npr->next!=NULL;npr=npr->next);
				npr->next = pr;
			}
			printf("已為進程%s分配資源%d\n", p->name, q->rid);
		}
		//資源已被占用
		else{
			PCB *n1, *n2;
			n1 = q->pHead;
			p->type = BLOCKED;
			ruHead->next = NULL;
//			for(n2=blHead;n2->next!=NULL;n2=n2->next);
//			n2->next = p;
			if(n1 == NULL) q->pHead = p;
			else{
				if(p->prio >n1->prio){
					q->pHead = p;
					p->next = n1;
				}
				else{
					for(;n1!=NULL&&n1->prio>=p->prio;n1=n1->next) n2=n1;
					n2->next = p;
					p->next = n1;
				}
			}
			re2ru();
			printf("資源%d被%d號進程占用,進程%s阻塞\n", q->rid, q->pid, p->name);
		}
	}
	
}

//釋放資源
void release(int rid){
	PCB *p = ruHead->next;
	RCB *q = rescs->next;
	for(;q!=NULL&&q->rid!=rid;q = q->next);
	if(q==NULL){
		printf("沒有資源%d\n", rid);
		printf("資源列表:\n");
		for(q=rescs->next;q!=NULL;q=q->next){
			printf("資源id:%d,", q->rid);
			if(q->state==FREE) printf("空閑\n");
			else printf("進程%d占用\n", q->pid);
		}
	}
	else{
		//資源被正在運行的進程占用
		if(q->pid == p->pid){
			PRS *n1, *n2;
			//先把資源從進程占用資源中刪除 
			n1 = p->rsc;
			if(n1->rid == q->rid){
				p->rsc = n1->next;
			}
			else{
				for(;n1->rid != q->rid;n1=n1->next) n2=n1;
				n2->next = n1->next;
			}
			//檢查此資源下有無阻塞進程并處理 
			Rel_resc(q, p);
		}
		//資源被其他進程占用 
		else printf("進程%s沒有占用資源%d,資源被%d號進程\n", p->name, q->rid, q->pid);
	}
}

//刪除進程的資源釋放 
void kill_rel(PCB *p){
	PRS *pr=p->rsc;
	if(pr!=NULL){
		RCB *q;
		for(;pr!=NULL;pr=pr->next){
			RCB *q = pr->rcb;
			Rel_resc(q, p); 
		}
	}
}

//關(guān)閉進程
void kill(int pid){
	PCB *p, *p1;
	p = ruHead->next;
	//刪除運行進程 
	if(p->pid == pid){
		ruHead->next = NULL;
		kill_rel(p); 
		printf("進程%d已被刪除!\n", pid);
		re2ru();
		return;
	}
	p = reHead;
	if(p!=NULL) for(;p!=NULL&&p->pid!=pid;p=p->next) p1=p;
	//刪除就緒進程 
	if(p!=NULL){
		p1->next = p->next;
		kill_rel(p);
		printf("進程%d已被刪除!\n", pid);
		re2ru();
		return;
	}
	RCB *q = rescs->next;
	//刪除阻塞進程 
	while(q!=NULL){
		p = q->pHead;
		if(p!=NULL) for(;p!=NULL&&p->pid!=pid;p=p->next) p1=p;;
		if(p==NULL) q=q->next;
		else break;
	}
	if(q!=NULL){
		p1->next = p->next;
		kill_rel(p);
		printf("進程%d已被刪除!\n", pid);
		re2ru();
		return;
	}
	else printf("查無進程%d\n", pid);
}

//查詢進程信息
void inquire(char *name){
	PCB *p;
	p = ruHead->next;
	if(strcmp(name, p->name) == 0){
		printf("進程id:%d,狀態(tài):運行,優(yōu)先級:%d\n", p->pid, p->prio);
		return;
	}
	p = reHead->next;
	for(;p!=NULL&&strcmp(name, p->name)!=0;p=p->next);
	if(p!=NULL){
		printf("進程id:%d,狀態(tài):就緒,優(yōu)先級:%d\n", p->pid, p->prio);
		return;
	}
	RCB *q = rescs->next;
	while(q!=NULL){
		p = q->pHead;
		for(;p!=NULL&&strcmp(name, p->name)!=0;p=p->next);
		if(p==NULL) q=q->next;
		else break;
	}
	if(q!=NULL){
		printf("進程id:%d,狀態(tài):阻塞,阻塞資源:%d\n", p->pid, q->rid);
		return;
	}
	printf("查無進程%s!\n");
}

//檢查就緒進程,調(diào)整運行隊列
void re2ru(){
	PCB *p1, *p2;
	p1 = ruHead->next;
	p2 = reHead->next;
	if(p2!=NULL){
		//無進程運行 
		if(p1 == NULL){
			ruHead->next = p2;
			reHead->next = p2->next;
			p2->type = RUNNING;
		}
		//有進程運行時 
		else{
			if(p2->prio>p1->prio){
				ruHead->next = p2;
				reHead->next = p2->next;
				p2->type = RUNNING;
				PCB *p, *q;
				p = reHead->next;
				if(p == NULL) reHead->next = p1;
				else{
					for(q = reHead;p!=NULL&&p->prio>=p1->prio;p=p->next) q = p;
					q->next = p1;
					p1->next = p;
				}
				p1->type = READY;
			}
		}
	}	
}

//處理釋放的資源
void Rel_resc(RCB *q, PCB *p){
	PCB *s1, *s2, *s3;
	PRS *n1; 
	s1 = q->pHead;
	//有被此資源阻塞的進程 
	if(s1 != NULL){
		q->pHead = s1->next;
		q->pid = s1->pid;
		s1->next = NULL;
		PRS *node;
		node = (PRS*)malloc(sizeof(PRS));
		node->rcb = q;
		node->rid = q->rid;
		node->next = NULL;
		n1 = s1->rsc;
		if(n1 == NULL) s1->rsc = node;
		else{
			for(;n1->next!=NULL;n1=n1->next);
			n1->next = node;
		}
		s1->type = READY;
		s2 = reHead->next;
		if(s2==NULL) reHead->next=s1;
		else{
			if(s1->prio>s2->prio){
				reHead->next = s1;
				s1->next = s2;
			}
			else{
				for(;s2->prio>=s1->prio&&s2!=NULL;s2=s2->next) s3=s2;
				s3->next = s1;
				s1->next = s2;
			}
		}
		re2ru();
	}
	//沒有被此資源阻塞的進程
	else q->pid = 0;
	printf("資源%d已被進程%s釋放\n", q->rid, p->name);
}

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧


本文標題:OS實驗之進程調(diào)度-創(chuàng)新互聯(lián)
當前地址:http://weahome.cn/article/dcochi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部