PDA

View Full Version : ipc message-passing hangs



testguest
11-17-2010, 02:36 PM
hi,

wrote 2 applications, one for sender and one for receiver. To send a msg, the sender application is started manually at commandline and 1 msg is sent and received. everthing works great. But when i penetrate the commandline and try to send as fast as i can with
the keyboard, the receiver everytime hangs up and relieves some msgs in the message
queue. (as i can see with ipcs). Is there any problem with synchronization?

I use basically:

msgget(2407, (IPC_CREAT | 0755))
msgsnd(msgID, &dataMsg, MSGSIZE, 0)) ....

and

msgget(2407, 0755);
msgrcv(msgID, &dataMsg, MSGSIZE, msgTyp, IPC_NOWAIT)) ....

Greetz

Salem
11-17-2010, 02:41 PM
Posting a whole program which shows the problem would be useful.

Random lines of where you think the problem is doesn't work.

testguest
11-17-2010, 03:00 PM
you'r right...





IN HEADERFILE---------------

#define MSGSIZE 20

struct myMsg {
long mtype;
char mtext[MSGSIZE];
} dataMsg;


SENDER -------------

void mysend(long type, int pid)
{
int msgID;
char msg[MSGSIZE];

dataMsg.mtype = type;
sprintf(msg, "%d", pid);
strncpy(dataMsg.mtext, msg, MSGSIZE);

msgID = msgget(2407, (IPC_CREAT | 0755));
if (msgID >= 0) {

if (-1==msgsnd(msgID, &dataMsg, MSGSIZE, 0)) {
perror("msgsnd");
} else {
printf("PID gesendet: %s \n", dataMsg.mtext);
}
} else {
perror("msgget");
}
}

int main(int argc, char **argv) {

int pid;

mysend(3333,pid);

return 0;
}

RECEIVER---------------

struct myMsg my_msgrcv(long type) {
int msgID;
struct myMsg dataMsg;
long msgTyp = 0;

msgTyp = type;

msgID = msgget(2407, 0755);
if (msgID >= 0) {
if (-1==msgrcv(msgID, &dataMsg, MSGSIZE, msgTyp, IPC_NOWAIT)) {
perror("msgrcv");
} else {
printf("received: %s \n", dataMsg.mtext);
}
} else {
perror("msgget");
}
return dataMsg;
}

int main() {

while(1) {
mymsgrcv(3333);
}
}



The original code is to long, but i only shorten in main. It finally always stucks in the line with the msgrcv cmd.

testguest
11-17-2010, 03:04 PM
Its an piece of code, that looks similar whereever i searched. I only experimented with
the flags to have a non-blocking behaviour and a receiver that runs in endless loop.

Salem
11-17-2010, 03:20 PM
Well the first big problem is that MSGSIZE isn't the same as sizeof(dataMsg)

If it's looping causing errors, then remove the while(1) loop and call it a finite number of times.

What output do you get?
Is it all just error messages?

testguest
11-17-2010, 03:20 PM
.... and please do not regard to the typing error in the RECEIVER main

1)everything is compiled well and works, my first thought was that it is relied on the flags
maybe i forgot something.

2) i checked the also the /proc/sys/kernel/msgm* informations, but they are default-valued.

3) when i kill the hanging process, and start the receiver again, the last message that is not
taken from queue is received. could it be that there are some queue entries that are empty for some reasons?

Salem
11-17-2010, 03:21 PM
> .... and please do not regard to the typing error in the RECEIVER main
Why not?
If it's approximate code, you're going to get approximate answers.

testguest
11-17-2010, 03:47 PM
> Well the first big problem is that MSGSIZE isn't the same as sizeof(dataMsg)

havent seen that, very good, thanks i will work it out

> What output do you get?

None, i have some printf() in the while loop and, sorry i forgot: a sleep(1) at the end to see
the progression of the loop. i can use kbd and type in the cmdline-window. But it simply stops every
output. Shouldnt it normally go into the if-clause to print perror("msgrcv"); ?