簡體   English   中英

System V 消息隊列不等待 msgrcv

[英]System V Message Queue is not waiting for msgrcv

我正在開發一個程序,該程序需要通過 sys V 消息隊列(一個機場模擬器,其中航班由線程表示,用於上下文)在線程和進程之間進行通信。 問題是,我的行為很奇怪,我無法確定出了什么問題。

我有一個while循環的過程:

void ControlTower(){

 printf("Control Tower created\n");
 while(1){
  t_message msg;     //message struct for the message queue
  msgrcv(mqid, &msg, sizeof(t_message), 0,0);
  printf("Control Tower got the message with the ID %ld\n", msg.mtype);
  msg.slot = 10;    //random change to the message to make sure it was getting sent back properly

  msgsnd(mqid, &msg, sizeof(t_message), 0);

 }

}

所以,這里發生的事情是這個while循環運行了很多次,而不是正確的次數,並且只有在它與之通信的線程打印一些東西時才停止(可能不相關,但我不確定)

線程正在執行這個 function:


void *departure(void *node){ //receives a node from a linked list, all OK here

 char str[100];
 char temp[100];
 printf("WHOHOO, DEPARTURE FLIGHT CREATED AT MOMENT %d", current_time); //current_time is a global
                                                                        //counting the simulation time
 t_message msg;
 msg.mtype = id; //Read from node, conversion ommited
 msgsnd(mqid,&msg, sizeof(t_message), 0);
 msgrcv(mqid,&msg, sizeof(t_message), id, 0);

 printf("SHARED MEMORY SLOT ATTRIBUTED IS: %d\n", msg.slot);

}

我收到很多打印件說“控制塔收到帶有 ID %d 的消息”(使用正確的 ID 打印而不是 %d),但是當線程離開打印“SHARED MEMORY SLOT ATTRIBUTES IS:”時,控制塔不再打印。

有沒有人知道如何解決這個問題? 我真的可以使用一些幫助

我相信您的服務器正在接收自己的消息。 客戶端無法接收它發送的消息,因為該消息的類型為 0 並且客戶端僅查找具有特定id的消息。 但是服務器接收所有消息,因此它可以接收它發送的相同消息。

服務器多次接收和發送自己的消息后,操作系統可能會判斷服務器有足夠的CPU時間,並讓其他進程運行。 此時,客戶端可以接收到服務器發送的最后一條消息。

解決此問題的一種方法是對客戶端到服務器的消息使用非零 ID。 (傳遞一個 0 ID 給msgrcv意味着等待任何 ID)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM