📄 p11-12.c
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <time.h>#include "err_exit.h"static void do_msgctl();static char warning_message[] = "If you remove read permission for \ yourself, this program will fail next time!";int main(void){ struct msqid_ds buf; /* 用于IPC_STAT和IP_SET命令的消息队列结构缓冲区 */ int cmd, /* msgctl的命令参数 */ msqid; /* 消息队列ID */ /* 读入消息队列id和控制命令 */ printf( "Enter the msqid = "); scanf("%d", &msqid); printf( " Enter the number for the command:\n"); printf( "\tIPC_RMID = %d\n", IPC_RMID); printf( "\tIPC_SET = %d\n", IPC_SET); printf( "\tIPC_STAT = %d\n", IPC_STAT); scanf("%d", &cmd); /* 检查输入值并执行相应命令 */ switch (cmd) { case IPC_STAT: /* 获取消息队列当前控制结构并报告给用户 */ do_msgctl(msqid, IPC_STAT, &buf); printf( "The USER ID = %d\n", buf.msg_perm.uid); printf( "The GROUP ID = %d\n", buf.msg_perm.gid); printf( "The CREATOR ID = %d\n", buf.msg_perm.cuid); printf( "The CREADTOR's GROUP ID = %d\n", buf.msg_perm.cgid); printf( "The operation permissions = %#o, ", buf.msg_perm.mode); printf( "access permissions = %#o\n", buf.msg_perm.mode & 0777); printf( "msg_cbytes = %d\n", buf.msg_cbytes); printf( "msg_qbytes = %d\n", buf.msg_qbytes); printf( "msg_qnum = %d\n", buf.msg_qnum); printf( "msg_lspid = %d\n", buf.msg_lspid); printf( "msg_lrpid = %d\n", buf.msg_lrpid); printf( "msg_stime = %s", buf.msg_stime ? ctime(&buf.msg_stime) : "Not Set\n"); printf( "msg_rtime = %s", buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not Set\n"); printf( "msg_ctime = %s", ctime(&buf.msg_ctime)); break; case IPC_SET: /* 修改消息队列控制结构的设置 */ /* 先获得消息队列的当前控制结构 */ do_msgctl(msqid, IPC_STAT, &buf); printf( "Enter msg_perm.uid: "); scanf ("%d", &buf.msg_perm.uid); printf( "Enter msg_perm.gid: "); scanf("%d", &buf.msg_perm.gid); printf("%s\n", warning_message); printf( "Enter msg_perm.mode: "); scanf("%d", &buf.msg_perm.mode); printf( "Enter msg_qbytes: "); scanf("%d", &buf.msg_qbytes); do_msgctl(msqid, IPC_SET, &buf); break; case IPC_RMID: default: /* 删除消息队列或试着执行未知的命令 */ do_msgctl(msqid, cmd, (struct msqid_ds *)NULL); break; } exit(0);}/* 打印出传递给msgctl的参数,调msgctl并报告结果。 如果msgctl失败,该函数不返回而是终止执行。*/static void do_msgctl(int msqid, int cmd, struct msqid_ds *buf){ register int rtrn; printf( "\nmsgctl: Calling msgctl(%d, %d, %s)\n", msqid, cmd, buf ? "&buf" : "(struct msqid_ds *)NULL"); rtrn = msgctl(msqid, cmd, buf); if (rtrn == -1) err_exit("msgctl failed"); else printf( " msgctl was successful for %d\n", msqid);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -