I need to make some threads in and out of classes using mmap () on Linux. To do this, I tried to make some test code that writes some integers to a file, saves it, loads it again and writes data to a file in cout. If this test code works, then it will not be a problem to create a stream and exit it.
When I first started working, I had segment errors, and if I hadnโt understood that nothing had happened, I was a little ruined. I found this book http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf , where on page 107 there is useful code. I copied this code and made some small changes and got this code:
int fd; void* file_memory; /* Prepare a file large enough to hold an unsigned integer. */ fd = open ("mapTester", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); //Make the file big enough lseek (fd, 4 * 10 + 1, SEEK_SET); write (fd, "", 1); lseek (fd, 0, SEEK_SET); /* Create the memory mapping. */ file_memory = mmap (0, 4 * 10, PROT_WRITE, MAP_SHARED, fd, 0); close (fd); /* Write a random integer to memory-mapped area. */ sprintf((char*) file_memory, "%d\n", 22); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory, 4 * 10); cout << "Mark" << endl; //Start the part where I read from the file int integer; /* Open the file. */ fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR); /* Create the memory mapping. */ file_memory = mmap (0, 4 * 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close (fd); /* Read the integer, print it out, and double it. */ scanf ((char *) file_memory, "%d", &integer); printf ("value: %d\n", integer); sprintf ((char*) file_memory, "%d\n", 2 * integer); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory, 4 * 10);
But I get the segment after the "mark" cout.
Then I will replace the โread partโ as follows:
fd = open("mapTester", O_RDONLY); int* buffer = (int*) malloc (4*10); read(fd, buffer, 4 * 10); for(int i = 0; i < 1; i++) { cout << buffer[i] << endl; }
This is some kind of working code that shows me that the file is empty. I am trying to write several ways to write to the display without any changes as a result.
So how can I write code? And my mmap reading code looks fine (just in case, you can see some obvious flaws)?
I found several other resources that have not yet helped me, but since I am a new user, I can only post max 2 links.