开发者

round robin id generator in C

开发者 https://www.devze.com 2023-04-03 03:31 出处:网络
I am trying to code up a small operatin开发者_JAVA百科g system and I have 100 processes that need to have unique process IDs generated automatically. they have to be generated sequentially in a round-

I am trying to code up a small operatin开发者_JAVA百科g system and I have 100 processes that need to have unique process IDs generated automatically. 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

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 */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消