📄 ipc.texi
字号:
IPC_SET, SETVAL, SETALL : sem_ctime is updated.@itemSETVAL, SETALL : Undo entries are cleared for altered semaphores inall processes. Processes sleeping on the wait queues areawakened if a semval becomes 0 or increases.@refill@itemIPC_SET : sem_perm.uid, sem_perm.gid, sem_perm.mode are updated fromuser supplied values.@refill@end itemize@noindentErrors:@noindentEACCES : do not have permission for specified access.@*@noindentEFAULT : arg is not accessible.@*@noindentEIDRM : The resource was removed.@*@noindentEINVAL : semid < 0 or semnum < 0 or semnum >= nsems.@*@noindentEPERM : IPC_RMID, IPC_SET ... not creator, owner or super-user.@*@noindentERANGE : arg.array[i].semval > SEMVMX or < 0 for some i.@node semlimits, Shared Memory, semctl, Semaphores@subsection Limits on Semaphore Resources@noindentSizeof various structures:@examplesemid_ds 44 /* 1 per semaphore array .. dynamic */sem 8 /* 1 for each semaphore in system .. dynamic */sembuf 6 /* allocated by user */sem_undo 20 /* 1 for each undo request .. dynamic */@end example@noindentLimits :@*@itemize @bullet@itemSEMVMX 32767 semaphore maximum value (short).@itemSEMMNI number of semaphore identifiers (or arrays) system wide...policy.@itemSEMMSL maximum number of semaphores per id.1 semid_ds per array, 1 struct sem per semaphore=> SEMMSL = (PAGE_SIZE - sizeof(semid_ds)) / sizeof(sem).Implementation maximum SEMMSL = 500.@refill@itemSEMMNS maximum number of semaphores system wide ... policy.Setting SEMMNS >= SEMMSL*SEMMNI makes it irrelevent.@refill@itemSEMOPM Maximum number of operations in one semop call...policy.@end itemize@noindentUnused or unimplemented:@*@noindentSEMAEM adjust on exit max value.@*@noindentSEMMNU number of undo structures system-wide.@*@noindentSEMUME maximum number of undo entries per process.@node Shared Memory, shmget, semlimits, Top@section Shared MemoryShared memory is distinct from the sharing of read-only code pages orthe sharing of unaltered data pages that is available due to thecopy-on-write mechanism. The essential difference is that theshared pages are dirty (in the case of Shared memory) and can bemade to appear at a convenient location in the process' address space.@refill@noindentA shared segment is described by :@examplestruct shmid_ds struct ipc_perm shm_perm; int shm_segsz; /* size of segment (bytes) */ time_t shm_atime; /* last attach time */ time_t shm_dtime; /* last detach time */ time_t shm_ctime; /* last change time */ ulong *shm_pages; /* internal page table */ ushort shm_cpid; /* pid, creator */ ushort shm_lpid; /* pid, last operation */ short shm_nattch; /* no. of current attaches */@end exampleA shmget allocates a shmid_ds and an internal page table. A shmatmaps the segment into the process' address space with pointersinto the internal page table and the actual pages are faulted inas needed. The memory associated with the segment must be explicitlydestroyed by calling shmctl with IPC_RMID.@refill@menu* shmget::* shmat::* shmdt::* shmctl::* shmlimits:: Limits imposed by this implementation.@end menu@node shmget, shmat, Shared Memory, Shared Memory@subsection shmget@noindentA shared memory segment is allocated by a shmget system call:@exampleint shmget(key_t key, int size, int shmflg);@end example@itemize @bullet@itemkey : an integer usually got from @code{ftok} or IPC_PRIVATE@itemsize : size of the segment in bytes (SHMMIN <= size <= SHMMAX).@itemshmflg :@itemize @asis@itemIPC_CREAT used to create a new resource@itemIPC_EXCL used with IPC_CREAT to ensure failure if the resource exists.@itemrwxrwxrwx access permissions.@end itemize@itemreturns : shmid on success. -1 on failure.@end itemizeA descriptor for a shared memory segment is allocated if there isn't onecorresponding to the given key. The access permissions specified arethen copied into the @code{shm_perm} struct for the segment along with theuser-id etc. The user must use the IPC_CREAT flag or key = IPC_PRIVATEto allocate a new segment.@refillIf the segment already exists, the access permissions are verified,and a check is made to see that it is not marked for destruction.@refill@code{size} is effectively rounded up to a multiple of PAGE_SIZE as sharedmemory is allocated in pages.@refill@noindentErrors:@*@noindentEINVAL : (allocate) Size not in range specified above.@* (procure) Size greater than size of segment.@*@noindentEEXIST : (allocate) IPC_CREAT | IPC_EXCL specified and resource exists.@*@noindentEIDRM : (procure) The resource is marked destroyed or was removed.@*@noindentENOSPC : (allocate) All id's are taken (max of SHMMNI id's system-wide).Allocating a segment of the requested size would exceed thesystem wide limit on total shared memory (SHMALL).@refill@*@noindentENOENT : (procure) Resource does not exist and IPC_CREAT not specified.@*@noindentEACCES : (procure) Do not have permission for specified access.@*@noindentENOMEM : (allocate) Could not allocate memory for shmid_ds or pg_table.@node shmat, shmdt, shmget, Shared Memory@subsection shmat@noindentMaps a shared segment into the process' address space.@examplechar *virt_addr;virt_addr = shmat (int shmid, char *shmaddr, int shmflg);@end example@itemize @bullet@itemshmid : id got from call to shmget.@itemshmaddr : requested attach address.@* If shmaddr is 0 the system finds an unmapped region.@* If a non-zero value is indicated the value must be page aligned or the user must specify the SHM_RND flag.@refill@itemshmflg :@* SHM_RDONLY : request read-only attach.@* SHM_RND : attach address is rounded DOWN to a multiple of SHMLBA.@itemreturns: virtual address of attached segment. -1 on failure.@end itemizeWhen shmaddr is 0, the attach address is determined by finding anunmapped region in the address range 1G to 1.5G, starting at 1.5Gand coming down from there. The algorithm is very simple so youare encouraged to avoid non-specific attaches.@noindentAlgorithm:@displayDetermine attach address as described above.Check region (shmaddr, shmaddr + size) is not mapped and allocatepage tables (undocumented SHM_REMAP flag!).Map the region by setting up pointers into the internal page table.Add a descriptor for the attach to the task struct for the process.@code{shm_nattch}, @code{shm_lpid}, @code{shm_atime} are updated.@end display@noindentNotes:@*The @code{brk} value is not altered.The segment is automatically detached when the process exits.The same segment may be attached as read-only or read-write andmore than once in the process' address space.A shmat can succeed on a segment marked for destruction.The request for a particular type of attach is made using the SHM_RDONLY flag.There is no notion of a write-only attach. The requested attachpermissions must fall within those allowed by @code{shm_perm.mode}.@noindentErrors:@*@noindentEACCES : Do not have permission for requested access.@*@noindentEINVAL : shmid < 0 or unused, shmaddr not aligned, attach at brk failed.@*@noindentEIDRM : resource was removed.@*@noindentENOMEM : Could not allocate memory for descriptor or page tables.@node shmdt, shmctl, shmat, Shared Memory@subsection shmdt@exampleint shmdt (char *shmaddr);@end example@itemize @bullet@itemshmaddr : attach address of segment (returned by shmat).@itemreturns : 0 on success. -1 on failure.@end itemizeAn attached segment is detached and @code{shm_nattch} decremented. Theoccupied region in user space is unmapped. The segment is destroyedif it is marked for destruction and @code{shm_nattch} is 0.@code{shm_lpid} and @code{shm_dtime} are updated.@refill@noindentErrors:@*@noindentEINVAL : No shared memory segment attached at shmaddr.@node shmctl, shmlimits, shmdt, Shared Memory@subsection shmctl@noindentDestroys allocated segments. Reads/Writes the control structures.@exampleint shmctl (int shmid, int cmd, struct shmid_ds *buf);@end example@itemize @bullet@itemshmid : id got from call to shmget.@itemcmd : IPC_STAT, IPC_SET, IPC_RMID (@xref{syscalls}).@itemize @asis@itemIPC_SET : Used to set the owner uid, gid, and shm_perms.mode field.@itemIPC_RMID : The segment is marked destroyed. It is only destroyedon the last detach.@refill@itemIPC_STAT : The shmid_ds structure is copied into the user allocated buffer.@end itemize@itembuf : used to read (IPC_STAT) or write (IPC_SET) information.@itemreturns : 0 on success, -1 on failure.@end itemizeThe user must execute an IPC_RMID shmctl call to free the memoryallocated by the shared segment. Otherwise all the pages faulted inwill continue to live in memory or swap.@refill@noindentErrors:@*@noindentEACCES : Do not have permission for requested access.@*@noindentEFAULT : buf is not accessible.@*@noindentEINVAL : shmid < 0 or unused.@*@noindentEIDRM : identifier destroyed.@*@noindentEPERM : not creator, owner or super-user (IPC_SET, IPC_RMID).@node shmlimits, Notes, shmctl, Shared Memory@subsection Limits on Shared Memory Resources@noindentLimits:@itemize @bullet@itemSHMMNI max num of shared segments system wide ... 4096.@itemSHMMAX max shared memory segment size (bytes) ... 4M@itemSHMMIN min shared memory segment size (bytes).1 byte (though PAGE_SIZE is the effective minimum size).@refill@itemSHMALL max shared mem system wide (in pages) ... policy.@itemSHMLBA segment low boundary address multiple.Must be page aligned. SHMLBA = PAGE_SIZE.@refill@end itemize@noindentUnused or unimplemented:@*SHMSEG : maximum number of shared segments per process.@node Notes, Top, shmlimits, Top@section Miscellaneous NotesThe system calls are mapped into one -- @code{sys_ipc}. This should betransparent to the user.@refill@subsection Semaphore @code{undo} requestsThere is one sem_undo structure associated with a process foreach semaphore which was altered (with an undo request) by the process.@code{sem_undo} structures are freed only when the process exits.One major cause for unhappiness with the undo mechanism is thatit does not fit in with the notion of having an atomic set ofoperations on an array. The undo requests for an array and eachsemaphore therein may have been accumulated over many @code{semop}calls. Thus use the undo mechanism with private semaphores only.@refillShould the process sleep in @code{exit} or should all undooperations be applied with the IPC_NOWAIT flag in effect?Currently those undo operations which go through immediately areapplied and those that require a wait are ignored silently.@refill@subsection Shared memory, @code{malloc} and the @code{brk}.Note that since this section was written the implementation waschanged so that non-specific attaches are done in the region1G - 1.5G. However much of the following is still worth thinkingabout so I left it in.On many systems, the shared memory is allocated in a special regionof the address space ... way up somewhere. As mentioned earlier,this implementation attaches shared segments at the lowest possibleaddress. Thus if you plan to use @code{malloc}, it is wise to malloc alarge space and then proceed to attach the shared segments. This waymalloc sets the brk sufficiently above the region it will use.@refillAlternatively you can use @code{sbrk} to adjust the @code{brk} valueas you make shared memory attaches. The implementation is not verysmart about selecting attach addresses. Using the system defaultaddresses will result in fragmentation if detaches do not occurin the reverse sequence as attaches.@refillTaking control of the matter is probably best. The rule appliedis that attaches are allowed in unmapped regions other thanin the text space (see <a.out.h>). Also remember that attach addressesand segment sizes are multiples of PAGE_SIZE.@refillOne more trap (I quote Bruno on this). If you use malloc() to get spacefor your shared memory (ie. to fix the @code{brk}), you must ensure youget an unmapped address range. This means you must mallocate more memorythan you had ever allocated before. Memory returned by malloc(), used,then freed by free() and then again returned by malloc is no good.Neither is calloced memory.@refillNote that a shared memory region remains a shared memory region untilyou unmap it. Attaching a segment at the @code{brk} and calling mallocafter that will result in an overlap of what malloc thinks is itsspace with what is really a shared memory region. For example in the caseof a read-only attach, you will not be able to write to the overlappedportion.@refill@subsection Fork, exec and exitOn a fork, the child inherits attached shared memory segments butnot the semaphore undo information.@refillIn the case of an exec, the attached shared segments are detached.The sem undo information however remains intact.@refillUpon exit, all attached shared memory segments are detached.The adjust values in the undo structures are added to the relevant semvalsif the operations are permitted. Disallowed operations are ignored.@refill@subsection Other FeaturesThese features of the current implementation arelikely to be modified in the future.The SHM_LOCK and SHM_UNLOCK flag are available (super-user) for use with the@code{shmctl} call to prevent swapping of a shared segment. The usermust fault in any pages that are required to be present after lockingis enabled.The IPC_INFO, MSG_STAT, MSG_INFO, SHM_STAT, SHM_INFO, SEM_STAT, SEMINFO@code{ctl} calls are used by the @code{ipcs} program to provide informationon allocated resources. These can be modified as needed or moved to a procfile system interface.@sp 3Thanks to Ove Ewerlid, Bruno Haible, Ulrich Pegelow and Linus Torvaldsfor ideas, tutorials, bug reports and fixes, and merriment. And morethanks to Bruno.@contents@bye
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -