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

講解Unix 消息隊列應(yīng)用

系統(tǒng) 其他OS
文章中,我們會以舉例說明一個Unix 消息隊列在 C/S 模式中的應(yīng)用來講解一下關(guān)于Unix 消息隊列的應(yīng)用知識。希望大家能夠提高自己的技術(shù)。

今天,我們來學(xué)習(xí)一下Unix 消息隊列的知識,首先,我們下面舉例說明一個Unix 消息隊列在 C/S 模式中的應(yīng)用。 C/S 的一個重要特性就是非對等性, 多個客戶機(jī)同時向一個服務(wù)器提出不同的請求 , 服務(wù)器接收并處理客戶機(jī)的請求后 , 將結(jié)果返回給相應(yīng)的客戶機(jī)。在這里 , 我們利用兩個Unix 消息隊列、一個服務(wù)進(jìn)程、若干客戶進(jìn)程來模擬客戶 / 服務(wù)應(yīng)用。

由各客戶進(jìn)程將請求信息寫入隊列 1( 鍵值 0x16), 消息類型定為 10; 服務(wù)進(jìn)程從隊列 1 中讀取類型為 10 的消息進(jìn)行處理 , 并將處理結(jié)果放入Unix 消息隊列 2( 鍵值0x17), 將類型置為原客戶進(jìn)程號 , 這樣每個客戶進(jìn)程可根據(jù)其中的消息類型即進(jìn)程號再從隊列 2 中讀出屬于自己的結(jié)果 。

具體操作時先啟動服務(wù)進(jìn)程 (server.c), 創(chuàng)建兩個Unix 消息隊列 , 鍵值分別為十六進(jìn)制 16和 17; 這時再啟動若干客戶進(jìn)程 (client.c), 輸入一任意字符串 , 這時服務(wù)進(jìn)程則顯示客戶進(jìn)程號和原字符串 , 客戶進(jìn)程則回顯***個字符被改為 "_" 的處理后的字符串和消息類型( 自身進(jìn)程號 ) 。如輸入字符 "q", 則刪除原Unix 消息隊列 , 同時退出。源程序清單附后 , 該程序在 UNIX3.2 、 4.2 環(huán)境下編譯調(diào)試通過。

這只是進(jìn)程通信在機(jī)器內(nèi)部的實現(xiàn) , 要實現(xiàn)進(jìn)程在網(wǎng)絡(luò)機(jī)器間的通信還要借助 UNIX 網(wǎng)際系統(tǒng)的套接字來實現(xiàn)。

源程序清單如下 :
 

  1. 服務(wù)進(jìn)程 :(server.c)   
  2. #include <stdio.h>   
  3. #include <sys/types.h>   
  4. #include <sys/ipc.h>   
  5. #include <sys/msg.h>   
  6. #define KEY16 (key_t)16   
  7. #define KEY17 (key_t)17   
  8. #include (ctype.h)   
  9. int i,msgid1,msgid2,val;   
  10. char *s   
  11. struct{   
  12. long mtype;   
  13. short stype;   
  14. char mtext[40];   
  15. }buf;   
  16. main()   
  17. {   
  18.  
  19. cr_q(); /* 創(chuàng)建消息隊列 */   
  20. /* 讀消息隊列 1, 消息隊列類型為 10, 等待客戶請求 */   
  21. while   (msgrcv(msgid1,&buf,42,(long)10, ~ IPC_NOWAIT)>=0)   
  22. {printf("\n text from client is:[%s]",buf.mtext);   
  23.  printf ("client pid is <%d> \n",buf.stype);   
  24.  process();/* 將處理結(jié)果寫入消息隊列 2*/   
  25. }   
  26. }   
  27. cr_q()   
  28. {  msgid1=msgget(KEY16,IPC_CREAT|0666);   
  29. if (msgid1<0) {perror("key16 msgget failed");exit(1);}   
  30. msgid2=msgget(KEY17,IPC_CREAT|0666);   
  31. if (msgid2<0) {perror("key17 msgget failed");exit(1);}   
  32. }   
  33. process()   
  34. {   
  35. bufbuf.mtype=buf.stype;/* 原客戶進(jìn)程號 */   
  36. buf.mtext[0]= ‘ _ ’ ;   
  37. if(msgsnd(msgid2,&buf,42,IPC_NOWAIT)<0)   
  38. {perror("key17 msgsnd failed");exit(1);}   
  39. }   

 

  1. 客戶進(jìn)程 :(client.c)   
  2. #include <stdio.h>   
  3. #include <sys/types.h>   
  4. #include <sys/ipc.h>   
  5. #include <sys/msg.h>   
  6. define KEY16 (key_t)16   
  7. define KEY17 (key_t)17   
  8. include <ctype.h>   
  9. int msgid1,msgid2;   
  10. struct{   
  11. long mtype;   
  12. short stype;   
  13. char mtext[40];   
  14. }buf;   
  15. char s[41];   
  16. int clean()   
  17. main()   
  18. {   
  19. get_q();/* 取消隊列標(biāo)志符 */   
  20. while (1)   
  21. {   
  22. printf ("\n @ input mtext:");/* 輸入字符串 */   
  23. scanf("%s",s);   
  24. if ((strcmp(s,"q")==0)) break;   
  25. strcpy(buf.mtext,s);   
  26. buf.mtype=10; /* 消息類型置為 10*/   
  27. buf.stype=getpid();/* 客戶進(jìn)程號 */   
  28. /* 寫消息隊列 1, 向服務(wù)進(jìn)程提出請求 */   
  29. if (msgsnd(msgid1,&buf,40, ~ IPC_NOWAIT)<0)   
  30. {perror("key16 msgsnd failed");exit(1);}   
  31. /* 讀消息隊列 2, 類型為自身進(jìn)程號 , 接收處理結(jié)果 */   
  32. if (msgrcv(msgid2,&buf,42,getpid(), ~ IPC_NOWAIT)<0)   
  33.  {perror("key17 msgrcv failed");exit(1);}   
  34. printf("\n the answer from server is:[%s]",buf.mtext);   
  35.  printf("mtype is:[%d]",buf.mtype);   
  36.  }   
  37. clean();/* 刪除消息隊列 */   
  38. }   
  39. clean()   
  40. {   
  41. if(msgct1(msgid1,IPC_RMID,(struct msgid*)NULL)<0)   
  42. {perror("key16 msgct1 failed");exit(1);}   
  43. if(msgct1(msgid2,IPC_RMID,(struct msgid*)NULL<0)   
  44. {perror("key17 msgct1 failed");exit(1);}   
  45. printf("msg queue removed\n");   
  46. exit(0);   
  47. }   
  48. get_q()   
  49. {   
  50. msgid1=msgget(KEY16,0);   
  51. if (msgid1<0) {perror("key16 msgget failed");exit(1);}   
  52. msgid2=msgget(KEY17,0);if (msgid2<0) {perror("key17 msgget failed");exit(1);}   
  53. }  

關(guān)于Unix 消息隊列的應(yīng)用,我們今天就講解到這里啦,希望大家能夠好好的學(xué)習(xí),我們會帶來更多的Unix 消息隊列應(yīng)用的知識。

【編輯推薦】

  1. Linux多線程同步之消息隊列
  2. 詳解Unix消息隊列知識
  3. VB.NET消息隊列相關(guān)內(nèi)容詳細(xì)介紹
  4. WCF消息隊列系列介紹
  5. 說明WCF消息隊列具體問題
責(zé)任編輯:小霞
相關(guān)推薦

2010-04-21 12:39:48

Unix 消息隊列

2010-04-21 14:49:13

Unix消息隊列

2010-04-13 17:00:43

Unix消息隊列

2010-04-21 14:39:59

Unix消息隊列

2018-04-26 15:18:49

RTOS應(yīng)用MPU

2010-05-05 13:45:21

Unix Telnet

2010-05-04 09:22:10

Unix文件

2010-05-05 16:05:36

Unix cfengi

2010-05-05 17:41:03

IBM Unix

2010-04-30 13:27:26

Unix cronta

2010-05-05 10:19:51

Unix系統(tǒng)

2010-05-04 12:25:28

Unix鏈接

2010-04-30 13:38:51

Unix at命令

2010-04-21 15:20:31

Unix線程

2010-05-04 11:59:39

Unix系統(tǒng)

2010-05-05 13:13:55

Unix內(nèi)核

2010-05-05 17:30:04

Unix MBB

2017-10-11 15:08:28

消息隊列常見

2010-04-30 18:20:23

Unix系統(tǒng)

2010-04-21 14:29:52

Unix 線程
點贊
收藏

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