Glove
Glove

Reputation: 960

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. they have to be generated sequentially in a round-robin fashion. Is there any algorithm for this? Any help? Thank you.

Upvotes: 0

Views: 544

Answers (1)

pmg
pmg

Reputation: 108968

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

Upvotes: 1

Related Questions