pthread_create b/w fork and exec

pthread_create b/w fork and exec

本文关键字:fork and exec create pthread      更新时间:2023-10-16

我希望在子进程中创建一个线程,然后使用exec系统调用改变子进程的图像。然而,pthread_create调用似乎被忽略了。

    pthread_t thread;
    pthread_attr_t attribute;
    pthread_attr_init(&attribute);
    pthread_attr_setdetachstate(&attribute, PTHREAD_CREATE_DETACHED);
pid_t cid = fork();
if(cid == 0)        //CHILD Process
{
    switch(x->option)
    {
        case 1:     pthread_create(&thread, &attribute, compressShow, NULL);                
                    execl("/home/aamir/Lab/ass3/compression", "compression", source, destination, NULL); 
                    cout<<"Execution failed."<<endl; break; //This segment will execute if exec fails.
    }
else            //PARENT Process
{
    wait(0);        //Prevents termination of original main until forked exec completes execution
    pthread_cancel(thread);
}

线程基本上只是一个用于输出'的进度显示。'(点)与fork子进程并发。

如果我删除exec调用,线程将执行。我在谷歌上搜索过,读到你不能在fork和exec之间使用pthread_create,这与异步安全函数有关。你能帮帮我吗?

exec位删除所有内容,包括线程,然后启动一个新进程。这包括内存等

程序可能(通常)不会到达启动线程的位。