Question about shared memory

This is a discussion on Question about shared memory within the C Programming forums, part of the General Programming Boards category; Hello! I'm trying to learn how to use shared memory, so I wrote a simple program to exchange an integer ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Question about shared memory

    Hello! I'm trying to learn how to use shared memory, so I wrote a simple program to exchange an integer value between a parent process and its child.
    The problem is that the program doesn't work, because regardless of the number that I write into the shared memory the child always reads 0. This is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/shm.h>
    
    sig_atomic_t flag = 0;
    
    void handler(int signum){
    	flag = 1;
    }
    
    int main(){
    
    	int key, num;
    	int *mem;
    	pid_t childpid;
    
    	key = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
    	mem = (int*) shmat(key, 0, 0);
    
    	signal(SIGUSR1, handler);
    
    	printf("Write an integer and press enter...\n");
    	scanf("%d", &num);
    
    	childpid = fork();
    
    	if(childpid == 0){
    
    		while(!flag) sleep(1);
    		printf("I'm the child. I've been awakened. Retrieving the number.\n");
    		printf("The number is %d\n", *mem);
    
    	} else {
    
    		printf("I'm the father, I received the number %d. I write it in the shared memory.\n", num);
    		mem = &num;
    		printf("I'm awaking the child.\n");
    		kill(childpid, SIGUSR1);
    	}
    
    	shmdt(mem);
    	shmctl(key, IPC_RMID, 0);
    	return 0;
    }
    Someone can help me to find the error and explain me why it's wrong?
    Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Quote Originally Posted by Soel View Post
    Code:
    	mem = (int*) shmat(key, 0, 0);
    ...
    		mem = &num;
    Quote Originally Posted by man shmat
    If shmaddr is NULL, the system chooses a suitable (unused) address at which to attach the segment.
    Your shmat call picks a random address to share and stores it in mem. Later, you assign to mem the address of num. This doesn't share num, it just causes you to lose your handle to the shared memory. Try the following (changes are in bold):
    Code:
        int key, *num;  // num is now a pointer
        int *mem;
    ...
        mem = (int*) shmat(key, 0, 0);
        num = mem;  // make num point to the shared memory location
    ...
        scanf("%d", num);
    
        childpid = fork();
    
        if (childpid == 0) {
            while(!flag) sleep(1);
            printf("I'm the child. I've been awakened. Retrieving the number.\n");
            printf("The number is %d\n", *num);
    
        }
        else {
            printf("I'm the father, I received the number %d. I write it in the shared memory.\n", *num);

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nice post, Anduril.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Thanks!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Thanks, but I was following an exercise that asked to read a number from the stdin, store it in a local variable in the parent process and then pass it to the child through the shared memory.
    Maybe the right solution was to use a memcpy(mem, &num, sizeof(int))?
    Last edited by Soel; 02-10-2011 at 11:21 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    It would be easier to just do a simple assignment:
    Code:
    *mem = num

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Ugh. Why do books continue to teach, and programmers continue to use, SYSV shared memory? Shared memory-mapped files are in all ways completely superior.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared Memory semaphores?
    By Ironic in forum C Programming
    Replies: 0
    Last Post: 10-31-2008, 07:13 PM
  2. Shared Memory...
    By suzan in forum Linux Programming
    Replies: 1
    Last Post: 02-16-2006, 01:29 AM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  4. Shared Memory
    By wardej2 in forum Linux Programming
    Replies: 8
    Last Post: 10-21-2005, 07:48 AM
  5. Shared memory woes
    By joshdick in forum C Programming
    Replies: 4
    Last Post: 07-28-2005, 08:59 AM

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