Advertisement
fr1sk

Untitled

Jun 12th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/uio.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/stat.h>
  11. #include <time.h>
  12. #include <string.h>
  13. #include <stdbool.h>
  14. #include <sys/mman.h>
  15. #include <fcntl.h>
  16. #include <sys/mman.h>
  17. #define MSGLEN (16)
  18.  
  19. void checker(bool exp, const char *usrmsg, const char *file, const char *func, const int line){
  20. if(!(exp)){
  21. perror(usrmsg);
  22. fprintf(stderr, "%s %s %d\n", file, func, line);
  23. exit(EXIT_FAILURE);
  24. }
  25. }
  26. #define osAssert(exp, usrmsg) checker(exp, usrmsg, __FILE__, __func__, __LINE__);
  27.  
  28. void *getMemoryBlock(char *name, int *size){
  29. int memFD = shm_open(name, O_RDONLY, 0);
  30. osAssert(-1 != memFD, "shm open failed");
  31.  
  32. struct stat finfo;
  33. osAssert(-1 != (fstat(memFD, &finfo)), "fstat fail");
  34. *size = finfo.st_size;
  35.  
  36. void *address;
  37. osAssert(MAP_FAILED != (address = mmap(NULL, *size,
  38. PROT_READ, MAP_SHARED, memFD, 0)), "mmap failed");
  39. close(memFD);
  40. return address;
  41.  
  42. }
  43.  
  44. int main(int argc, char **argv){
  45. osAssert(2==argc, "nema argumenta");
  46. int size;
  47. int *array = getMemoryBlock(argv[1], &size);
  48. int n = size / sizeof(int);
  49. for(int i=0; i<n; i++){
  50. printf("%d\n", array[i]);
  51. }
  52. osAssert(-1 != munmap(array, size), "unmap failed");
  53. osAssert(-1 != shm_unlink(argv[1]),"unlink failed");
  54. return 0;
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement