• c语言实现调度器


    c语言实现调度器

    原文:https://gist.github.com/hlippek/3187590

    https://www.cs.ucr.edu/~vahid/rios/

    ——————————————————————————

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_PROCESSES 32  /* the maximal number of processes in the system */
    #define MAX_NAME_LEN  32
    
    /* Process control block - 
     * holding all process relevant informations 
    */
    struct pcb{
    	int pid;                   /* ID of the proces */
    	int prio;                  /* process priority */
    	int attached;              /* 1 if attached to processlist, else 0 */
    	int *function;             /* pointer to the process function */
    	char name[MAX_NAME_LEN];   /* Name of the process */
    };
    
    static struct pcb processlist[MAX_PROCESSES];
    int process0();
    int process1();
    
    int process_attach(char *name, int prio, void *function)
    {
    	int i = 0;
    	int ret = -1;
    	printf("[dbg] process_attach
    ");
    	while(i < MAX_PROCESSES)
    	{
    		if(strlen(name) > MAX_NAME_LEN)
    		{
    			printf("[err] wrong stringlen
    ");
    			return ret;
    		}
    			
    		if(processlist[i].attached != 1)
    		{
    			printf("attach process at %d
    ", i);
    			processlist[i].pid = i;
    			strcpy(processlist[i].name, name);
    			processlist[i].prio = prio;
    			processlist[i].function = function;
    			processlist[i].attached = 1;
    			ret = 0;
    			break;
    		}
    		printf("
    ");
    		i++;
    	}
    	return ret; 
    	
    
    }
    
    int process_detach(int pid)
    {
    	processlist[pid].attached = 0;
    	return 0;
    }
    
    /*
     * basic implementation of a RR scheduler 
     */
    int scheduler()
    {
    	int i = 0;
    	void (*p)(void);
    	
    	while(1)
    	{
    		for(i = 0; i < MAX_PROCESSES; i++)
    		{
    			if(processlist[i].attached == 1)
    			{
    				p = (void *)processlist[i].function;
    				(*p)();
    			}
    		}
    	}
    	return 0;
    }
    
    /*** Testdriver ***/
    int process0()
    {
    	printf("0
    ");
    	return 0;
    }
    
    int process1()
    {
    	printf("1
    ");
    	return 0;
    }
    
    int main()
    {
    	/*
    	 * test run here
    	 * */
    	printf("basic_scheduler Demo
    ");
    	process_attach("process0", 100, process0);
    	process_attach("process1", 50,  process1);
    	scheduler();
    	return 0;
    }
    

      

  • 相关阅读:
    软件工程概论总结第三章
    软件工程概论总结第二章
    软件工程概论总结
    软件工程概论10-软件测试
    软件工程概论9-软件实现
    软件工程概论-8面向对象设计
    软件工程概论-7面向对象分析
    软件工程概论-6面向对象基础
    软件工程概论-5软件工程中的形式化方法
    软件工程概论-4需求过程
  • 原文地址:https://www.cnblogs.com/oxspirt/p/13985723.html
Copyright © 2020-2023  润新知