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 */
}
精彩评论