Unix Programming - segmentation fault with shared memory

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > segmentation fault with shared memory





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author segmentation fault with shared memory
naourez

2004-08-17, 5:54 pm

hi everybody
i ve a pb with this code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHM_SIZE 4096 /* make it a 4K shared memory segment */

int main()
{
key_t key,key_flag;
int shmid,id_flag;
char *data,*message;
int mode,*flag,c;


/* make the key: */
if ((key_flag = ftok("shm.c", 'A')) == -1) {
perror("ftok");
exit(1);
}
if ((id_flag= shmget(key_flag,sizeof(int), 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}
flag =(int*) shmat(id_flag, NULL, 0);
if (flag ==(int*) -1) {
perror("shmat");
exit(1);
}
printf("quel est votre choix \n");
scanf("%d",&c);
(*flag)=c;
printf("le drapeau ecrit dans le segment a pour numero: \"%d\"\n",c);


switch(c)
{
case 1:
printf("demande d'établissement d'une connexion\n");
break;
case 2:
printf("demande d'ouverture de session\n");
break;
case 3:
printf("demande d'envoi de message \n");

{if ((key = ftok("shm.c", 'R')) == -1) {
perror("ftok");
exit(1);
}

/* connect to (and possibly create) the segment: */
if ((shmid = shmget(key,SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
perror("shmget");
exit(1);
}

/* attach to the segment to get a pointer to it: */
data =(char*) shmat(shmid, NULL, 0);
if (data == (char*)-1) {
perror("shmat");
exit(1);
}

/* modify the segment */

printf("entrez ici votre message \n");
fflush(stdin);
fgets(message,sizeof(message),stdin);
printf("message ecrit dans le segment: \"%s\"\n",message);
strncpy(data, message,SHM_SIZE);


/* detach from the segment: */
if (shmdt((char*)data) == -1) {
perror("shmdt1");
exit(1);
}

break;
}
default:
break;
}
if (shmdt(flag) == -1) {
perror("shmdt2");
exit(1);
}

return 0;
}
in fact where c ==3
i obtain segmentation fault before i even enter the message
and i don't see why
any ideas !!
thanks a lot
Jens.Toerring@physik.fu-berlin.de

2004-08-17, 5:54 pm

naourez <naourez_m@yahoo.fr> wrote:
> hi everybody


Please don't post the same stuff again and again. This isn't IRC and
it can take more than a day that your question gets seen by all people.

> i ve a pb with this code


<snipped irrelevant code>

> char *data,*message;
> int mode,*flag,c;


> printf("entrez ici votre message \n");
> fflush(stdin);
> fgets(message,sizeof(message),stdin);


'message' is still an unnitialized pointer, pointing to some random
location. When fgets() now tries to use the pointer it crahes You
need to make 'message' point to some memory you own, either an
array of chars, a malloc()ed memory area or to the shared memory
segment you just got. Moreover, "sizeof(message)" is the size of a
char pointer and that's rather unlikely to be the size of the buffer
you want pass to fgets().
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com