多线程的进程调用exec时,将终止整个进程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nice_wen/article/details/81258578

由于本人才疏学浅,本文难免存在遗漏之处,欢迎大家留言指正,本人将感激不尽。

最近比较好奇,若是一个多线程的进程执行exec()系统调用时,进程将作何反应,详情请如下代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

void * fun(){
    printf("start exec\n");
    execl("test", NULL);
}

int main(){
    pthread_t tid;

    pthread_create(&tid, NULL, fun, NULL);

    while(1){
        printf("1\n");
    }

    return 0;
}

  其中主线程创建了一个子线程,然后主线程循环输出1。其中,子线程中通过execl()加载可执行文件test,将真个进程的地址空间替换掉。

  结果显示,主线程输出一段1之后不再输出。显然,线程调用exec之后,整个进程都将终止掉。

猜你喜欢

转载自blog.csdn.net/nice_wen/article/details/81258578