📄 ipc.texi
字号:
@exampleint msgsnd (int msqid, struct msgbuf *msgp, int msgsz, int msgflg);@end example@itemize @bullet@item@code{msqid} : id obtained by a call to msgget.@item@code{msgsz} : size of msg text (@code{mtext}) in bytes.@item@code{msgp} : message to be sent. (msgp->mtype must be positive).@item@code{msgflg} : IPC_NOWAIT.@itemreturns : msgsz on success. -1 on error.@end itemizeThe message text and type are stored in the internal @code{msg}structure. @code{msg_cbytes}, @code{msg_qnum}, @code{msg_lspid},and @code{msg_stime} fields are updated. Readers waiting on thequeue are awakened.@refill@noindentErrors:@*@noindentEACCES : Do not have write permission on queue.@*@noindentEAGAIN : IPC_NOWAIT specified and queue is full.@*@noindentEFAULT : msgp not accessible.@*@noindentEIDRM : The message queue was removed.@*@noindentEINTR : Full queue ... would have slept but ... was interrupted.@*@noindentEINVAL : mtype < 1, msgsz > MSGMAX, msgsz < 0, msqid < 0 or unused.@*@noindentENOMEM : Could not allocate space for header and text.@*@node msgrcv, msgctl, msgsnd, Messages@subsection msgrcv@exampleint msgrcv (int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);@end example@itemize @bullet@itemmsqid : id obtained by a call to msgget.@itemmsgsz : maximum size of message to receive.@itemmsgp : allocated by user to store the message in.@itemmsgtyp :@itemize @asis@item0 => get first message on queue.@item> 0 => get first message of matching type.@item< 0 => get message with least type which is <= abs(msgtyp). @end itemize@itemmsgflg :@itemize @asis@itemIPC_NOWAIT : Return immediately if message not found.@itemMSG_NOERROR : The message is truncated if it is larger than msgsz.@itemMSG_EXCEPT : Used with msgtyp > 0 to receive any msg except of specifiedtype.@refill@end itemize@itemreturns : size of message if found. -1 on error.@end itemizeThe first message that meets the @code{msgtyp} specification isidentified. For msgtyp < 0, the entire queue is searched for themessage with the smallest type.@refillIf its length is smaller than msgsz or if the user specified theMSG_NOERROR flag, its text and type are copied to msgp->mtext andmsgp->mtype, and it is taken off the queue.@refillThe @code{msg_cbytes}, @code{msg_qnum}, @code{msg_lrpid},and @code{msg_rtime} fields are updated. Writers waiting on thequeue are awakened.@refill@noindentErrors:@*@noindentE2BIG : msg bigger than msgsz and MSG_NOERROR not specified.@*@noindentEACCES : Do not have permission for reading the queue.@*@noindentEFAULT : msgp not accessible.@*@noindentEIDRM : msg queue was removed.@*@noindentEINTR : msg not found ... would have slept but ... was interrupted.@*@noindentEINVAL : msgsz > msgmax or msgsz < 0, msqid < 0 or unused.@*@noindentENOMSG : msg of requested type not found and IPC_NOWAIT specified.@node msgctl, msglimits, msgrcv, Messages@subsection msgctl@exampleint msgctl (int msqid, int cmd, struct msqid_ds *buf);@end example@itemize @bullet@itemmsqid : id obtained by a call to msgget.@itembuf : allocated by user for reading/writing info.@itemcmd : IPC_STAT, IPC_SET, IPC_RMID (@xref{syscalls}).@end itemizeIPC_STAT results in the copy of the queue data structureinto the user supplied buffer.@refillIn the case of IPC_SET, the queue size (@code{msg_qbytes})and the @code{uid}, @code{gid}, @code{mode} (low 9 bits) fieldsof the @code{msg_perm} struct are set from the user supplied values.@code{msg_ctime} is updated.@refillNote that only the super user may increase the limit on the size of amessage queue beyond MSGMNB.@refillWhen the queue is destroyed (IPC_RMID), the sequence number isincremented and all waiting readers and writers are awakened.These processes will then return with @code{errno} set to EIDRM.@refill@noindentErrors:@noindentEPERM : Insufficient privilege to increase the size of the queue (IPC_SET)or remove it (IPC_RMID).@*@noindentEACCES : Do not have permission for reading the queue (IPC_STAT).@*@noindentEFAULT : buf not accessible (IPC_STAT, IPC_SET).@*@noindentEIDRM : msg queue was removed.@*@noindentEINVAL : invalid cmd, msqid < 0 or unused.@node msglimits, Semaphores, msgctl, Messages@subsection Limis on Message Resources@noindentSizeof various structures:@itemize @asis@itemmsqid_ds 52 /* 1 per message queue .. dynamic */@itemmsg 16 /* 1 for each message in system .. dynamic */@itemmsgbuf 8 /* allocated by user */@end itemize@noindentLimits@itemize @bullet@itemMSGMNI : number of message queue identifiers ... policy.@itemMSGMAX : max size of message.Header and message space allocated on one page.MSGMAX = (PAGE_SIZE - sizeof(struct msg)).Implementation maximum MSGMAX = 4080.@refill@itemMSGMNB : default max size of a message queue ... policy.The super-user can increase the size of aqueue beyond MSGMNB by a @code{msgctl} call.@refill@end itemize@noindentUnused or unimplemented:@*MSGTQL max number of message headers system-wide.@*MSGPOOL total size in bytes of msg pool. @node Semaphores, semget, msglimits, Top@section SemaphoresEach semaphore has a value >= 0. An id provides access to an arrayof @code{nsems} semaphores. Operations such as read, increment or decrementsemaphores in a set are performed by the @code{semop} call which processes@code{nsops} operations at a time. Each operation is specified in a struct@code{sembuf} described below. The operations are applied only if all ofthem succeed.@refillIf you do not have a need for such arrays, you are probably better off usingthe @code{test_bit}, @code{set_bit} and @code{clear_bit} bit-operationsdefined in <asm/bitops.h>.@refillSemaphore operations may also be qualified by a SEM_UNDO flag whichresults in the operation being undone when the process exits.@refillIf a decrement cannot go through, a process will be put to sleepon a queue waiting for the @code{semval} to increase unless it specifiesIPC_NOWAIT. A read operation can similarly result in a sleep on aqueue waiting for @code{semval} to become 0. (Actually there aretwo queues per semaphore array).@refill @noindentA semaphore array is described by:@examplestruct semid_ds struct ipc_perm sem_perm; time_t sem_otime; /* last semop time */ time_t sem_ctime; /* last change time */ struct wait_queue *eventn; /* wait for a semval to increase */ struct wait_queue *eventz; /* wait for a semval to become 0 */ struct sem_undo *undo; /* undo entries */ ushort sem_nsems; /* no. of semaphores in array */@end example@noindentEach semaphore is described internally by :@examplestruct sem short sempid; /* pid of last semop() */ ushort semval; /* current value */ ushort semncnt; /* num procs awaiting increase in semval */ ushort semzcnt; /* num procs awaiting semval = 0 */@end example@menu* semget::* semop::* semctl::* semlimits:: Limits imposed by this implementation.@end menu@node semget, semop, Semaphores, Semaphores@subsection semget@noindentA semaphore array is allocated by a semget system call:@examplesemid = semget (key_t key, int nsems, int semflg);@end example@itemize @bullet@item@code{key} : an integer usually got from @code{ftok} or IPC_PRIVATE@item@code{nsems} :@itemize @asis@item# of semaphores in array (0 <= nsems <= SEMMSL <= SEMMNS)@item0 => dont care can be used when not creating the resource.If successful you always get access to the entire array anyway.@refill@end itemize@itemsemflg :@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 : semid on success. -1 on failure.@end itemizeAn array of nsems semaphores is allocated if there is no resourcecorresponding to the given key. The access permissions specified arethen copied into the @code{sem_perm} struct for the array along with theuser-id etc. The user must use the IPC_CREAT flag or key = IPC_PRIVATEif a new resource is to be created.@refill@noindentErrors:@*@noindentEINVAL : nsems not in above range (allocate).@* nsems greater than number in array (procure).@*@noindentEEXIST : (allocate) IPC_CREAT | IPC_EXCL specified and resource exists.@*@noindentEIDRM : (procure) The resource was removed.@*@noindentENOMEM : could not allocate space for semaphore array.@*@noindentENOSPC : No arrays available (SEMMNI), too few semaphores available (SEMMNS).@*@noindentENOENT : Resource does not exist and IPC_CREAT not specified.@*@noindentEACCES : (procure) do not have permission for specified access.@node semop, semctl, semget, Semaphores@subsection semop@noindentOperations on semaphore arrays are performed by calling semop :@exampleint semop (int semid, struct sembuf *sops, unsigned nsops);@end example@itemize @bullet@itemsemid : id obtained by a call to semget.@itemsops : array of semaphore operations.@itemnsops : number of operations in array (0 < nsops < SEMOPM).@itemreturns : semval for last operation. -1 on failure.@end itemize@noindentOperations are described by a structure sembuf:@examplestruct sembuf ushort sem_num; /* semaphore index in array */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */@end exampleThe value @code{sem_op} is to be added (signed) to the current value semvalof the semaphore with index sem_num (0 .. nsems -1) in the set.Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO.@refill@noindentTwo kinds of operations can result in wait:@enumerate@itemIf sem_op is 0 (read operation) and semval is non-zero, the processsleeps on a queue waiting for semval to become zero or returns witherror EAGAIN if (IPC_NOWAIT | sem_flg) is true.@refill@itemIf (sem_op < 0) and (semval + sem_op < 0), the process either sleepson a queue waiting for semval to increase or returns with error EAGAIN if(sem_flg & IPC_NOWAIT) is true.@refill@end enumerateThe array sops is first read in and preliminary checks performed onthe arguments. The operations are parsed to determine if any ofthem needs write permissions or requests an undo operation.@refillThe operations are then tried and the process sleeps if any operationthat does not specify IPC_NOWAIT cannot go through. If a process sleepsit repeats these checks on waking up. If any operation that requestsIPC_NOWAIT, cannot go through at any stage, the call returns with errnoset to EAGAIN.@refillFinally, operations are committed when all go through without an interveningsleep. Processes waiting on the zero_queue or increment_queue are awakenedif any of the semval's becomes zero or is incremented respectively.@refill@noindentErrors:@*@noindentE2BIG : nsops > SEMOPM.@*@noindentEACCES : Do not have permission for requested (read/alter) access.@*@noindentEAGAIN : An operation with IPC_NOWAIT specified could not go through.@*@noindentEFAULT : The array sops is not accessible.@*@noindentEFBIG : An operation had semnum >= nsems.@*@noindentEIDRM : The resource was removed.@*@noindentEINTR : The process was interrupted on its way to a wait queue.@*@noindentEINVAL : nsops is 0, semid < 0 or unused.@*@noindentENOMEM : SEM_UNDO requested. Could not allocate space for undo structure.@*@noindentERANGE : sem_op + semval > SEMVMX for some operation.@node semctl, semlimits, semop, Semaphores@subsection semctl@exampleint semctl (int semid, int semnum, int cmd, union semun arg);@end example@itemize @bullet@itemsemid : id obtained by a call to semget.@itemcmd :@itemize @asis@item GETPID return pid for the process that executed the last semop.@item GETVAL return semval of semaphore with index semnum.@item GETNCNT return number of processes waiting for semval to increase.@item GETZCNT return number of processes waiting for semval to become 0@item SETVAL set semval = arg.val.@item GETALL read all semval's into arg.array.@item SETALL set all semval's with values given in arg.array.@end itemize@itemreturns : 0 on success or as given above. -1 on failure.@end itemizeThe first 4 operate on the semaphore with index semnum in the set.The last two operate on all semaphores in the set.@refill@code{arg} is a union :@exampleunion semun int val; value for SETVAL. struct semid_ds *buf; buffer for IPC_STAT and IPC_SET. ushort *array; array for GETALL and SETALL@end example@itemize @bullet@item
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -