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


Algorithm Employed by System V IPC



tải về 6.93 Mb.
Chế độ xem pdf
trang786/806
Chuyển đổi dữ liệu08.07.2022
Kích6.93 Mb.
#52607
1   ...   782   783   784   785   786   787   788   789   ...   806
The Linux Programming Interface

45.5
Algorithm Employed by System V IPC 
get Calls
Figure 45-1 shows some of the structures used internally by the kernel to represent
information about System V IPC objects (in this case semaphores, but the details
are similar for other IPC mechanisms), including the fields used to calculate IPC
keys. For each IPC mechanism (shared memory, message queue, or semaphore),


932
Chapter 45
the kernel maintains an associated ipc_ids structure that records various global
information about all instances of that IPC mechanism. This information includes
a dynamically sized array of pointers, entries, to the associated data structure for
each object instance (semid_ds structures in the case of semaphores). The current
size of the entries array is recorded in the size field, with the max_id field holding the
index of the highest currently in-use element.
Figure 45-1: Kernel data structures used to represent System V IPC (semaphore) objects
When an IPC get call is made, the algorithm used on Linux (other systems use simi-
lar algorithms) is approximately as follows:
1. The list of associated data structures (pointed to by elements of the entries
array) is searched for one whose key field matches that specified in the get call.
a) If no match is found, and 
IPC_CREAT
was not specified, then the error 
ENOENT
is returned.
b) If a match is found, but both 
IPC_CREAT
and 
IPC_EXCL
were specified, then the
error 
EEXIST
is returned.
c) Otherwise, if a match is found, then the following step is skipped.
2. If no match was found, and 
IPC_CREAT
was specified, then a new mechanism-specific
associated data structure (semid_ds in Figure 45-1) is allocated and initialized.
This also involves updating various fields of the ipc_ids structure, and may
involve resizing the entries array. A pointer to the new structure is placed in the
first free element of entries. Two substeps are included as part of this initialization:
a) The key value supplied in the get call is copied into the xxx_perm.__key field
of the newly allocated structure.
b) The current value of the seq field of the ipc_ids structure is copied into the
xxx_perm.__seq field of the associated data structure, and the seq field is
incremented by one.
seq = 10
entries
0
1
2
3
sem_perm.__key = 0x4d0731db
sem_perm.__seq = 9
sem_perm.__key = 0x4b079002
sem_perm.__seq = 5

tải về 6.93 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   782   783   784   785   786   787   788   789   ...   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