• 进程管理 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
    ",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
    ",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!
    ");
    	printf("
     process printing m1=%d
    
    ",m1);
    	if(p(1,1,'m') == 0) return(0);
    m: printf("
    =>process1 i=%d
    
    ",i);
    	i+=5;
    	goto a;
    }
    int process2()
    {
    	if( addr == 'm') goto m;
    	if( addr == 'n') goto n;
    	i = 1;
    a: printf("
      process2 printing m1=%d
    ",m1);
    	m2=2*m1;
    	printf("process2 call p on sem2
    ");
    	if( p(2,2,'m')==0 ) return(0);
    m: printf("process2 call v on sem1!
    ");
    	if( v(1,2,'n')==0 ) return (0);
    n: printf("
    =>process2 i=%d
    
    ",i);
    	i+=10;
    	goto a;
    }int process3()
    {
    	if(addr == 'm') goto m;
    	if(addr == 'n') goto n;
    	i = 1;
    a: printf("
    =>process3 i=%d
    ",i);
    	if( i>=num ) goto b;
    	printf("
      process3 printing 2^i=%d
    
    ",m2);
    	m1 = m2;
    	getchar();
    	printf("process3 calls v on sem2!
    ");
    	if( v(2,3,'m') == 0) return (0);
    m: i++;
    	goto a;
    b: printf("
    process3 calls p on sem2!
    ");
    	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
    ",ep);
    			pcb[pd].status = 'e';
    			ep=pd;
    		}else if ( pcb[pd].priority < pcb[ep].priority )
    		{
    			pcb[ep].status = 'r';
    			printf("process%d ready
    ",ep);
    			pcb[pd].status = 'e';
    			ep=pd;
    		}
    		else return 1;
    	printf("%d
    ",pd);
    	printf("prcess%d executing 
    ",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
    ");
    	 }
    	}
    }
    
    void main()
    {
    	printf("
      intput num. == ");
    	scanf("%d",&num);
    	printf("
    
    ==>we begin manage of process 
    ");
    	printf("================================
    
    ");
    	getchar();
    	init();
    	if(scheduler()== 0 )
    	{
    		printf("
    
    ===>result printing 2^i=%d
    ",m2);
    		printf("=====================================");
    		getchar();
    	}
    }
    
  • 相关阅读:
    springmvc入门详解
    getClass 与getSimpleName
    mybati的存储过程
    mybatis与spring的整合
    mybatis分页插件以及懒加载
    mybatis知识总结
    【Java面试题】30 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。
    【Java面试题】29 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
    【Java面试题】28 简述synchronized和java.util.concurrent.locks.Lock的异同 ?
    【Java面试题】27 多线程笔试面试概念问答
  • 原文地址:https://www.cnblogs.com/20chenguanglin/p/5359567.html
Copyright © 2020-2023  润新知