进程管理 C语言代码_百度文库

#include<stdio.h>
int m1,m2,i,ep,num;
char addr;
struct{
	int id,waiter1,priority;
	char status,stack[5];
}pcb[4];
struct{
	int value,waiter2;
}sem[3];
void init(){
 int j,k;
 for(j=1;j<=3;j++)
 {
	 pcb[j].id       = j;
	 pcb[j].status   = 'r';
	 pcb[j].waiter1  = 0;
	 pcb[j].priority = j;
	 for( k=0; k<=4; k++ ) pcb[j].stack[k] = '0'; 
 }
 for(j=1;j<=2;j++)
 {
	 sem[j].value   = 0;
	 sem[j].waiter2 = 0;
 }
 i    = 0;
 ep   = 0;
 addr = '0';
 m2   = 0;
 m1   = 0;
}
int find()
{
	int j;
for(j=1;j<=3;j++)
	{
		if( pcb[j].status == 'r') return(j);
	}
	return (0);
}
int p(int se,int p,char ad)

{
	int w;
	sem[se].value--;
	if(sem[se].value >= 0) return (1);
	else {
		printf("process%d blocked\n",p);
		ep = 0;
		pcb[p].status = 'w';
		pcb[p].waiter1 = 0;
		w = sem[se].waiter2;
		if(w == 0) sem[se].waiter2 = p;
		else {
			while (pcb[w].waiter1 != 0)
				w = pcb[w].waiter1;
			pcb[w].waiter1 = p;
		}
		pcb[p].stack[1] = i;
		pcb[p].stack[2] = ad;
		return (0);
	}
}
int v(int se, int p, char ad)
{
	int w;
	sem[se].value++;
	if(sem[se].value > 0) return(1);
	else {
		w = sem[se].waiter2;
sem[se].waiter2 = pcb[w].waiter1;
		pcb[w].status = 'r';
		printf("wake up process%d\n",w);
	}
	pcb[p].stack[1] = i;
	pcb[p].stack[2] = ad;
	return (0);
}
int process1()
{
	if (addr == 'm') goto m;
	i = 1;
	m1 = 1;
a: printf("process1 calls p on sem1!\n");
	printf("\n process printing m1=%d\n\n",m1);
	if(p(1,1,'m') == 0) return(0);
m: printf("\n=>process1 i=%d\n\n",i);
	i+=5;
	goto a;
}
int process2()
{
	if( addr == 'm') goto m;
	if( addr == 'n') goto n;
	i = 1;
a: printf("\n  process2 printing m1=%d\n",m1);
	m2=2*m1;
	printf("process2 call p on sem2\n");
	if( p(2,2,'m')==0 ) return(0);
m: printf("process2 call v on sem1!\n");
	if( v(1,2,'n')==0 ) return (0);
n: printf("\n=>process2 i=%d\n\n",i);
	i+=10;
	goto a;
}int process3()
{
	if(addr == 'm') goto m;
	if(addr == 'n') goto n;
	i = 1;
a: printf("\n=>process3 i=%d\n",i);
	if( i>=num ) goto b;
	printf("\n  process3 printing 2^i=%d\n\n",m2);
	m1 = m2;
	getchar();
	printf("process3 calls v on sem2!\n");
	if( v(2,3,'m') == 0) return (0);
m: i++;
	goto a;
b: printf("\nprocess3 calls p on sem2!\n");
	if( p(2,3,'n') == 0 ) return (0);
n: ;
}
int scheduler()
{
	int pd;
	for( ; ; )
	{
		pd = find();
		if( pd == 0 ) return 0;
		if( ep == 0 ) {
			pcb[pd].status = 'e';
			ep=pd;
		}
		else if ( pcb[pd].priority < pcb[ep].priority )
		{
			pcb[ep].status = 'r';
			printf("process%d ready\n",ep);
			pcb[pd].status = 'e';
			ep=pd;
		}else if ( pcb[pd].priority < pcb[ep].priority )
		{
			pcb[ep].status = 'r';
			printf("process%d ready\n",ep);
			pcb[pd].status = 'e';
			ep=pd;
		}
		else return 1;
	printf("%d\n",pd);
	printf("prcess%d executing \n",ep);
	i    = pcb[pd].stack[1];
	addr = pcb[pd].stack[2];
	switch(pd)
	{
	case 1: process1(); break;
	case 2: process2(); break;
	case 3: process3(); break;
	default:printf("pd error\n");
	 }
	}
}

void main()
{
	printf("\n  intput num. == ");
	scanf("%d",&num);
	printf("\n\n==>we begin manage of process \n");
	printf("================================\n\n");
	getchar();
	init();
	if(scheduler()== 0 )
	{
		printf("\n\n===>result printing 2^i=%d\n",m2);
		printf("=====================================");
		getchar();
	}
}

 

转载于:https://www.cnblogs.com/20chenguanglin/p/5359567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值