I know calling fork() sys_call
from a thread is a bad idea.
However, what will happen if a thread creates a new process using fork()
?
The new process will be the child of the main thread that created the thread. I think.
If its parent finishes first, the new process will be attached to init process.
And its parent is main thread, not the thread that created it.
Correct me if I am wrong.
#include <stdio.h>
#include <pthread.h>
int main ()
{
thread_t pid;
pthread_create(&(pid), NULL, &(f),NULL);
pthread_join(tid, NULL);
return 0;
}
void* f()
{
int i;
i = fork();
if (i < 0) {
// handle error
} else if (i == 0) // son process
{
// Do something;
} else {
// Do something;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…