How to get the maximum number of semaphores possible in a semaphore set inside a c program?
The following program gets a set of 10 semaphored defined by the semaphore id semid
#include <sys/ipc.h>
#include <sys/sem.h>
key_t key;
int semid;
key = ftok("/home/somefile", 'E');
semid = semget(key, 10, 0666 | IPC_CREAT);
But what if i wanted to call the semget function so that it will always return a set of the maximum number of semaphores possibe in a system (SEMMSL, 250 in my case). It is not a #define statement. So is there a way of getting it other than opening the /proc/sys/kernel/sem file?
It sounds like you already have the answer– open /proc/sys/kernel/sem and call atoi() on the first number you read out. Sometimes, parsing the /proc filesystem is the best solution to a Linux problem.
However, I grep’d for 250 in /usr/include/linux. It looks like the #define you seek lives in /usr/include/linux/sem.h:
So, assuming Linux headers are installed, you should be able to #include linux/sem.h and get access to that SEMMSL define.
The trade-off here is that using the #define will hardcode SEMMSL into your binary. If you happen to move the binary to another machine that has a smaller number, I can see that causing a problem. FWIW, I just checked on x86_32, x86_64, and PowerPC Linux machines I’m running (all recent Ubuntu distros) and they all report 250. However, parsing through the /proc filesystem gives you the current limit for the running kernel, which strikes me as a better solution.