Inter-Process Communication (IPC) using Shared Memory - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Inter-Process Communication (IPC) using Shared Memory

Share This

Source Code of memwriter.c
/** Compilation: gcc -o memwriter memwriter.c -lrt -lpthread **/ #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <semaphore.h> #include <string.h> #include "shmem.h" void report_and_exit(const char* msg) { perror(msg); exit(-1); } int main() { int fd = shm_open(BackingFile, /* name from smem.h */ O_RDWR | O_CREAT, /* read/write, create if needed */ AccessPerms); /* access permissions (0644) */ if (fd < 0) report_and_exit("Can't open shared mem segment..."); ftruncate(fd, ByteSize); /* get the bytes */ caddr_t memptr = mmap(NULL, /* let system pick where to put segment */ ByteSize, /* how many bytes */ PROT_READ | PROT_WRITE, /* access protections */ MAP_SHARED, /* mapping visible to other processes */ fd, /* file descriptor */ 0); /* offset: start at 1st byte */ if ((caddr_t) -1 == memptr) report_and_exit("Can't get segment..."); fprintf(stderr, "shared mem address: %p [0..%d]\n", memptr, ByteSize - 1); fprintf(stderr, "backing file: /dev/shm%s\n", BackingFile ); /* semaphore code to lock the shared mem */ sem_t* semptr = sem_open(SemaphoreName, /* name */ O_CREAT, /* create the semaphore */ AccessPerms, /* protection perms */ 0); /* initial value */ if (semptr == (void*) -1) report_and_exit("sem_open"); strcpy(memptr, MemContents); /* copy some ASCII bytes to the segment */ /* increment the semaphore so that memreader can read */ if (sem_post(semptr) < 0) report_and_exit("sem_post"); sleep(12); /* give reader a chance */ /* clean up */ munmap(memptr, ByteSize); /* unmap the storage */ close(fd); sem_close(semptr); shm_unlink(BackingFile); /* unlink from the backing file */ return 0; }


Source Code of memreader.c
/** Compilation: gcc -o memreader memreader.c -lrt -lpthread **/ #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <semaphore.h> #include <string.h> #include "shmem.h" void report_and_exit(const char* msg) { perror(msg); exit(-1); } int main() { int fd = shm_open(BackingFile, O_RDWR, AccessPerms); /* empty to begin */ if (fd < 0) report_and_exit("Can't get file descriptor..."); /* get a pointer to memory */ caddr_t memptr = mmap(NULL, /* let system pick where to put segment */ ByteSize, /* how many bytes */ PROT_READ | PROT_WRITE, /* access protections */ MAP_SHARED, /* mapping visible to other processes */ fd, /* file descriptor */ 0); /* offset: start at 1st byte */ if ((caddr_t) -1 == memptr) report_and_exit("Can't access segment..."); /* create a semaphore for mutual exclusion */ sem_t* semptr = sem_open(SemaphoreName, /* name */ O_CREAT, /* create the semaphore */ AccessPerms, /* protection perms */ 0); /* initial value */ if (semptr == (void*) -1) report_and_exit("sem_open"); /* use semaphore as a mutex (lock) by waiting for writer to increment it */ if (!sem_wait(semptr)) { /* wait until semaphore != 0 */ int i; for (i = 0; i < strlen(MemContents); i++) write(STDOUT_FILENO, memptr + i, 1); /* one byte at a time */ sem_post(semptr); } /* cleanup */ munmap(memptr, ByteSize); close(fd); sem_close(semptr); unlink(BackingFile); return 0; }




Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.