r/C_Programming • • Oct 30 '26

Question Help with understanding shmget() and semaphores

I have a program which does some calculations, but I need to fork parts of it and run the calculations in parallel. Also, we are not allowed to use POSIX semaphores. I have the calculation part done, I just need to do the parallelism part. I'm also supposed to use shmget() to create a shared memory block for my threads, but I cannot find any useful resources about that function, and/or I don't understand it well. Does anyone have any advice or tips?

4 Upvotes

12 comments sorted by

17

u/ThatOneCSL Oct 30 '26

Not trying to be snarky - have you tried running, or even googling, man shmget?

3

u/rayriflepie Oct 30 '26

I have, I guess it seemed pretty complex at first glance, but I will go back and give it a re read!

4

u/ThatOneCSL Oct 30 '26

We can work at it together!

Fun fact: I've never written a single line of C.

With that out of the way, shmget() takes three input parameters, and does one of two things. It either returns the memory address of some shared memory block, or it creates a new shared memory block.

One of the three parameters that it takes as input is a... struct(? my lack of C exposure is poking through here) called shmflag (shared memory flag), which contains the following:

  • IPC_CREAT - if present/used, we're making a new shared memory block; else we aren't
  • IPC_EXCL - if IPC_CREAT, also IPC_EXCL; they're used together, I'm not entirely sure on the mechanics behind it
  • SHM_HUGETLB - newish. Used for issuing "huge" pages of memory
  • SHM_2MB / SHM_1GB - used with the above to specify "huge" memory page sizes
  • SHM_NORESERVE - this has to do with really, nitty-gritty memory management details. If you aren't ready to read the man page on your own, you probably aren't ready to deal with these memory constraints. Go ahead and leave this false.

I've covered the main notes from the man page, but as the other user (as of the time I began writing this comment) said, perhaps Claude or Google AI or some other LLM would be able to answer some of the specific questions you have. I'm definitely willing to keep working forward with you, but I may not be able to answer all of your questions sufficiently

3

u/rayriflepie Oct 30 '26

Okay, I think that's helpful so I'll get back to work. Thanks!

2

u/ThatOneCSL Oct 30 '26

Sounds good! :extremely-overblown-thumbs-up:
If you need any additional help, lemme know. I may not be able to provide it to you directly, but I am a Google wizard.

3

u/wqferr Oct 30 '26

Thank you for the kind reply, I expected an asshole to crawl out the woodwork but you came first!

1

u/21Ali-ANinja69 Oct 31 '26

All caps means named constants, that can presumably be combined in the form IPC_CREAT | IPC_EXCL for example It's a pretty common pattern in Linux Syscalls

2

u/ThatOneCSL Oct 31 '26

Is that bitwise-ORing them together?

1

u/21Ali-ANinja69 Oct 31 '26

Yup, the flags are ints under the hood. Pretty easy to use, even from assembly.

4

u/8d8n4mbo28026ulk Oct 31 '26

shmget() is ancient and you should probably not use it. You'll find code online that uses it, but ignore it, it's just spread confusion. On POSIX, use shm_open() with mmap().

But I'm very curious now! What system are you working on?

1

u/rayriflepie Oct 31 '26

Debian on WSL. I have to use shmget because that's what the assignment instructions say to use.

-20

u/zemdega Oct 30 '26

Ask Claude