我正在阅读 Unix 环境中的高级编程第 3 版,§7.3,进程终止,以下语句让我感到困惑:
There are eight ways for a process to terminate. Normal termination occurs in five ways:
- Return from main
- Calling exit
- Calling _exit or _Exit
- Return of the last thread from its start routine (Section 11.5)
- Calling pthread_exit (Section 11.5) from the last thread
对于
- Return of the last thread from its start routine (Section 11.5)
- Calling pthread_exit (Section 11.5) from the last thread
我认为即使进程中的最后一个线程已终止,如果进程未从 main 函数返回,该进程也不会终止,对吗?如果不是,为什么 4 和 5 是正确的?
最佳答案
主
线程是线程之一。例如,在
void *start(void *arg) {
sleep(1);
pthread_exit(0);
}
int main() {
pthread_t t;
pthread_create(&t, 0, start, 0);
pthread_exit(0);
}
主线程立即退出,但进程继续运行直到最后一个线程退出。反过来也是这样,
void *start(void *arg) {
pthread_exit(0);
}
int main() {
pthread_t t;
pthread_create(&t, 0, start, 0);
sleep(1);
pthread_exit(0);
}
主线程是最后一个剩下的。
关于linux - Linux 中有多少种进程终止方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43691689/