好好爱自己!

c语言实现调度器

c语言实现调度器

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

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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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\n");
    while(i < MAX_PROCESSES)
    {
        if(strlen(name) > MAX_NAME_LEN)
        {
            printf("[err] wrong stringlen\n");
            return ret;
        }
             
        if(processlist[i].attached != 1)
        {
            printf("attach process at %d\n", 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("\n");
        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\n");
    return 0;
}
 
int process1()
{
    printf("1\n");
    return 0;
}
 
int main()
{
    /*
     * test run here
     * */
    printf("basic_scheduler Demo\n");
    process_attach("process0", 100, process0);
    process_attach("process1", 50,  process1);
    scheduler();
    return 0;
}

  

posted @   立志做一个好的程序员  阅读(500)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2016-11-16 Object.setPrototypeOf 方法的使用
2016-11-16 安装完php 后添加到环境变量
2016-11-16 【转】How to build and install PHP 5.6.9 from source on Ubuntu 14.04 VPS
2016-11-16 【转】Xshell 十个技巧
2016-11-16 ubuntu 上下载PHP的源代码
2016-11-16 js中Object.__proto__===Function.prototype
2016-11-16 Array.length vs Array.prototype.length

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示