0

I wish to create a thread in a child process before the respective child process changes it's image using exec system call. However, seemingly, the pthread_create call is being overlooked.

    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);
}

The thread is basically just a progress display that is intended to output '.' (dots) in concurrence with the forked child.

If I remove the exec call the thread does execute. I've searched on google and read somewhere that you cannot use pthread_create between a fork and exec, something to do with async safe functions. Can you please help?

4

1 回答 1

2

The exec bit zapps everything including threads and just starts a new process. That includes memory etc.

The program might (and usually) does not get to the bit to fire up the thread.

于 2013-03-30T20:53:34.450 回答