0

I am trying to build a shared memory application where I can share data between unrelated processes. In my example, POSIX shm_open call works fine if I use the Dynamic linking while building as below

gcc main.c -o example -lpthread -lrt

But when I build it with static linking using the below command, it builds successfully but throws SIGSEGV while calling shm_open call.

gcc main.c -static -o example  -lrt -lpthread 

What could be the possible reason for the segmentation fault while calling shm_open.

Here is the example main.c code that I am using:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>


#define DATA "/mtxmydata"

struct mydata { int a, b; pthread_mutex_t mtxlock; };

int main()
{
    int fd;
    unsigned int val;
    struct mydata *addr;
    pthread_mutexattr_t pshared;

    pthread_mutexattr_init(&pshared);
    pthread_mutexattr_setpshared(&pshared, PTHREAD_PROCESS_SHARED);
    pthread_mutexattr_getpshared(&pshared , &val);
    printf(" %d\n", val);

    fd = shm_open(DATA, O_CREAT | O_RDWR, 0666);
    if (fd == -1) {
        perror("shm_open");
        exit(1);
    }

    /* 4k is min shared memory */
    if (ftruncate(fd, getpagesize()) == -1) {
        perror("ftruncate");
        exit(1);
    }

    addr = (struct mydata *)mmap(NULL, sizeof(struct mydata),
                  PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED) {
        perror("mmap");
        exit(1);
    }
    pthread_mutex_init(&addr->mtxlock, &pshared);
    addr->a = 0;
    addr->b = 0;
    munmap(addr, sizeof(struct mydata));

    return 0;
}
F.K
  • 1
  • 1
  • 1
    AFAIK the `mode` parameter in `shm_open` must be an octal, try with `0666` instead of `666` – David Ranieri Mar 20 '22 at 08:07
  • @DavidRanieri Thanks, it's still giving segmentation fault. Actually, it worked fine when I compile it without -static flag. – F.K Mar 20 '22 at 08:26
  • 1
    Is running fine for me without SEGFAULT even compiling with `static` (with your toolchain), adding `#include unistd.h` for `truncate` and using `struct mydata { int a, b; pthread_mutex_t mtxlock; };` as data definition. That's my snippet: https://www.ideone.com/yIekHd – David Ranieri Mar 20 '22 at 08:49
  • Edited the code with the above details, can you please share the command for building it. I tried on two different Ubuntu machines with GCC versions 9.3.0 and 7.5.0 with `gcc main.c -static -o example -lrt -lpthread` command, in both the case it gives SEGFAULT. – F.K Mar 20 '22 at 09:32
  • I compiled with `gcc main.c -static -o example -lrt -lpthread` gcc version: 11.2.0 – David Ranieri Mar 20 '22 at 09:39
  • _it builds successfully but throws SIGSEGV while calling shm_open call._ What is the exact message? – David Ranieri Mar 20 '22 at 09:40
  • Here is the output:```sudo ./example 1 Segmentation fault``` – F.K Mar 20 '22 at 12:33
  • @DavidRanieri thanks for the hint, I searched related posts and found a solution [here](https://stackoverflow.com/questions/65335620/terminate-called-after-throwing-an-instance-of-stdsystem-error/65348893#65348893), by adding `-Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive` flags – F.K Mar 20 '22 at 12:36

0 Answers0