Date: Fri, 21 Nov 1997 12:57:52 -0800 (PST) From: Curtis Bray <cbray@best.com> To: freebsd-hackers@freebsd.org Subject: Re: malloc() problems in children after using rfork() Message-ID: <Pine.BSF.3.96.971121125505.26552A-100000@shell5.ba.best.com>
next in thread | raw e-mail | index | archive | help
Curtis Bray wrote:
> I'm trying to use rfork(RFPROC | RFMEM) so that all the children can
> share the same address space with their parent.
>
> If I have multiple children issuing mallocs the children seem to
> core
> dump. Once I turn the RFMEM flag off I have no problem mallocing (but
> of course I loose the shared address space). Anyone know what I could
> be doing wrong here? Do I have to put semaphores around every
> malloc??
> I hope that's not the case... Thanks in advance!
By the way, I am running on 2.2.5 (STABLE).
After further examination I noticed that the segmentation fault only
occurs if the child tries to read or write to the piece of memory that it
allocated. For example:
void child_proc(int procNum, int time)
{
char *buf;
buf = (char *) malloc(255);
printf("Child %d created...: buf = %p\n", procNum, buf);
sleep(time);
GlobalCount++;
printf("%c\n", buf[0]); <<-- It will page fault here...
printf("Freeing %p\n", buf);
free(buf);
exit(0);
}
Also if I run with only one child I don't have a problem. But if two
children try to modify the their malloced space, then only one succeedes
and the other faults. Here's the output from running the above with 2
children:
---------------
Going to create 2 rfork() process
Parent waiting for children.
Child 0 (PID 8343) created...: buf = 0x15000
Child 1 (PID 8344) created...: buf = 0x15100
Freeing 0x15000
Parent woke up on wait for 8344 with status 139 (#0)
8344 : Signaled? 1 / SigNum 11
Parent woke up on wait for 8343 with status 0 (#1)
8343 : Signaled? 0 / SigNum 0
Parent exiting: Global Count: 2!
---------------
The same problem occrurs even if I don't call free() in each child.
Other ideas?
Curtis
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.96.971121125505.26552A-100000>
