• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating array of semaphores  RSS feed

 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an array of semaphores as follows. it compiles correctly but gives" segmentation fault "error on execution.Can anyone tell me why?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does sema[i] point to?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I got it. I should do it like below and it works

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ravindra koranga wrote:OK I got it. I should do it like below and it works


The mmap size is still wrong. You allocated enough room for 100 semaphore pointers -- which is very different than room for 100 semaphores.

Henry
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about now

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, since the mmap() function returns a pointer, and a pointer isn't assignable to a sem_t, then the new code shouldn't compile, right?

Also, why do you actually need a declared variable to take the size of? Why can't you just use "sizeof(sem_t)"?

Henry
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this right

 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, the previous code is not right.
Here is the correct one

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is still not correct.

You're creating enough space for 100 semaphore pointers. You need space for 100 semaphores.
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I cannot figure it out. Will you please guide me?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this code correct?


 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you test it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!