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



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

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.


System V Message Queues
941
To send a message with msgsnd(), we must set the mtype field of the message structure
to a value greater than 0 (we see how this value is used when we discuss msgrcv() in
the next section) and copy the desired information into the programmer-defined mtext
field. The msgsz argument specifies the number of bytes contained in the mtext field.
When sending messages with msgsnd(), there is no concept of a partial write as
with write(). This is why a successful msgsnd() needs only to return 0, rather
than the number of bytes sent.
The final argument, msgflg, is a bit mask of flags controlling the operation of msgsnd().
Only one such flag is defined:
IPC_NOWAIT
Perform a nonblocking send. Normally, if a message queue is full, msgsnd()
blocks until enough space has become available to allow the message to be
placed on the queue. However, if this flag is specified, then msgsnd()
returns immediately with the error 
EAGAIN
.
A msgsnd() call that is blocked because the queue is full may be interrupted by a signal
handler. In this case, msgsnd() always fails with the error 
EINTR
. (As noted in Sec-
tion 21.5, msgsnd() is among those system calls that are never automatically restarted,
regardless of the setting of the 
SA_RESTART
flag when the signal handler is established.)
Writing a message to a message queue requires write permission on the queue.
The program in Listing 46-2 provides a command-line interface to the msgsnd()
system call. The command-line format accepted by this program is shown in the
usageError() function. Note that this program doesn’t use the msgget() system call. (We
noted that a process doesn’t need to use a get call in order to access an IPC object in
Section 45.1.) Instead, we specify the message queue by providing its identifier as a
command-line argument. We demonstrate the use of this program in Section 46.2.2.

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   790   791   792   793   794   795   796   797   ...   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