MSGOP(2) System Calls Manual MSGOP(2) msgrcv, msgsnd - System V (libc -lc) #include ssize_t msgrcv(size_t msgsz; int msqid, void msgp[msgsz], size_t msgsz, long msgtyp, int msgflg); int msgsnd(size_t msgsz; int msqid, const void msgp[msgsz], size_t msgsz, int msgflg); msgsnd() msgrcv() System V . . msgp : struct msgbuf { long mtype; /* > 0 */ char mtext[1]; /* */ }; mtext ( ) msgsz . ( mtext). mtype . ( msgrcv() ). msgsnd() msgsnd() msgp msqid. msgsnd() . msg_qbytes . MSGMNB msgctl(2). : o ( msg_qbytes). o ( msg_qbytes). . (). msgsnd() . IPC_NOWAIT msgflg EAGAIN. msgsnd() : o errno EIDRM o errno EINTR signal(7). ( msgsnd() SA_RESTART .) : o msg_lspid . o msg_qnum 1. o msg_stime . msgrcv() msgrcv() msqid msgp. msgsz mtext msgp. msgsz MSG_NOERROR msgflg. MSG_NOERROR ( ) MSG_NOERROR -1 errno E2BIG. MSG_COPY msgflg ( ) msgtyp : o msgtyp 0 . o msgtyp 0 msgtyp MSG_EXCEPT msgflg msgtyp. o msgtyp 0 msgtyp. msgflg OR : IPC_NOWAIT . errno ENOMSG. MSG_COPY ( 3.8) msgtyp ( 0). IPC_NOWAIT ENOMSG. msgtyp MSG_COPY MSG_EXCEPT msgflg. MSG_COPY CONFIG_CHECKPOINT_RESTORE. MSG_EXCEPT msgtyp 0 msgtyp. MSG_NOERROR msgsz . IPC_NOWAIT msgflg : o . o . errno EIDRM. o . errno EINTR. ( msgrcv() SA_RESTART .) : msg_lrpid . msg_qnum 1. msg_rtime . msgsnd() 0 msgrcv() mtext. -1 errno . msgsnd() : EACCES CAP_IPC_OWNER IPC . EAGAIN msg_qbytes IPC_NOWAIT msgflg. EFAULT msgp . EIDRM . EINTR . EINVAL msqid mtype msgsz ( 0 MSGMAX). ENOMEM msgp. msgrcv() : E2BIG msgsz MSG_NOERROR msgflg. EACCES CAP_IPC_OWNER IPC . EFAULT msgp . EIDRM . EINTR signal(7). EINVAL msqid msgsz 0. EINVAL ( 3.14) msgflg MSG_COPY IPC_NOWAIT. EINVAL ( 3.14) msgflg MSG_COPY MSG_EXCEPT. ENOMSG IPC_NOWAIT msgflg . ENOMSG IPC_NOWAIT MSG_COPY msgflg msgtyp . ENOSYS ( 3.8) MSG_COPY IPC_NOWAIT msgflg CONFIG_CHECKPOINT_RESTORE. POSIX.1-2024 XSI. MSG_EXCEPT MSG_COPY _GNU_SOURCE. SVr4, SUSv1, POSIX.1-2001 XSI. msgp struct msgbuf * glibc 2.0 2.1. void * glibc 2.2 SUSv2 SUSv3. msgsnd(): MSGMAX ( : 8192 ). /proc/sys/kernel/msgmax. MSGMNB ( : 16384 ). /proc/sys/kernel/msgmnb. (: CAP_SYS_RESOURCE) MSGMNB msgctl(2) IPC_SET. (MSGTQL) (MSGPOOL). 3.13 msgrcv() MSG_COPY IPC_NOWAIT msgtyp . msgtyp. 3.14. MSG_COPY MSC_EXCEPT msgflg ( msgtyp). 3.13 msgrcv(). 3.14. msgsnd() msgrcv(). -s -r . : $ ./a.out -s sent: a message at Wed Mar 4 16:25:45 2015 $ ./a.out -r message received: a message at Wed Mar 4 16:25:45 2015 #include #include #include #include #include #include #include struct msgbuf { long mtype; char mtext[80]; }; static void usage(char *prog_name, char *msg) { if (msg != NULL) fputs(msg, stderr); fprintf(stderr, "Usage: %s [options]\n", prog_name); fprintf(stderr, "Options are:\n"); fprintf(stderr, "-s send message using msgsnd()\n"); fprintf(stderr, "-r read message using msgrcv()\n"); fprintf(stderr, "-t message type (default is 1)\n"); fprintf(stderr, "-k message queue key (default is 1234)\n"); exit(EXIT_FAILURE); } static void send_msg(int qid, int msgtype) { time_t t; struct msgbuf msg; msg.mtype = msgtype; time(&t); snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s", ctime(&t)); if (msgsnd(qid, &msg, sizeof(msg.mtext), IPC_NOWAIT) == -1) { perror("msgsnd error"); exit(EXIT_FAILURE); } printf("sent: %s\n", msg.mtext); } static void get_msg(int qid, int msgtype) { struct msgbuf msg; if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype, MSG_NOERROR | IPC_NOWAIT) == -1) { if (errno != ENOMSG) { perror("msgrcv"); exit(EXIT_FAILURE); } printf("No message available for msgrcv()\n"); } else { printf("message received: %s\n", msg.mtext); } } int main(int argc, char *argv[]) { int qid, opt; int mode = 0; /* 1 = send, 2 = receive */ int msgtype = 1; int msgkey = 1234; while ((opt = getopt(argc, argv, "srt:k:")) != -1) { switch (opt) { case 's': mode = 1; break; case 'r': mode = 2; break; case 't': msgtype = atoi(optarg); if (msgtype <= 0) usage(argv[0], "-t option must be greater than 0\n"); break; case 'k': msgkey = atoi(optarg); break; default: usage(argv[0], "Unrecognized option\n"); } } if (mode == 0) usage(argv[0], "must use either -s or -r option\n"); qid = msgget(msgkey, IPC_CREAT | 0666); if (qid == -1) { perror("msgget"); exit(EXIT_FAILURE); } if (mode == 2) get_msg(qid, msgtype); else send_msg(qid, msgtype); exit(EXIT_SUCCESS); } msgctl(2), msgget(2), capabilities(7), mq_overview(7), sysvipc(7) 3 . . : . 6.18 11 2026 MSGOP(2)