shm.c 3.57 KB
Newer Older
1 2
#ifndef LIBRARIES
#define LIBRARIES
3
#define _GNU_SOURCE
4
#include <fcntl.h> ///< For O_* constants *
5 6 7
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
8 9
#endif

10
#include <string.h>
11 12
#include <sys/mman.h>
#include <sys/types.h>
13

14
#define BUFF_SHM_NAME "/11777707_buff"
15

16 17 18 19 20 21 22 23
/**
 * @brief Function that executes shm_open(3) and does error handling.
 * @details The function executes shm_open(3), giving it a name as a param,
 *          and handles the errors if any. The same behaviour as shm_open(3) should be expected.
 *          Returns a shared memory object file descriptor
 * @param shm_name
 * @return shmfd
 **/
24
int create_shmfd(char *shm_name) {
25 26
    int shmfd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
    if(shmfd == -1) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
27
        fprintf(stderr, "ERROR: Failed creating shared memory object\n");
28 29
        exit(EXIT_FAILURE);
    }
30 31
    return shmfd;
}
32

33 34 35 36 37 38 39
/**
 * @brief Function that executes ftruncate(2) and does error handling.
 * @details The function executes ftruncate(2), giving it a file descriptor and size as parameters,
 *          and handles the errors if any. The same behaviour as ftruncate(2) should be expected.
 * @param shmfd, shm_size
 * @return none
 **/
40
void truncate_shm(int shmfd, size_t shm_size) {
41
    if(ftruncate(shmfd, shm_size) < 0) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
42
        fprintf(stderr, "ERROR: Failed truncating shared memory object\n");
43 44
        exit(EXIT_FAILURE);
    }
45
}
46

47 48 49 50 51 52 53 54
/**
 * @brief Function that executes mmap(2) and does error handling.
 * @details The function executes mmap(2), giving it a file descriptor and size as parameters,
 *          and handles the errors if any. The same behaviour as mmap(2) should be expected.
 *          Returns a mapped shared memory object
 * @param shmfd, shm_size
 * @return shm
 **/
55
void* open_shm(int shmfd, size_t shm_size) {
56 57 58 59
    void* shm;
    shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);

    if(shm == MAP_FAILED) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
60
        fprintf(stderr, "ERROR: Failed mapping shared memory object\n");
61 62 63 64 65 66
        exit(EXIT_FAILURE);
    }

    return shm;
}

67 68 69 70 71 72
/**
 * @brief Function to write a value to shared memory object
 * @details The functions copies the value of a string with a specified size to a shared memory object
 * @param shm, message, message_size
 * @return none
 **/
73 74 75 76
void write_to_shm(void* shm, char *message, size_t message_size) {
    memcpy(shm, message, message_size);
}

77 78 79 80 81 82 83 84
/**
 * @brief Function that executes munmap(2) and does error handling.
 * @details The function executes munmap(2), giving it a shared memory object and size as parameters,
 *          and handles the errors if any. The same behaviour as munmap(2) should be expected.
 *          Returns a mapped shared memory object
 * @param shmfd, shm_size
 * @return none
 **/
85 86
void close_shm(void* shm, size_t shm_size) {
    if (munmap(shm, shm_size) == -1) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
87
        fprintf(stderr, "ERROR: Failed unmapping shared memory object\n");
88 89 90 91
        exit(EXIT_FAILURE);
    }
}

92 93 94 95 96 97 98 99
/**
 * @brief Function that executes shm_unlink(3) and close(2) and does error handling.
 * @details The function executes shm_unlink(3) and close(2), giving them a shared memory object name
 *          and a file descriptor respectively, and handles the errors if any.
 *          The same behaviour as shm_unlink(3) and close(2) should be expected.
 * @param shm_name, shmfd
 * @return none
 **/
100
void destroy_shm(char *shm_name, int shmfd) {
101
    if (shm_unlink(shm_name) == -1) {
Ivaylo Ivanov's avatar
Ivaylo Ivanov committed
102
        fprintf(stderr, "ERROR: Failed unlinking shared memory object\n");
103 104
        exit(EXIT_FAILURE);
    }
105 106 107 108
    if(close(shmfd) < 0) {
        fprintf(stderr, "ERROR: Failed closing file descriptor\n");
        exit(EXIT_FAILURE);
    }
109
}