我有一個小型的C++程序,其中主進程是「創建數據」並將它們發送到應讀取數據的子進程。我的問題是,在學校我的代碼運行良好,但在我自己的筆記本電腦上,這兩個進程在程序啓動後就卡住了。具體來說,他們都在等待頻道「do_msgrcv」。C++ msgsnd和msgrvc陷入睡眠
這裏是我的代碼:
#define VYROBA 1 // Manufacturer
#define PREPRAVA 2 // Transport
void manufacturer () {
static int count = 0;
int rcv [ 2 ];
while (1) {
int snd [ 2 ] = { VYROBA, count };
int ret = msgsnd (glb_msg_id, &snd, sizeof (int), 0);
ret = msgrcv (glb_msg_id, &rcv, sizeof (int), PREPRAVA, 0);
printf ("Got crate\n");
}
}
void consumer () {
static int count = 0;
int rcv [ 2 ];
while (1) {
int ret = msgrcv (glb_msg_id, &rcv, sizeof (int), VYROBA, 0);
usleep (500000);
if (ret < 0) {
printf ("Can't read message.\n");
}
printf ("Got product: %d\r\n", rcv [ 1 ]);
fflush (stdout);
rcv [ 1 ]++;
if (rcv [ 1 ] == 10) {
int snd [ 2 ] = { PREPRAVA, rcv [ 1 ] };
ret = msgsnd (glb_msg_id, &snd, sizeof (int), 0);
} else {
ret = msgsnd (glb_msg_id, &rcv, sizeof (int), 0);
}
}
}
如果有幫助的學校,我們擁有的Ubuntu 12.04,我使用Ubuntu 16.04。
感謝您的任何幫助。
請您在這篇文章中直接發送發送/接收的相關代碼?使用patstebin鏈接被認爲是不好的做法,因爲如果有人在6個月內閱讀這個問題,關鍵元素將不再可用。 – Christophe
@Christophe對不便,現在已修好。 – cmoud94
是否有可能你的Ubuntu是32位而其他64位? – Christophe