940 Chapter 46
case 'x':
flags |= IPC_EXCL;
break;
default:
usageError(argv[0], "Bad option\n");
}
}
if (numKeyFlags != 1)
usageError(argv[0], "Exactly one of the options -f, -k, "
"or -p must be supplied\n");
perms = (optind == argc)? (S_IRUSR | S_IWUSR) :
getInt(argv[optind], GN_BASE_8, "octal-perms");
msqid = msgget(key, flags | perms);
if (msqid == -1)
errExit("msgget");
printf("%d\n", msqid);
exit(EXIT_SUCCESS);
}
––––––––––––––––––––––––––––––––––––––––––––––––––––––svmsg/svmsg_create.c
46.2 Exchanging Messages
The msgsnd() and msgrcv() system calls perform I/O on message queues. The first
argument to both system calls (msqid) is a message queue identifier. The second
argument, msgp, is a pointer to a programmer-defined structure used to hold the
message being sent or received. This structure has the following general form:
struct mymsg {
long mtype; /* Message type */
char mtext[]; /* Message body */
}
This definition is really just shorthand for saying that the first part of a message
contains the message type, specified as a long integer, while the remainder of the
message is a programmer-defined structure of arbitrary length and content; it need
not be an array of characters. Thus, the mgsp argument is typed as void * to allow it
to be a pointer to any type of structure.
A zero-length mtext field is permitted, and is sometimes useful if the informa-
tion to be conveyed can be encoded solely in the message type or if the existence of
a message is in itself sufficient information for the receiving process.
46.2.1 Sending Messages
The msgsnd() system call writes a message to a message queue.