MSGOP(2) System Calls Manual MSGOP(2) msgrcv, msgsnd - System V LIBRARY Standard C library (libc, -lc) #include int msgsnd(int msqid, const void msgp[.msgsz], size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, void msgp[.msgsz], size_t msgsz, long msgtyp, 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() . msgflg IPC_NOWAIT, 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 , ( ); , -1 errno E2BIG. msgflg ( ) MSG_COPY, msgtyp : o msgtyp , . o msgtyp , msgtyp ( msgflg MSG_EXCEPT. , msgtyp). o msgtyp , , msgtyp. msgflg : IPC_NOWAIT , . , errno ENOMSG. MSG_COPY ( Linux 3.8) , msgtyp ( 0). IPC_NOWAIT; , , ENOMSG . MSG_COPY MSG_EXCEPT msgflg, msgtyp. MSG_COPY / , CONFIG_CHECKPOINT_RESTORE. MSG_EXCEPT , msgtyp 0, , msgtyp. MSG_NOERROR , msgsz . msgflg IPC_NOWAIT, , : o . o . errno EIDRM. o , . errno EINTR. msgrcv() , SA_RESTART . : msg_lrpid . msg_qnum 1. msg_rtime . On success, msgsnd() returns 0 and msgrcv() returns the number of bytes actually copied into the mtext array. On failure, both functions return -1, and set errno to indicate the error. msgsnd() can fail with the following errors: EACCES CAP_IPC_OWNER , IPC. EAGAIN , , msg_qbytes, msgflg IPC_NOWAIT. EFAULT , msgp, . EIDRM . EINTR , . EINVAL msqid, mtype msgsz ( 0, , MSGMAX). ENOMEM , msgp. msgrcv() can fail with the following errors: E2BIG , msgsz, msgflg MSG_NOERROR. EACCES CAP_IPC_OWNER , IPC. EFAULT , msgp, . EIDRM , . EINTR ; . signal(7). EINVAL msqid msgsz 0. EINVAL ( Linux 3.14) msgflg MSG_COPY, IPC_NOWAIT. EINVAL ( Linux 3.14) msgflg MSG_COPY MSG_EXCEPT. ENOMSG , msgflg IPC_NOWAIT. ENOMSG IPC_NOWAIT MSG_COPY msgflg, msgtyp . ENOSYS ( Linux 3.8) Both MSG_COPY and IPC_NOWAIT were specified in msgflg, and this kernel was configured without CONFIG_CHECKPOINT_RESTORE. POSIX.1-2008. MSG_EXCEPT MSG_COPY Linux; _GNU_SOURCE. POSIX.1-2001, SVr4. msgp glibc 2.0 2.1 struct msgbuf *. glibc 2.2 , SUSv2 SUSv3, void *. msgsnd() : MSGMAX : 8192 ( Linux /proc/sys/kernel/msgmax). MSGMNB , ( 16384 ). Linux /proc/sys/kernel/msgmnb. (Linux: CAP_SYS_RESOURCE) MSGMNB msgctl() IPC_SET. (MSGTQL) (MSGPOOL). Linux 3.13 , msgrcv() MSG_COPY flag, IPC_NOWAIT, msgtyp , , . written to the queue. , , msgtyp. Linux 3.14. MSG_COPY MSC_EXCEPT msgflg ( - msgtyp). Linux 3.13 msgrcv(). Linux 3.14. msgsnd() msgrcv(). -s , -- -r . : $ ./a.out -s : Wed Mar 4 16:25:45 2015 $ ./a.out -r : 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) aereiae , Alexey , Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , ITriskTI , Max Is , Yuri Kozlov , ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . MSGOP(2)