📄 mainserv.c
字号:
printf("previous head address is %X\n",srcArray); /* Rebuild the address array accrodding to the previous marks */ q = srcArray = malloc( LEN_P_LOCINFO * fRecords ); printf("Later head address is %X\n",srcArray); for( p=t; p< t+megCount; p++) if( *p != NULL ) { *q = *p; printf("Found records' address is %X\n",*p); q++; } /* Release the memory of former address array */ free( t ); } break; case BY_LOCTIME: /* Search the records by the given time limit */ fRecords=0; /* Mark the non-fixed records' pointer to NULL*/ for( p=srcArray; p<srcArray+megCount; p++) if( ! CompareLocTime( queFilter, p )) { *p = NULL; } else fRecords++; if( fRecords != 0 )/* Fixed records are found */ { t = srcArray; /* Rebuild the address array accrodding to the previous marks */ q = srcArray = malloc( LEN_P_LOCINFO * fRecords ); for( p=t; p<t+megCount; p++) if( *p != NULL ) { *q = *p; q++; } /* Release the memory of former address array */ free( t ); } else printf("No this records!\n"); break; case BY_BOTH: /* Search the records by the given time limit and source user ID */ /* Mark the non-fixed records' pointer to NULL*/ for( p=srcArray; p<srcArray+megCount; p++) if( ((*p)->dwxx.srcID) != queFilter.srcID ) { if( !CompareLocTime( queFilter, p )) { printf("The not found DESID is %X\n",(*p)->dwxx.srcID); *p = NULL; } } else { printf("The found DESID is %X,%X\n",(*p)->dwxx.srcID,queFilter.srcID); fRecords++; } printf("Found records is %d\n",fRecords); if( fRecords != 0 )/* Fixed records are found */ { t = srcArray; /* Rebuild the address array accrodding to the previous marks */ q = srcArray = malloc( LEN_P_LOCINFO * fRecords ); for( p=t; p<t+megCount; p++) if( *p != NULL ) { *q = *p; q++; } /* Release the memory of former address array */ free( t ); } else printf("No this records!\n"); break; } return (void **)srcArray; break; case TYPE_OF_TXXX: srcArray1 = (COMMINFO **)arg; switch( filter ) { case BY_LOCADDRSS: /* Search the records by the given source ID */ /* Mark the non-fixed records' pointer to NULL*/ fRecords=0; for( x=srcArray1; x< srcArray1+megCount; x++) if( (*x)->txIndex.srcID != queFilter.srcID ) { *x = NULL; } else { fRecords++; printf("Found records' address is %X\n",*x); } printf("Found records is %d\n",fRecords); if( fRecords != 0 )/* Fixed records are found */ { z = srcArray1; printf("previous head address is %X\n",srcArray1); /* Rebuild the address array accrodding to the previous marks */ y = srcArray1 = malloc( LEN_P_COMMINFO * fRecords ); printf("Later head address is %X\n",srcArray1); for( x=z; x< z+megCount; x++) if( *x != NULL ) { *y = *x; printf("Found records' address is %X\n",*p); y++; } /* Release the memory of former address array */ free( z ); } break; case BY_NEWEST3: /* Search the records which are the lastest 3 ones */ fRecords=3; printf("Found records is %d\n",fRecords); if( fRecords != 0 )/* Fixed records are found */ { z = srcArray1; printf("previous head address is %X\n",srcArray1); /* Rebuild the address array accrodding to the previous marks */ y = srcArray1 = malloc( LEN_P_COMMINFO * 3 ); printf("Later head address is %X\n",srcArray1); for( i=3; i> 0; i--) { *y = *(z+megCount-i ); printf("Found records' address is %X\n",*y); y++; } /* Release the memory of former address array */ free( z ); } break; case BY_AUTOFBACK: /* Search the records by the AUTOFEEDBACK message */ /*???????????????????What is autoFeedBack message??????????????*/ break; }//end of switch return (void **)srcArray1; break; }//End of switch }void *Consume( void *arg ) { int i; /* For Debug */static int counter=0; static FILE *fp; MYSHM *ptr=(MYSHM *)arg; while( 1 ) { Pthread_mutex_lock(&ptr->nready_mutex); while (ptr->nready == 0) Pthread_cond_wait(&ptr->cond, &ptr->nready_mutex); ptr->nready--; Pthread_mutex_unlock(&ptr->nready_mutex); /******There is no need for the following line*****/ Pthread_mutex_lock(&ptr->curMesg_mutex); switch( ptr->currentMesg ) { case TYPE_OF_DWXX: printf("Hour:%d,Minute:%d,DESID:%X\n",ptr->dwxx.hour,ptr->dwxx.minute,ptr->dwxx.srcID); newMesg.dwxx = ptr->dwxx; AutoRecord( TYPE_OF_DWXX ); //perror("Error occured for"); /*LoadData(TYPE_OF_DWXX,countDWXX ); perror("Error occured for"); Sort( SORT_BY_ADDRESS, loadedMesg.pLocInfo);*/ /* For Debug*/counter++; if( counter==4) return NULL; printf("counter is %d\n",counter); break; case TYPE_OF_TXXX: //Pthread_mutex_lock(&ptr->TXXX_mutex); printf("SRCID is %X\n",ptr->txxx.srcID); newMesg.txIndex.hour = ptr->txxx.hour; newMesg.txIndex.minute = ptr->txxx.minute;
newMesg.txIndex.srcID = ptr->txxx.srcID;
newMesg.txIndex.contentLen = ptr->txxx.contentLen; newMesg.txIndex.lockSymbol = 0;
newMesg.txIndex.CRC_Sign = ptr->txxx.CRC_Sign;
newMesg.txIndex.chType = ptr->txxx.chType;
newMesg.txIndex.commType = ptr->txxx.commType;
newMesg.txIndex.isSecretKey = ptr->txxx.isSecretKey; printf("***The length of data content is %d*****\n",ptr->txxx.contentLen); if( ptr->txxx.contentLen <= 20 ) { //only stored the whole message in file F_TXINDEX memcpy( newMesg.txIndex.message, ptr->txxx.message,20); newMesg.txIndex.mainIndex = -1; } else { /* divide the message to two parts,one of it is the */ /* content the other part is the indexer of the message */ newMesg.txIndex.mainIndex = AddCountMain( 1 ); /* Add the big data block to file F_TXMAIN*/ if( GetCountMain() <= AUTOROLLVAL_TX) { fp = Fopen( "F_TXMAIN","a+"); Fwrite( &(ptr->txxx.message),MAXSIZE,1,fp ); Fclose( fp ); } else { if( (i = DEQUEUE( INBOX )) != 0) { fp = Fopen( "F_TXMAIN","w+"); fseek( fp, (i-1)*LEN_TXINDEX, SEEK_SET ); Fwrite( &(ptr->txxx.message),MAXSIZE,1,fp ); Fclose( fp ); } else if( i == 0) { printf("****Fail to get data for the empty queue****\n"); } } } AutoRecord( TYPE_OF_TXXX ); /* For Debug*/counter++; if( counter==4) return NULL; printf("counter is %d\n",counter); //Pthread_mutex_unlock(&ptr->TXXX_mutex); break; case TYPE_OF_ICXX: //Pthread_mutex_lock(&ptr->ICXX_mutex); printf("BroadcastID is %X\n",ptr->icxx.broadcastID); /* Print the Value of each member of ICXX */ //Pthread_mutex_unlock(&ptr->ICXX_mutex); break; case TYPE_OF_ZJXX: //Pthread_mutex_lock(&ptr->ZJXX_mutex); printf("ElecQuantity is %f\n",ptr->zjxx.elecQuantity); /* Print the Value of each member of ZJXX */ //Pthread_mutex_unlock(&ptr->ZJXX_mutex); break; case TYPE_OF_SJXX: //Pthread_mutex_lock(&ptr->SJXX_mutex); printf("Year is %X\n",ptr->sjxx.sj.year); /* Print the Value of each member of SJXX */ //Pthread_mutex_unlock(&ptr->SJXX_mutex); break; case TYPE_OF_BBXX: //Pthread_mutex_lock(&ptr->BBXX_mutex); printf("version is %s\n",ptr->bbxx.verInfor); /* Print the Value of each member of BBXX */ //Pthread_mutex_unlock(&ptr->BBXX_mutex); break; case TYPE_OF_FKXX: //Pthread_mutex_lock(&ptr->FKXX_mutex); printf("FeedBackSign is %uc\n",ptr->fkxx.FeedBackSign); /* Print the Value of each member of FKXX */ //Pthread_mutex_unlock(&ptr->FKXX_mutex); break; } /******There is no need for the following line*****/ Pthread_mutex_unlock(&ptr->curMesg_mutex); } return(NULL);}//void TestMainServ( void )int main(int argc, char **argv)//int mainServ( )
{
int i,fd,arryLen; pthread_t tid_produce[MAXNTHREADS], tid_Consume; pthread_mutexattr_t nready_mattr; pthread_mutexattr_t curMesg_mattr; pthread_condattr_t condattr; MYSHM *myshmPtr; struct sched_param param; int priority; int newPrio=20; //if (argc != 2) // err_quit("usage: ./MainServ <name> "); /* open and map shared memory */ //shm_unlink(argv[1]); shm_unlink("knight"); fd = shm_open("knight", O_RDWR | O_CREAT | O_EXCL, FILE_MODE); myshmPtr = mmap(NULL, sizeof( MYSHM ), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); ftruncate(fd, sizeof( MYSHM )); close(fd); myshmPtr->nready = 0 ; /* Use the default properties to initial the mutex lock during the time of data operation */ Pthread_mutex_init(&countDW_Mutex,NULL); Pthread_mutex_init (&(fLock.DW_FLOCK),NULL); Pthread_mutex_init (&(fLock.DWtmp1_FLOCK),NULL); Pthread_mutex_init (&(fLock.DWtmp2_FLOCK),NULL); Pthread_mutex_init(&countTXINDEX_Mutex,NULL); Pthread_mutex_init(&countTXMAIN_Mutex,NULL); Pthread_mutex_init (&(fLock.TXINDEX_FLOCK),NULL); Pthread_mutex_init (&(fLock.TXtmp1_FLOCK),NULL); Pthread_mutex_init (&(fLock.TXtmp2_FLOCK),NULL); /* Initial the attribute of mutex and cond to be shared between the main progress and Daemon */ Pthread_mutexattr_init(&curMesg_mattr ); Pthread_mutexattr_setpshared( &curMesg_mattr,PTHREAD_PROCESS_SHARED ); Pthread_mutex_init(&myshmPtr->curMesg_mutex,&curMesg_mattr); Pthread_mutexattr_destroy( &curMesg_mattr); Pthread_mutexattr_init( &nready_mattr ); Pthread_mutexattr_setpshared( &nready_mattr,PTHREAD_PROCESS_SHARED ); Pthread_mutex_init(&myshmPtr->nready_mutex,&nready_mattr); Pthread_mutexattr_destroy( &nready_mattr); Pthread_condattr_init( &condattr); Pthread_condattr_setpshared( &condattr,PTHREAD_PROCESS_SHARED ); pthread_cond_init(&myshmPtr->cond,&condattr); Pthread_condattr_destroy( &condattr); /*初始化属性*/ pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); memset( &tid_DatProcess, 0, sizeof(tid_DatProcess) ); INITQUEUE( INBOX); INITQUEUE( OUTBOX); Pthread_create(&tid_Consume, &attr, Consume, (void *)myshmPtr); sleep(5 ); loadArg.sType = INBOX; loadArg.mesgType = TYPE_OF_TXXX; loadArg.mesgCount = GetCount( TYPE_OF_TXXX); CheckTidCount(); Pthread_create( &tid_DatProcess[tidCount], &attr, LoadData, NULL); //LoadData(INBOX, TYPE_OF_TXXX,GetCount( TYPE_OF_TXXX) ); printf("Current total number of mesg is %d,Unread ones are %d\n",loadedMesg.countOfMesg,loadedMesg.countOfUnread); printf("*************************First time Load data****************************\n"); /* Debug */printf(" Address of the address array head is %lX.\n",loadedMesg.pCommInfo ); queFilter.srcID = 2606; /* Test Filatrate */ //loadedMesg.pCommInfo=(COMMINFO **)Filtrate(TYPE_OF_TXXX,loadedMesg.countOfMesg, BY_LOCADDRSS,queFilter, (void **)loadedMesg.pCommInfo ); //loadedMesg.pCommInfo=(COMMINFO **)Filtrate(TYPE_OF_TXXX,arryLen, BY_NEWEST3,queFilter, (void **)loadedMesg.pCommInfo ); //PrintForTest( NULL ,loadedMesg.pCommInfo ); //Delete( INBOX, TYPE_OF_TXXX, SINGLEMODE, 2, (void **)loadedMesg.pCommInfo); //Delete( INBOX, TYPE_OF_TXXX, MUTIPLEMODE, 3, (void **)loadedMesg.pCommInfo); //UnloadData( INBOX, TYPE_OF_TXXX ); sleep(5); loadArg.sType = INBOX; loadArg.mesgType = TYPE_OF_TXXX; loadArg.mesgCount = GetCount( TYPE_OF_TXXX); CheckTidCount(); Pthread_create( &tid_DatProcess[tidCount], &attr, LoadData, NULL); //LoadData( INBOX, TYPE_OF_TXXX,GetCount( TYPE_OF_TXXX) ); printf("*************************Second time Load data****************************\n"); //printf("******************Delete mutiple mode************************\n"); sleep(5); exit(0);
}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -