メイン関数では、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 );
}
問題 :
スレッドは 10 秒ではなく 4 ~ 5 秒しか待機しませんが、興味深いことに、スタンドアロンの C プログラムの ThreadWait1 関数と同じロジックを使用すると、うまく機能します。CPUのスレッドスケジューリングなどに関連している可能性があります。