Im trying to write into shared memory but for some reason after i call shmat() and strcpy i get segmentation fault(core dumped) why is that?
This is my code:
int fd,shmid;
key_t shmkey;
char *shm_add;
pid_t pid,pid1=0,pid2=0;
shmkey=ftok("shmdemo.c",'j');
if ( shmkey == (key_t)-1 )
{
printf("main: ftok() for shm failed\n");
return -1;
}
shmid=shmget(shmkey, 50, 0666 | IPC_CREAT | IPC_EXCL);
if (shmid == -1)
{
printf("main: shmget() failed\n");
return -1;
}
shm_add=(char *)shmat(shmid,0,0);
if ( shm_add==NULL )
{
printf("main: shmat() failed\n");
return -1;
}
strcpy(shm_add,"hello");
edit: I have file name shmdemo.c on the directory, and the errno of shmget say "File exists" but when i delete "shmdemo.c" from the directory, a new errno comes in the ftok that say "No such file or directory".
Thank you, Asaf.