Hi,

I'm trying to compile a program that uses shm_open and mmap methods with
emcc to run it in the browser.
The program runs as expected when compiling with gcc.
The program compiles without errors with emcc.
Running the generated js/html output in the browser shows a different
behaviour. Is there anything wrong with the code? Thanks for any insights.

https://pastebin.com/raw/xwENCbiJ
-----%<--------

#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>

//gcc -o shm_open_test shm_open_test.c -lrt
//emcc -O1 -s ASM_JS=1 -o test/shmopen.html shm_open_test.c

int main()
{
        //shm handle / filename
        char handle[4]={'a','b','c','\0'};

        //create file in shared memory, size 100
        int fd=shm_open(handle, O_CREAT | O_RDWR, 0666);
        if(fd<0)
        {
                fprintf(stderr, "could not get shared memory file 
descriptor.\n");
                return 1;
        }

        int r=ftruncate(fd, 100);
        if(r!=0)
        {
                fprintf(stderr, "could not ftruncate shared memory file 
descriptor.\n");
                return 1;
        }

        //map file
        void *ptr_mmap = mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, 
fd, 0);
        if(ptr_mmap==NULL || ptr_mmap==MAP_FAILED)
        {
                fprintf(stderr, "map failed.\n");
                return 1;
        }

        //write something
        memcpy(ptr_mmap,"hello",5);
        //sync back to file (?)
        msync(ptr_mmap,100,MS_SYNC);

        //open existing file in shared memory
        int fd2=shm_open(handle, O_RDWR, 0666);
        if(fd2<0)
        {
                fprintf(stderr, "could not get shared memory file 
descriptor.\n");
                return 1;
        }

        //map file
        void *ptr_mmap2 = mmap(NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED,
fd2, 0);
        if(ptr_mmap2==NULL || ptr_mmap2==MAP_FAILED)
        {
                fprintf(stderr, "map failed.\n");
                return 1;
        }

        //read
        char buf[100];
        memcpy(buf,ptr_mmap2,100);
        fprintf(stderr,"found: %s\n",buf);

        //expected: found: hello
        //gcc:      found: hello
        //emcc:     found:
}

-----%<--------

Best regards
Thomas Brand


-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to