⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 book.c

📁 一个模拟图书经营系统的程序,在linux的gcc环境下编译的,利用了多线程的pthread.h函数,希望对大家在linux下编写程序有所借鉴!
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <stdio.h>#include <malloc.h>#include <pthread.h>#include <string.h>#include <time.h>#include <math.h>#define TIMEOUT 8                     /*the main thread's whole life time*/#define CHECKOUT 10#define PRINT_SUM 1000#define LACK_CTRL 500#define LEN sizeof(struct book)#define LEN_LACK sizeof(struct lack_info)#define WHOLE 0#define ERROR -1#define MANEGE -1#define NEW 1#define REPRINT 2#define OLD 3#define F_BUY 5          /*the sign of F have buy*/#define G_BUY 7          /*the sign of G have buy*/#define ALL_BUY 12       /*F and G all have buy*/#define PRINT_DELAY_MIN 2#define PRINT_DELAY_MAX 5#define AUTO_ORDER_DELAY 1#define AUTO_SELL_DELAY 1#define F_BUY_DELAY 1#define BOOK_SUM_MAX 500#define BOOK_SUM_MIN 200#define SELL_SUM_MAX 100#define SELL_SUM_MIN 20struct book{ int sign; int id; char sort[10]; int num; struct book *next;};struct lack_info{ char store_id; int book_id; int lack_num; struct lack_info *next;};struct book *head_A;struct book *head_B;struct book *head_C;struct book *head_D;struct book *head_E;struct book *head_F;struct book *head_G;struct lack_info *lack_A;struct lack_info *lack_B;char *bnamA[] = {"science  ", "forigen  ", "society  ", "hygiene  ", "life      ", "sports   "};char *bnamB[] = {"science  ", "society  ", "hygiene  ", "life      ", "sports   ", "education"};int book_id = 1;                       /*book'ID just as book's name*/pthread_mutex_t mutex_book;pthread_mutex_t mutex_work;pthread_mutex_t mutex_work_tmp;pthread_mutex_t mutex_A;pthread_mutex_t mutex_B;pthread_mutex_t mutex_C;pthread_mutex_t mutex_D;pthread_mutex_t mutex_E;pthread_mutex_t mutex_F;pthread_mutex_t mutex_G;pthread_mutex_t mutex_lack_A;pthread_mutex_t mutex_lack_B;/*call_store*/void call_store(char press_id, char store_id, int book_id, int book_num){ FILE *fp; time_t time_book; struct book *press_p; struct book *store_p; pthread_mutex_t mutex_press;  press_p = (struct book *)malloc(LEN); store_p = (struct book *)malloc(LEN); if (press_id == 'A') {  press_p = head_A;  mutex_press = mutex_A; }  if (press_id == 'B') {  press_p = head_B;  mutex_press = mutex_B; }  while (press_p != NULL) {  if (press_p->id == book_id)  {   store_p->sign = press_p->sign;   pthread_mutex_lock(&mutex_press);   press_p->sign = OLD;   press_p->num -= book_num;   time(&time_book);   pthread_mutex_unlock(&mutex_press);   store_p->id = press_p->id;   strcpy(store_p->sort, press_p->sort);   store_p->num = book_num;   break;  }  press_p = press_p->next; }   switch (store_id) {  case 'C': 	 pthread_mutex_lock(&mutex_C);		 if (head_C->id == 0)		 {		  store_p->next = head_C->next; 		  head_C = store_p;/******HERE!***********/		 } 		 else		 { 		 store_p->next = head_C;		 head_C = store_p;		 } 		 pthread_mutex_unlock(&mutex_C);		 break;  case 'D': 	 pthread_mutex_lock(&mutex_D);		 if (head_D->id == 0) 		 { 		  store_p->next = head_D->next;		  head_D = store_p; 		 } 		  else		 {		  store_p->next = head_D;		  head_D = store_p;		 }		 pthread_mutex_unlock(&mutex_D);		 break;  case 'E': 	 pthread_mutex_lock(&mutex_E);		 if (head_E->id == 0)		 {		  store_p->next = head_E->next;		  head_E = store_p;		 }		 else		 {		  store_p->next = head_E;		  head_E = store_p;		 } 		 pthread_mutex_unlock(&mutex_E);    		 break;  default: break; } fp = fopen("work.txt", "a"); pthread_mutex_lock(&mutex_work); fprintf(fp, "store%c newOrder book%d %s %d from press%c remain %d at %s", store_id, book_id, press_p->sort, book_num, press_id, store_p->num, ctime(&time_book)); pthread_mutex_unlock(&mutex_work); fclose(fp); fp = fopen("work~.txt", "a"); pthread_mutex_lock(&mutex_work_tmp); fprintf(fp, "store%c newOrder book%d %s %d from press%c remain %d at %s", store_id, book_id, press_p->sort, book_num, press_id, store_p->num, ctime(&time_book)); pthread_mutex_unlock(&mutex_work_tmp); fclose(fp);}/*A_print*/void A_print(void){ struct book *print_p; FILE *fp; time_t time_print; int book_ID; int i;  while (1) {  for (i = 0; i < 6; i++)  {   print_p = (struct book *)malloc(LEN);   print_p->sign = NEW;   print_p->id = book_id;   strcpy(print_p->sort, bnamA[i]);   print_p->num = PRINT_SUM;   pthread_mutex_lock(&mutex_A);   if (head_A->sign == 0)   {    print_p->next = head_A->next;    head_A = print_p;   }    else   {    print_p->next = head_A;    head_A = print_p;   }    time(&time_print);   book_ID = book_id;   book_id++;   pthread_mutex_unlock(&mutex_A);   fp = fopen("book.txt", "a");   pthread_mutex_lock(&mutex_book);   fprintf(fp, "PressA newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_book);      fclose(fp);   fp = fopen("work.txt", "a");   pthread_mutex_lock(&mutex_work);   fprintf(fp, "PressA newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_work);      fclose(fp);   fp = fopen("work~.txt", "a");   pthread_mutex_lock(&mutex_work_tmp);   fprintf(fp, "PressA newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_work_tmp);      fclose(fp);   /***********/   print_p = head_A;   while (print_p != NULL)   {    printf("-A-%d %d %s %d--\n", print_p->sign, print_p->id, print_p->sort, print_p->num);    print_p = print_p->next;   }   /**************/   switch (i)   {    case 0: call_store('A', 'C', book_ID, BOOK_SUM_MAX); 	   	   call_store('A', 'D', book_ID, BOOK_SUM_MIN); 	   	   call_store('A', 'E', book_ID, BOOK_SUM_MIN);	   	   break;    case 1: call_store('A', 'C', book_ID, BOOK_SUM_MIN); 	   	   call_store('A', 'D', book_ID, BOOK_SUM_MAX);   	   	   call_store('A', 'E', book_ID, BOOK_SUM_MIN);	   	   break;    case 2: call_store('A', 'D', book_ID, BOOK_SUM_MIN);	   	   call_store('A', 'E', book_ID, BOOK_SUM_MIN);	   	   break;    case 3: call_store('A', 'E', book_ID, BOOK_SUM_MIN); break;    case 4: call_store('A', 'E', book_ID, BOOK_SUM_MIN); break;    case 5: call_store('A', 'E', book_ID, BOOK_SUM_MIN); break;    default: break; 	   }   free(print_p);   if (book_id < 22)sleep(PRINT_DELAY_MIN);   else sleep(PRINT_DELAY_MAX); 	   } }}/*B_print*/void B_print(void){ struct book *print_p; FILE *fp; time_t time_print; int book_ID; int i;  while (1) {  for (i = 0; i < 6; i++)  {   print_p = (struct book *)malloc(LEN);   print_p->sign = NEW;   print_p->id = book_id;   strcpy(print_p->sort, bnamB[i]);   print_p->num = PRINT_SUM;   pthread_mutex_lock(&mutex_B);   if (head_B->sign == 0)   {    print_p->next = head_B->next;    head_B = print_p;   }    else   {    print_p->next = head_B;    head_B = print_p;   }    time(&time_print);   book_ID = book_id;   book_id++;   pthread_mutex_unlock(&mutex_B);   fp = fopen("book.txt", "a");   pthread_mutex_lock(&mutex_book);   fprintf(fp, "PressB newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_book);      fclose(fp);   fp = fopen("work.txt", "a");   pthread_mutex_lock(&mutex_work);   fprintf(fp, "PressB newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_work);      fclose(fp);   fp = fopen("work~.txt", "a");   pthread_mutex_lock(&mutex_work_tmp);   fprintf(fp, "PressB newPrint book%d %s %d remain %d at %s", print_p->id, print_p->sort, print_p->num, print_p->num, ctime(&time_print));   pthread_mutex_unlock(&mutex_work_tmp);   fclose(fp);   /***********/   print_p = head_B;   while (print_p != NULL)   {    printf("-B-%d %d %s %d--\n", print_p->sign, print_p->id, print_p->sort, print_p->num);    print_p = print_p->next;   }   /**************/   switch (i)   {    case 0: call_store('B', 'C', book_ID, BOOK_SUM_MAX);	       call_store('B', 'D', book_ID, BOOK_SUM_MIN);   	       call_store('B', 'E', book_ID, BOOK_SUM_MIN);	       break;    case 1: call_store('B', 'D', book_ID, BOOK_SUM_MIN);	       call_store('B', 'E', book_ID, BOOK_SUM_MIN);	 	   break;    case 2: call_store('B', 'E', book_ID, BOOK_SUM_MIN); break;    case 3: call_store('B', 'E', book_ID, BOOK_SUM_MIN); break;    case 4: call_store('B', 'E', book_ID, BOOK_SUM_MIN); break;    case 5: call_store('B', 'C', book_ID, BOOK_SUM_MIN);	  	   call_store('B', 'D', book_ID, BOOK_SUM_MIN);	  	   break;    default: break; 	   }   free(print_p);   if (book_id < 22)sleep(PRINT_DELAY_MIN);   else sleep(PRINT_DELAY_MAX); 	   } }}/*pressA*/void pressA(void){ pthread_t A_print_id;  pthread_create(&A_print_id, NULL, (void *)A_print, NULL);}/*pressB*/void pressB(void){ pthread_t B_print_id;  pthread_create(&B_print_id, NULL, (void *)B_print, NULL);}/*inquire*/struct book *inquire(char press_id, int inq_mode, char *book_sort){ struct book *head; struct book *press_p; struct book *inq_p;  head = (struct book *)malloc(LEN); inq_p = (struct book *)malloc(LEN); head->next = NULL; if (press_id == 'A') press_p = head_A; if (press_id == 'B') press_p = head_B; if (inq_mode == WHOLE) {  while (press_p != NULL)  {   if (strcmp(press_p->sort, book_sort) == 0)   {    inq_p = press_p;    inq_p->next = head->next;    head = inq_p;   }   press_p = press_p->next;  }   }else {  while (press_p != NULL)  {   if ((press_p->sign == inq_mode) && (strcmp(book_sort, press_p->sort) == 0))   {	inq_p = press_p;    inq_p->next = head->next;    head = inq_p;   }    press_p = press_p->next;  }  }	 free(inq_p); return(head);}/*enroll*/void enroll(char press_id, char store_id, int book_id, int lack_num){ struct lack_info *p; struct lack_info *lack_p;  printf("Enroll: %c %c %d %d\n", press_id, store_id, book_id, lack_num); lack_p = (struct lack_info *)malloc(LEN_LACK); lack_p->store_id = store_id; lack_p->book_id = book_id; lack_p->lack_num = lack_num; lack_p->next = NULL; if (press_id == 'A') {  printf("/*Enroll into A*/\n");  pthread_mutex_lock(&mutex_lack_A);  if (lack_A->book_id == 0)  {   lack_p->next = lack_A->next;   lack_A = lack_p;   printf("/*%c %d %d*/\n", lack_p->store_id, lack_p->book_id, lack_p->lack_num);  }   else  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -