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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:16 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