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



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

Figure 46-1: Example of a message queue containing messages of different types
As with msgsnd(), if a blocked msgrcv() call is interrupted by a signal handler, then
the call fails with the error 
EINTR
, regardless of the setting of the 
SA_RESTART
flag
when the signal handler was established.
Reading a message from a message queue requires read permission on the queue.
Example program
The program in Listing 46-3 provides a command-line interface to the msgrcv() system
call. The command-line format accepted by this program is shown in the
usageError() function. Like the program in Listing 46-2, which demonstrated the use
of msgsnd(), this program doesn’t use the msgget() system call, but instead expects a
message queue identifier as its command-line argument.
Message type
(mtype)
1
300
2
100
3
200
4
400
5
100
Message body
(mtext)
...
...
...
...
...
queue
position


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.

tải về 6.93 Mb.

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