issues with posix semaphores in suse

Hello,

I’m trying to run the next code in suse 10


#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>  //printf
#include <stdlib.h> //exit
#include <unistd.h> //close
#include <string.h> //strerror
#include <semaphore.h>

/* This will be created under /dev/shm/ */
#define STATE_FILE "/program.shared"
#define  NAMESIZE 1024
#define   MAXNAMES 100

/* Define a struct we wish to share. Notice that we will allocate
 * only sizeof SHARED_VAR, so all sizes are constant
 */
typedef struct
{
  char name[MAXNAMES][NAMESIZE];
  int flags;
}  SHARED_VAR;

int main (void)
{
int first = 0;
int shm_fd;
static SHARED_VAR *conf;

  /* Try to open the shm instance with  O_EXCL,
   * this tests if the shm is already opened by someone else
   */
  if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_EXCL | O_RDWR),
                       (S_IREAD | S_IWRITE))) > 0 ) {
          first = 1; /* We are the first instance */
  }
  else if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_RDWR),
                        (S_IREAD | S_IWRITE))) < 0) {
   /* Try to open the shm instance normally and share it with
    * existing clients
    */
    printf("Could not create shm object. %s
", strerror(errno));
    return errno;
  } 

	int sem_des = sem_open("semaphore",0100,666,0);

  /* Set the size of the SHM to be the size of the struct. */
  ftruncate(shm_fd, sizeof(SHARED_VAR));

  /* Connect the conf pointer to set to the shared memory area,
   * with desired permissions
   */
  if((conf =  mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE),
                   MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {

    return errno;

  }
  if(first) {
   /* Run a set up for the first time, fill some args */
   printf("First creation of the shm. Setting up default values
");
   conf->flags = 4;
  }
  else
  {
    printf("Value of flags = %d
", conf->flags);
  }

/* Do some work… */

  close(shm_fd);
  exit(0);
} 

In theory, a semaphore called “semaphore” should be created under /dev/shm due to the next line:

int sem_des = sem_open(“semaphore”,0100,666,0);

but I get a file called “sem.semaphore”. I have a similar software running on an old unix machine and the same line doesn’t add the “sem.” part in front of the name chosen for the semaphore. This is a real problem because I should recompile the software for all the machines, even the old ones, to communicate properly with the “sem.semaphore” created on the new suse machine, and finding all the sources and recompiling a software written 15 years ago is not a trivial thing.

Any suggestions on how to create a semaphore called simply “semaphore” or ideas of what can be going on?

I’ve simplified the code to leave only the important part, the problem is the one I described before:


#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>  //printf
#include <stdlib.h> //exit
#include <unistd.h> //close
#include <string.h> //strerror
#include <semaphore.h>

/* Define a struct we wish to share. Notice that we will allocate
 * only sizeof SHARED_VAR, so all sizes are constant
 */


int main (void)
{

	int sem_des = sem_open("semaphore",0100,666,0);


/* Do some work… */
  exit(0);
} 

Well that is the documented behaviour in man sem_overview:

Accessing named semaphores via the file system

On Linux, named semaphores are created in a virtual file system, normally mounted under /dev/shm, with names of the form sem.name.

The exact implementation is dependent on the OS. As long as the semaphore semantics are followed, POSIX is satisfied. The exact name of the file wouldn’t be specified by POSIX.

Don’t you have to recompile all the programs that use semaphores that run on Linux anyway? I mean, you can’t run binaries from other machines.

You’re right, semaphores are accessed only from processes on the local machine, right? Then it should’t affect the other machines (I’m updating the code only in one of the machines, but it communicates with the others)

Well you’re not exporting /dev/shm by a network filesystem right? So it shouldn’t be visible to other machines.