I have multiple MPI
processes running in parallel all these processes access a shared memory and performs following operations :
sem_wait(sem);
shmid = shmget(key, sizeof(int), 0777)) < 0)
shm = shmat(shmid, NULL, 0);
printf("process id = %d, shm = ",rank, *shm);//rank from MPI_Comm_rank()
new_val = *shm+1;
*shm = new_val;
sem_post(sem);
This code is a part of a much bigger code which I cannot put here so I have given only a small part which I think needs to be changed.
I get the follwing result (for first run):
process id = 1 shm = 1
process id = 2 shm = 2
process id = 3 shm = 3
process id = 4 shm = 4
process id = 5 shm = 5
I get the follwing result (for second run):
process id = 2 shm = 1
process id = 3 shm = 2
process id = 1 shm = 3
process id = 5 shm = 4
process id = 4 shm = 5
Similarly different results for different runs.
Is it possible to change the code in a way that it generates same value everytime?
I am working on C in linux environment.
I badly need repeatability in my code. Any kind of help would be greatly appreciated. Thanks !