📄 inter process communication.txt
字号:
GETZCNT
the number of waiting tasks (see description).
For other values of cmd, it returns zero. On error, the call returns -1 and errno is set to
one of the following values:
EACCESS, EFAULT, EIDRM
EINVAL: invalid value for cmd or semid.
EPERM: the calling task tried IPC_SET or IPC_RMID but is not the creator or the owner of
the semaphore set or the superuser.
ERANGE: the specified value for the semaphore is out of range.
**************************************************************************************
semget
SYNOPSIS
int semget(key_t key, int nsems, int semflg);
PARAMETERS
key: [in] the semaphore set identificator.
nsems: [in] the number of semaphore in the set.
semflg: [in] flags (see description).
DESCRIPTION
Gets a semaphore set identifier. If key is IPC_PRIVATE, a new set is created. Otherwise,
the result depends on the value of semflg:
IPC_CREAT
creates a new queue for the key if it does not already exist.
IPC_EXCL
if there is already a existing queue associated with key, the call fails.
The 9 lower bits of semflg specify the permission bits of the new set. They have the same
layout and meaning as those for files. However, the execute permissions are meaningless for
sets.
When creating a set the system sets the appropriate parameters in the semid_ds structure
associated with the new set. When accessing an already existing set, the system simply
check if the set can be accessed.
RETURN VALUE
On success, the call returns the new semaphore set identificator. On error -1 is returned
and errno is set to one of the following values:
EACCESS: the task has no access permission to the set.
EEXIST: IPC_CREAT and IPC_EXCL were specified and the set already exists.
EIDRM: the set no longer exists in the system.
ENOENT: the set never existed.
ENOSPC: the maximum number of semaphore sets for the system has been reached.
ENOMEM
**************************************************************************************
semop
SYNOPSIS
int semop(int semid, struct sembuf *sops, unsigned nsops);
PARAMETERS
semid: [in] the semaphore set on which to perform the operations.
sops: [in] points to an array of operations to perfrom.
nsops: [in] the number of element in sops.
DESCRIPTION
Perform operations on a semaphore set. The sops parameter a structure sembuf of the form:
short sem_num
which semaphore to operate on (0 is the first).
short sem_op;
the operation.
short sem_flg;
sem_flg may be IPC_NOWAIT for non blocking calls and SEM_UNDO to specify an operation that
will be undone on task termination. The individual operations are perfomed if and only if
all of them will succeed.
The possible values of cmd are:
cmd positive: the value of cmd is added to the value of the semaphore. The calling task must
have alter privileges on the semaphore set.
cmd equals to zero: if the value of the semaphore is zero, the call returns. Otherwise, the
call block until: the value of the semaphore becomes zero, the semaphore set is removed, or
a signal occurs. The calling task must have read privileges on the semaphore set.
cmd is lower than zero: if the value of the semaphore is greater than the absolute value of
cmd, the value of the semaphore is decremented by the value of cmd and the call returns.
Otherwise, the call block until: the value of the semaphore becomes greater than the
absolute value of cmd at which time the value of the semaphore is decremented by the value
of cmd and then the call returns, the semaphore set is removed, or a signal occurs. The
calling task must have alter privileges on the semaphore set.
Whenever a call succeed, the sempid member of the semaphore set structure is set to the pid
of the current task and the sem_ctime and sem_otime member are set to the current time.
RETURN VALUE
On success, the call returns zero. On error -1 is returned and errno is set to one of the
following values:
E2BIG: there is too many operations requested (the maximum is SEMOPM.
EFBIG: the semaphore number of some operation is out of range.
EINVAL: invalid value for semid or nsops.
ERANGE: semaphore value out of range.
EACCESS, EAGAIN, EFAULT, EIDRM, EINTR or ENOMEM.
**************************************************************************************
shmat and shmdt
SYNOPSIS
char *shmat(int shmid, char *shmaddr, int shmflg);
int shmdt(char *shmaddr);
PARAMETERS
shmid: [in] the shared memory identificator.
shmaddr: [in] the start of the shared memory.
shmflg: [in] some flags (see description).
DESCRIPTION
shmat attaches a shared memory identificator to a memory range in the task address space.
If shmaddr is zero the range where to attach the shared memory is choosen by the system.
If shmaddr is not zero and the flag SHM_RND is specified, the address is rounded down at a
multiple of SHMBLA. In any other cases, the address must be page alligned.
If the flag SHM_RDONLY is specified the task must have read privileges to the segment and
it is attached for reading only. Otherwise, the task must have read and write privileges
on the segment and it is attached for reading and writing. The same segment may be attached
more than once is the same address space with different flags.
On success, the following members of the shmid_ds structure of the segment are modified:
shm_atime
is set to the current time.
shm_lpid
is set to the current pid.
shm_nattch
is incremented by one.
shmdt detaches shared memory segments attached by shmat. The shmaddr parameter must be the
same value returned by the shmat that attached the segment.
On success, the following members of the shmid_ds structure of the segment are modified:
shm_dtime
is set to the current time.
shm_lpid
is set to the current pid.
shm_nattch
is decremented by one. If it becomes 0 and the segment is marked for deletionm, the segment
is deleted.
Attached memory segments are inherited through the fork call. They are detached after a
exec or exit call.
RETURN VALUE
On success shmat returns the address of the new memory segment and shmdt returns zero. On
error, both calls return -1, and set errno. The possibles values of errno are:
for shmat:
EACCESS: not enough privileges on the segment.
EINVAL: shmid or shmaddr invalid.
ENOMEM.
for shmdt: the only possible value is EINVAL for an invalid value of shmaddr.
**************************************************************************************
shmctl
SYNOPSIS
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
PARAMETERS
shmid: [in] the memory segment to manipulate.
cmd: [in] the operation to perform.
buf: [in out] parameter for the operation (see description).
DESCRIPTION
This calls manipulates some operational parameters of shared memory segments. The shmid_ds
structure has the following layout:
struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
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 */
unsigned short shm_cpid; /* pid of creator */
unsigned short shm_lpid; /* pid of last operator */
short shm_nattch; /* no. of current attaches */
/* the following are private */
unsigned short shm_npages; /* size of segment (pages) */
unsigned long *shm_pages; /* array of ptrs to frames -> SHMMAX */
struct shm_desc *attaches; /* descriptors for attaches */
};
cmd can take the following values:
IPC_STAT
retreives the shmid_ds structure of the memory segment. The calling taks must have read
privileges on the segment.
IPC_SET
sets the shmid_ds structure of the memory segment. The calling task must have alter
privileges on the segment. Only the members uid, gid and the lower 9 bits of mode of
shm_perm can be modified.
IPC_RMID
mark the shared memory segment as destroyed. The calling taks must have the same uid of
the creator or the owner of the memory segment or be the superuser.
SHM_LOCK
prevents the memory segment to be swaped out to disk. Only a task with superuser privileges
may use this command.
SHM_UNLOCK
allows the memory segment to be swaped out to disk. Only a task with superuser privileges
may use this command.
RETURN VALUE
On success, the call returns zero. On error -1 is returned and errno is set to one of the
following values:
EINVAL: shmid or cmd are not valid.
EPERM: the calling task does not have enough privileges on the memory segment to perform
the operation.
EFAULT or EIDRM.
**************************************************************************************
shmget
SYNOPSIS
int shmget(key_t key, int size, int shmflg);
PARAMETERS
key: [in] the shared memory segment identificator.
size: [in] size of the segment.
shmflg: [in] flags (see description).
DESCRIPTION
Gets a shared memory segment identifier. If key is IPC_PRIVATE, a new segment is created.
Otherwise, the result depends on the value of shmflg:
IPC_CREAT
creates a new segment for the key if it does not already exist.
IPC_EXCL
if there is already a existing segment associated with key, the call fails.
The value of size is rounded up to a multiple of PAGE_SIZE.
The 9 lower bits of shmflg specify the permission bits of the new segment. They have the
same layout and meaning as those for files. However, the execute permissions are
meaningless for segments.
When creating a segment the system sets the appropriate parameters in the shmid_ds structure
associated with the new segment. When accessing an already existing segment, the system
simply check if the segment can be accessed.
RETURN VALUE
On success, the call returns the new shared memory segment identificator. On error -1 is
returned and errno is set to one of the following values:
EACCESS: the task has no access permission to the segment.
EEXIST: IPC_CREAT and IPC_EXCL were specified and the segment already exists.
EIDRM: the segment no longer exists in the system.
ENOENT: the segment never existed.
ENOSPC: the maximum number of shared memory segment for the system has been reached.
EINVAL: size is outside the range [SHMMIN,SHMMAX] or is greater than the size of the
segment (in the case where it already exists).
ENOMEM
**************************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -