⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inter process communication.txt

📁 Linux C 语言函数
💻 TXT
📖 第 1 页 / 共 2 页
字号:
ipc

SYSNOPSIS
int sys_ipc (uint call, int first, int second, int third, void *ptr); 


PARAMETERS
call: [in] the ipc call to perform. 

first, second, third: parameters. Depend on call. 

ptr: [in] pointer to a buffer. 


DESCRIPTION
This is an in kernel wrapper for other ipc calls. call can take the following values: 


SEMOP 
sys_semop(first, (struct sembuf *)ptr, second); 

SEMGET 
sys_semget (first, second, third); 

SEMCTL 
sys_semctl (first, second, third, ptr); 

MSGSND 
sys_msgsnd (first, (struct msgbuf *) ptr, second, third); 

MSGRCV 
This one is a little tricky... 
struct ipc_kludge tmp; if (!ptr) return -EINVAL; 

memcpy_fromfs (&tmp,(struct ipc_kludge *) ptr, sizeof (tmp)); 

return sys_msgrcv (first, tmp.msgp, second, tmp.msgtyp, third); 


MSGGET 
sys_msgget ((key_t) first, second); 

MSGCTL 
sys_msgctl (first, second, (struct msqid_ds *) ptr); 

SHMAT 
sys_shmat (first, (char *) ptr, second, (ulong *) third); 

SHMDT 
sys_shmdt ((char *)ptr); 

SHMGET 
sys_shmget (first, second, third); 

SHMCTL 
sys_shmctl (first, second, (struct shmid_ds *) ptr); 


**************************************************************************************
msgctl

SYNOPSIS
int msgctl(int msqid,int cmd,struct msqid_ds *buf); 


PARAMETERS
msqid: [in] the message queue to manipulate. 

cmd: [in] the operation to perform on the message queue. 

buf: the interpretation of this parameter depends on cmd. 


DESCRIPTION
Manipulates a message queue. cmd may be one of: 


IPC_STAT 
retreives some information on the queue. 

IPC_SET 
modify some operating parameters of the queue. buf points to a msqid_ds structure. The only
modifiable parameters are: msg_perm.uid, msg_perm.gid, msg_perm.mode (only lowest 9 bits) 
and msg_qbytes. The calling task uid must be one of the creator uid or the owner uid of the
queue or the superuser uid. Only the superuser may rise msg_qbytes beyond the system limit 
of MSGMNB. 

IPC_RMID 
immediately destroys the message queue and awake all sleaping reader and writer processes. 

RETURN VALUE
On success zero is returned. On error -1 is returned and errno is set to one of the 
following values: 


EACCESS: the caller tried IPC_STAT but does not have read permission on the queue. 
EIDRM: the queue is already removed. 
EINVAL: invalid value for cmd or msqid. 
EPERM: the taks tried an operation for which it does not have the necessary privileges. 
EFAULT. 



**************************************************************************************
msgget

SYNOPSIS
int msgget(key_t key, int msgflg); 


PARAMETERS
key: [in] the message queue identifier to get. 

msgflg: [in] some flags (see description). 


DESCRIPTION
Gets a message queue identifier. If key is IPC_PRIVATE, a new queue is created, otherwise 
the result depends on msgflg. The possible values for msgflg are: 


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 msgflg specify the permission bits of the new queue. They have the same
layout and meaning as those for files. However, the execute permissions are meaningless for
queues. 

When creating a queue the system sets the appropriate parameters in the msqid_ds structure 
associated with the new queue. When accessing an already existing queue, the system simply 
check if the queue can be accessed. 


RETURN VALUE
On success, the call returns the new queue 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 queue. 
EEXIST: IPC_CREAT and IPC_EXCL were specified and the queue already exists. 
EIDRM: the message queue no longer exists in the system. 
ENOENT: the message queue never existed. 
ENOSPC: the maximum number of message queues for the system has been reached. 
ENOMEM 



**************************************************************************************
msgrcv and msgsnd

SYNOPSIS
int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg); 

int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg); 


PARAMETERS
msqid: [in] the message queue. 

msgp: for msgsnd, [in] points to the message to send. For msgrcv, [out] points to the buffer
	where to put the message. 

msgsz: [in] size of the mtext part of the buffer. The maximum possible size is MSGMAX and is
	 currently 4080 bytes. 

msgflg: [in] flags (see description). 

msgtyp: [in] the type of message to receive. 


DESCRIPTION
msgp must point to a buffer having the following structure: 


struct msgbuf {
        long mtype;    /* message type, must > 0 */
        char mtext[1]; /* message data */
}

The calling process must have read permission on the queue to call msgrcv and write 
permission on the queue to call msgsnd. 

msgsnd tries to enqueue the message. If msglfg is set to IPC_NOWAIT and the queue is full, 
the call fails. Otherwise the call blocks. If the send succeed, the message queue structure
is updated as follow: 


msg_lspid is set to the pid of the calling process. 
msg_qnum is incremented by 1. 
msg_stime is set to the current time. 

msgrcv dequeues a message from the message queue. If the message to be dequeued has a length
greater than msgsz, then the call fails. If the length is greater but the MSG_NOERROR flag 
is specified, the message gets truncated (and the truncated information is lost forever). 
The message to be dequeued can be choosed by the following values of msgtyp: 


msgtyp equals 0: in this case the 1st message on the queue is dequeued. 

msgtyp is greater than 0: if the flag MSG_EXCEPT is not specified, the first message of type
msgtyp on the queue will be dequeued, otherwise the first message not of type msgtyp on the
queue will be dequeued. 

msgtyp is less than 0: the first message of type in the range [1,-msgtyp] will be dequeued. 

If the flag IPC_NOWAIT is specified and there is no message of the specified type on the 
message queue, the call will fail, otherwise the call will block. On success, the queue 
data structure is updated as follow: 


msg_lrpid is set to the pid of the calling process. 
msg_qnum is decremented by 1. 
msg_rtime is set to the current time. 

RETURN VALUE
On success msgsnd returns zero and msgrcv returns the number of bytes copied in the mtext 
array. On error -1 is returned and errno is set to one of the following values: 

for msgsnd: 


EAGAIN: IPC_NOWAIT was specified and the queue is full. 
EACCESS: the calling task does not have write permission on the queue. 
EIDRM: the queue has been removed. 
EINVAL: invalid msqid, msgsz or mtype value. 
EFAULT, EINTR and ENOMEM. 
for msgrcv: 


E2BIG: MSG_NOERROR is not specified and the message that should be dequeued is too bigger 
	than msgsz. 
EACCESS: the calling task does not have read permission on the queue. 
EIDRM: the queue has been removed. 
EINVAL: invalid msqid or msgsz value. 
ENOMSG: IPC_NOWAIT was specified no message of the specified type is on the queue. 
EFAULT, EINTR 


**************************************************************************************
semctl

SYNOPSIS
int semctl(int semid, int semnun, int cmd, union semun arg); 


PARAMETERS
semid: [in] the semaphore set to manipulate. 

semnum: [in] the semaphore in the set to manipulate (0 is the first). 

cmd: [in] the operation to perform. 

arg: [in out] an argument to the operation (see description). 


DESCRIPTION
Manipulates a semaphore set or members of a semaphore set. The possible values for cmd are: 


IPC_STAT 
gets some information on the semaphore set. The calling task must have read access to the 
semaphore set. 

IPC_SET 
modify some members of the semid_ds structure of a semaphore in the set. The members that 
can be modified are: sem_perm.uid, sem_perm.gid, and the lower 9 bits of sem_perm.mode. The
calling task must be the owner or the creator of the semaphore set or the superuser. The 
sem_ctime field of the set is updated to the current time. 

IPC_RMID 
removes the semaphore set and awakens the process blocked on operations performed on that 
set. Only the creator or the owner of the set or the superuser may use this option. 

GETALL 
returns the semval value of all semaphore in an array. 

GETNCNT 
returns the number of processes waiting for the value of a specific semaphore in the 
semaphore set to increase. The calling task must have read privileges on the semaphore set.
 

GETPPID 
returns the pid of the last process to have executed a semaphore opreation a specific 
semaphore of the semaphore set. The calling task must have read privileges on the semaphore
set. 

GETVAL 
returns the value of a specific semaphore in the semaphore set. The calling task must have 
read privileges on the semaphore set. 

GETZCNT 
returns the number of processes waiting for the value of a specific semaphore in the 
semaphore set to be zero. The calling task must have read privileges on the semaphore set. 

SETALL 
sets the value of all semaphore in the semaphore set. The calling task must have alter 
privileges on the semaphore set. 

SETVAL 
sets the value of a specific semaphore in the semaphore set. The calling task must have 
alter privileges on the semaphore set. 

RETURN VALUE
On success, with cmd equals to: 


GETNCNT 
the number of waiting tasks (see description). 

GETPID 
the task pid (see description). 

GETVAL 
the value of the semaphore. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -