C program IPC message -
i using ipc 2 programs communication.
snippet of code sender:
int msgflg = ipc_creat | 0666; key_t key_id; struct msgbuf sbuf; size_t buflen; key_id = 1235; sbuf.mtype = 1; if (msgsnd(msqid, &sbuf, buflen, ipc_nowait) < 0) { die("msgsnd"); } else { mvprintw(8, 0, "%s", " "); mvprintw(8, 0, "%s", sbuf.mtext); } receiving end:
if ((msqid = msgget(key_id, 0666)) < 0) die("msgget()"); //break; //receive answer of message type 1. if (msgrcv(msqid, &rcvbuffer, maxsize, 1, 0) < 0) die("msgrcv"); mvprintw(curr_row + 1, 0, "cleaning process monitor: %s", rcvbuffer.mtext); the communication working perfect message received @ receiver's end not received.
if sending "hello" receives "h" buffer size 200
you can't declare struct msgbuf directly mtext size 1
struct msgbuf { long mtype; /* message type, must > 0 */ char mtext[1]; /* message data */ }; what need declare long char array in structure, , convert void*
for example,
struct msgbuf { long type; char mtext[200]; }; struct msgbuf mybuf; // set type, store data in mtext msgsnd(msqid, (void*)&mybuf, sizeof(msgbuf)+1, ipc_nowait)
Comments
Post a Comment