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

标签 linux multithreading process pthreads

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

There are eight ways for a process to terminate. Normal termination occurs 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/

相关文章:

c# - 在后台线程中监听事件

c# - 向父进程发出子进程已完全初始化的信号

linux - Bash:了解是否存在具有指定线程 ID 的线程的简便方法?

bash - 递归调用函数时如何阻止bash创建子shell

java - 缓冲读取器线程安全吗?

linux - 对 nvidia GPU 上的计算单元和预期核心的混淆

linux - 监控 pthread 上下文切换

c++ - LD_LIBRARY_PATH 不包含导出路径

c - 如何修复 "dangerous relocation: unsupported relocation"

c# - 异步向主线程发送信号