2012-01-23 31 views
1

我試着從MATLABMATLAB fork一個(微軟視窗)工藝

myCaller = ['theExe.exe' ' its arguments' ' &']; 
system(myCaller); 

但是fork一個(微軟視窗)過程中,我cannt實現了叉。

最後,我試圖做的是啓動一個(MS Windows)進程,並關閉調用MATLAB例程。

如何實現上述任何建議?

回答

0

這很難;目前我在Ubuntu上也在做同樣的事情。

基本上,MATLAB會坐等待生成的程序完成,然後繼續其自己的進程,儘管最後是&

我一直在使用的解決方法是使用創建單獨線程的mex文件(使用pthread)。這個單獨的線程開始theExe.exe,而主要的mex線程退出。

我的代碼一些未經檢驗的黑客聚會位,說明了這個過程:

#include "mex.h" 
#include <iostream> 
#include "pthread.h" /* for threading */ 

// thread function which calls system call, and waits for it to finish 
void *do_thread(void *pid) 
{ 
    mexPrintf("In thread\n"); 

    // replace this with your system call - don't know if std::system works on windows? 
    std::system("your_system_call"); 

    pthread_exit(NULL); 
} 

// main mex function 
void mexFunction (int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]) 
{ 
    pthread_t thread; 
    mexPrintf("In main: creating thread\n"); 
    rc = pthread_create(&thread, NULL, do_thread, (void *)v);  
    return; 
} 

在Ubuntu上,這可以被編譯類似:

mex mex_filename.cpp -I/usr/include/ -lpthread -lrt 

我承認我不知道如何在Windows上進行編譯,但pthreads的版本確實存在於Windows中。

+0

最終,我結束了僞造分支。 ...一個通用的函數,在它繼續工作時調用cmd上的進程。 – HeinrichStack

+1

@HeinrichStack,你能解釋一下,或者你可以指示我的任何鏈接.. – sridutt

+0

@Sridutt Nayak:你只需調用cmd(MS Windows命令提示符),並從打開的commpand提示符執行所需的進程。 – HeinrichStack