0

After reading the man pages I understood that shm_open and shm_unlink are basically analogous to mmap and munmap, with the difference being that shm is for System V and mmap is for POSIX. Since both can be used for sharing memory, is there an advantage in using both toghether? For example:

this code

int main( ) {
    
    int size = 1024;
    char* name   = "test";

    int fd  = shm_open(name, O_RDWR|O_CREAT|O_TRUNC, 0600 );
    ftruncate(fd, size);
    char *ptr = (char *) mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

    if (fork()) { // parent
        ftruncate(fd, size); // cut the file to a specific length
        char* msg = (char*)malloc(50*sizeof(char));
        sprintf(msg, "parent process with id %d wrote this in shared memory",getpid()); // copy this in msg
        strcpy((char*)ptr,msg); // copy msg in ptr (the mmap)
        munmap(ptr,size); // parent process no longer has access to this memory space
        shm_unlink(name); // parent process is no longer linked to the space named "test"
    } else {
        sleep(0.00001);
        printf("Inside child process %d :  %s\n", getpid(), ptr);
        munmap(ptr,size);
        exit(0);
    }
}

will output

Inside child process 5149 :  parent process with id 5148 wrote this in shared memory

and if I remove the fd and replace it with -1 and add the flag MAP_ANONYMOUS,

int main( ) {
    
    int size = 1024;
    char* name   = "test";

    char *ptr = (char *) mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);

    if (fork() != 0) { // parent
        char* msg = (char*)malloc(50*sizeof(char));
        sprintf(msg, "parent process with id %d wrote this in shared memory",getpid()); // copy this in msg
        strcpy((char*)ptr,msg); // copy msg in ptr (the mmap)
        munmap(ptr,size); // parent process no longer has access to this memory space
    } else {
        sleep(0.00001);
        printf("Inside child process %d :  %s\n", getpid(), ptr);
        munmap(ptr,size);
        exit(0);
    }
}

the output does not change. So why use shm_get?

Thanks

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
qwerty_99
  • 640
  • 5
  • 20
  • shm with the so-called shmget(), shmctl() and so on, is a legacy of the Unix System V. It is deprecated today. But shm_open(), shm_unlink() are POSIX. – Rachid K. Oct 17 '20 at 20:03
  • You are using sprintf() to fill the msg buffer. But you are writing more than 50 chars into it. So, you have a buffer overflow (you are corrupting memory). Instead of sprintf(), use the secured snprintf(). – Rachid K. Oct 17 '20 at 20:05
  • You are calling sleep() with a float number. But sleep() only accept an unsigned int. – Rachid K. Oct 17 '20 at 20:08
  • thanks for pointing it out. I didn't realize it because I didn't get a compilation error nor a segfault – qwerty_99 Oct 17 '20 at 20:12
  • 1
    Concerning the second case, you are right : there is no difference. But it is because you are in the same process hierarchy : a father process shares its mapping with its childs. But what about independent processes ? There will not be able to share the same memory area through mmap(). But with shm_open() they will be able to make it. By "independent processes", I mean processes from different programs. – Rachid K. Oct 17 '20 at 20:16

0 Answers0