Shared memory not work… Programm print “mmap faild : 0”
Where I make error ?
#include"…/hfile.h"
int main(int argc, char *argv[])
{
int fd=0, i=0, max_size=1;
char *path, *tmp_char;
char *map;
path=malloc(255);
strcpy(path,"modbus_slave_ip_502_");
tmp_char=malloc(5);
itoa(i,tmp_char,10);
strcat(path,tmp_char);
printf("%s\n",path);
fd=shm_open(path,O_WRONLY | O_CREAT,0777);
map=mmap(0,max_size,PROT_WRITE,MAP_SHARED,fd,0);
if(map==-1)
{
fprintf(stderr,"mmap failed : %s\n");
strerror(errno);
}
else
{
printf("Map addr is %p\n",map);
if(ltrunc(fd, max_size, SEEK_SET) == -1)
{
fprintf(stderr,"ltrunc: %s\n",strerror(errno));
}
munmap(map,max_size);
}
close(fd);
shm_unlink(path);
free(path);
free(tmp_char);
return(0);
}