[Prev][Next][Index]

FORCING POINTERS POINTING TO THE SAME ADDRESS TO BE EQUALS



Hi,
 
I have a process, P, doing the following:


#define MAXCHILD   4
#define PERM       0660              /* rw- rw- --- */



/* base value for shared mem. segment -*/
key_t SHMKEY[MAXCHILD] = {1001, 1101, 1201, 1301};


int  shmid[MAXCHILD];
char *screen[MAXCHILD];
int cid;

                     ....
                     ....
 
  for( cid = 0; cid < MAX; cid++) {

  /*
   * Create Shared memory regions.
   */

  shmid[cid] = shmget(SHMKEY[cid], IMAGESIZE, PERM | IPC_EXCL |
IPC_CREAT);
 
  if (shmid[cid] < 0)
        MY_perror("shmget");

  /* Attach to shared memory */

  screen[cid] = shmat(shmid[cid], (char *) 0, 0);
  if (screen[cid] == (char *) -1)
     MY_perror("shmat");


  /* Create child and exec new session  */
  
  if ( (pid = fork()) < 0)
        MY_perror("fork");

  if (pid == 0) {                        /* child */
  
    if (execl("child", "child", itoa(SHMKEY[cid]), (char *) 0, 0) < 0)
        MY_perror("execl");
  }

  else                          /* parent */
        cid++;                  /* inc child id */
 
  }                       /* end for */

              ......
              .......

  /*   ********************  END OF PARENT ***************** */


  /*   ******************** BEGIN OF CHILD ***************** */

  int key = atoi(argv[1]);
  int Cshmid;                 /* shared mem. id. in child */
  char * Cscreen;            /* pointer to top of shared mem. segment */



  /* Get shared mem. region created by parent (via key) */
  /* and attach to */

  Cshmid = shmget(key, IMAGESIZE, 0);
  
  Cscreen = shmat(Cscreen, (char *) 0, 0);

            ......
               ....
               ...

 
  /*   ********************  END OF CHILD  ***************** */



I observed that the shared mem. regions in the Parent, begin, say, at 0
and 4096, and so, BUT in the childs they all begin at 0, although
their shared mem. regions are in fact completely different/separated.

I think that in the child, the pointer to the shared mem. is relative
to the address space of the process, that is 2 differents processes can
have 2 pointers EQUALS in their values, but NOT in the contents of the
pointed regions, and inversely 2 pointers of 2 differents processes
might be differents in their value, but EQUALS in the contents of the
pointed regions.

My question is:  Can I force 2 pointers of 2 differents processes
pointing to the same address to be equals, (at least between parent and
child) ?

If yes, How to do that ?

             Thanks a lot to everyone

             -- Meir <meir@bis.co.il>