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 *osCreateMemoryBlock(char *name, unsigned size){
- mode_t mod = 0600;
- int memFD = shm_open(name, O_CREAT | O_RDWR, mod);
- osAssert(-1 != memFD, "shm open failed");
- osAssert(-1 != ftruncate(memFD, size), "ftrunc failed");
- void *address;
- osAssert(MAP_FAILED != (address = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, memFD, 0)), "map failed");
- close(memFD);
- return address;
- }
- int main(int argc, char **argv){
- osAssert(3<argc, "nema argumenta");
- int memSize = (argc-2)*sizeof(int);
- int *array = osCreateMemoryBlock(argv[1], memSize);
- for(int i=2;i<argc; i++){
- array[i-2] = atoi(argv[i]);
- }
- osAssert(-1 != munmap(array, memSize), "unmap failed");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement