??????????????????????????????????????????
1 //????????????
2 void InitQue(PT_QUEUE ptQue)
3 {
4     memset(ptQue?? 0?? sizeof(*ptQue));
5 }
6
7 //??????????в??????
8 void EnterQue(PT_QUEUE ptQue?? int dwElem)
9 {
10     if(IsQueFull(ptQue))
11     {
12         printf("Elem %d cannot enter Queue %p(Full)! "?? dwElem?? ptQue);
13         return;
14     }
15     ptQue->aData[ptQue->dwTail]= dwElem;
16     ptQue->dwTail = (ptQue->dwTail + 1) % QUEUE_SIZE;
17 }
18
19 //?????????????????
20 int LeaveQue(PT_QUEUE ptQue)
21 {
22     if(IsQueEmpty(ptQue))
23     {
24         printf("Queue %p is Empty! "?? ptQue);
25         return -1;
26     }
27     int dwElem = ptQue->aData[ptQue->dwHead];
28     ptQue->dwHead = (ptQue->dwHead + 1) % QUEUE_SIZE;
29     return dwElem;
30 }
31
32 //?????????β???????????????
33 void DisplayQue(PT_QUEUE ptQue)
34 {
35     if(IsQueEmpty(ptQue))
36     {
37         printf("Queue %p is Empty! "?? ptQue);
38         return;
39     }
40
41     printf("Queue Element: ");
42     int dwIdx = ptQue->dwHead;
43     while((dwIdx % QUEUE_SIZE) != ptQue->dwTail)
44         printf("%d "?? ptQue->aData[(dwIdx++) % QUEUE_SIZE]);
45
46     printf(" ");
47 }
??????????п??????????????????????
1 //?ж??????????????
2 int IsQueEmpty(PT_QUEUE ptQue)
3 {
4     return ptQue->dwHead == ptQue->dwTail;
5 }
6
7 //?ж??????????????
8 int IsQueFull(PT_QUEUE ptQue)
9 {
10     return (ptQue->dwTail + 1) % QUEUE_SIZE == ptQue->dwHead;
11 }
12
13 //????????????????
14 int QueDataNum(PT_QUEUE ptQue)
15 {
16     return (ptQue->dwTail - ptQue->dwHead + QUEUE_SIZE) % QUEUE_SIZE;
17 }
18
19 //?????????ж???λ??
20 int GetQueHead(PT_QUEUE ptQue)
21 {
22     return ptQue->dwHead;
23 }
24 //?????????ж??????
25 int GetQueHeadData(PT_QUEUE ptQue)
26 {
27     return ptQue->aData[ptQue->dwHead];
28 }
29 //?????????ж?βλ??
30 int GetQueTail(PT_QUEUE ptQue)
31 {
32     return ptQue->dwTail;
33 }
?????????QueueTest()????????????к???????
1 static T_QUEUE gtQueue;
2 void QueueTest(void)
3 {
4     InitQue(&gtQueue);
5     printf("Enter Queue 1??2??3??4??5... ");
6     EnterQue(&gtQueue?? 1);
7     EnterQue(&gtQueue?? 2);
8     EnterQue(&gtQueue?? 3);
9     EnterQue(&gtQueue?? 4);
10     EnterQue(&gtQueue?? 5);
11     printf("Queue Status: Empty(%d)?? Full(%d) "?? IsQueEmpty(&gtQueue)?? IsQueFull(&gtQueue));
12     printf("Queue Elem Num: %u "?? QueDataNum(&gtQueue));
13     printf("Head: %u(%d) "?? GetQueHead(&gtQueue)?? GetQueHeadData(&gtQueue));
14     printf("Tail: %u "?? GetQueTail(&gtQueue));
15     DisplayQue(&gtQueue);
16
17     printf(" Leave Queue... ");
18     printf("Leave %d "?? LeaveQue(&gtQueue));
19     printf("Leave %d "?? LeaveQue(&gtQueue));
20     printf("Leave %d "?? LeaveQue(&gtQueue));
21     DisplayQue(&gtQueue);
22
23     printf(" Enter Queue 6??7... ");
24     EnterQue(&gtQueue?? 6);
25     EnterQue(&gtQueue?? 7);
26     DisplayQue(&gtQueue);
27
28     printf(" Leave Queue... ");
29     printf("Leave %d "?? LeaveQue(&gtQueue));
30     printf("Leave %d "?? LeaveQue(&gtQueue));
31     printf("Leave %d "?? LeaveQue(&gtQueue));
32     DisplayQue(&gtQueue);
33 }
????????????????н?????£?
1 Enter Queue 1??2??3??4??5...
2 Elem 5 cannot enter Queue 0x8053f9c(Full)!
3 Queue Status: Empty(0)?? Full(1)
4 Queue Elem Num: 4
5 Head: 0(1)
6 Tail: 4
7 Queue Element: 1 2 3 4
8
9 Leave Queue...
10 Leave 1
11 Leave 2
12 Leave 3
13 Queue Element: 4
14
15 Enter Queue 6??7...
16 Queue Element: 4 6 7
17
18 Leave Queue...
19 Leave 4
20 Leave 6
21 Leave 7
22 Queue 0x8053f9c is Empty!
?????????????????????????????
??????  ????????????????
????2.1 ???????
?????????????Linux????????о??????????/??????(producer-consumer)???????????漰?????С??????н綹????(bounded buffer)?????????????(?????????????)??
??????????????п??????????????????????(???????)??????????(???????)?????????????????????????????(???????)?????Щ?????????????????????????????????????IPC?????
????????????乲?????????????????????????????????????(??洢?????????)?????????????????洢????????????????????????????
?????????????????????????????????????

????????????/???????????У???????????????????????п??????????????з????????????????(??????????)??????????????????λ?á??????????????????????????黺??????????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????????д???????????????????????????????????????????????????????????????????????
????2.2 ????????
?????????????????????????????????/????????????????????
???????????????????????????±???????????????????????????????????????
??????????????????????
????1 #define PRODUCER_NUM   5  //????????
????2 #define CONSUMER_NUM   3  //????????
????3 #define PRD_NUM        20 //????????????
????4 #define DELAY_TIME     3  //????(??????)??????????????
????5
????6 #define QUEUE_SIZE     (PRD_NUM+1) //???д?????QUEUE_SIZE-1?????
????7
????8 T_QUEUE gtQueue;
????9 pthread_mutex_t gtQueLock = PTHREAD_MUTEX_INITIALIZER;
????10 pthread_cond_t gtPrdCond = PTHREAD_COND_INITIALIZER; //Full->Not Full
????11 pthread_cond_t gtCsmCond = PTHREAD_COND_INITIALIZER; //Empty->Not Empty
?????????QUEUE_SIZE??????????????塣??????gtQueLock?????????????gtQueue????????????????????????gtPrdCond?????????????????????(????)????????????gtCsmCond????????????????????????????
????????????????????????????????????????????????????????????????gtCsmCond(??????????в??)??????????????????????????????????????????????????????????????????????????????????????????????????????gtCsmCond??????б??????????????????????????“????”????????????????????????????????????????????????????????????????????