我有一個thread用來收msg (msg是一道command string),
然後將收到的指令利用popen執行,
並把執行結果透過一個function送出(或直接印出),但其效率奇差,一道指令被收到,
到把全部結果全部讀出送出,差不多需要三秒,執行結果不算很多,少的才一兩行而已,
真的很神奇的慢!
請問有大大可以指點一下,有沒辦法把這段式加快,像是獨佔CPU,還是啥的????
我的thread while(1) function如下:
while( OSS_MailBoxReceive(sMK.cli.LC.sess_tab[sess_ix].mbx_id, relayed_cmd, 128) == OSS_OK )
{
ii = 0;
len = strlen(relayed_cmd);
if(len == 0)
{
IP4800_MK_IMC_tx(au8prompt, strlen(au8prompt) + 1 ,(U16)sess_ix, sMK.role, MK_IMC_S2M_CLI_PLD, 0, 0);
continue;
}
sprintf(executed_cmd, "echo \"%s\" | clish", relayed_cmd);
fp = popen(executed_cmd, "r");
while(fgets(buffer, sizeof(buffer), fp) != NULL)
{
ii++;
if(ii < CLISH_RSP_STARTING_LINE_NUM)
{ continue; }
if(ii > CLISH_RSP_STARTING_LINE_NUM)
{
IP4800_MK_IMC_tx(buffer, strlen(buffer) + 1, (U16)sess_ix, sMK.role, MK_IMC_S2M_CLI_PLD, 0, 0);
continue;
}
if(ii == CLISH_RSP_STARTING_LINE_NUM)
{
IP4800_MK_IMC_tx(g_aRecordSeparator, sizeof(g_aRecordSeparator), (U16)sess_ix, sMK.role, MK_IMC_S2M_CLI_PLD, 0, 0);
if( buffer[2+len] != '\0' )
{
strcpy(buffer, &buffer[2+len]); //NOT GOOD???
IP4800_MK_IMC_tx(buffer, strlen(buffer) + 1, (U16)sess_ix, sMK.role, MK_IMC_S2M_CLI_PLD, 0, 0);
}
}
}
IP4800_MK_IMC_tx(au8prompt, strlen(au8prompt) + 1 ,(U16)sess_ix, sMK.role, MK_IMC_S2M_CLI_PLD, 0, 0);
memset(relayed_cmd, 0, sizeof(relayed_cmd));
pclose(fp); //debug-by-tewen 2010.05.19
}