【Linux】消息队列和信号量

1.消息队列

1.1介绍

  • 消息队列提供了一个从一个进程向另一个进程发送有类型块数据的方法
  • 每个数据块都被认为是有一个类型,接收者进程接收的数据块可以有不同的类型值

1.2 接口介绍

  • msgget
NAME
		msgget - get a System V message queue identifier
SYNOPSIS
		#include <sys/types.h>
		#include <sys/ipc.h>
		#include <sys/msg.h>
		int msgget(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>
		int msgctl(int msqid, int cmd, struct msqid_ds *buf);
		struct msqid_ds {
		struct ipc_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 */
		unsigned long __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 return 0. 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>
		int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
//msgp:是一个指针,指针指向准备发送的消息
//msgsz:是msgp指向的消息队列,这个长度不含保存消息类型的那个long int长整型
//msgflg:控制着当前消息队列满/到达系统上限将要发生的事情,0即可
//返回值:成功返回0;失败返回-1
  • 消息主体
struct msgbuf {
	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>
		int semget(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>
		int semctl(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
		this union as follows:
		union semun {
		int val; /* Value for SETVAL */
		struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */
		unsigned short *array; /* Array for GETALL, SETALL */
		struct seminfo *__buf; /* Buffer for IPC_INFO
		(Linux-specific) */
		};
RETURN VALUE
		On failure, semctl() returns -1 with errno indicating the error.
		All other cmd values return 0 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>
		int semop(int semid, struct sembuf *sops, size_t nsops);
		The elements of this structure are of type struct sembuf, containing the
		following members:
		unsigned short 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() and semtimedop() return 0; otherwise they return -1 with errno indicating the error.
//sops:指向一个结构sembuf的指针
//nsops:sops对应的信号量的个数,也就是可以同时对多个信号量进行PV操作

2.2 结论

  • 信号量的生命周期随内核
  • ipcs -s && ipcrm -s semid
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值