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

📄 os-lib.txt

📁 开发环境
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	setcolor(15);
	line(490,128,630,128);
	line(491,129,631,129);
	line(490,128,490,411);
	line(491,129,491,412);

        setcolor(15);
	line(491,411,630,411);
	line(490,412,631,412);
	line(630,411,630,131);
	line(631,410,631,130);

	setcolor(4);
	outtextxy(605,112,"KB");

	for(i=1;i<40;i++)
	{
		if(M_room[i-1]<10)
		{
			setfillstyle(1, 1);
    			bar(492,124+7*i,629,129+7*i);
		}
		setcolor(8);
		line(492,130+7*i,629,130+7*i);
	}
}

void init_Memory(void )
{
	int i;
	for(i=0;i<40;i++)
		M_room[i]=100;
}		

void ap_M(PCB *k)
{
	int i, j;
	for(i=0;i<k->M_size;)
		for(j=0;j<40;j++)
			if(M_room[j]>10)
			{
				M_room[j]=k->id;
				i++;
				break;
			}
}

void re_M(PCB *k)
{
	int i;
	for(i=0;i<40;i++)
		if(M_room[i]==k->id)
			M_room[i]=100;
}
		
void RandCr_Process(int n)
{
    int i, j;
    PCB *p, *q;
    srand((unsigned)time(NULL));
    j=0;
    while(n--)
    {
	p=(PCB *)malloc(sizeof(PCB));
	if(Pro_head!=NULL)
		for(q=Pro_head;q!=NULL;q=q->next)
			if(q->id==j)
				j++;
	p->id=j;
        p->need_time=rand()%50+1;
        p->static_prior=rand()%4;
        p->dynamic_prior=200;
	p->M_size=rand()%5+1;
        Rest_Source--;
        if(Rest_Source>=0)
	{
            p->Source_state=1;
	    p->state=1;
	}
        else
        {
            p->Source_state=0;
            p->state=2;
            Rest_Source++;
        }
	if(Begin)
        	p->used_time=rand()%p->need_time;
	else
		p->used_time=0;

        p->next=NULL;
        if(Pro_head==NULL)
            Pro_head=p;
        else
        {
            q=Pro_head;
            while(q->next!=NULL)
                q=q->next;
            q->next=p;
        }
	Length++;
/*分配内存*/
	ap_M(p);
    }
}

void Detel_Process(void )			/*取消进程*/
{
	int i, num;
	char k, j;
	PCB *q, *p, *r;

	setfillstyle(1, 7);
	bar(2,2,636,477);

	setcolor(15);
	line(158,108,465,108);
	line(159,109,464,109);
	line(158,108,158,341);
	line(159,109,159,342);

	setcolor(8);
	line(159,341,464,341);
	line(158,342,465,342);
	line(464,341,464,109);
	line(465,340,465,108);

	setcolor(8);
	line(308,198,405,198);
	line(309,199,404,199);
	line(308,198,308,217);
	line(309,199,309,218);

	setcolor(15);
	line(309,217,404,217);
	line(308,218,405,218);
	line(404,217,404,199);
	line(405,218,405,198);

	for(i=0;i<6;i++)
		drawmat(Creat_gr1+32*i,16,200+16*i,200,4);

	k=getch();
	q=NULL;
	if(k<=57&&k>=48)
	{
		j=k-48;
		drawmat(number[j],12,325,202,4);
		k=getch();
		if(k==13)
		{
			p=Pro_head;
			if(p->id==j)
			{
				Pro_head=Pro_head->next;
				q=p;
			}
			else
				for(q=p->next;q!=NULL;p=p->next,q=p->next)
					if(q->id==j)
					{
						p->next=q->next;
						break;
					}
						
			if(q!=NULL&&q->Source_state)
    			{
        			Rest_Source++;
	        		q->Source_state=0;        /*释放资源*/
    	        		if(Length>Source)
	        		{
		        		p=Pro_head;
        	        		r=p;
        	        		num=p->static_prior+p->dynamic_prior;
        	        		p=p->next;
        	        		for(;p!=NULL;p=p->next)
        	        			if(num>=p->static_prior+p->dynamic_prior&&p->state==2)
        	        			{
            	        				num=p->static_prior+p->dynamic_prior;
	    	        				r=p;
        	        			}
		        		Rest_Source--;
		        		r->Source_state=1;
	        			r->state=1;
	        		}
            		}
			re_M(q);
			free(q);
			Length--;
		}
		else
			Detel_Process();
	}
}

PCB *Pri_Process(void)        /*选出优先级最高的进程*/
{
    PCB    *p, *q;
    int    num;
    p=Pro_head;
    while((p!=NULL)&&!(p->Source_state))
	p=p->next;
    q=p;
    if(p!=NULL)
    {
        num=p->static_prior+p->dynamic_prior;
	p=p->next;
        for(;p!=NULL;p=p->next)
	    if((num>p->static_prior+p->dynamic_prior)&&p->Source_state)
            {
                num=p->static_prior+p->dynamic_prior;
                q=p;
	    }
	return q;
    }
    else
        return NULL;
}

void Guaqi_Process(void)       /*当进程数大于8时,挂起优先级低的进程*/
{
    PCB    *p, *q;
    int    num;
    p=Pro_head;
    while(p!=NULL&&p->state==3)
	p=p->next;
    q=p;
    num=q->static_prior+q->dynamic_prior;
    p=p->next;
    for(;p!=NULL;p=p->next)
        if((num<p->static_prior+p->dynamic_prior)&&(p->state!=3))
        {
            num=p->static_prior+p->dynamic_prior;
	    q=p;
        }
    q->state=3;    /*设置为挂起状态*/
    if(q->Source_state)
    {
        Rest_Source++;
	q->Source_state=0;        /*释放资源*/
	re_M(q);
    	if(Length>Source)
	{
		p=Pro_head;
        	q=p;
        	num=p->static_prior+p->dynamic_prior;
        	p=p->next;
        	for(;p!=NULL;p=p->next)
        		if(num>=p->static_prior+p->dynamic_prior&&p->state==2)
        		{
            			num=p->static_prior+p->dynamic_prior;
	    			q=p;
        		}
		Rest_Source--;
		q->Source_state=1;
		q->state=1;
	}
    }
}

int End_Process()        /*结束进程,分配资源*/
{
    PCB    *q, *p;
    int    num;
    p=Pri_Process();
    if(p->used_time>=p->need_time)
    {
        if(p==Pro_head)
            Pro_head=Pro_head->next;
        else
        {
            q=Pro_head;
            while(q->next!=p)
                q=q->next;
            q->next=p->next;
        }
	re_M(p);
        free(p);
        Rest_Source++;
    	if(Length>Source)
	{
		p=Pro_head;
        	q=p;
        	num=p->static_prior+p->dynamic_prior;
        	p=p->next;
        	for(;p!=NULL;p=p->next)
        		if(num>p->static_prior+p->dynamic_prior&&p->state==2)
        		{
            			num=p->static_prior+p->dynamic_prior;
	    			q=p;
        		}
		Rest_Source--;
		q->Source_state=1;
		q->state=1;
	}
	Length--;
    }
}

void Run_Process()
{
    PCB    *p, *q;
    q=Pri_Process();
    if(q!=NULL)
    {
	for(p=Pro_head;p!=NULL;p=p->next)
        {
	    if(p==q)
            {
                p->state=0;
                p->used_time++;
                p->dynamic_prior=200;
            }
            else
	    {
		if(p->state!=3)
			p->dynamic_prior--;
	    }
        }
    }
}

void Change_state(void)
{
	PCB *q;
	q=Pro_head;
	while(q!=NULL)
	{
		if(q->Source_state)
			q->state=1;
		else
			q->state=2;
		q=q->next;
	}
	if(Length>8)
		Guaqi_Process();
	if(Length>9)
		Guaqi_Process();

}


void read_key(void)
{
	int bkey;
	if(bioskey(1))
	{
		bkey=bioskey(0);
		switch(bkey)
		{
			case 19200:
				{
					if(key>1)
						key--;
					else
						key=5;
					break;
				}
			case 19712:
				{
					if(key<5)
						key++;
					else
						key=1;
					break;
				}
			case 18432:
				{
					if(Delay_time>10)
						Delay_time-=10;
					break;
				}
			case 20480:
				{
					if(Delay_time<100)
						Delay_time+=10;
					break;
				}
			case 7181:
				{
					if(key==1)
					{
						xx=!xx;
						menu();
					}
					if(key==2)
						RandCr_Process(1);
					if(key==3)
						Detel_Process();
					if(key==5)
						exit(0);
					break;
				}
			defult: break;
		}
		menu();
	}
}

void Delay(int n)
{
	long int i;
	while(n--)
		for(i=0;i<20000;i++)
			read_key();
}

main()
{
	PCB *p, *q;
	key=1;
	Cpu_time=1;
	xx=1;
	Begin=1;
	Delay_time=50;
	Pro_head=NULL;
	Length=0;
	Rest_Source=Source;
	initgr();
	init_Memory();
	menu();
	RandCr_Process(10);
	Begin=0;
	while(1)
	{
		if(xx)
	{
    		Change_state();
		Run_Process();
		board();
		Draw_Process();
    		End_Process();
		read_key();
		Cpu_time++;
	}
	Delay(Delay_time);
}
}

⌨️ 快捷键说明

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