Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <string.h>
- #include <stdbool.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #define MSGLEN (16)
- void checker(bool exp, const char *usrmsg, const char *file, const char *func, const int line){
- if(!(exp)){
- perror(usrmsg);
- fprintf(stderr, "%s %s %d\n", file, func, line);
- exit(EXIT_FAILURE);
- }
- }
- #define osAssert(exp, usrmsg) checker(exp, usrmsg, __FILE__, __func__, __LINE__);
- void *getMemoryBlock(char *name, int *size){
- int memFD = shm_open(name, O_RDONLY, 0);
- osAssert(-1 != memFD, "shm open failed");
- struct stat finfo;
- osAssert(-1 != (fstat(memFD, &finfo)), "fstat fail");
- *size = finfo.st_size;
- void *address;
- osAssert(MAP_FAILED != (address = mmap(NULL, *size,
- PROT_READ, MAP_SHARED, memFD, 0)), "mmap failed");
- close(memFD);
- return address;
- }
- int main(int argc, char **argv){
- osAssert(2==argc, "nema argumenta");
- int size;
- int *array = getMemoryBlock(argv[1], &size);
- int n = size / sizeof(int);
- for(int i=0; i<n; i++){
- printf("%d\n", array[i]);
- }
- osAssert(-1 != munmap(array, size), "unmap failed");
- osAssert(-1 != shm_unlink(argv[1]),"unlink failed");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement