• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

creating array of semaphores

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does sema[i] point to?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I got it. I should do it like below and it works

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how about now

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this right

 
ravindra koranga
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, the previous code is not right.
Here is the correct one

 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I cannot figure it out. Will you please guide me?
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this code correct?


 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you test it?
 
reply
    Bookmark Topic Watch Topic
  • New Topic