You need to call shmget
, not semget
.
Why am I getting a segmentation-fault?
-
04-08-2022 - |
Question
This is my code:
struct bacchetta {
bool in_uso;
};
int main()
{
key_t key;
if ((key = ftok(".", 'a')) == -1) {
perror("ftok");
exit(1);
}
int shmid;
if ((shmid = semget(key, sizeof(struct bacchetta)*5, 0600 | IPC_CREAT )) == -1) {
perror("Errore creazione dell'area di memoria per le bacchette");
exit(1);
}
bacchetta *bacchette = (bacchetta*)shmat(shmid, NULL, 0);
if (bacchette == NULL) {
perror("Errore nell'attachment dell'area di memoria");
exit(2);
}
//!!!HERE segmentation-fault (core dumped)
if (!bacchette[0].in_uso) printf("ok");// = false;
return 0
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow