gpt4 book ai didi

linux - Linux 中有多少种进程终止方式?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:30 24 4
gpt4 key购买 nike

我正在阅读 Unix 环境中的高级编程第 3 版,§7.3,进程终止,以下语句让我感到困惑:

There are eight ways for a process to terminate. Normal terminationoccurs in five ways:

  1. Return from main
  2. Calling exit
  3. Calling _exit or _Exit
  4. Return of the last thread from its start routine (Section 11.5)
  5. Calling pthread_exit (Section 11.5) from the last thread

对于

  1. Return of the last thread from its start routine (Section 11.5)
  2. 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com