Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread_创建b/w fork和exec_C++_Linux_Pthreads_Exec_Fork - Fatal编程技术网

C++ pthread_创建b/w fork和exec

C++ pthread_创建b/w fork和exec,c++,linux,pthreads,exec,fork,C++,Linux,Pthreads,Exec,Fork,我希望在各个子进程使用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()

我希望在各个子进程使用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);
}
pthread\u t thread;
pthread_attr_t属性;
pthread_attr_init(&attribute);
pthread\u attr\u setdetachstate(&attribute,pthread\u CREATE\u DETACHED);
pid_t cid=fork();
if(cid==0)//子进程
{
开关(x->选项)
{
案例1:pthread_create(&thread,&attribute,compressShow,NULL);
execl(“/home/aamir/Lab/ass3/compression”,“compression”,源,目标,空);

coutexec位处理包括线程在内的所有内容,并启动一个新进程,包括内存等


该计划可能(而且通常是)无法启动线程。

根据定义,每个进程都有自己的线程集和地址空间。因此,父进程不会看到子进程创建的线程。我看到的一个可能的解决方案是在fork之前在父进程中创建线程。是否有其他可能的解决方法?是的,在父进程中创建线程。Al因此,创建一个
管道
。让子线程在管道上写入,父线程读取它。这是正确的,但不要解释错误。线程是特定于进程的。请参阅我在问题中的注释。但是exec调用在线程之后。从技术上讲,不应该创建线程,然后执行zapps所有内容吗?@BasileStarynkevitch-I正在使用KISS原则。哦,等等。因此,即使线程是在exec系统调用之前创建的,新程序也会对其进行处理?我如何解决这个问题?我可以在fork之前的父线程中创建线程。是否有其他可能的解决方法,让我创建线程并从子线程运行/维持它,即使在exec之后?