Cygwin裡的子進程在跳轉到正確的位置之前要做一些特定的處理,這個處理由child_info_fork::handle_fork函數完成,在child_info_fork::handle_fork函數調用的第一個函數就是cygheap_fixup_in_child:
cygheap_fixup_in_child (false);
看它的實現:
/* Called by fork or spawn to reallocate cygwin heap */
void __stdcall
cygheap_fixup_in_child (bool execed)
{
cygheap_max = child_proc_info->cygheap;
cygheap = (init_cygheap *) cygheap_max;
_csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
child_copy (child_proc_info->parent, false, "cygheap", cygheap, cygheap_max, NULL);
cygheap_init ();
debug_fixup_after_fork_exec ();
if (execed)
{
cygheap->hooks.next = NULL;
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
/* Walk the allocated memory chain looking for orphaned memory from
previous execs */
for (_cmalloc_entry *rvc = cygheap->chain; rvc; rvc = rvc->prev)
{
cygheap_entry *ce = (cygheap_entry *) rvc->data;
if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
continue;
else if (ce->type < HEAP_1_MAX)
ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
else
_cfree (ce); /* Marked by parent for freeing in child */
}
}
}
在這裡child_proc_info是父進程調用CreateProcess時傳遞進來的參數,其cygheap和cygheap_max保存了父進程的cygheap的范圍,這個函數做的第一件事就是將父進程這一段空間的內容復制到子進程相當地址的空間中,這個功能由child_copy函數完成。
緊接著,又調用了一個叫cygheap_init的函數:
extern "C" void __stdcall
cygheap_init ()
{
cygheap_protect.init ("cygheap_protect");
if (!cygheap)
{
cygheap = (init_cygheap *) memset (_cygheap_start, 0,
_cygheap_mid - _cygheap_start);
cygheap_max = cygheap;
_csbrk (sizeof (*cygheap));
}
if (!cygheap->fdtab)
cygheap->fdtab.init ();
if (!cygheap->sigs)
sigalloc ();
}
此時,由於cygheap的內容已經從父進程完整復制過來,這個函數相當於什麼事沒做。唯一有效果的就是cygheap_protect.init函數調用。
在cygheap_init之後,是一行空語句:
# define debug_fixup_after_fork_exec() do {} while (0)
由於從child_info_fork::handle_fork傳遞進來的execed為false,因此函數執行到此結束。