msgsnd problem

L

Ludwig Moser

i got a struct like that:

/* msg uses msgtype, and the data that is transferred */
struct Msg {
long msgType; /* 0 or 1*/
char msgData[SIZE];
};

and i try to send it with msgsnd
so i wrote a fkt to send the msg into my messagequeue:

void send( int lineNumber, long msgTyp ) {
struct Msg msg;
int msgID = -1;

msg.msgType = msgTyp;
sprintf(msg.msgData,"%d\n", lineNumber);

printf("\nmsgData: %s", msg.msgData);

msgID = msgget(msgQueueID, /*IPC_CREAT|*/IPC_EXCL|0666);

if (msgID < 0) {
perror( strerror(errno) );
printf("msgget failed, msgID = %d\n", msgID);
exit(-1);
}
printf("sending msgType %ld\n", msg.msgType);
if (msgsnd(msgID, &msg, SIZE, 0) == -1) {
perror("msgsnd"); /* Fehler */
printf("\nWHY ARE WE HERE? Aliens stole our data! *cry*\n");
exit(-1);
} else {
/* we are done */
printf("Data sent: %s\n", msg.msgData);
exit(0);
}
} // end void send( int lineNumber, long msgTyp )

so, when i run the program
one time i get to "WHY ARE WE HERE?" when msgType=0
and the next time i get OK, when msgType=1

why?
thougt its on my own what type-value i choose?

TIA

Luke
 
R

Régis Troadec

"Ludwig Moser" <[email protected]> a écrit dans le message de

Hi,
i got a struct like that:

/* msg uses msgtype, and the data that is transferred */
struct Msg {
long msgType; /* 0 or 1*/
char msgData[SIZE];
};

and i try to send it with msgsnd
so i wrote a fkt to send the msg into my messagequeue:

void send( int lineNumber, long msgTyp ) {
struct Msg msg;
int msgID = -1;

msg.msgType = msgTyp;
sprintf(msg.msgData,"%d\n", lineNumber);

printf("\nmsgData: %s", msg.msgData);

msgID = msgget(msgQueueID, /*IPC_CREAT|*/IPC_EXCL|0666);

if (msgID < 0) {
perror( strerror(errno) );
printf("msgget failed, msgID = %d\n", msgID);
exit(-1);
}
printf("sending msgType %ld\n", msg.msgType);
if (msgsnd(msgID, &msg, SIZE, 0) == -1) {
perror("msgsnd"); /* Fehler */
printf("\nWHY ARE WE HERE? Aliens stole our data! *cry*\n");
exit(-1);
} else {
/* we are done */
printf("Data sent: %s\n", msg.msgData);
exit(0);
}
} // end void send( int lineNumber, long msgTyp )

so, when i run the program
one time i get to "WHY ARE WE HERE?" when msgType=0
and the next time i get OK, when msgType=1

why?
thougt its on my own what type-value i choose?

Even it doesn't really deal with C programming, I can answer you that's
quite normal according the informations I found in the POSIX.1 documentation
:

Assuming this message structure :
struct mymsg {
long mtype; /* Message type. */
char mtext[1]; /* Message text. */
};

The int msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg)
function shall fail if:

[EACCES]
Operation permission is denied to the calling process; see XSI Interprocess
Communications.
[EAGAIN]
The message cannot be sent for one of the reasons cited above and (msgflg &
IPC_NOWAIT) is non-zero.
[EIDRM]
The message queue identifier msqid is removed from the system.
[EINTR]
The msgsnd() function was interrupted by a signal.
[EINVAL]
The value of msqid is not a valid message queue identifier, or the value of
mtype is less than 1; or the value of msgsz is less than 0 or greater than
the system-imposed limit.

In your case, msgsnd() fails because your mtype (msg.MsgType) is 0 so less
than 1. errno should be set to EINVAL.

You can find all these informations about msgsnd() here :
http://www.opengroup.org/onlinepubs/007904975/functions/msgsnd.html

You'll find the main page of the POSIX.1 standard doc at :
http://www.opengroup.org/onlinepubs/007904975/toc.htm

Regis
 
C

Christopher Benson-Manica

Ludwig Moser said:
/* msg uses msgtype, and the data that is transferred */
struct Msg {
long msgType; /* 0 or 1*/
char msgData[SIZE];
};
and i try to send it with msgsnd
so i wrote a fkt to send the msg into my messagequeue:

(It's called a "function".)

Your post is off-topic for comp.lang.c. Please visit

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

for posting guidelines and frequently asked questions. Thank you.
 
L

Ludwig Moser

and i try to send it with msgsnd
(It's called a "function".)

no need to comment that, everybody knows that its called a function

and i am really not surprised, that also this time, a little nerd wrote his
stupid comment....

that's weird, think it and don't spam around
 
C

Christopher Benson-Manica

Ludwig Moser said:
no need to comment that, everybody knows that its called a function

Then you should have said so.
and i am really not surprised, that also this time, a little nerd wrote his
stupid comment....

If you consider a polite invitiation to read the posting guidelines
for this group "stupid", I suspect you'll be residing in various
killfiles soon.
 
K

Keith Thompson

Ludwig Moser said:
no need to comment that, everybody knows that its called a function

Christopher's point, I think, was that the word "function" is much
clearer to readers than the abbreviation "fkt". If I had actually
read your original article, I probably would have had to pause for
several seconds to figure out what a "fkt" might be. That's longer
that it would have taken you to type the full word. Now multiply that
by the number of readers to get an idea of how much of other people's
time you're wasting for the sake of saving a few keystrokes.

If you're asking people for help, it's a good idea to take the time
and effort to write as clearly as possible.

And, as it happens, Christopher was absolutely right about your
question being off-topic here. The standard C library has no msgsnd()
function. If you want to ask about it, you should post in a newsgroup
that's specific to your system. I'm guessing that
comp.unix.programmer would be a good place to start (assuming you've
first checked your documentation and their FAQ).

We're not mad at you for not knowing that your question was off-topic,
but I hope you understand that telling you to go elsewhere is actually
the best way we can help you.

YMMV, HTH, HAND. <-- (irony)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,366
Latest member
IanCulpepp

Latest Threads

Top