⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.c

📁 设计一个按优先数调度算法实现处理器调度的程序。 [提示]: (1) 假定系统有5个进程
💻 C
字号:
/**
*Title :problem1 处理器调度
*Author:fanbo
*Date  :2005.3.20
*No.   :200231500348
*
**/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

struct pcb
{
	char name[3];
	int runtime;  //要求运行时间
	int level;    //优先数
	char state;
	struct pcb *next;
};

struct pcb *creat(){
	int i,j;
	struct pcb *head,*tail,*temp,*previous;
	char pcbName[5][3]={"p1","p2","p3","p4","p5"};

	i = 1;
	j = 1;
	temp = (struct pcb *)malloc(sizeof(struct pcb));
	temp->level = rand()%5;
	strcpy(temp->name,pcbName[0]);
	temp->next = NULL;
	temp->runtime = rand()%10;
	temp->state = 'R';
	    
	head  = tail = temp;

	//////将初始化产生的结构体数组,按优先数的大小进行排序////////////////////
	while(i<5){
		temp = (struct pcb *)malloc(sizeof(struct pcb));
		temp->level = rand()%5;
		strcpy(temp->name,pcbName[j]);
		temp->runtime = rand()%10+1;
		temp->state = 'R';
		if(temp->level>head->level){
			head = temp;
			temp->next = tail;
		}
		else{
			previous = head;
			while(previous->next!=NULL){
				if((temp->level <= previous->level)&&(temp->level >= (previous->next)->level)){
					temp->next = previous->next;
					previous->next = temp;
					break;
				}
				else previous = previous->next;
			}//while
			if((previous->next)==NULL){
				tail->next = temp;
				temp->next = NULL;
				tail = temp;
			}
		}
		i++;
		j++;
	}//while
    	
	//////////////////////////////////////////////////////////////////////
	return(head);
}

void main( void ){
	struct pcb *head,*tail,*p;
	head = creat();
	p =  head;
	tail = p;
    ////////////////////////////将五个进程信息显示/////////////////////////////
	printf("The 5 process are :\n\tname runtime level state nextProcess\n");
	while((p!=NULL)){		
		printf("\t%s",p->name);
		printf(" \t%d",p->runtime);
		printf(" \t%d",p->level);
		printf("   %c",p->state);
		printf(" \t      %s\n",p->next->name);

		p = p->next;
		if(tail->next!=NULL)
			tail = tail->next;
		
	}
    ///处理器总是选队首进程运行。采用动态改变优先数的办法,进程每运行1次,优先数减1,运行时间减1。
	///进程运行一次后,若要求运行时间不等于0,则将它加入队列,否则,将状态改为"结束",退出队列。
	printf("The series of these process is as follows:\n");
	while(head->next!=NULL){
		printf("%s--->",head->name);
		head->level --;
		head->runtime --;
		if(head->runtime == 0){
			head->state = 'E';
			head = head->next;
		}
			
		else{
			p = head;
			if(head->next!=NULL)
				head = head->next;			
			tail->next = p;	
			tail = p;
			p->next = NULL;
			
		}
	}//while
	printf("END!\n");
	getchar();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -