void main(void) { int k; Queue* q = qCreate(10); for (int i = 0; i < 16; ++i) { qEnqueue(q, i); } while (!qEmpty(q)) { printf("%d ", qDequeue(q)); printf(" (size left = %d)\n", qSize(q)); } for (int i = 0; i < 16; ++i) { qEnqueue(q, i); printf("(size = %d)\n", qSize(q)); } Stack s; stackCreate(&s, 128); for (int i = 0; i < 255; ++i) { stackPush(&s, i); } while (!stackEmpty(&s)) { printf("%d ", stackTop(&s)); stackPop(&s); } }
status_t createQueueSockInfo(void* i_SqsockInfo) { QSOCKINFO* SqsockInfo = (QSOCKINFO*)i_SqsockInfo; status_t status; status=EOK; SqsockInfo->readQueue =NULL; SqsockInfo->writeQueue = NULL; SqsockInfo->readQueue = qCreate(&status, "readQueue", 0xFF); if (SqsockInfo->readQueue == NULL) printLog("readQueue Create Fail..\n"); else{ SqsockInfo->writeQueue = qCreate(&status, "writeQueue", 0xFF); if (SqsockInfo->writeQueue == NULL) printLog("writeQueue Create Fail..\n"); } SqsockInfo->iUartState = 0; return status; }
// Initializes a queue and a registry, puts them into a handle, and returns the handle void * createEventLoop( void ){ EventQueue * queue1 = qCreate( ); HashTable * tab1 = createHashTable( TABLESIZE ); Handle * hand = calloc( 1, sizeof( Handle ) ); if( pthread_mutex_init( &hand->mu, NULL ) != 0){ fprintf( stderr, "Cannot instantiate mutex\n" ); exit( -1 ); } if( pthread_cond_init( &hand->cv, NULL ) != 0 ){ fprintf( stderr, "Cannot instantiate condition variable\n" ); exit( -1 ); } hand->cue = queue1; hand->registry = tab1; hand->terminated = 0; return ( void * )hand; }
// Main function int main(int argc,char* argv[]) { int iListenFd, iClientFd; int iPort; status_t status; pthread_t recvThreadId; pthread_t sendThreadId; pthread_t commandThreadId; pthread_t ipcHandlerId; /* pthread_t posionerThId; pthread_t trackerThId; pthread_t gpsThId; pthread_t modemThId; */ QSOCKINFO *SqsockInfo; // Server socket addr struct sockaddr_in SsockaddrServer; // Client socket addr struct sockaddr_in SsockaddrClient; socklen_t socklenSize; if(argc != 2) errexit("Usage : %s [PORT]:\n", argv[0]); iPort = atoi(argv[1]); if(iPort<0 || iPort >65536) errexit("PORT RANGE (1 ~ 65535)\n"); system("echo 0 > /tmp/trackerSw"); socklenSize = sizeof(struct sockaddr_in); printLog("[SERVER] creating passive socket..\n"); iListenFd = passiveSock(&SsockaddrServer, iPort); printLog("[SERVER] passive socket %d created.\n", iListenFd); while(1) { // connect to first client, create temporary socket printLog("[SERVER] waiting for client..\n"); if ((iClientFd = accept(iListenFd, (struct sockaddr*)&SsockaddrClient, &socklenSize)) < 0) errexit("accept failed: %s\n", strerror(errno)); printLog("[SERVER] servant socket %d created.\n", iClientFd); // Initial client information struct SqsockInfo = (QSOCKINFO*)malloc(sizeof(QSOCKINFO)); SqsockInfo->iSock = iClientFd; fprintf(stderr,"### %s():%d ###\n",__func__,__LINE__); SqsockInfo->writeQueue=NULL; SqsockInfo->readQueue=NULL; //소켓정보와 큐 정보생성 및 관리 변수에 할당 SqsockInfo->writeQueue = qCreate(&status, "WtcpIp", 0xFF); if (SqsockInfo->writeQueue == NULL){ close(iClientFd); close(iListenFd); errexit("create TCP IP Write Queue fail.\n"); } SqsockInfo->readQueue = qCreate(&status, "RtcpIp", 0xFF); if (SqsockInfo->readQueue == NULL){ close(iClientFd); close(iListenFd); errexit("create TCP IP Read Queue fail.\n"); } //NOTE: threads sharing the memory, client variable cannot reuse memcpy(&SqsockInfo->SsockaddrClient, &SsockaddrClient, sizeof(SsockaddrClient)); // commandGroup 쓰레드 생성 if(pthread_create(&recvThreadId, NULL, recvCommand, (void*)SqsockInfo)) errexit("create recv thread failed: %s\n", strerror(errno)); //쓰레드와 메인 분리 pthread_detach(recvThreadId); // commandGroup 쓰레드 생성 if(pthread_create(&commandThreadId, NULL, commandGroup, (void*)SqsockInfo)) errexit("create command thread failed: %s\n", strerror(errno)); //쓰레드와 메인 분리 pthread_detach(commandThreadId); // commandGroup 쓰레드 생성 if(pthread_create(&sendThreadId, NULL, sendCommandResult, (void*)SqsockInfo)) errexit("create send thread failed: %s\n", strerror(errno)); //쓰레드와 메인 분리 pthread_detach(sendThreadId); // Posioner 쓰레드 생성 if(pthread_create(&ipcHandlerId, NULL, ipcHandlerFunction, (void*)SqsockInfo)) errexit("create send thread failed: %s\n", strerror(errno)); //쓰레드와 메인 분리 pthread_detach(ipcHandlerId); } close(iListenFd); return 0; }
/* Create a stack */ void stackCreate(Stack *stack, int maxSize) { stack->q = qCreate(maxSize); }