Guest_imported
New member
- Jan 1, 1970
- 0
I'm really confused about unix fork() even after reading many articles. Really appreciate your reply if you know answers.
It's said the child process duplicates everything of parent process including the code. Does the child execute the parent program code from the beginning once it's created? For example:
/*some statements before fork()*/
int pid = fork();
if( pid < 0 ){ perror("..."
; exit(EXIT_FAILURE);}
else if( pid ) //parent process
{
/*some code here*/
}
else // child process
{
/*child code here*/
}
Because I just want the child process execute the child code within braces. And I tried several times, some results are weird, and often many many child processes are spawned. So I'm wondering the child process executes the parent code from the beginning? Am I right? I hope I'm wrong.But I really found fork() is hard to use.
Another puzzle is if parent creates a share memory segment before fork(), does the child also duplicates this share memory space? or only the share memory id?
One question about share memory again. if use semaphore for mutual exclusion, shmget(key_t key,..,..), do I have to the value of semaphore id to key? as I read one sentence like this: "the key is associated with the semaphore ID".
Thnaks for reply!
It's said the child process duplicates everything of parent process including the code. Does the child execute the parent program code from the beginning once it's created? For example:
/*some statements before fork()*/
int pid = fork();
if( pid < 0 ){ perror("..."
else if( pid ) //parent process
{
/*some code here*/
}
else // child process
{
/*child code here*/
}
Because I just want the child process execute the child code within braces. And I tried several times, some results are weird, and often many many child processes are spawned. So I'm wondering the child process executes the parent code from the beginning? Am I right? I hope I'm wrong.But I really found fork() is hard to use.
Another puzzle is if parent creates a share memory segment before fork(), does the child also duplicates this share memory space? or only the share memory id?
One question about share memory again. if use semaphore for mutual exclusion, shmget(key_t key,..,..), do I have to the value of semaphore id to key? as I read one sentence like this: "the key is associated with the semaphore ID".
Thnaks for reply!