Linux??????????????????????
?????Linux ???????[ 2014/10/21 10:53:36 ] ????????Linux ?????? ???
????????????к?????????????????£?
1 int main(void)
2 {
3 InitQue(>Queue);
4 srand(getpid()); //??????????????????
5
6 pthread_t aThrd[CONSUMER_NUM+PRODUCER_NUM];
7 int dwThrdIdx;
8 for(dwThrdIdx = 0; dwThrdIdx < CONSUMER_NUM; dwThrdIdx++)
9 { //????CONSUMER_NUM????????????????(void*)dwThrdIdx???ConsumerThread?????
10 pthread_create(&aThrd[dwThrdIdx]?? NULL?? ConsumerThread?? (void*)dwThrdIdx);
11 }
12 sleep(2);
13 for(dwThrdIdx = 0; dwThrdIdx < PRODUCER_NUM; dwThrdIdx++)
14 {
15 pthread_create(&aThrd[dwThrdIdx]?? NULL?? ProducerThread?? (void*)dwThrdIdx);
16 }
17 while(1);
18 return 0 ;
19 }
????????????????????ProducerThread()??????£?
1 void *ProducerThread(void *pvArg)
2 {
3 pthread_detach(pthread_self());
4 int dwThrdNo = (int)pvArg;
5 while(1)
6 {
7 pthread_mutex_lock(>QueLock);
8 while(IsQueFull(>Queue)) //????????????????????????????????
9 pthread_cond_wait(>PrdCond?? >QueLock);
10
11 EnterQue(>Queue?? GetQueTail(>Queue)); //??????????±???????????
12 if(QueDataNum(>Queue) == 1) //??????????????????(????)?????????
13 pthread_cond_broadcast(>CsmCond);
14 printf("[Producer %2u]Current Product Num: %u "?? dwThrdNo?? QueDataNum(>Queue));
15
16 pthread_mutex_unlock(>QueLock);
17 sleep(rand()%DELAY_TIME + 1);
18 }
19 }
???????б??????????????????????????????????????????????????????????????????????????α??????
????????????????????ConsumerThread()??????£?
1 void *ConsumerThread(void *pvArg)
2 {
3 pthread_detach(pthread_self());
4 int dwThrdNo = (int)pvArg;
5 while(1)
6 {
7 pthread_mutex_lock(>QueLock);
8 while(IsQueEmpty(>Queue)) //???????????????????????????????
9 pthread_cond_wait(>CsmCond?? >QueLock);
10
11 if(GetQueHead(>Queue) != GetQueHeadData(>Queue))
12 {
13 printf("[Consumer %2u]Product: %d?? Expect: %d "?? dwThrdNo??
14 GetQueHead(>Queue)?? GetQueHeadData(>Queue));
15 exit(0);
16 }
17 LeaveQue(>Queue);
18 if(QueDataNum(>Queue) == (PRD_NUM-1)) //??????????????????????(????)?????????
19 pthread_cond_broadcast(>PrdCond);
20 printf("[Consumer %2u]Current Product Num: %u "?? dwThrdNo?? QueDataNum(>Queue));
21
22 pthread_mutex_unlock(>QueLock);
23 sleep(rand()%DELAY_TIME + 1);
24 }
25 }
???????????????????????????????????????????exit()????????
???????????????????????н??????(??????????????????????????????????)??
????[wangxiaoyuan_@localhost Thread]$ gcc -Wall -o procon procon.c -pthread
????[wangxiaoyuan_@localhost Thread]$ ./procon
????[Producer 4]Current Product Num: 1
????[Consumer 1]Current Product Num: 0
????[Producer 3]Current Product Num: 1
????[Consumer 0]Current Product Num: 0
????[Producer 2]Current Product Num: 1
????[Consumer 2]Current Product Num: 0
????[Producer 1]Current Product Num: 1
????[Producer 0]Current Product Num: 2
????//... ...
????[Consumer 0]Current Product Num: 17
????[Producer 3]Current Product Num: 18
????[Producer 1]Current Product Num: 19
????[Consumer 2]Current Product Num: 18
????[Producer 4]Current Product Num: 19
????[Producer 2]Current Product Num: 20
????[Consumer 1]Current Product Num: 19
????[Consumer 2]Current Product Num: 18
????[Producer 0]Current Product Num: 19
????[Producer 4]Current Product Num: 20
????//Ctrl + C
????2.3 ???????
?????????????????/????????????????????????Щ??????????????д????????????????????"pthread_"??
????1. ????????????????????????б???????????????????(shared data)??????????????Э????(cooperative)????????????????????????????磬????????????????????????????????????????????????????????????(????????????????????????????????????)??
??????????????????????gtQueLock??????????gtQueue???????????????(????????)???????????????????????е???????????????????gtQueLock???????????к?????????????????????????????????????????????????????У???????к????????????Ч????????????????????????????????Э??????????????????????(?????)???????????????Щ????δ???????????????????????????????????????????????????塣??????????????к??????????(?????????д??)?????????????gtQueLock????????????????????????????????????????????????????????????????????
???????????????????????????????????????????
????2. ????????????????????????????????????????????
???????磬????????????????ProducerThread()?У???pvArg???и????????????????????????(???????????)??????????????????????????????/???????????乲??????????????????????????????????????????????
??????????????????????????????????С???????????????(coarse-grained locking)???????????????????????????????????????????????к????????????????????????????????????????????????(fine-grained locking)???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????3. ????????????????????????????????cond_wait()??????????????????????????????????????????????????????????(????????δ????????)??
????????cond_wait()????????????????????????ú??????????????б??????ú???????????????????????????б????????????????????????????????????????????????????????????????????????????????????????????????????????????????????κα仯???ú???????????????????????????ü??????????????????????????(??????)????????????????????????????棬????????????????????????????????????????????????????????cond_wait()???????????????α??????????????????У???????????????????
?????????cond_wait()??????????????????????“?????”??????????????????????????????????????B??????????????A????????????????????????B????cond_ signal/broadcast()??????????????仯???????????????A?????????(????????????)??
????????????????????????????????????cond_wait()?????????Ч??δ???塣???????????????????????????????????????????????????????(???)??????cond_wait()???÷??????????
????4. ????cond_signal()????????????????????????????????????????????ú????????????????????????????????????????????????????????????????????????Щ????????????????????(scheduling policy)????????
????cond_broadcast()???????????????????????????????Щ???????????ξ????????????????????????????????????????д?????????????д???????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????ù?????(??????)??
????????????κ??????????????????????????cond_signal/broadcast()???????á?
????5. ??????(spurious wakeup)??????????????cond_signal/broadcast()???cond_wait()????????????????????в??????????cond_signal/broadcast()????????????????????????????????????μ??????????????У????????o??????????????????????cond_wait()??????????????2???????????????ù????????while???????????
????1 pthread_mutex_lock();
????2 while(IsConditionFalse)
????3 pthread_cond_wait();
????4 //Do something
????5 pthread_mutex_unlock();
???????while?????????????????????????????????????侺?????μ?“???Ч?”??
???????磬ProducerThread()???????cond_wait()?????????????????????????????????????????????????????????????????ú?????????????????Щ?????????(????????????????)??????ξ??????????????????????????????????????????????????????????(????????????????α???)???????????????????????????????????????????????????cond_wait()??????????????????????????????????cond_wait()???????????????????2??????????????
????????cond_timedwait()?????????????????????????????????????????????????????ú????????????????????????????
????6. ?????δ?????????????????????????????cond_signal/broadcast()??????????????????
?????????????????????????????????????????
????1) ?????????cond_signal/broadcast()??????
????2) ????????????????????????δ????cond_wait()??????
????3) ????????????????????????
????????????й????????????????????????????????????cond_signal/broadcast()??????????Щ?????????й????????????
?????????????cond_signal/broadcast()???????????????????????????????????????????????ü????????????????????????磬??????????????????????????????????????ü?????????????????????????????????????????
??????????????????????????????(?????????)??????????????????????η???????????????????
????7. ProducerThread()?У?cond_broadcast()??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????к??????????????????????????????????????????С?????????????????????????????????????????????????????????α???????????????????????л??????????????????
???????????????????(?????Ч??)??????ж??????????????????????????????bHasOnePrd?????????????gtQueLock????ж?bHasOnePrd?????????????????????gtCsmCond????????
????Posix????漲??????cond_signal/broadcast()???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????cond_signal/broadcast()??
???????????????????????????????????У???????ж??????????????????????????(?????????????????????????????????Щ??Ч???)??
????8. ?????????????????????????????????????????п??????????????????????磬???????????????β?????????????????????????????????£?
1 #define QUEUE_SIZE 20
2 volatile unsigned int gdwPrdNum = 0?? gdwCsmNum = 0;
3 int gQueue[QUEUE_SIZE] = {0};
4
5 void *Producer(void *pvArg) {
6 while(1) {
7 while(gdwPrdNum - gdwCsmNum == QUEUE_SIZE)
8 ; //Full
9
10 gQueue[gdwPrdNum % QUEUE_SIZE]++;
11 gdwPrdNum++;
12 }
13 pthread_exit(0);
14 }
15
16 void *Consumer(void *pvArg) {
17 while(1) {
18 while(gdwPrdNum - gdwCsmNum == 0)
19 ; //Empty
20
21 gQueue[gdwCsmNum % QUEUE_SIZE]--;
22 gdwCsmNum++;
23 }
24 pthread_exit(0);
25 }
????????????????(polling)??????????????????????????????????????Ч???????????п??????????????????????????????????????????????????????????????????????????(??????????????)??????????????????Ч?????????????????????????????????С????????????????????????????
???????????????ο?????????????????????????????????????
????9. ???Posix??????????????????????????????????????????
??????????sem_wait()??sem_post()???????????????????ü?????????????????????????????????????????????????????Ч??
?????????????????????????????????????????????????????????????????????????????????????????????????Щ?????????????????????????
?????????????????????????????????????????????????????????????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Linux??????DDR????????????Linux?????????????????豸??????????linux???в???????????м??????????????????????????Linux????Linux??DNS????????????γ???????????Linux?????????????Linux????(???)????????Linux ????????????????????????????????Windows???У?Linux???????Linux????????????????Linux iommu??vfio???????Linux????ε???TCP???????Linux?????????????СLinux?·???????????Linux???Jexus?й?Asp.Net Core??ó???Linux?????????Щ??
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????