八皇后问题C语言实现.docx
- 文档编号:10182301
- 上传时间:2023-05-24
- 格式:DOCX
- 页数:33
- 大小:21.12KB
八皇后问题C语言实现.docx
《八皇后问题C语言实现.docx》由会员分享,可在线阅读,更多相关《八皇后问题C语言实现.docx(33页珍藏版)》请在冰点文库上搜索。
八皇后问题C语言实现
/*
*AHeartBeatreceiverinRSStoreceiveheart-beatfromeStations.IfaneStationdoesnot
*sentoutheart-beatsthreeconsecutivetimes,theeStationwillberegardedasdead.Youcan
*checkHeartBeatReceiver.txtforthefurtherinformation.
*
*ModificationHistory:
*#0002000-06-07PhantomFilecreate.
*#0012000-06-19ViperForkprocesstodofirst-timeregistration
*#0022000-06-22PhantomForshutdownsignalfromanyeStation
*#0032000-06-28PhantomModifybecausecommunicationAPIchanged
*#0042000-07-13ViperchangethepathofRSSFTRegister
*#0052000-07-17PhantomCheckpotentialmemoryleak
*#0062000-07-25PhantomChangetheeventnameandvalue
*#0072000-07-31PhantomDebugchangeflag=1toflag=0forredevent.
*#0082000-08-01PhantomTopreventeidgreaterthan8*MAX_SIZE
*#0092000-08-03ViperZombieByeBye
*#0102000-08-03PhantomDebugforpotentialbugthatnowhereto
resetinitreqbackto1ifinitreq=2.
*#0112000-08-15PhantomPreventtoomanynetworkinaccessevents.
*#0122000-09-01ViperPreventRSSFTRegistergetwrongeid&
*avoidmemoryleak
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"RSS_MySQL.h"
#include"RSS_Receiver.h"
#include"ipTCommModule.h"
#include"HeartBeatReceiver.h"
#include"lib2ndAPI.h"
/*#001begin*/
#ifndefRSSFTRegister
/*#004begin*/
//#defineRSSFTRegister("./RSSFTRegister")
#defineRSSFTRegister("/opt/iptrend/RSS/bin/RSSFTRegister")
/*#004end*/
#endif
/*#001end*/
unsignedcharbeat0[MAX_SIZE];
unsignedcharbeat1[MAX_SIZE];
unsignedcharbeat3[MAX_SIZE];
unsignedcharbeat4[MAX_SIZE];
unsignedcharbeat5[MAX_SIZE];
intflipflag=0;
intfirstcheck=1;
structtimebstart,end;
staticpthread_attr_tattr;
voidsetBit(unsignedcharbeat[MAX_SIZE],intindex,intoffset)
{
if(index>=MAX_SIZE)//#008
return;
switch(offset){
case0:
beat[index]=beat[index]|0x01;
break;
case1:
beat[index]=beat[index]|0x02;
break;
case2:
beat[index]=beat[index]|0x04;
break;
case3:
beat[index]=beat[index]|0x08;
break;
case4:
beat[index]=beat[index]|0x10;
break;
case5:
beat[index]=beat[index]|0x20;
break;
case6:
beat[index]=beat[index]|0x40;
break;
case7:
beat[index]=beat[index]|0x80;
break;
}/*endofswitch*/
}/*endoffunctionsetBit()*/
voidclearBit(unsignedcharbeat[MAX_SIZE],intindex,intoffset)
{
if(index>=MAX_SIZE)//#008
return;
switch(offset){
case0:
beat[index]=beat[index]&0xfe;
break;
case1:
beat[index]=beat[index]&0xfd;
break;
case2:
beat[index]=beat[index]&0xfb;
break;
case3:
beat[index]=beat[index]&0xf7;
break;
case4:
beat[index]=beat[index]&0xef;
break;
case5:
beat[index]=beat[index]&0xdf;
break;
case6:
beat[index]=beat[index]&0xbf;
break;
case7:
beat[index]=beat[index]&0x7f;
break;
}/*endofswitch*/
}/*endofclearBit()*/
intgetBit(unsignedcharbeat,intoffset)
{
switch(offset){
case0:
return(beat&0x01);
break;
case1:
return(beat&0x02);
break;
case2:
return(beat&0x04);
break;
case3:
return(beat&0x08);
break;
case4:
return(beat&0x10);
break;
case5:
return(beat&0x20);
break;
case6:
return(beat&0x40);
break;
case7:
return(beat&0x80);
break;
default:
writeLog("ErroringetBit(),offsetmustbetween0to7",0);
return0;
break;
}/*endofswitch*/
}/*endofgetBit()*/
voiderrexit(char*err_msg)
{
chartempstr[TEMPSTR_LEN];
//sprintf(tempstr,"Errornois%d,%s",ipTGetError(),err_msg);
sprintf(tempstr,"CommunicationAPIError");
writeLog(tempstr,1);
}/*endoferrexit()*/
voidwriteLog(char*err_msg,intleave)
{
FILE*fptr;
time_tt;
charfilename[1000];
sprintf(filename,"%s%s",getenv("IPT_HOME"),LOG_FILENAME);
fptr=fopen(filename,"a+");
time(&t);
fprintf(fptr,"%s:
%s\n",ctime(&t),err_msg);
fclose(fptr);
if(leave==1)
exit
(1);
}/*endofwrite_log*/
voidinitialize()
{
MYSQL*mysql;
constchar*host=RSS_MYSQL_HOST;
constchar*user=RSS_MYSQL_USER;
constchar*password=RSS_MYSQL_PASSWORD;
constchar*database=RSS_MYSQL_DATABASE;
constchar*unix_socket=RSS_MYSQL_UNIX_SOCKET;
constchar*stmt="selecteid,alive,initreqfromeStationorderbyeid";
constunsignedintport=RSS_MYSQL_PORT;
constunsignedintclient_flag=RSS_MYSQL_CLIENT_FLAG;
chartempstr[TEMPSTR_LEN];
unsignedlongnum_fields=0;
MYSQL_ROWrow;
MYSQL_RES*result;
intnum=0;
intindex=0;
intoffset=0;
intinitreq=0;
memset(beat0,0,MAX_SIZE);
memset(beat1,0,MAX_SIZE);
memset(beat3,0,MAX_SIZE);
memset(beat4,0,MAX_SIZE);
memset(beat5,0,MAX_SIZE);
if(ipTRegisterComm(HEARTBEAT_PORT)!
=IPT_SUCCESS)
errexit("Cannotregistercommunication");
pthread_attr_init(&attr);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
/*
*ReadeStationstatusfromthedb,andsetintobeat4andbeat1.
*/
if((mysql=mysql_init(NULL))==NULL){
writeLog("mysql_init()fail",1);
}
if(mysql_real_connect(mysql,host,user,password,database,port,
unix_socket,client_flag)==NULL){
sprintf(tempstr,"mysql_real_connect()error=%s",mysql_error(mysql));
writeLog(tempstr,1);
}
if(mysql_query(mysql,stmt)!
=RSS_MYSQL_SUCCESS){
sprintf(tempstr,"mysql_query_error=%s",mysql_error(mysql));
mysql_close(mysql);
writeLog(tempstr,1);
}
result=mysql_store_result(mysql);
if(result==NULL){
sprintf(tempstr,"mysql_store_result()error=%s",
mysql_error(mysql));
mysql_close(mysql);
writeLog(tempstr,1);
}
else{
num_fields=mysql_num_fields(result);
while((row=mysql_fetch_row(result))!
=NULL){
num=atol(row[0]);
index=(num-1)/8;
offset=(num-1)%8;
initreq=atol(row[2]);
if(initreq>=1){//#002
setBit(beat4,index,offset);
setBit(beat1,index,offset);
}
}
mysql_free_result(result);
}/*endofelsehasdata*/
mysql_close(mysql);
}/*endofinitialize()*/
void*checkHeartBeat(void*argv)
{
MYSQL*mysql;
constchar*host=RSS_MYSQL_HOST;
constchar*user=RSS_MYSQL_USER;
constchar*password=RSS_MYSQL_PASSWORD;
constchar*database=RSS_MYSQL_DATABASE;
constchar*unix_socket=RSS_MYSQL_UNIX_SOCKET;
charstmt[STMT_LEN];
chartempstr[TEMPSTR_LEN];
constunsignedintport=RSS_MYSQL_PORT;
constunsignedintclient_flag=RSS_MYSQL_CLIENT_FLAG;
inti,j;
if(firstcheck==1){
firstcheck=0;
sleep(CHECK_INTERVAL);
}
while
(1){
ftime(&start);
for(i=0;i beat3[i]=beat0[i]|beat1[i]; if(flipflag==0){ memset(beat1,0,MAX_SIZE); flipflag=1; } elseif(flipflag==1){ memset(beat0,0,MAX_SIZE); flipflag=0; } else{ /* *Thiscaseshouldnothappen,justincase */ writeLog("flipflagError,resetto0",0); memset(beat0,0,MAX_SIZE); flipflag=0; } for(i=0;i beat5[i]=beat3[i]^beat4[i]; /* *Forceallthealive=1ineStationwhereinitreq>=1 */ if((mysql=mysql_init(NULL))==NULL){ writeLog("mysql_init()fail",0); returnNULL; } if(mysql_real_connect(mysql,host,user,password,database,port, unix_socket,client_flag)==NULL){ sprintf(tempstr,"mysql_real_connect()error=%s", mysql_error(mysql)); writeLog(tempstr,0); mysql_close(mysql); returnNULL; } sprintf(stmt,"UpdateeStationsetalive=1whereinitreq>=1;");//#002 if(mysql_query(mysql,stmt)! =RSS_MYSQL_SUCCESS){ sprintf(tempstr,"mysql%serror%s",stmt,mysql_error(mysql)); writeLog(tempstr,0); mysql_close(mysql); returnNULL; } mysql_close(mysql); for(i=0;i if(beat5[i]! =0){ for(j=0;j<8;j++){ if(getBit(beat5[i],j)! =0){ if(getBit(beat4[i],j)! =0){ /* *aneStationinaccessable */ #ifdefDEBUG printf("Inaccessindex=%d,offset=%d\n",i,j); fflush(stdout); #endif esInaccess(i,j); } else{ /* *aneStationfirst-timereg */ #ifdefDEBUG printf("FirstRegindex=%d,offset=%d\n",i,j); fflush(stdout); #endif esFirstReg(i,j); } } }/*endofforj=0to7(scanbitbybit)*/ }/*endofifbeat5[i]! =0*/ }/*endoffori=0toMAX_SIZE*/ ftime(&end); /*SleeptheresttimeofCHECK_INTERVAL*/ sleep(CHECK_INTERVAL-(end.time-start.time)); }/*endofwhile (1)*/ }/*endofcheckHeartBeat()*/ voidesInaccess(intindex,intoffset) { MYSQL*mysql; constchar*host=RSS_MYSQL_HOST; constchar*user=RSS_MYSQL_USER; constchar*password=RSS_MYSQL_PASSWORD; constchar*database=RSS_MYSQL_DATABASE; constchar*unix_socket=RSS_MYSQL_UNIX_SOCKET; charstmt[STMT_LEN]; chartempstr[TEMPSTR_LEN]; constunsignedintport=RSS_MYSQL_PORT; constunsignedintclient_flag=RSS_MYSQL_CLIENT_FLAG; chareid[EID_SIZE+1]; charetype[3]; inttotalgroup=0,gid=1; intinitreq=0; MYSQL_RES*result; MYSQL_ROWrow; time_ttnow; structtm*t; eid[0]='\0'; etype[0]='\0'; /* *InitializeDatabaseconection */ if((mysql=mysql_init(NULL))==NULL){ writeLog("mysql_init()fail",0); } if(mysql_real_connect(mysql,host,user,password,database,port, unix_socket,client_flag)==NULL){ sprintf(tempstr,"mysql_real_connect()error=%s",mysql_error(mysql)); writeLog(tempstr,0); return; } sprintf(eid,"%07d",index*8+offset+1); /* *Selectetypefromtable"eStation" */ sprintf(stmt,"Selectetype,initreqfromeStationwhe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 皇后 问题 语言 实现