System
V Message Queues
945
The following shell session demonstrates the use of the programs in Listing 46-1,
Listing 46-2, and Listing 46-3. We begin by creating a message queue using the
IPC_PRIVATE
key, and then write three messages with different types to the queue:
$
./svmsg_create -p
32769
ID
of message queue
$
./svmsg_send 32769 20 "I hear and I forget."
$
./svmsg_send 32769 10 "I see and I remember."
$
./svmsg_send 32769 30 "I do and I understand."
We then use the program in Listing 46-3 to read messages with a type less than or
equal to 20 from the queue:
$
./svmsg_receive -t -20 32769
Received: type=10; length=22; body=I see and I remember.
$
./svmsg_receive -t -20 32769
Received: type=20; length=21; body=I hear and I forget.
$
./svmsg_receive -t -20 32769
The last of the above commands blocked, because there was no message in the
queue whose type was less than or equal to 20. So, we
continue by typing Control-C
to terminate the command, and then execute a command that reads a
message of
any type from the queue:
Type Control-C to terminate program
$
./svmsg_receive 32769
Received: type=30; length=23; body=I do and I understand.
Chia sẻ với bạn bè của bạn: