我希望在各个子进程使用exec系统调用更改它的映像之前,在子进程中创建一个线程。但是,看起来,pthread_create调用正在被忽略。pthread_create b/w fork和exec
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);
}
该线程基本上只是一个进度显示,旨在输出'。' (点)与分叉的孩子同时发生。
如果我删除exec调用,线程执行。我在google上搜索过,并在某处读过你不能在fork和exec之间使用pthread_create,这与异步安全函数有关。你能帮忙吗?
每个进程都有自己的一组线程和自己的地址空间。所以父母不会看到孩子创建的线程。 –
我看到的一种可能的解决方案是在fork之前在父级创建线程。有没有其他可能的解决方法? –
是的,在父级创建线程。另外创建一个'pipe'。让孩子在管道上书写,并在父母的线程读取它。 –