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


Using a single message queue for server and clients



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

Using a single message queue for server and clients
Using a single message queue may be suitable when the messages exchanged
between servers and clients are small. However, note the following points:
z
Since multiple processes may attempt to read messages at the same time, we
must use the message type (mtype) field to allow each process to select only
those messages intended for it. One way to accomplish this is to use the client’s
process ID as the message type for messages sent from the server to the client.
The client can send its process ID as part of its message(s) to the server. Fur-
thermore, messages to the server must also be distinguished by a unique message
type. For this purpose, we can use the number 1, which, being the process ID
of the permanently running init process, can never be the process ID of a client
process. (An alternative would be to use the server’s process ID as the message
type; however, it is difficult for the clients to obtain this information.) This
numbering scheme is shown in Figure 46-2.
z
Message queues have a limited capacity. This has the potential to cause a couple
of problems. One of these is that multiple simultaneous clients could fill the
message queue, resulting in a deadlock situation, where no new client requests
can be submitted and the server is blocked from writing any responses. The
other problem is that a poorly behaved or intentionally malicious client may
fail to read responses from the server. This can lead to the queue becoming
clogged with unread messages, preventing any communication between clients
and server. (Using two queues—one for messages from clients to the server,
and the other for messages from the server to the clients—would solve the first
of these problems, but not the second.)
Figure 46-2: Using a single message queue for client-server IPC
Using one message queue per client
Using one message queue per client (as well as one for the server) is preferable
where large messages need to be exchanged, or where there is potential for the
message queue
Client sends request
(mtype = 1, mtext
includes client PID)
1
Server sends
response (mtype
= PID of client)
3
Server reads
request (select
msgtyp = 1)
2
Client reads
response (select
msgtyp = own PID)
4
Client
Server


System V Message Queues
955
problems listed above when using a single message queue. Note the following
points regarding this approach:
z
Each client must create its own message queue (typically using the 
IPC_PRIVATE
key) and inform the server of the queue’s identifier, usually by transmitting the
identifier as part of the client’s message(s) to the server.
z
There is a system-wide limit (
MSGMNI
) on the number of message queues, and the
default value for this limit is quite low on some systems. If we expect to have a
large number of simultaneous clients, we may need to raise this limit.
z
The server should allow for the possibility that the client’s message queue no
longer exists (perhaps because the client prematurely deleted it).
We say more about using one message queue per client in the next section.

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   798   799   800   801   802   803   804   805   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