2

在我的主函数中,我创建了一个执行 pingOMS 函数的线程。以下是从我的主要功能中提取的代码。

if (status = pthread_create(&agentPingThread,NULL,(void *) &pingOMS, NULL) != 0 ){
    LogError("%s: Failed to create the OM agent ping thread -- %s\n", prog,
strerror(status));        
}

此 pingOMS 函数通过调用 pingOMS 函数中的另一个函数 ThreadWait1 来强制当前线程等待,每 10 秒使用套接字联系服务器。函数 ThreadWait1 使用 pthread_cond_timedwait 来实现这一点。以下是 pingOMS 的代码:

int pingOMS(){
DEBUG("Inside %s %s() \n",__FILE__,__func__);

if(This.stopped || failedPings > 1){
    DEBUG("  Ping manager stopping ...\n");
    return TRUE;
}

int socketPING,returnHB;
returnHB = FALSE;
struct sockaddr_in serverADDRESS;
struct hostent *hostINFO;    
char remoteFILE[4096],recvBUFF[4096];

if ((hostINFO = gethostbyname(This.servername)) == NULL){
    failedPings++;
    LogError("Ping manager unable to reach OM Server.\n");
    if(failedPings < 2) goto SKIP_POINT1;        
    if(failedPings > 1){
        This.stopped = TRUE;
        return FALSE;
    }
}

if ( (socketPING = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    failedPings++;
    LogError("Ping manager unable to create socket.\n");
    if(failedPings < 2) goto SKIP_POINT1;        
    if(failedPings > 1){
        This.stopped = TRUE;
        return FALSE;            
    }
}

serverADDRESS.sin_family = hostINFO->h_addrtype;
memcpy((char *) &serverADDRESS.sin_addr.s_addr, hostINFO->h_addr_list[0], hostINFO-
>h_length);
serverADDRESS.sin_port = htons((int) This.serverport);

if (connect(socketPING, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0) {
    failedPings++;
    LogError("Ping manager unable to connect OM Server on port %d\n",
 (int)This.serverport);
    if(failedPings < 2) goto SKIP_POINT2;        
    if(failedPings > 1){
        This.stopped = TRUE;
        close(socketPING);
        return FALSE;            
    }
}else{
    DEBUG("  Ping manager successfully connected to OM Server on port 
%d\n",This.serverport);
}

sprintf(remoteFILE,"STARTPING|%d|%s|ENDPING",This.agentID,getOracleDate());    

if (send(socketPING, remoteFILE, sizeof(remoteFILE), 0) >= 0){
    DEBUG("  Server ping initiated.\n");
}else{
    LogError("Server ping failed. OM agent has lost connection to OM server.\n");
    failedPings++;    
    if(failedPings < 2) goto SKIP_POINT2;
    if(failedPings > 1){
        This.stopped = TRUE;
        close(socketPING);
        return FALSE;
    }
}                 


// Start - Following code interpret server ping status sent by OM server
int fr_block_sz = 0;
recvBUFF[0] = 0;
    while((fr_block_sz = recv(socketPING, recvBUFF, sizeof(recvBUFF), 0)) > 0){
        returnHB = TRUE;
        DEBUG("  Receiving ping data from OM server.\n");
    }       
    DEBUG("  Received buffer %s\n",recvBUFF);
    if(returnHB == TRUE){
        if(!strncmp(recvBUFF,"SUCCESS",7)) {   
            DEBUG("  Server ping succeeded.\n");                
        }        
        else{
            LogError("OM agent has lost connection with OM server and will shutdown.\n");
            failedPings++;
            if(failedPings < 2) goto SKIP_POINT2;
            if(failedPings > 1){
                This.stopped = TRUE;
                close(socketPING);
                return FALSE;
            }
        }                        
}else{
            failedPings++;
            LogError("OM agent has not received heartbeat from server.\n");
            if(failedPings < 2) goto SKIP_POINT2;
            if(failedPings > 1){
                This.stopped = TRUE;
                close(socketPING);
                return FALSE;
            }
        }

//End
SKIP_POINT2:       
close(socketPING);  
DEBUG("  Agent ping thread going into sleep mode ... \n");
SKIP_POINT1:
ThreadWait1(10000);
pingOMS();

}

这是 ThreadWait1 的代码:

void ThreadWait1(int timeInMSec)
{
DEBUG("Inside %s %s() \n",__FILE__,__func__); 
int rt;
pthread_mutexattr_t mtx_attr;
pthread_mutex_t mtx;
pthread_condattr_t cond_attr;
pthread_cond_t cond;

int milliseconds;

pthread_mutexattr_init ( &mtx_attr );
//pthread_mutexattr_settype ( &mtx_attr, PTHREAD_MUTEX_NORMAL );
pthread_mutexattr_setpshared ( &mtx_attr, PTHREAD_PROCESS_PRIVATE );

pthread_mutex_init ( &mtx, &mtx_attr );
pthread_mutexattr_destroy ( &mtx_attr );

#ifdef USE_CONDATTR
pthread_condattr_init ( &cond_attr );
if ( pthread_condattr_setclock ( &cond_attr, CLOCK_REALTIME ) != 0 )
{
    fputs ( "pthread_condattr_setclock failed", stderr );
    exit ( EXIT_FAILURE );
}

pthread_cond_init ( &cond, &cond_attr );
pthread_condattr_destroy ( &cond_attr );
#else
pthread_cond_init ( &cond, NULL );
#endif

    struct timespec now, ts;
        clock_gettime ( CLOCK_REALTIME, &now );

    ts.tv_sec = now.tv_sec + timeInMSec / 1000;
        ts.tv_nsec = now.tv_nsec + (timeInMSec % 1000) * 1000000;
    if (ts.tv_nsec > 1000000000)
    {
        ts.tv_nsec -= 1000000000;
        ++ts.tv_sec;
    }

    DEBUG ( " %ld.%09ld %ld.%09ld \n", now.tv_sec, now.tv_nsec,
             ts.tv_sec, ts.tv_nsec );

    pthread_mutex_lock ( &mtx );
    rt = pthread_cond_timedwait ( &cond, &mtx, &ts );
    ASSERT(rt);

    pthread_mutex_unlock ( &mtx );

}

问题 :

线程只等待 4 ~ 5 秒而不是 10 秒,但非常有趣的是,当独立 c 程序中的 ThreadWait1 函数的相同逻辑时,它运行良好。它可能与CPU上的线程调度或其他东西有关。

4

1 回答 1

0

这不是问题的答案,而是我心中的疑问。你用的是什么平台?

其次,为什么你创建了一个递归调用,我认为 ThreadWait1 应该在每 X 次(在你的情况下为 10 秒)之后调用 pingOMS 函数。

试试下面的代码:

#include <unistd.h>


if (status = pthread_create(&agentPingThread,NULL,(void *) &ThreadWait1, NULL) != 0 ){
    LogError("%s: Failed to create the OM agent ping thread -- %s\n", prog,
strerror(status));        
}


void ThreadWait1()
{
    int timeInMSec = 10;
    while(1)
    {
        pingOMS();
        // Add your locks here if required
        sleep(timeInMSec);
        // release the locks if locked
    }
}

并删除 pingOMS(); 从 pingOMS() 函数调用,使其不会调用自身(无递归)

于 2013-05-30T05:28:51.280 回答