Hello All,
I am having troubles creating a program that uses shared memory. As far as I can tell I am doing everything correctly but I keep getting a seg fault when I run the code (see code below for explanation). The basic thought is for a program to create a shared memory place and then fork a process that will use the shared space to sum up numbers passed in through the command line.
Code:
printf("in main");
/** the identifier for the shared memory segment */
int shared_mem_id;
/** pointer to the address space */
int *shared_mem;
/** the size (in bytes) of the shared memory segment */
int segment_size = sizeof(int);
printf("declariation of stuff");
/** generate key used in shmget() */
key_t key = ftok(argv[2], 1);
printf("got key");
/** allocate a shared memory segment */
shared_mem_id = shmget(key, segment_size, S_IRUSR | S_IWUSR);
printf("got mem_id");
/** attach the shared memory segment */
shared_mem = shmat(shared_mem_id, NULL, 0);
printf("created mem stuff");
/** initalize shared_mem to 0 */
/** this is where i get to before it seg faults */
*shared_mem = 0;
printf("created mem stuff and inialize");
Any help would be greatly appriciated.
Thanks