Advertisement
fr1sk

Untitled

Jun 12th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 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 *osCreateMemoryBlock(char *name, unsigned size){
  29. mode_t mod = 0600;
  30. int memFD = shm_open(name, O_CREAT | O_RDWR, mod);
  31. osAssert(-1 != memFD, "shm open failed");
  32. osAssert(-1 != ftruncate(memFD, size), "ftrunc failed");
  33. void *address;
  34. osAssert(MAP_FAILED != (address = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, memFD, 0)), "map failed");
  35. close(memFD);
  36. return address;
  37.  
  38. }
  39.  
  40. int main(int argc, char **argv){
  41. osAssert(3<argc, "nema argumenta");
  42. int memSize = (argc-2)*sizeof(int);
  43. int *array = osCreateMemoryBlock(argv[1], memSize);
  44. for(int i=2;i<argc; i++){
  45. array[i-2] = atoi(argv[i]);
  46. }
  47. osAssert(-1 != munmap(array, memSize), "unmap failed");
  48. return 0;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement