segmentation fault with shared memory
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > segmentation fault with shared memory




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    segmentation fault with shared memory  
naourez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-17-04 10: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





[ Post a follow-up to this message ]



    Re: segmentation fault with shared memory  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-17-04 10: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:13 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register