; /*等待方式在include文件中未定义*/ int rmsqid; /*读消息队列标识符*/
int wmsqid; /*写消息队列标识符*/
struct msgbuf {
long mtype;
char mtext[200];
} buf;
/*若读消息队列已存在就取得标识符,否则则创建并取得标识符*/
if ((rmsqid=msgget(RKEY,MSGFLG|IPC_CREAT))<0) {
printf("get read message queue failed\n");
exit(1);
}
/*若写消息队列已存在则失败,若不存在则创建并取得标识符*/
if ((wmsqid=msgget(WKEY,
MSGFLG|IPC_CREAT|IPC_TRUNC))<0) {
printf("get write message queue failed\n");
exit(2);
}
/*接收所有类型的消息*/
if (msgrcv(rmsqid,&buf,sizeof(struct msgbuf)-sizeof(long),
0L,IPC_WAIT)>0)
printf("get %ld type message from queue:%s\n",
buf.mtype,buf.mtext);
else {
printf("get message failed\n");
exit(3);
}
buf.mtype=3L
if (msgsnd(wmsqid,&buf,sizeof(struct msgbuf)-sizeof(long),
IPC_NOWAIT)>0)
printf("send message OK\n");
else {
printf("send message failed\n");
exit(4);
}
msgctl(wmsqid,IPC_RMID,(struct msqid *)NULL);
17.shmat()
功能:联接共享内存的操作.
语法:#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void *shmat(shmid,shmaddr,shmflg)
int shmid;
void *shmaddr;
int