进程管理 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(); } }