/********************************************************************************* * Copyright: (C) 2025 LingYun IoT System Studio * All rights reserved. * * Filename: mq_send.c * Description: This file is Message Queue(sender) example program. * * Version: 1.0.0(11/10/2025) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "11/10/2025 10:06:53 AM" * ********************************************************************************/ #include #include #include #include #include #include #include #include #define FTOK_PATH "/dev/zero" #define FTOK_PROJID 0x22 typedef struct msg_s { long mtype; char mtext[512]; } msg_t; int main(int argc, char **argv) { key_t key; msg_t msg; int msgid; int type=2; int i; if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 ) { printf("ftok() get IPC token failure: %s\n", strerror(errno)); return -1; } msgid = msgget(key, IPC_CREAT|0666); if( msgid < 0) { printf("shmget() create shared memroy failure: %s\n", strerror(errno)); return -2; } printf("key[%d] msgid[%d] msgypte[0x%0x]\n", (int)key, msgid, type); while (1) { printf("Input message: "); memset(msg.mtext, 0, sizeof(msg.mtext)); fgets(msg.mtext, sizeof(msg.mtext), stdin); msg.mtype = type; if( msgsnd(msgid, &msg, strlen(msg.mtext)+1, IPC_NOWAIT) < 0) { printf("msgsnd() send message failure: %s\n", strerror(errno)); break; } if (strncasecmp(msg.mtext, "exit", 4) == 0) break; } msgctl(msgid, IPC_RMID, NULL); return 0; }