资源简介
本程序是用单链表对列来管理资源(进程),实现了对进程的优先级调度。
代码片段和文件信息
#include
#include
#include
#define NAME_LEN 10
#define MAX 5
/* 进程的所以状态*/
typedef enum{
WAIT
READY
RUNNING
FINISH
}pcb_state;
/*进程的所有优先级级别*/
typedef enum{
MAX_HIGH=0
HIGH=10
MIDDLE=20
LOW=30
MIN_LOW=40
DEFFAULT=50
}pcb_priority;
typedef struct pcb_node{
char name[NAME_LEN];
int priority;
int needtime;
}PCB_NODE;
typedef struct node{
struct node *next;
PCB_NODE imm_pcb;
int round;
int cputime;
int count;
int state;
}PCB;
/*
* Function: init_pcb
*
*Description:初始化进程结点
*
*/
void init_pcb(PCB *pnode)
{
if(pnode == NULL)
return ;
pnode->next = NULL;
// pnode->imm_pcb.name = NULL;
memset(pnode->imm_pcb.name‘0‘NAME_LEN);
pnode->imm_pcb.priority =MIDDLE;
pnode->imm_pcb.needtime =0;
pnode->round =0;
pnode->cputime =0;
pnode->count =0;
pnode->state = READY;
}
PCB *pcb_malloc(int size)
{
return malloc(size);
}
/*
* Function: creat_pcb
*
* Description:建立一个进程结点,即给进程结点一个实际的意义
*
*/
PCB *creat_pcb(PCB *pnodePCB_NODE *pcbNode)
{
if(pnode == NULL && pcbNode == NULL)
return NULL;
init_pcb(pnode);
strcpy(pnode->imm_pcb.namepcbNode->name);
if(pcbNode->priority !=DEFFAULT)
pnode->imm_pcb.priority = pcbNode->priority;
pnode->imm_pcb.needtime = pcbNode->needtime;
return pnode;
}
/*
*Function:insert_ready
*
*Description:把有实际意义的结点插入到准备对列中。
*
*/
PCB *insert_ready(PCB *readyHeadPCB *pnode)
{
PCB *node;
PCB *front;
if(pnode == NULL)
return readyHead;
/* 准备对列为空时*/
if(readyHead == NULL)
{
readyHead = pnode;
return readyHead;
}
/**/
else if(readyHead->next == NULL)
{
if(readyHead->imm_pcb.priority > pnode->imm_pcb.priority)
{
pnode->next = readyHead;
readyHead = pnode;
}
else
{
readyHead->next = pnode;
// pnode->next = NULL;
}
return readyHead;
}
front = readyHead;
for(node = readyHead;node->next != NULL;node= node->next)
{
if(pnode->imm_pcb.priority < node->imm_pcb.priority)
{
/*pnode的优先级比准备对列中的第一个元素的优先级还要高*/
if(front == node)
{
pnode->next = node;
readyHead = pnode;
}
else
{
pnode->next = node;
front->next = pnode;
}
return readyHead;
}
else
front = node;
}
/*准备对列中最后一个结点*/
if(pnode->imm_pcb.priority < node->imm_pcb.priority)
{
pnode->next = node;
front->next = pnode;
}
else
{
node->next = pnode;
pnode->next = NULL;
}
return readyHead;
}
/*
* Function:insert_RUNNING
*
* Description:把准备对列中的第一个结点交给运行对列
*
*/
PCB *insert_run(PCB *runHeadPCB *pnode)
{
if(pnode == NULL)
return runHead;
pnode->next = runHead;
pnode->state = RUNNING;
return pnode;
}
/*
*Function:insert_wait
*
*Description:把运行对列中优先级小于准备对列的头结点时,则把这个结点插入等待对列
*
*/
PCB *insert_wait(PCB *ppnodePCB *waitHead)
{
ppnode->next = NULL;
ppnode->state =
- 上一篇:使用c语言编写的灰色预测模型
- 下一篇:C语言实现DES算法对任意文件加解密报告
评论
共有 条评论