Skip to content

Instantly share code, notes, and snippets.

@run
Forked from garcia556/get.c
Created September 6, 2018 06:12
Show Gist options
  • Save run/fba03b5a13d3500fe643c08cfa564b13 to your computer and use it in GitHub Desktop.
Save run/fba03b5a13d3500fe643c08cfa564b13 to your computer and use it in GitHub Desktop.

Revisions

  1. @garcia556 garcia556 created this gist Dec 3, 2017.
    42 changes: 42 additions & 0 deletions get.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #include <stdio.h>
    #include <sys/mman.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>

    #define STORAGE_ID "/SHM_TEST"
    #define STORAGE_SIZE 32

    int main(int argc, char *argv[])
    {
    int res;
    int fd;
    char data[STORAGE_SIZE];
    pid_t pid;
    void *addr;

    pid = getpid();

    // get shared memory file descriptor (NOT a file)
    fd = shm_open(STORAGE_ID, O_RDONLY, S_IRUSR | S_IWUSR);
    if (fd == -1)
    {
    perror("open");
    return 10;
    }

    // map shared memory to process address space
    addr = mmap(NULL, STORAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED)
    {
    perror("mmap");
    return 30;
    }

    // place data into memory
    memcpy(data, addr, STORAGE_SIZE);

    printf("PID %d: Read from shared memory: \"%s\"\n", pid, data);

    return 0;
    }
    32 changes: 32 additions & 0 deletions run.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/bin/bash

    function is_linux
    {
    if [[ "$(uname)" == "Linux" ]]; then
    echo 1
    return
    fi

    echo 0
    }

    SET="set"
    GET="get"
    CFLAGS=""
    if [[ "$(is_linux)" == "1" ]]; then
    CFLAGS="-lrt"
    fi

    cc ${CFLAGS} -o ${SET} ${SET}.c
    cc ${CFLAGS} -o ${GET} ${GET}.c

    ./${SET} &
    sleep 1
    ./${GET}
    if [[ "$(is_linux)" == "1" ]]; then
    echo "/dev/shm:"
    ls -l /dev/shm
    fi
    sleep 1

    rm ${SET} ${GET}
    71 changes: 71 additions & 0 deletions set.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #include <stdio.h>
    #include <sys/mman.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>

    #define STORAGE_ID "/SHM_TEST"
    #define STORAGE_SIZE 32
    #define DATA "Hello, World! From PID %d"

    int main(int argc, char *argv[])
    {
    int res;
    int fd;
    int len;
    pid_t pid;
    void *addr;
    char data[STORAGE_SIZE];

    pid = getpid();
    sprintf(data, DATA, pid);

    // get shared memory file descriptor (NOT a file)
    fd = shm_open(STORAGE_ID, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (fd == -1)
    {
    perror("open");
    return 10;
    }

    // extend shared memory object as by default it's initialized with size 0
    res = ftruncate(fd, STORAGE_SIZE);
    if (res == -1)
    {
    perror("ftruncate");
    return 20;
    }

    // map shared memory to process address space
    addr = mmap(NULL, STORAGE_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED)
    {
    perror("mmap");
    return 30;
    }

    // place data into memory
    len = strlen(data) + 1;
    memcpy(addr, data, len);

    // wait for someone to read it
    sleep(2);

    // mmap cleanup
    res = munmap(addr, STORAGE_SIZE);
    if (res == -1)
    {
    perror("munmap");
    return 40;
    }

    // shm_open cleanup
    fd = shm_unlink(STORAGE_ID);
    if (fd == -1)
    {
    perror("unlink");
    return 100;
    }

    return 0;
    }