The Linux Programming Interface: a linux and unix system Programming Handbook



tải về 6.93 Mb.
Chế độ xem pdf
trang798/806
Chuyển đổi dữ liệu08.07.2022
Kích6.93 Mb.
#52607
1   ...   794   795   796   797   798   799   800   801   ...   806
The Linux Programming Interface

Listing 46-3: Using 
msgrcv()
to read a message
–––––––––––––––––––––––––––––––––––––––––––––––––––––
svmsg/svmsg_receive.c
#define _GNU_SOURCE /* Get definition of MSG_EXCEPT */
#include 
#include 
#include "tlpi_hdr.h"
#define MAX_MTEXT 1024
struct mbuf {
long mtype; /* Message type */
char mtext[MAX_MTEXT]; /* Message body */
};
static void
usageError(const char *progName, const char *msg)
{
if (msg != NULL)
fprintf(stderr, "%s", msg);
fprintf(stderr, "Usage: %s [options] msqid [max-bytes]\n", progName);
fprintf(stderr, "Permitted options are:\n");
fprintf(stderr, " -e Use MSG_NOERROR flag\n");
fprintf(stderr, " -t type Select message of given type\n");
fprintf(stderr, " -n Use IPC_NOWAIT flag\n");


946
Chapter 46
#ifdef MSG_EXCEPT
fprintf(stderr, " -x Use MSG_EXCEPT flag\n");
#endif
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
int msqid, flags, type;
ssize_t msgLen;
size_t maxBytes;
struct mbuf msg; /* Message buffer for msgrcv() */
int opt; /* Option character from getopt() */
/* Parse command-line options and arguments */
flags = 0;
type = 0;
while ((opt = getopt(argc, argv, "ent:x")) != -1) {
switch (opt) {
case 'e': flags |= MSG_NOERROR; break;
case 'n': flags |= IPC_NOWAIT; break;
case 't': type = atoi(optarg); break;
#ifdef MSG_EXCEPT
case 'x': flags |= MSG_EXCEPT; break;
#endif
default: usageError(argv[0], NULL);
}
}
if (argc < optind + 1 || argc > optind + 2)
usageError(argv[0], "Wrong number of arguments\n");
msqid = getInt(argv[optind], 0, "msqid");
maxBytes = (argc > optind + 1) ?
getInt(argv[optind + 1], 0, "max-bytes") : MAX_MTEXT;
/* Get message and display on stdout */
msgLen = msgrcv(msqid, &msg, maxBytes, type, flags);
if (msgLen == -1)
errExit("msgrcv");
printf("Received: type=%ld; length=%ld", msg.mtype, (long) msgLen);
if (msgLen > 0)
printf("; body=%s", msg.mtext);
printf("\n");
exit(EXIT_SUCCESS);
}
–––––––––––––––––––––––––––––––––––––––––––––––––––––

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   794   795   796   797   798   799   800   801   ...   806




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương