PDA

View Full Version : [C] Segmentation fault dove sbaglio???


Porcorosso
09-10-2008, 16:38
ciao ragazzio ho un problema con la memoria condivisa, il programma viene compilato senza nessun errore ma da Segmentation fault in fase d'esecuzione
ecco la porzione di codice


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

struct area_condivisa {
int contatore;
char messaggio[10];
};
int shmid;
struct area_condivisa *shmaddr;

int main(){
key_t area;
shmid = shmget(area, sizeof(struct area_condivisa), IPC_CREAT);
if (shmid < 0) {
perror("La shared memory non e' stata creata \n");
exit(1);
}
shmaddr = (struct area_condivisa *) shmat (shmid, 0, SHM_RND);
if (shmid < 0) {
perror("Errore nella funzione Shmat \n");
exit(1);
}
strcpy(shmaddr->messaggio, "@@PROVA@@");
printf("##%s## \n",shmaddr->messaggio);
}
cosa c'è che non va?

Porcorosso
09-10-2008, 16:47
ciao ragazzio ho un problema con la memoria condivisa, il programma viene compilato senza nessun errore ma da Segmentation fault in fase d'esecuzione
ecco la porzione di codice


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

struct area_condivisa {
int contatore;
char messaggio[10];
};
int shmid;
struct area_condivisa *shmaddr;

int main(){
key_t area;
shmid = shmget(area, sizeof(struct area_condivisa), IPC_CREAT);
if (shmid < 0) {
perror("La shared memory non e' stata creata \n");
exit(1);
}
shmaddr = (struct area_condivisa *) shmat (shmid, 0, SHM_RND);
if (shmid < 0) {
perror("Errore nella funzione Shmat \n");
exit(1);
}
strcpy(shmaddr->messaggio, "@@PROVA@@");
printf("##%s## \n",shmaddr->messaggio);
}
cosa c'è che non va?


Problema risolto avevo dimenticato di dare i permessi

- shmid = shmget(area, sizeof(struct area_condivisa), IPC_CREAT);
+ shmid = shmget(area, sizeof(struct area_condivisa), IPC_CREAT | 0666);