操作系统对进程的管理方式
各位,下面是一个C的关于操作系统和进程管理的程序,是一个表弟要帮忙的,没学过操作系统完全不懂啊,请懂的帮忙一下,把下面的程序注释一下,尽量详细,比较急,拜托了!
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define getpch(type) (type*)malloc(sizeof(type))
#define NULL 0
/* 定义进程控制块PCB */
struct pcb
{
char cxm[10];
char state;
int yxs;
int ntime;
int rtime;
struct pcb* link;
}*ready=NULL,*p;
typedef struct pcb PCB; /* 排序输出各个进程PCB */
sort() /* 建立对进程进行优先级排列函数*/
{
PCB *first, *second;
int insert=0;
if((ready==NULL)||((p->yxs)>(ready->yxs))) /*优先级最大者,插入队首*/
{
p->link=ready;
ready=p;
}
else /* 进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->link;
while(second!=NULL)
{
if((p->yxs)>(second->yxs)) /*若插入进程比当前进程优先数大,*/
{ /*插入到当前进程前面*/
p->link=second;
first->link=p;
second=NULL;
insert=1;
}
else /* 插入进程优先数最低,则插入到队尾*/
{
first=first->link;
second=second->link;
}
}
if(insert==0) first->link=p;
}
}
input() /* 建立进程控制块函数*/
{
int i,num;
clrscr(); /*清屏*/
printf("\n 请输入进程数:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n 进程号No.%d:\n",i);
p=getpch(PCB);
printf("\n 输入进程名:");
scanf("%s",p->cxm);
printf("\n 输入进程优先数:");
scanf("%d",&p->yxs);
printf("\n 输入进程运行时间:");
scanf("%d",&p->ntime);
printf("\n");
p->rtime=0;
p->state='w';
p->link=NULL;
sort(); /* 调用sort函数*/
}
}
int space()
{
int l=0; PCB* pr=ready;
while(pr!=NULL)
{
l++;
pr=pr->link;
}
return(l);
}
disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/
{
printf("\n程序名\t状态\t优先数\t需要时间\t运行时间 \n");
printf(" %s",pr->cxm);
printf("\t%c",pr->state);
printf("\t%d",pr->yxs);
printf("\t%d",pr->ntime);
printf("\t\t%d",pr->rtime);
printf("\n");
}
check() /* 建立进程查看函数 */
{
PCB* pr;
printf("\n **** 当前正在运行的进程是:%s",p->cxm); /*显示当前运行进程*/
disp(p);
pr=ready;
printf("\n ****当前就绪队列状态为:\n"); /*显示就绪队列状态*/
while(pr!=NULL)
{
disp(pr);
pr=pr->link;
}
}
destroy() /*建立进程撤消函数(进程运行结束,撤消进程)*/
{
printf("\n 进程 [%s] 已完成.\n",p->cxm);
free(p);
}
running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/
{
(p->rtime)++;
if(p->rtime==p->ntime)
destroy(); /* 调用destroy函数*/
else
{
(p->yxs)--;
p->state='w';
sort(); /*调用sort函数*/
}
}
main() /*主函数*/
{
int len,h=0;
char ch;
input();
len=space();
while((len!=0)&&(ready!=NULL))
{
ch=getchar();
h++;
printf("\n The execute number:%d \n",h);
p=ready;
ready=p->link;
p->link=NULL;
p->state='R';
check();
running();
printf("\n 按任一键继续......");
ch=getchar();
}
printf("\n\n 进程已经完成.\n");
ch=getchar();
}
[最优解释]
本质上就是一个链表,每次选择链表中优先级数最高的那一项,它的运行时间+1,优先级数-1.
然后你输入回车,就当做是一个时间单位,进入下一次,同样是选优先级最高的,运行时间+1,优先级数-1.
如果有一项运行时间到了,释放该项,其余的继续执行,直到最终都执行完。
这其实反映的是操作系统中,进程的管理机制。各种抽象的概念都源自于基本的数据结构和算法实现
[其他解释]
《深入解析Windows操作系统-Windows Internals》
[其他解释]
没人回答的话大家赶紧跟贴拿分吧,福利来了~
[其他解释]
该回复于2011-11-01 11:38:52被版主删除
[其他解释]
该回复于2011-11-01 14:14:41被版主删除
[其他解释]
++,另外原本注释的就很好的啊
[其他解释]
没人回答的话大家赶紧跟贴拿分吧,福利来了~
[其他解释]
没人回答的话大家赶紧跟贴拿分吧,福利来了~