ravindra koranga wrote:I am creating an array of semaphores as follows.
Actually, you didn't allocate (mmap) the space for an array of semaphores. You allocated (mmap) the space for an array of semaphore pointers. And of course, you also never initialize those pointers.
Do you want an array of semaphores, or an array of semaphore pointers?
I'm going to assume the first, because the second will introduce a layer of indirection that you probably don't need.
That means that if you want to have 100 semaphores in memory, you need to map memory the size of 100 semaphores. How can you get the size of one semaphore?
After you've done that, you'll have a pointer to a block of memory that has enough space for 100 semaphores. You just need to initialize them in their respective places.
sem_init() requires a pointer to the location where the semaphore will be initialized. You can use pointer arithmetic on the memory block pointer to get a pointer to the semaphore that you want to initialize: