
This is already my n-th attempt to write this program. Some help would be appreciated. I have created some children processes, a couple of them writes to a shared memory (array of ints, where the first element is the "counter" of all elements in the array). The other child deletes an element from the shared mem. I placed semaphores in appropriate places (I think) to protect the critical sections. But then each child is supposed to print out the whole array one character at a time. I did it like this:
note: buff is the pointer to the shm segment. I think the problem lies with the marked lines...(gcc gives me warnings there)
Code:
for(j = 0; j < 50; j++) {
if((*cnt) >= 29) /* array full, counter is a ptr to 1st el */
;
else {
(*cnt)++;
rand_n = random_num(26, 97);
fprintf((buff + (*cnt)*sizeof(int)), "%c ", rand_n);
} /* end of writing cs */
}
for(k = 0; k < (*counter); k++) {
rand_n = random_num(990001, 1000000);
usleep(rand_n);
fprintf(stdout, "%c ", buff + k*sizeof(int));
}