share malloced data between vforked processes

Lei Sun lei_sun at comcast.net
Thu Jan 19 16:47:14 UTC 2006


Hi:
  I should've asked uClinux list, but somehow it didn't appear on that list 12 hours after i posted it. I thought it was related to uClibc as well? Sorry if it's not and just ignore it please!

 I know vfork has been discussed quite a lot on MMU-less uClinux,
but I didn't find any topic related to mine.
 Basically i need to create multiple processes using vfork, they
will be talking to each other using pipes. The parent malloced some
data that the child will be using.
The following piece of psudo code shows the flow.

typedef struct _mydata
{
  int count;
  char * pointer;
}mydata_t
main(int argc, char ** argv)
{
  int execed = 0;
  static mydata_t data;
  data.pointer = (char*)malloc(1024);

  /* check the argument, if it sees "-e" than execed = 1 */

  if(execed) {
      /* parent invoked program using -e option, call the child function .*/
     child_func(mydata_t * datap);
  }
  /* fork the child .*/
  if(pid = vfork()) < 0) {
      /* error. */
  }else if(pid == 0) {
      /* child. , exec itself with -e argument. */
      argv[argc++] = "-e";
      argv[argc++] = NULL;
      execv(argv[0], argv);
      /* not reached. */
  }

  /* parent do something, reading /writing from pipe to communicate
with child.*/
}

void child_func(mydata_t * datap)
{
   fprintf(stderr, "datap->pointer=%p\n", datap->pointer);
   /* Here, it print out "datap->pointer=(nil) " , access pointer
cause memory fault.*/
}

I have to use exec() to unblock the parent, because I want the 2
process to execute simutinusly. Looks like all the static data, data
on the stack can be shared, but how do i shared the malloced data
within multiple processes? I tried to pass the pointer value as
argument when execv the program(thus calling the child_func), but it
seems not working, besides, if the data structure is complicated, it
won't be a good solution. Any suggestion?

Thank you!

lei
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.busybox.net/pipermail/uclibc/attachments/20060119/7c9d1209/attachment-0001.htm 


More information about the uClibc mailing list