📄 book.c
字号:
enroll('A', 'E', store_p->id, order_result); if (order_result <= LACK_CTRL) reprint('B', store_p->id, LACK_CTRL); else reprint('B', store_p->id, MANEGE); } break; case 0: break; default : enroll('A', 'E', store_p->id, order_result); if (order_result <= LACK_CTRL) reprint('A', store_p->id, LACK_CTRL); else reprint('A', store_p->id, MANEGE); break; } } store_p = store_p->next; } free(store_p); }}/*C_auto_sell*/void C_auto_sell(void){ FILE *fp; struct book *store_p; time_t time_sell; int sell_num; int sell_num_real; while (1) { sleep(AUTO_SELL_DELAY); /*store_p = (struct book *)malloc(LEN);*/ store_p = head_C; while (store_p != NULL) { if (strcmp(store_p->sort, "science ") == 0) sell_num = SELL_SUM_MAX; else sell_num = SELL_SUM_MIN; if (store_p->num > 0) { if (store_p->num <= sell_num) { sell_num_real = store_p->num; pthread_mutex_lock(&mutex_C); store_p->num = 0; time(&time_sell); pthread_mutex_unlock(&mutex_C); }else { sell_num_real = sell_num; pthread_mutex_lock(&mutex_C); store_p->num -= sell_num; time(&time_sell); pthread_mutex_unlock(&mutex_C); } fp = fopen("work.txt", "a"); pthread_mutex_lock(&mutex_work); fprintf(fp, "storeC sell book%d %s %d remain %d at %s", store_p->id, store_p->sort, sell_num_real, store_p->num, ctime(&time_sell)); pthread_mutex_unlock(&mutex_work); fclose(fp); fp = fopen("work~.txt", "a"); pthread_mutex_lock(&mutex_work_tmp); fprintf(fp, "storeC sell book%d %s %d remain %d at %s", store_p->id, store_p->sort, sell_num_real, store_p->num, ctime(&time_sell)); pthread_mutex_unlock(&mutex_work_tmp); fclose(fp); }/***/ store_p = store_p->next; } free(store_p); }}/*D_auto_sell*/void D_auto_sell(void){ FILE *fp; struct book *store_p; time_t time_sell; int sell_num; int sell_num_real; while (1) { sleep(AUTO_SELL_DELAY); store_p = (struct book *)malloc(LEN); store_p = head_D; while (store_p != NULL) { if (strcmp(store_p->sort, "forigen ") == 0) sell_num = SELL_SUM_MAX; else sell_num = SELL_SUM_MIN; if (store_p->num > 0) { if (store_p->num <= sell_num) { sell_num_real = store_p->num; pthread_mutex_lock(&mutex_D); store_p->num = 0; time(&time_sell); pthread_mutex_unlock(&mutex_D); }else { sell_num_real = sell_num; pthread_mutex_lock(&mutex_D); store_p->num -= sell_num; time(&time_sell); pthread_mutex_unlock(&mutex_D); } fp = fopen("work.txt", "a"); pthread_mutex_lock(&mutex_work); fprintf(fp, "storeD sell book%d %s %d remain %d at %s", store_p->id, store_p->sort, sell_num_real, store_p->num, ctime(&time_sell)); pthread_mutex_unlock(&mutex_work); fclose(fp); fp = fopen("work~.txt", "a"); pthread_mutex_lock(&mutex_work_tmp); fprintf(fp, "storeD sell book%d %s %d remain %d at %s", store_p->id, store_p->sort, sell_num_real, store_p->num, ctime(&time_sell)); pthread_mutex_unlock(&mutex_work_tmp); fclose(fp); } store_p = store_p->next; } free(store_p); }}/*storeC*/void storeC(void){ pthread_t C_auto_order_id; pthread_t C_auto_sell_id; pthread_create(&C_auto_order_id, NULL, (void *)C_auto_order, NULL); pthread_create(&C_auto_sell_id, NULL, (void *)C_auto_sell, NULL);}/*storeD*/void storeD(void){ pthread_t D_auto_order_id; pthread_t D_auto_sell_id; pthread_create(&D_auto_order_id, NULL, (void *)D_auto_order, NULL); pthread_create(&D_auto_sell_id, NULL, (void *)D_auto_sell, NULL);}/*storeE*/void storeE(void){ pthread_t E_auto_order_id; pthread_create(&E_auto_order_id, NULL, (void *)E_auto_order, NULL);}/*buyerF*/void buyerF(void){ FILE *fp; int buy_sign; time_t time_buy; struct book *store_p; struct book *buy_p; while (1) { sleep (F_BUY_DELAY); store_p = (struct book *)malloc(LEN); buy_p = (struct book *)malloc(LEN); store_p = head_E; while (store_p != NULL) { if ((store_p->sign != F_BUY) && (store_p->sign != ALL_BUY)) { if (store_p->sign == G_BUY) buy_sign = ALL_BUY; else buy_sign = F_BUY; if (store_p->num > 0) { if ((strcmp(store_p->sort, "science ") == 0) || (strcmp(store_p->sort, "sports ") == 0) || (strcmp(store_p->sort, "life ") == 0)) { buy_p->sign = store_p->sign; buy_p->id = store_p->id; buy_p->num = 1; buy_p->next = NULL; pthread_mutex_lock(&mutex_E); store_p->num--; store_p->sign = buy_sign; time(&time_buy); pthread_mutex_unlock(&mutex_E); pthread_mutex_lock(&mutex_F); if (head_F->sign == 0) { buy_p->next = head_F->next; head_F = buy_p; } else { buy_p->next = head_F; head_F = buy_p; } pthread_mutex_unlock(&mutex_F); fp = fopen("work.txt", "a"); pthread_mutex_lock(&mutex_work); fprintf(fp, "buyerF buy book%d %s 1 from storeE remain 1 at %s", buy_p->id, buy_p->sort, ctime(&time_buy)); pthread_mutex_unlock(&mutex_work); fclose(fp); fp = fopen("work~.txt", "a"); pthread_mutex_lock(&mutex_work_tmp); fprintf(fp, "buyerF buy book%d %s 1 from storeE remain 1 at %s", buy_p->id, buy_p->sort, ctime(&time_buy)); pthread_mutex_unlock(&mutex_work_tmp); fclose(fp); } } } store_p = store_p->next; } free(store_p); free(buy_p); }}/*buyerG*/void buyerG(void){ FILE *fp; int buy_sign; int buy_num; time_t time_buy; int delay_buy; struct book *store_p; struct book *buy_p; while (1) { delay_buy = (rand()%4)+1; /*delay a random time whthin 5*/ sleep(delay_buy); store_p = (struct book *)malloc(LEN); buy_p = (struct book *)malloc(LEN); store_p = head_E; while (store_p != NULL) { buy_num = (rand()%499)+1; /*buy a random number within 500*/ if ((store_p->sign != G_BUY) && (store_p->sign != ALL_BUY)) { if (store_p->sign == F_BUY) buy_sign = ALL_BUY; else buy_sign = G_BUY; if (store_p->num > 0) { if (store_p->num <= buy_num) { buy_p->sign = store_p->sign; buy_p->id = store_p->id; strcpy(buy_p->sort, store_p->sort); buy_p->num = store_p->num; buy_p->next = NULL; pthread_mutex_lock(&mutex_E); store_p->num = 0; store_p->sign = buy_sign; time(&time_buy); pthread_mutex_unlock(&mutex_E); } else { buy_p->sign = store_p->sign; buy_p->id = store_p->id; strcpy(buy_p->sort, store_p->sort); buy_p->num = buy_num; buy_p->next = NULL; pthread_mutex_lock(&mutex_E); store_p->num -= buy_num; store_p->sign = buy_sign; time(&time_buy); pthread_mutex_unlock(&mutex_E); } pthread_mutex_lock(&mutex_G); if (head_G->sign == 0) { buy_p->next = head_G->next; head_G = buy_p; } else { buy_p->next = head_G; head_G = buy_p; } pthread_mutex_unlock(&mutex_G); fp = fopen("work.txt", "a"); pthread_mutex_lock(&mutex_work); fprintf(fp, "buyerG buy book%d %s %d from storeE remain %d at %s", buy_p->id, buy_p->sort, buy_p->num, buy_p->num, ctime(&time_buy)); pthread_mutex_unlock(&mutex_work); fclose(fp); fp = fopen("work~.txt", "a"); pthread_mutex_lock(&mutex_work_tmp); fprintf(fp, "buyerG buy book%d %s %d from storeE remain %d at %s", buy_p->id, buy_p->sort, buy_p->num, buy_p->num, ctime(&time_buy)); pthread_mutex_unlock(&mutex_work_tmp); fclose(fp); } } store_p = store_p->next; } free(store_p); free(buy_p); }}/*check*/void check(void){ FILE *fp; char ch; while (1) { sleep(CHECKOUT); pthread_mutex_lock(&mutex_work_tmp); printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"); fp = fopen("work~.txt", "r"); ch = fgetc(fp); while (ch != EOF) { putchar(ch); ch = fgetc(fp); } fclose(fp); fp = fopen("work~.txt", "w"); fclose(fp); pthread_mutex_unlock(&mutex_work_tmp); }}/*main*/int main(void){ pthread_t A_id; pthread_t B_id; pthread_t C_id; pthread_t D_id; pthread_t E_id; pthread_t F_id; pthread_t G_id; pthread_t check_id; FILE *fp; fp = fopen("book.txt", "w"); fclose(fp); fp = fopen("work.txt", "w"); fclose(fp); fp = fopen("work~.txt", "w"); fclose(fp); head_A = (struct book *)malloc(LEN); head_B = (struct book *)malloc(LEN); head_C = (struct book *)malloc(LEN); head_D = (struct book *)malloc(LEN); head_E = (struct book *)malloc(LEN); head_F = (struct book *)malloc(LEN); head_G = (struct book *)malloc(LEN); lack_A = (struct lack_info *)malloc(LEN_LACK); lack_B = (struct lack_info *)malloc(LEN_LACK); head_A->next = NULL; head_B->next = NULL; head_C->next = NULL; head_D->next = NULL; head_E->next = NULL; head_F->next = NULL; head_G->next = NULL; lack_A->next = NULL; lack_B->next = NULL; pthread_mutex_init(&mutex_book, NULL); pthread_mutex_init(&mutex_work, NULL); pthread_mutex_init(&mutex_work_tmp, NULL); pthread_mutex_init(&mutex_A, NULL); pthread_mutex_init(&mutex_B, NULL); pthread_mutex_init(&mutex_C, NULL); pthread_mutex_init(&mutex_D, NULL); pthread_mutex_init(&mutex_E, NULL); pthread_mutex_init(&mutex_F, NULL); pthread_mutex_init(&mutex_G, NULL); pthread_mutex_init(&mutex_lack_A, NULL); pthread_mutex_init(&mutex_lack_B, NULL); pthread_create(&check_id, NULL, (void *)check, NULL); pthread_create(&A_id, NULL, (void *)pressA, NULL); pthread_create(&B_id, NULL, (void *)pressB, NULL); pthread_create(&C_id, NULL, (void *)storeC, NULL); pthread_create(&D_id, NULL, (void *)storeD, NULL); pthread_create(&E_id, NULL, (void *)storeE, NULL); pthread_create(&F_id, NULL, (void *)buyerF, NULL); pthread_create(&G_id, NULL, (void *)buyerG, NULL); sleep(TIMEOUT); pthread_cancel(A_id); pthread_cancel(B_id); pthread_cancel(C_id); pthread_cancel(D_id); pthread_cancel(E_id); pthread_cancel(F_id); pthread_cancel(G_id); pthread_cancel(check_id); free(head_A); free(head_B); free(head_C); free(head_D); free(head_E); free(head_F); free(head_G); free(lack_A); free(lack_B); return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -