简体   繁体   English

C语言中的循环ID生成器

[英]round robin id generator in C

I am trying to code up a small operating system and I have 100 processes that need to have unique process IDs generated automatically. 我正在尝试编写一个小型操作系统,我有100个进程需要具有自动生成的唯一进程ID。 they have to be generated sequentially in a round-robin fashion. 它们必须以循环方式顺序生成。 Is there any algorithm for this? 有什么算法吗? Any help? 有什么帮助吗? Thank you. 谢谢。

Just make an array with 100 elements (initialized to 0) and manage that 只需制作一个包含100个元素的数组(初始化为0)并进行管理

int array[100] = {0};

/* kill process N */
void killprocess(int N) {
    array[N] = 0;
}

/* add process N */
void addprocess(int N) {
    array[N] = 1;
}

/* find free process starting with N */
int findfreeprocess(int N) {
    int k, ndx;
    for (k = 0; k < 100; k++) {
        ndx = (N + k) % 100;
        if (array[ndx] == 0) return ndx;
    }
    return -1; /* indicate no free process */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM