![](/img/trans.png)
[英]C Linux pthreads: sending data from one thread to antoher using message queue gives unexpected result
[英]2nd thread not able to receive messages via message queue sent by thread 1 in C/C++/linux
我正在模擬一個udp服務器,該服務器通過udp客戶端接收多個請求消息。 我需要在此udp服務器示例代碼中生成2個線程。 線程1將在到達時接收所有udp請求,並通過消息隊列將其發送到線程2進行處理。
線程1(SndMq)能夠通過udp接收請求消息,也可以使用結構將其放入消息隊列。 但是問題是線程2(RcvMq)甚至無法讀取消息隊列中的單個消息。 請提出任何解決方案。
錯誤部分(線程2功能):
void* RcvMq(void*) {
time_t t2;
time(&t2);
msgst *msg = new msgst;
int mqid = msgget((key_t)12345, 0666 | IPC_CREAT);
if (mqid == -1) {
printf("thread 2: msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
else
printf("\nthread 2: got MQ id: %d\n", mqid);
long receive_m_type = 123;
printf("thread 2: going to enter while(1) loop\n");
while(1) {
printf("\nthread 2: inside while(1) loop, going to msgrcv()\n");
if ( msgrcv(mqid, (void *)&msg, BUFSIZE, receive_m_type, 0) < 0 ) {
//printf("\nthread 2: msgrcv failed with error: [%s]\n", strerror(errno));
printf("\nthread 2: msgrcv failed with error\n");
exit(EXIT_FAILURE);
}
else {
printf("thread 2: going to ctime()\n");
printf("\nthread 2: DeQueued at: %s\n", ctime(&t2));
}
printf("\nthread 2: going to print struct's buffer\n");
printf("thread 2: Message received at MQ rcv is: %s \n", msg->buffer);
}
}
udp服務器模擬器的代碼
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/msg.h>
#include<pthread.h>
#define PORT 5000
#define BUFSIZE 2048
pthread_t sndtid;
pthread_t rcvtid;
typedef struct msgst {
long int mtype;
char buffer[BUFSIZE];
}msgst;
void* SndMq(void*) {
time_t t1;
time(&t1);
struct sockaddr_in myaddr; /* our address */
struct sockaddr_in remaddr; /* remote address */
socklen_t addrlen = sizeof(remaddr); /* length of addresses */
int recvlen; /* # bytes received */
int fd; /* our socket */
int rc, on=1;
char buf[BUFSIZE]; /* receive buffer */
size_t buflen;
/* create a UDP socket: socket, setsockopt, bind */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
return 0;
}
if((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) < 0) {
perror("thread 1: UDP Server setsockopt() - ERROR");
close(fd);
exit(-1);
}
/* bind the socket to any valid IP address and a specific port */
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
//myaddr.sin_port = htons(SERVICE_PORT);
myaddr.sin_port = htons(PORT);
if ( bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr) ) < 0) {
perror("bind failed");
return 0;
}
printf("\nthread 1: Server waiting on port %d\n", PORT);
//MsgQ implementation:
int mqid = msgget((key_t)12345, 0666 | IPC_CREAT);
if(mqid == -1) {
printf("thread 1: msgget() failed, errno: %d\n",errno );
}
else
printf("thread 1: created MQ id: %d\n", mqid);
/* now loop, receiving data and printing what we received */
for (;;) {
//rcv from udp client
recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
printf("\nthread 1: received %d bytes\n", recvlen);
if (recvlen > 0) {
buf[recvlen] = 0;
printf("thread 1: received message from UDP client: %s\n", buf);
}
//send received msg via MsgQ
msgst *msgptr = new msgst;
msgptr->mtype = 123;
strcpy(msgptr->buffer, buf);
//msgptr->buffer=buf;
//memcpy(msgptr->buffer, buf, BUFSIZE);
buflen = strlen(msgptr->buffer);
if (msgsnd(mqid, (void *)&msgptr, buflen, IPC_NOWAIT) == -1) {
printf("thread 1: msgsnd failed\n");
exit(EXIT_FAILURE);
}
printf("thread 1: added Req to MsgQ at: %s\n", ctime(&t1));
}/* never exits */
}
void* RcvMq(void*) {
time_t t2;
time(&t2);
msgst *msg = new msgst;
int mqid = msgget((key_t)12345, 0666 | IPC_CREAT);
if (mqid == -1) {
printf("thread 2: msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
else
printf("\nthread 2: got MQ id: %d\n", mqid);
long receive_m_type = 123;
printf("thread 2: going to enter while(1) loop\n");
while(1) {
printf("\nthread 2: inside while(1) loop, going to msgrcv()\n");
if ( msgrcv(mqid, (void *)&msg, BUFSIZE, receive_m_type, 0) < 0 ) {
//printf("\nthread 2: msgrcv failed with error: [%s]\n", strerror(errno));
printf("\nthread 2: msgrcv failed with error\n");
exit(EXIT_FAILURE);
}
else {
printf("thread 2: going to ctime()\n");
printf("\nthread 2: DeQueued at: %s\n", ctime(&t2));
}
printf("\nthread 2: going to print struct's buffer\n");
printf("thread 2: Message received at MQ rcv is: %s \n", msg->buffer);
}
}
int main() {
int rtn=0;
rtn = pthread_create(&(sndtid), NULL, &SndMq, NULL);
if (rtn != 0)
printf("\ncan't create thread SndMq, error:[%s]", strerror(rtn));
else
printf("\n\ncreated SndMq thread\n");
rtn = pthread_create(&(rcvtid), NULL, &RcvMq, NULL);
if (rtn != 0)
printf("\ncan't create thread RcvMq :[%s]", strerror(rtn));
else
printf("created RcvMq thread\n");
printf("main(): All threads spawned successfully, now waiting for threads to end...\n");
pthread_join(sndtid, NULL);
pthread_join(rcvtid, NULL);
printf("\nThreads ended, main() exiting now...\n");
return 0;
}
created SndMq thread
created RcvMq thread
main(): All threads spawned successfully, now waiting for threads to end...
thread 2: got MQ id: 6422528
thread 2: going to enter while(1) loop
thread 2: inside while(1) loop, going to msgrcv()
thread 1: Server waiting on port 5000
thread 1: created MQ id: 6422528
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>1</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>2</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>3</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>4</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>5</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>6</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>7</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>8</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>9</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
thread 1: received 1500 bytes
thread 1: received message from UDP client: <TransactionId>10</TransactionId>
thread 1: added Req to MsgQ at: Sat Apr 5 18:16:45 2014
(1)您的主要問題是C錯誤。 您正在傳遞的是ptr(指向ptr的ptr)的地址,而不是指針。
msgst *msgptr = new msgst;
//...
if (msgsnd(mqid, (void *)&msgptr, buflen, IPC_NOWAIT) == -1)
應該
if (msgsnd(mqid, (void *) msgptr, buflen, IPC_NOWAIT) == -1)
結果,您正在寫&msgptr上的任何隨機垃圾,並且由於您正在寫msgtype 123,然后嘗試稍后再讀msgtype 123(但寫為垃圾),因此您將永遠(或非常少!)找到該消息類型。
您在郵件接收端犯了同樣的錯誤:
msgst *msg = new msgst;
//.......
if ( msgrcv(mqid, (void *)&msg, BUFSIZE, receive_m_type, 0) < 0 )
(2)您可能還沒有讀到它,但是由於您new
了緩沖區但從未delete
它,所以內存泄漏。 無論如何,您實際上並不需要動態內存分配。 只在堆棧上分配緩沖區會更容易,因為無論如何它都將由msgsnd
復制,然后就完成了。 這也可以消除您遇到的問題(1)。
(3)您將在msgsnd
上遇到IPC_NOWAIT的問題。 在消費者對它們進行處理之前,很容易在生產者端填充隊列。 至少您應該檢查EAGAIN並在寫入失敗時采取適當的措施。
可能與所詢問的問題無關。
但是在SndMq()
如果recvfrom()
接收到BUFSIZE
字節,則代碼將在這里超出buf
的范圍:
buf[recvlen] = 0;
更新:
要解決此問題,請更改此行
recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
成為
recvlen = recvfrom(fd, buf, BUFSIZE - 1, 0, (struct sockaddr *)&remaddr, &addrlen);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.