NAME
msgget - get a System V message queue identifier
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intmsgget(key_t key,int msgflg);
RETURN VALUE
If successful, the return value will be the message queue identifier(a nonnegative integer), otherwise -1 with errno indicating the error.//key:某个消息队列的名字//msgflg:由九个权限标志位构成,用法和创建文件时使用的mode模式一样//返回值:成功返回一个非负整数,即消息队列的标识码;失败返回-1
msgctl
NAME
msgctl - System V message control operations
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intmsgctl(int msqid,int cmd,structmsqid_ds*buf);structmsqid_ds{structipc_perm msg_perm;/* Ownership and permissions */
time_t msg_stime;/* Time of last msgsnd(2) */
time_t msg_rtime;/* Time of last msgrcv(2) */
time_t msg_ctime;/* Time of last change */unsignedlong __msg_cbytes;/* Current number of bytes in
queue (nonstandard) */
msgqnum_t msg_qnum;/* Current number of messages
in queue */
msglen_t msg_qbytes;/* Maximum number of bytes
allowed in queue */
pid_t msg_lspid;/* PID of last msgsnd(2) */
pid_t msg_lrpid;/* PID of last msgrcv(2) */};
RETURN VALUE
On success, IPC_STAT, IPC_SET,and IPC_RMID return0. A successful IPC_INFO or MSG_INFO operation returns the index of the highest used entry in the kernel's internal array record‐
ing information about all message queues.(This information can be used with repeated MSG_STAT or MSG_STAT_ANY operations to obtain information about all queues on the system.) A
successful MSG_STAT or MSG_STAT_ANY operation returns the identifier of the queue whose index was given in msqid.
On error,-1 is returned with errno indicating the error.//msgid:由msgget函数返回的消息队列标识码//cmd:将要采取的动作://1.IPC_STAT:把msqid_ds结构中的数据设置为消息队列的当前关联值//2.IPC_SET:在进程有足够权限的前提下,把消息队列的当前关联值设置为msqid_ds数据结构中给出的值//IPC_RMID:删除消息队列//buf:缓冲区//返回值:成功返回0;失败返回-1
msgsnd
NAME
msgrcv, msgsnd - System V message queue operations
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>intmsgsnd(int msqid,constvoid*msgp, size_t msgsz,int msgflg);//msgp:是一个指针,指针指向准备发送的消息//msgsz:是msgp指向的消息队列,这个长度不含保存消息类型的那个long int长整型//msgflg:控制着当前消息队列满/到达系统上限将要发生的事情,0即可//返回值:成功返回0;失败返回-1
消息主体
structmsgbuf{long mtype;/* message type, must be > 0 */char mtext[1];/* message data */};// 以⼀个long int⻓整数开始,接收者函数将利⽤⽤这个⻓整数确定消息的类型
msgrcv
NAME
msgrcv, msgsnd - System V message queue operations
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/msg.h>
ssize_t msgrcv(int msqid,void*msgp, size_t msgsz,long msgtyp,int msgflg);//msgtype:它可以实现接收消息的类型,也可以模拟优先级的简单形式进行接收//返回值:成功返回实际放到接收缓冲区⾥⾥去的字符个数,失败返回 -1
1.3 结论
消息队列的生命周期是随内核的
ipcs -q &&ipcrm -q msgid
消息队列支持全双工
2.信号量
理解:执行流间互斥与同步,可以当作锁来使用
2.1 接口介绍
semget
NAME
semget - get a System V semaphore set identifier
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intsemget(key_t key,int nsems,int semflg);
RETURN VALUE
If successful, the return value will be the semaphore set identifier(a nonnegative integer), otherwise,-1 is returned, with errno indicating the error.//key:信号量集的键值,同消息队列和共享内存//nsems:信号量集中信号量的个数//semflg:同消息队列
semctl
NAME
semctl - System V semaphore control operations
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intsemctl(int semid,int semnum,int cmd,...);
This function has three or four arguments, depending on cmd. When there are
four, the fourth has the type union semun. The calling program must define
thisunion as follows:union semun {int val;/* Value for SETVAL */structsemid_ds*buf;/* Buffer for IPC_STAT, IPC_SET */unsignedshort*array;/* Array for GETALL, SETALL */structseminfo*__buf;/* Buffer for IPC_INFO
(Linux-specific) */};
RETURN VALUE
On failure,semctl() returns -1 with errno indicating the error.
All other cmd values return0 on success
//semid:由semget返回的信号量集标识码//semnum:信号集中信号量的序号//cmd:将要采取的动作
semop
NAME
semop, semtimedop - System V semaphore operations
SYNOPSIS
#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>intsemop(int semid,structsembuf*sops, size_t nsops);
The elements of this structure are of type structsembuf, containing the
following members:unsignedshort sem_num;/* semaphore number */short sem_op;/* semaphore operation :-1,P操作。1,V操作
*/short sem_flg;/* operation flags */
Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates
RETURN VALUE
If successful,semop()andsemtimedop()return0; otherwise they return-1 with errno indicating the error.//sops:指向一个结构sembuf的指针//nsops:sops对应的信号量的个数,也就是可以同时对多个信号量进行PV操作