【发布时间】:2012-10-28 12:35:56
【问题描述】:
假设我有一个主进程正在运行,并且在它的执行过程中它初始化了一些指针并创建了一些预定义结构的实例。
现在如果我分叉这个主进程,是否为指针分配了单独的内存?并且是先前存在的变量的重复实例,为这个新进程创建的数据结构?
作为我的要求的一个例子考虑 -
struct CKT
{
...
}
main()
{
...Some computations with the structure and other pointers.....
pid_t pid = fork();
if(pid == 0) //child
{
..some more computations with the structure...but I need a
..separate instance of it with all the pointers in it as well..
}
else if(pid > 0) // parent
{
..working with the original instance of the structure..
}
// merging the child process with the parent...
// after reading the data of the child processes structure's data...
// and considering a few cases...
}
谁能解释我如何做到这一点??
【问题讨论】:
-
您的问题不完整。
-
除了fork()的返回值外,内存是一样的。
-
@KirilKirov - 是的,它不完整......对此我感到非常抱歉......但现在我已经完成了......
标签: c multithreading fork