Shared Memory Segmentation Error

This is a discussion on Shared Memory Segmentation Error within the C Programming forums, part of the General Programming Boards category; This is a little urgent. I have to do use shared memory segments. I wrote this programme. I have tried ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    19

    Simple Shared Memory Segmentation Error

    This is a little urgent. I have to do use shared memory segments. I wrote this programme. I have tried everything, as you can see (void *, int * printing and so on), but havent been able to debug the error.
    I will be grateful if some one can point out the bug.
    Code:
    #include<stdio.h>
    #include<sys/ipc.h>
    #define KEY 600
    #define SIZE 20
    
    int main()
    {
    	int sid;
    	int i;
    	void *a;
    	int *shm;
    	if(fork()>0)
    	{
    		printf("%d\n",getpid());
    		sleep(1);
    		if(sid=shmget(KEY,SIZE,0)<0)
    		{
    			printf("Client Error\n");
    			exit(1);
    		}
    		a=shmat(sid,0,0);
    		shm=(int*)a;
    		shm++;
    		printf("Client Attach\n");		
    		i=*shm;
    		printf("Read");
    		printf("%d",i);
    		printf("Client Release");
    		shmdt(a)
    		//shmctl(sid,IPC_RMID,(struct shmid_ds*)0);
    	}
    	else
    	{
    		if((sid=shmget(KEY,SIZE,IPC_CREAT|0666))<0)
    		{
    			printf("Error");
    			exit(1);
    		}
    		printf("Got\n");
    		a=shmat(sid,0,0);
    		shm=(int*)a;
    		printf("Attached\n");
    		shm++;
    		*shm=10;
    		printf("Value put, waiting for %d\n",getppid());
    		while(wait((int *)0)!=getppid());
    		printf("got back");
    		//shmdt(shm);		
    		printf("Released\n");
    		shmctl(sid,IPC_RMID,(struct shmid_ds*)0);
    	}
    	return 0;	
    }
    Please post any thing you think may be causing this error.
    It prints till 'Value put, waiting for ...' in the child and then
    Client Attach
    this is followed by a Segmentation Fault message. It simply says 'Segmentation Fault'.
    Last edited by doom; 12-11-2005 at 06:27 AM.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    19
    Code:
                  shm++;
                    printf("Client Attach\n");              
                    i=*shm; //Error in this line
                    printf("Read");
                    printf("%d",i);
    If i remove this line, the program works fine.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > I will be grateful if some one can point out the bug.
    Ah, just the one then huh?

    Well lets see
    1. if(fork()>0)
    You have no control over which process next runs, and since you only go about creating the shared memory in one of them after the fork(), it seems likely to me that one could try and get it before the other has created it.

    In the manual page for shmat(), there is
    "fork() After a fork() the child inherits the attached shared memory segments."
    So creating then attaching (just the once) before the fork() should be all you need to do.

    2. shmat()/shmdt()
    Your use seems to suggest that the act like a semaphore, one process has it at once.
    This is not the case, so all your updates to the shared memory can happen in any order.

    3. a=shmat(sid,0,0); //shmdt(shm);
    The manual clearly states that the thing you detach is the thing you attached.
    You did it right in the other place in the code.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    19
    Thanks a lot. Problem solved. I attached the segment before forking. When i was later printing the memory address, it was giving different values. So didnt knw what to do . But now works fine.

    After forking, the parent process sleeps for 1 seconds so, the child should have run. Then Child waits for parent to terminate and then removes the segment. Thats why i didnt use semaphores. Already was having trouble, so simply decided to wait and sleep.
    Thanks for the third point too. I overlooked earlier, sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21