自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你Unix消息隊列的應(yīng)用

系統(tǒng) 其他OS
我們在以前的文章中已經(jīng)介紹過了Unix消息隊列的創(chuàng)建、刪除、發(fā)送和接收的知識,這次,我們來介紹下在線程中Unix消息隊列應(yīng)用的知識。

我們以前學(xué)習(xí)過Unix消息隊列的創(chuàng)建、刪除、發(fā)送和接收的知識,今天,我們來學(xué)習(xí)在線程中Unix消息隊列應(yīng)用以實現(xiàn)多任務(wù)線程間的數(shù)據(jù)共享的知識,洗大家能夠在Unix的學(xué)習(xí)上有所收獲。
 
首先在main主函數(shù)中創(chuàng)建Unix消息隊列和線程://定義全局變量
 

  1. Int msqueue_record, msqueue_process;   
  2. Void main()   
  3. {   
  4. pthread_t pthreadID1;   
  5. //創(chuàng)建消息隊列,用于線程間通信   
  6. msqueue_record = msqueue_create ( “record”, 200, 200);   
  7. msqueue_process = msqueue_create ( “process”, 200, 200);   
  8. //創(chuàng)建數(shù)據(jù)采集線程   
  9. pthread_create ( &&pthreadID1, NULL, receiveData, NULL);   
  10. //創(chuàng)建數(shù)據(jù)處理線程   
  11. pthread_create ( &&pthreadID2, NULL, process, NULL);   
  12. //創(chuàng)建數(shù)據(jù)記錄線程   
  13. pthread_create ( &&pthreadID1, NULL, record, NULL);   
  14. //等待進(jìn)程結(jié)束   
  15. wait_thread_end( );   
  16. }  


數(shù)據(jù)采集線程:
 

  1. void receiveData( )   
  2. {   
  3. int count;   
  4. unsigned char buff[200];   
  5. for(;;) {   
  6. //從數(shù)據(jù)口采集數(shù)據(jù),并將數(shù)據(jù)放置于buff中   
  7. //wait_data_from_data_port( buff )   
  8. //將數(shù)據(jù)寫入消息隊列msqueue_record中   
  9. msqueue_send ( msqueue_record, buff, 200 );   
  10. //將數(shù)據(jù)寫入消息隊列msqueue_process中   
  11. msqueue_send ( msqueue_process, buff, 200 );   
  12. }   
  13. }   

記錄線程函數(shù):
 

  1. void record ( )   
  2. {   
  3. int num, count;   
  4. unsigned char buffer[200];   
  5. for ( ;; ) {   
  6. count = msqueue_receive ( msg_record, &&buffer, 200 );   
  7. if ( count < 0) {   
  8. perror ( "msgrcv in record");   
  9. continue;   
  10. }   
  11. //將取到的消息進(jìn)行記錄處理   
  12. //record_message_to_lib();   
  13. }   
  14. }   

數(shù)據(jù)處理線程函數(shù):
 

  1. int process( )   
  2. {   
  3. int count;   
  4. unsigned char buffer[200];   
  5. for ( ;; ) {   
  6. count = msqueue_receive ( msg_process, &&buffer, 200 );   
  7. if ( count < 0) {   
  8. perror ( "msgrcv in record");   
  9. continue;   
  10. }   
  11. //將取到的消息進(jìn)行處理   
  12. //process_message_data()   
  13. }   
  14. }   

 

在實現(xiàn)多任務(wù)系統(tǒng)時,作者曾經(jīng)做過以下三種實現(xiàn)方法的比較:進(jìn)程間通信采用IPC機制,線程間通信采用進(jìn)程通信方式IPC,線程間通信采用基于作者開發(fā)的Unix消息隊列。結(jié)果表明:利用用戶下的數(shù)據(jù)區(qū)進(jìn)行線程間通信的速度最快,效率最高,而IPC方式慢。這次,關(guān)于Unix消息隊列我們就講解到這里了。

【編輯推薦】

  1. 教你如何創(chuàng)建Unix消息隊列
  2. 知識講堂Unix內(nèi)核教學(xué)
  3. 初步講解Unix 線程知識
  4. 探析Unix操作系統(tǒng)啟動
  5. 知識講解Unix 消息隊列
責(zé)任編輯:小霞
相關(guān)推薦

2010-04-21 14:39:59

Unix消息隊列

2010-04-21 12:12:56

Unix 消息隊列

2010-04-21 12:39:48

Unix 消息隊列

2010-04-13 17:00:43

Unix消息隊列

2018-04-26 15:18:49

RTOS應(yīng)用MPU

2017-10-11 15:08:28

消息隊列常見

2024-03-29 08:33:10

應(yīng)用場景存儲搜索

2024-05-29 14:34:07

2021-03-11 06:01:41

Linux消息隊列

2018-03-29 08:38:10

2023-12-18 08:36:39

消息隊列微服務(wù)開發(fā)

2022-12-13 09:19:26

分布式消息隊列

2025-01-02 09:23:05

2019-07-19 07:56:13

消息隊列消息代理消息中間件

2023-12-30 13:47:48

Redis消息隊列機制

2024-05-10 09:36:36

架構(gòu)消息隊列

2009-12-07 09:23:05

2017-02-27 14:25:50

Java隊列Web

2022-04-12 11:15:31

Redis消息隊列數(shù)據(jù)庫

2012-09-24 11:48:05

IBMdw
點贊
收藏

51CTO技術(shù)棧公眾號