2006-1-10
时间:2006-01-11 来源:晏东
2006年一月十日星期一 阴天在不开灯的房间
i. msgctl控制函数
1. msqid_ds结构及ipc_perm结构
/* one msqid structure for each queue on the system */
struct msqid_ds {
struct ipc_perm msg_perm;
struct msg *msg_first; /* first message on queue */
struct msg *msg_last; /* last message in queue */
time_t msg_stime; /* last msgsnd time */
time_t msg_rtime; /* last msgrcv time */
time_t msg_ctime; /* last change time */
struct wait_queue *wwait;
struct wait_queue *rwait;
ushort msg_cbytes;
ushort msg_qnum;
ushort msg_qbytes; /* max number of bytes on queue */
ushort msg_lspid; /* pid of last msgsnd */
ushort msg_lrpid; /* last receive pid */
};
struct ipc_perm
{
key_t key;
ushort uid; /* owner euid and egid */
ushort gid;
ushort cuid; /* creator euid and egid */
ushort cgid;
ushort mode; /* access modes see mode flags below */
ushort seq; /* slot usage sequence number */
};
2.Msgctl函数原型
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
成功返回0,错误返回0,errno略
其中cmd:
IPC_STAT 取指定msqid的msqid_ds结构,保存到buf中;
IPC_SET 设置指定msqid的msqid_ds结构,唯一可以改动的就是ipc_perm结构中的id相关数据;
IPC_RMID 删除指定msqid的队列
注意:1)类型定义一般在sys/types.h或者bits/types.h中
2) turbo linux中ipc_perm结构进行了重定义。
3) 新建队列可以用key = IPC_PRIVATE;
具体示例:
static int get_msg(int msqid, struct msqid_ds *buf)
{
if (msgctl(msqid, IPC_STAT, buf) == -1)
{
perror("get_msg error");
return -1;
}
return 0;
}
static int set_msg(int msqid, struct msqid_ds *buf)
{
if (msgctl(msqid, IPC_SET, buf) == -1)
{
perror("set_msg error");
return -1;
}
return 0;
}
static int rm_msg(int msqid)
{
if (msgctl(msqid, IPC_RMID, 0) == -1)
{
perror("rm_msg error");
return -1;
}
return 0;
}
static int show_msg_info(struct msqid_ds *buf)
{
//ipc_perm
printf("\n");
printf("key seq uid gid cuid cgid mode\n");
printf("%-6d%-6d%-6d%-6d%-6d%-6d%-6ho\n",
buf->msg_perm.__key,
buf->msg_perm.__seq,
buf->msg_perm.uid,
buf->msg_perm.gid,
buf->msg_perm.cuid,
buf->msg_perm.cgid,
buf->msg_perm.mode
);
//msqid_ds
printf("%-27s%-27s%-27s\n", "last_sndtime", "last_rcvtime", "last_chgtime");
printf("%-27s%-27s%-27s\n", ctime(&buf->msg_stime), ctime(&buf->msg_rtime), ctime(&buf->msg_ctime));;
printf("%-27s%-27s%-27s\n", "cur_bytes", "cur_num", "able_bytes");
printf("%-27d%-27d%-27d\n",
buf->msg_cbytes, buf->msg_qnum, buf->msg_qbytes);
printf("%-27s%-27s\n", "last_sndpid", "last_rcvpid");
printf("%-27d%-27d\n", buf->msg_lspid, buf->msg_lrpid);
}
(turbo linux真的是对你无语了,你为什么要搞得和大家不一样,把key定义成__key,弄得我找了一上午!)