for (int i = i; i < xprocs; i++) {
pid = fork ();
if (pid == 0)
func ();
}
void func ()
{
static error = 0;
while (do something) {
if (something goes wrong)
error ++;
}
}
So each forked process will have its own "error" variable that starts with the counter 0 and have one final value.
At the end of all the processes, I want to know the total number of "errors" across the 4 processes. One way is to write out , at the end of func, the value of error to a file (xproc.1, xproc.2, xproc.3 and xproc.4) and read it out from them. That looks a very elementary way of doing things - which will involve opening those many FILE pointers etc.
I know I can have a global variable say n_error; and increment it using semaphores.
void func ()
{
int semid = sem_getid ("/tmp/abc");
static error = 0;
while (do something) {
if (something goes wrong) {
sem_wait (sem_id);
n_error ++;
sem_post (sem_id);
}
}
But I want these processes to be exclusive to each other - and not spend time in acquiring and releasing the semaphore lock.
So the question I have is, if my func is actually doing a lot of stuff with "n" numbers in parallel, would adding a semaphore slow things down a bit - especially if "n" is a large number?
Am I better off having a map wherein
map [xproc1] = nerror;
map [xproc2] = nerror;
i.e., different processes will only modify the value corresponding to their key. I do understand that the map object, as such, is just one. But could there be a possibility of something getting corrupted in the map because of the race condition?

New Topic/Question
Reply



MultiQuote




|