// ********************************************************* void *limitWorker(void *arg){ char none; while (1) { none = 0; signalWait(lim); pthread_mutex_lock(lsq->mut); pthread_mutex_lock(lbq->mut); pthread_mutex_lock(price_mut); if((lsq->empty == 0) && (lbq->empty == 0) && (none == 0)) { if ((currentPriceX10 >= lsq->item[lsq->head].price1) && (currentPriceX10 <= lbq->item[lbq->head].price1)) { llPairDelete(lsq,lbq); none = 1; } } pthread_mutex_unlock(price_mut); pthread_mutex_unlock(lbq->mut); pthread_mutex_unlock(lsq->mut); if (none == 1) signalSend(slimit); fflush(log_file); } return arg; }
/* * This is the worker thread that manages the stop orders. * It checks if the current price if suitable so it can convert * the order to a limit order. * If after 2 tries it hasn't converted any orders it waits for a signal * that the current price has changed in order to try again. */ void *stopWorker(void *arg){ order o1; char tr = 0; while(1){ tr++; pthread_mutex_lock(price_mut); pthread_mutex_lock(ssq->mut); if(ssq->empty == 0 && currentPriceX10 <= ssq->item[ssq->head].price1 ){ pthread_mutex_unlock(ssq->mut); pthread_mutex_unlock(price_mut); qSafeDelete(ssq,&o1); o1.type = 'M'; o1.timestamp = getTimestamp(); qSafeAdd(msq,o1); tr = 0; } else{ pthread_mutex_unlock(ssq->mut); pthread_mutex_unlock(price_mut); } pthread_mutex_lock(price_mut); pthread_mutex_lock(sbq->mut); if(sbq->empty == 0 && currentPriceX10 >= sbq->item[sbq->head].price1 ){ pthread_mutex_unlock(sbq->mut); pthread_mutex_unlock(price_mut); qSafeDelete(sbq,&o1); o1.type = 'M'; o1.timestamp = getTimestamp(); qSafeAdd(mbq,o1); tr = 0; } else { pthread_mutex_unlock(sbq->mut); pthread_mutex_unlock(price_mut); } /* * If no convertion has happened for 2 iterations we * wait for a signal that the current stock price has changed. */ if(tr >= 2){ signalWait(slimit); } } return arg; }
void *thread_operation(void *i){ float d; boolean finish = FALSE; int k = (int)i+1; printf("\nThread %2d: Starting\n", k); // while(!finish){ FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; char fileName[11]; sprintf(fileName, "%s%d%s", "thread", k, ".txt"); fp = fopen(&fileName, "r"); while ((!finish && (read = getline(&line, &len, fp)) != -1)) { /* Sleep random time (0.1~1s)*/ d = (float)(rand() % 900000 + 100000); // printf("sleep: %f\n", d); usleep(d); //printf("command: %s, the length is %d\n", line, strlen(line)); if(strncmp(line,"quit",4)==0){ /*quit operation*/ hangUp[k-1] = 1; finish = TRUE; }else if(strncmp(line,"send",4)==0){ /*send operation*/ pthread_mutex_lock(&lock_it); printf("\n-----Thread %d enters monitor for sending-----",k); /*In the case the buffer is full*/ if(pool.num == SIZE){ printf("\n-----Thread %d hangs because of full buffer-----",k); hangUp[k-1] = 1; //printf("\nThe hanup: %d-%d-%d-%d",hangUp[0],hangUp[1],hangUp[2],hangUp[3]); deadLock = deadLockDetect(hangUp); if(deadLock){ printf("\n\n!!!!! Deadlock! All threads try to put message into currently full buffer!!!!!\n"); printf("\nProgram Terminates!\n"); finish = TRUE; exit(1); } pthread_cond_wait(&write_it,&lock_it); } hangUp[k-1] = 0; /*put the message into the buffer*/ pool = messageAdd(k,line,pool); messageSent(dstID(line)); pool.counter[dstID(line)-1]++; printf("\nSend notification to thread %d",dstID(line)); pthread_mutex_unlock(&lock_it); printf("\n-----Thread %d leaves monitor after sending-----",k); }else if(strncmp(line,"receive",7)==0){ /*receive operation*/ pthread_mutex_lock(&lock_it); printf("\n-----Thread %d enters monitor for receiving-----",k); /*wait for the message sent to me*/ if(pool.counter[k-1] == 0){ printf("\n-----Thread %d waits because of no meesage for it-----",k); hangUp[k-1] = 1; deadLock = deadLockDetect(hangUp); if(deadLock){ printf("\n\n!!!!! Deadlock! Buffer is occupied by messages that no one needs!!!!!\n "); printf("\nProgram Terminates!\n"); finish = TRUE; exit(1); } signalWait(k); } hangUp[k-1] = 0; pool = messageTake(k,pool); pool.counter[k-1]--; pthread_mutex_unlock(&lock_it); printf("\n-----Thread %d leaves monitor after receiving-----",k); }else{ /*Error command file*/ printf("Something wrong with the command file\n"); finish = TRUE; } } return NULL; }