Well the problem was that the second shared memory segment was getting attached to the one created earlier and it was incrementing the value of pid by one (*count = *count + 1). Hence I was getting one value greater than the actual pid.
Now, I have a new problem. I have 4 processes running this. When I am attempting to remove the shared memory region, it gives error if the region is already removed. How do I make sure that, it is not called if the region is already removed?