su
useradd testl;passwd testl
useradd testh;passwd testh
安全管理员登录
semanage user -m -r s0:i15-s15:i0:c0.c1023 user_u
semanage login -a -s user_u -r s0:i1 testh
2.创建两个c源文件msgrecv.c msgsend.c
msgrecv.c文件内容:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGKEY 707070
#define MTEXTSIZE 512
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MTEXTSIZE]; /* message data */
};
int main(int argc, char* argv[])
{
int msgid;
ssize_t size;
struct msgbuf msg;
msgid = atoi(argv[1]);
if((size = msgrcv(msgid, (void *)&msg, MTEXTSIZE, 1, 0)) <0){
perror("msgrcv error");
return -1;
}
printf("%s\n",msg.mtext);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGKEY 707070
#define MTEXTSIZE 512
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MTEXTSIZE]; /* message data */
};
int main(int argc, char* argv[])
{
int msgid, ret;
struct msgbuf msg;
msg.mtype = 1;
sprintf(msg.mtext,"I,m sender, and you?");
if((msgid = msgget(MSGKEY, 0666|IPC_CREAT)) < 0 ){
perror("msgget error");
return -1;
}
printf("%d\n",msgid);
if((ret = msgsnd(msgid, (void *)&msg, MTEXTSIZE, 0 )) <0){
perror("msgsnd error");
return -1;
}
}
3.编译程序
gcc msgsend.c -o msgsend
gcc msgrecv.c -o msgrecv
cp msg* /home/testl
cp msg* /home/testh
4.以testl登录
执行./msgsend
5.以testh登录
执行./msgsrecv xxx
结果:
输出:I,m sender, and you?
若以testh运行./msgsend ,以testl运行./msgrecv xxx,会输出错误。表示本功能测试正确。