Move the truncation outside of the open function

This commit is contained in:
Ivaylo Ivanov 2019-01-12 13:27:02 +01:00
parent 2c4471a7b5
commit 0c024698e3
2 changed files with 4 additions and 1 deletions

View File

@ -22,12 +22,14 @@ int create_shmfd(char *shm_name) {
return shmfd; return shmfd;
} }
void* open_shm(int shmfd, size_t shm_size) { void truncate_shm(int shmfd, size_t shm_size) {
if(ftruncate(shmfd, shm_size) < 0) { if(ftruncate(shmfd, shm_size) < 0) {
fprintf(stderr, "ERROR: Failed truncating shared memory object\n"); fprintf(stderr, "ERROR: Failed truncating shared memory object\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}
void* open_shm(int shmfd, size_t shm_size) {
void* shm; void* shm;
shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); shm = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);

View File

@ -33,6 +33,7 @@ int main(int argc, char *argv[]) {
/// Open shared memory objects /// Open shared memory objects
int buffer_shmfd = create_shmfd(BUFFER_SHM_NAME); int buffer_shmfd = create_shmfd(BUFFER_SHM_NAME);
truncate_shm(buffer_shmfd, BUFF_SIZE);
circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t)); circ_buffer = (buffer_t *)open_shm(buffer_shmfd, sizeof(buffer_t));
/// Open semaphores /// Open semaphores