Mmap e struct em C [fechado]
Gostaria de ler e escrever estruturas com mmap em C.
Eu tenho uma função chamada insert_med que permite a inserção de uma nova estruturamed no mmap e em cada estrutura (com um únicochave) deve ser gravada em uma posição diferente da matriz (quando uma nova estrutura é adicionada, ela deve ser adicionada na última posição vazia da matriz).
Duas estruturasmed NÃO PODE TER O MESMOchave como você pode ver no código abaixo. ochave é único.
Meu código não está funcionando - mensagens de erro com a variável "struct map": quando declarado e quando o arquivo está pronto para ser mapeado - mas não sei por que. Acho que provavelmente estou fazendo algo da maneira errada.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define FILEPATH "/tmp/mmapped.bin"
#define NUMINTS (1000)
#define FILESIZE (NUMINTS * sizeof(int))
struct med{
int key;
char name[25];
int quant_min;
int quant;
};
insert_med(int argc, char *argv[]){
int i;
int fd;
int result;
struct map*; /* mmapped array of structs */
/* Open a file for writing.
* - Creating the file if it doesn't exist.
* - Truncating it to 0 size if it already exists. (not really needed)
*
* Note: "O_WRONLY" mode is not sufficient when mmaping.
*/
fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if (fd == -1) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
/* Stretch the file size to the size of the (mmapped) array of structs
*/
result = lseek(fd, FILESIZE-1, SEEK_SET);
if (result == -1) {
close(fd);
perror("Error calling lseek() to 'stretch' the file");
exit(EXIT_FAILURE);
}
/* Something needs to be written at the end of the file to
* have the file actually have the new size.
* Just writing an empty string at the current file position will do.
*
* Note:
* - The current position in the file is at the end of the stretched
* file due to the call to lseek().
* - An empty string is actually a single '\0' character, so a zero-byte
* will be written at the last byte of the file.
*/
result = write(fd,"",1);
if (result != 1) {
close(fd);
perror("Error writing last byte of the file");
exit(EXIT_FAILURE);
}
/* Now the file is ready to be mmapped.
*/
map = (struct*)mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
struct med m;
struct med s;
int a=0;
printf("Key of med: ");
scanf("%d",&m.key);
//strcmp return "0" when the two strings are equal
for (i = 0; i <=NUMINTS; ++i) {
int a=0;
map[i]=&s;
read(fd,&s,1);
if ((strcmp(&m.key,&s.key))==0){
a++;
printf("Med %d already exits. \n",s.key);
break;
}
}
if (a==0){
printf("Name of med: ");
scanf("%s",&m.name);
printf("Quant. min. of med: ");
scanf("%d",&m.quant_min);
printf("Quant. of med: ");
scanf("%d",&m.quant);
map[i]=&m;
write(fd,&m,1);
printf("Med %d saved. \n",m.key);
}
/* Don't forget to free the mmapped memory
*/
if (munmap(map, FILESIZE) == -1) {
perror("Error un-mmapping the file");
/* Decide here whether to close(fd) and exit() or not. Depends... */
}
/* Un-mmaping doesn't close the file, so we still need to do that.
*/
close(fd);
}