/**: 进程调度模拟程序
&
* 本程序来自一位网友,她写了大部分代码,可是错误太多,
*漏洞百出,不尽人意,下面是我修改后的。希望用到的人可以多多参考,
* 看了的人给些意见!
*/
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define READY 2 /* 进程就绪 */
#define END 4 /* 进程结束 */
typedef int status;
typedef struct PCBSTRU
{ int jc_name; /* 进程名 */
int NeedTime; /* 要求运行的时间 */
int UsedTime; /* 已运行的时间 */
int youxian; /* 进程优先数标记 */
status jc_status; /* 进程状态 */
struct PCBSTRU *pPrevPCB; /* 前一个PCB地址 */
struct PCBSTRU *pNextPCB; /* 下一个PCB地址 */
} OSPCB;
typedef struct PCBQUEUEHEAD /* 进程队列头结构 */
{ OSPCB *ProceQueueHead; /* 进程队列头指针 */
int PCBNumber; /* 进程队列当前PCB总数 */
} QueueHeadNode;
void PrintResult(QueueHeadNode *pHeadNode);
void AttemperProcess(QueueHeadNode *pHeadNode)
{
printf("/n进程初始状态:/n");
PrintResult(pHeadNode);
OSPCB *p; /* 当前运行的进程PCB的指针*/
int iCounterPCB, iCounterTime,
iEndNum = 0; /* 完成运行进程的计数器 */
/* 处理机对进程队列按优先数顺次进行调度 */
printf("/n/n开始运行:/n");
for (iCounterPCB = 0, p = pHeadNode->ProceQueueHead;
iCounterPCB < pHeadNode->PCBNumber; iCounterPCB++)
for (iCounterTime = 0; iCounterTime < p->NeedTime; iCounterTime++)
{
p->UsedTime++; /* 运行1次 */
if (p->UsedTime == p->NeedTime)
{
p->jc_status = END; /* 设置为结束状态 */
p = p->pNextPCB; /* 指向下1个进程PCB */
iEndNum++;
}
PrintResult(pHeadNode); /* 打印当前进程队列状态 */
/* 如果所有的进程都运行完则退出 */
if (iEndNum == pHeadNode->PCBNumber) break;
}
printf("/n按优先数调度运行完毕!/n");
}
void DeleteQueue(QueueHeadNode *pHeadNode)
{
OSPCB *pDeletePCB;
while (pHeadNode->PCBNumber--)
{
pDeletePCB = pHeadNode->ProceQueueHead;
pHeadNode->ProceQueueHead =
pHeadNode->ProceQueueHead->pNextPCB;
free(pDeletePCB);
}
}
void PrintResult(QueueHeadNode *pHeadNode)
{
int iCounter;
OSPCB *p = pHeadNode->ProceQueueHead;
printf("/n进程名 要求运行时间 已运行时间 优先数 状态/n");
for (iCounter = 0; iCounter < pHeadNode->PCBNumber; iCounter++)
{
printf("PCB%-4d", p->jc_name);
printf("%9d", p->NeedTime);
printf("%12d", p->UsedTime);
printf("%9d", p->youxian);
switch (p->jc_status)
{
case READY:printf("%s", " READY");break;
case END:printf("%s", " END");break;
default:break;
}
p = p->pNextPCB;
printf("/n");
}
}
int main()
{ int a[5][4]={{1,1,0,1},{2,2,0,2},{3,3,0,3},{4,4,0,4},{5,5,0,5}};
QueueHeadNode ProcHeadNode;
QueueHeadNode *pHeadNode= NULL;
OSPCB *pNewPCBNode= NULL;
int i,j;
ProcHeadNode.PCBNumber = 0; /*进程队列当前pcb总数为0*/
ProcHeadNode.ProceQueueHead = NULL; /*进程队列头指针为空*/
pHeadNode =&ProcHeadNode;
for (i=4;i>=0;i--)
{
if (!(pNewPCBNode = (struct PCBSTRU *)malloc(sizeof(struct PCBSTRU))))
{printf("生成结点时出错!/n");exit(1);}
pNewPCBNode->jc_name =a[i][0];
pNewPCBNode->NeedTime =a[i][1];
pNewPCBNode->UsedTime =a[i][2];
pNewPCBNode->youxian =a[i][3];
pNewPCBNode->jc_status =READY;
pNewPCBNode->pPrevPCB = pNewPCBNode->pNextPCB = NULL;
/*入队*/
OSPCB *p;
if (pHeadNode->ProceQueueHead == NULL)
{
pHeadNode->ProceQueueHead = p = pNewPCBNode;
}
else
{
pNewPCBNode->pPrevPCB = p;
p->pNextPCB = pNewPCBNode;
pNewPCBNode->pNextPCB = pHeadNode->ProceQueueHead;
pHeadNode->ProceQueueHead->pPrevPCB =pNewPCBNode;
p = pNewPCBNode;
}
ProcHeadNode.PCBNumber++;
}
/*调度*/
AttemperProcess(&ProcHeadNode);
DeleteQueue(&ProcHeadNode);
getch();
return 0;
}
&
* 本程序来自一位网友,她写了大部分代码,可是错误太多,
*漏洞百出,不尽人意,下面是我修改后的。希望用到的人可以多多参考,
* 看了的人给些意见!
*/
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define READY 2 /* 进程就绪 */
#define END 4 /* 进程结束 */
typedef int status;
typedef struct PCBSTRU
{ int jc_name; /* 进程名 */
int NeedTime; /* 要求运行的时间 */
int UsedTime; /* 已运行的时间 */
int youxian; /* 进程优先数标记 */
status jc_status; /* 进程状态 */
struct PCBSTRU *pPrevPCB; /* 前一个PCB地址 */
struct PCBSTRU *pNextPCB; /* 下一个PCB地址 */
} OSPCB;
typedef struct PCBQUEUEHEAD /* 进程队列头结构 */
{ OSPCB *ProceQueueHead; /* 进程队列头指针 */
int PCBNumber; /* 进程队列当前PCB总数 */
} QueueHeadNode;
void PrintResult(QueueHeadNode *pHeadNode);
void AttemperProcess(QueueHeadNode *pHeadNode)
{
printf("/n进程初始状态:/n");
PrintResult(pHeadNode);
OSPCB *p; /* 当前运行的进程PCB的指针*/
int iCounterPCB, iCounterTime,
iEndNum = 0; /* 完成运行进程的计数器 */
/* 处理机对进程队列按优先数顺次进行调度 */
printf("/n/n开始运行:/n");
for (iCounterPCB = 0, p = pHeadNode->ProceQueueHead;
iCounterPCB < pHeadNode->PCBNumber; iCounterPCB++)
for (iCounterTime = 0; iCounterTime < p->NeedTime; iCounterTime++)
{
p->UsedTime++; /* 运行1次 */
if (p->UsedTime == p->NeedTime)
{
p->jc_status = END; /* 设置为结束状态 */
p = p->pNextPCB; /* 指向下1个进程PCB */
iEndNum++;
}
PrintResult(pHeadNode); /* 打印当前进程队列状态 */
/* 如果所有的进程都运行完则退出 */
if (iEndNum == pHeadNode->PCBNumber) break;
}
printf("/n按优先数调度运行完毕!/n");
}
void DeleteQueue(QueueHeadNode *pHeadNode)
{
OSPCB *pDeletePCB;
while (pHeadNode->PCBNumber--)
{
pDeletePCB = pHeadNode->ProceQueueHead;
pHeadNode->ProceQueueHead =
pHeadNode->ProceQueueHead->pNextPCB;
free(pDeletePCB);
}
}
void PrintResult(QueueHeadNode *pHeadNode)
{
int iCounter;
OSPCB *p = pHeadNode->ProceQueueHead;
printf("/n进程名 要求运行时间 已运行时间 优先数 状态/n");
for (iCounter = 0; iCounter < pHeadNode->PCBNumber; iCounter++)
{
printf("PCB%-4d", p->jc_name);
printf("%9d", p->NeedTime);
printf("%12d", p->UsedTime);
printf("%9d", p->youxian);
switch (p->jc_status)
{
case READY:printf("%s", " READY");break;
case END:printf("%s", " END");break;
default:break;
}
p = p->pNextPCB;
printf("/n");
}
}
int main()
{ int a[5][4]={{1,1,0,1},{2,2,0,2},{3,3,0,3},{4,4,0,4},{5,5,0,5}};
QueueHeadNode ProcHeadNode;
QueueHeadNode *pHeadNode= NULL;
OSPCB *pNewPCBNode= NULL;
int i,j;
ProcHeadNode.PCBNumber = 0; /*进程队列当前pcb总数为0*/
ProcHeadNode.ProceQueueHead = NULL; /*进程队列头指针为空*/
pHeadNode =&ProcHeadNode;
for (i=4;i>=0;i--)
{
if (!(pNewPCBNode = (struct PCBSTRU *)malloc(sizeof(struct PCBSTRU))))
{printf("生成结点时出错!/n");exit(1);}
pNewPCBNode->jc_name =a[i][0];
pNewPCBNode->NeedTime =a[i][1];
pNewPCBNode->UsedTime =a[i][2];
pNewPCBNode->youxian =a[i][3];
pNewPCBNode->jc_status =READY;
pNewPCBNode->pPrevPCB = pNewPCBNode->pNextPCB = NULL;
/*入队*/
OSPCB *p;
if (pHeadNode->ProceQueueHead == NULL)
{
pHeadNode->ProceQueueHead = p = pNewPCBNode;
}
else
{
pNewPCBNode->pPrevPCB = p;
p->pNextPCB = pNewPCBNode;
pNewPCBNode->pNextPCB = pHeadNode->ProceQueueHead;
pHeadNode->ProceQueueHead->pPrevPCB =pNewPCBNode;
p = pNewPCBNode;
}
ProcHeadNode.PCBNumber++;
}
/*调度*/
AttemperProcess(&ProcHeadNode);
DeleteQueue(&ProcHeadNode);
getch();
return 0;
}