ä¸é¢æ¥è¯´è¯´å¦ä½ç¨ä¸ç¨æ¶æ¯éåæ¥è¿è¡è¿ç¨é´çéä¿¡ï¼æ¶æ¯éåä¸å½å管éæå¾å¤ç¸ä¼¼ä¹å¤ãæå
³å½å管éçæ´å¤å
容å¯ä»¥åé
æçå¦ä¸ç¯æç« ï¼Linuxè¿ç¨é´éä¿¡ââ使ç¨å½å管é
ä¸ãä»ä¹æ¯æ¶æ¯éå
æ¶æ¯éåæä¾äºä¸ç§ä»ä¸ä¸ªè¿ç¨åå¦ä¸ä¸ªè¿ç¨åéä¸ä¸ªæ°æ®åçæ¹æ³ã æ¯ä¸ªæ°æ®åé½è¢«è®¤ä¸ºå«æä¸ä¸ªç±»åï¼æ¥æ¶è¿ç¨å¯ä»¥ç¬ç«å°æ¥æ¶å«æä¸åç±»åçæ°æ®ç»æãæ们å¯ä»¥éè¿åéæ¶æ¯æ¥é¿å
å½å管éçåæ¥åé»å¡é®é¢ãä½æ¯æ¶æ¯éåä¸å½å管éä¸æ ·ï¼æ¯ä¸ªæ°æ®åé½æä¸ä¸ªæ大é¿åº¦çéå¶ã
Linuxç¨å®MSGMAXåMSGMNBæ¥éå¶ä¸æ¡æ¶æ¯çæ大é¿åº¦åä¸ä¸ªéåçæ大é¿åº¦ã
äºãå¨Linuxä¸ä½¿ç¨æ¶æ¯éå
Linuxæä¾äºä¸ç³»åæ¶æ¯éåçå½æ°æ¥å£æ¥è®©æ们æ¹ä¾¿å°ä½¿ç¨å®æ¥å®ç°è¿ç¨é´çéä¿¡ãå®çç¨æ³ä¸å
¶ä»ä¸¤ä¸ªSystem V PICæºå¶ï¼å³ä¿¡å·éåå
±äº«å
åç¸ä¼¼ã
1ãmsggetå½æ°
该å½æ°ç¨æ¥å建å访é®ä¸ä¸ªæ¶æ¯éåãå®çåå为ï¼
int msgget(key_t, key, int msgflg);
ä¸å
¶ä»çIPCæºå¶ä¸æ ·ï¼ç¨åºå¿
é¡»æä¾ä¸ä¸ªé®æ¥å½åæ个ç¹å®çæ¶æ¯éåãmsgflgæ¯ä¸ä¸ªæéæ å¿ï¼è¡¨ç¤ºæ¶æ¯éåç访é®æéï¼å®ä¸æ件ç访é®æéä¸æ ·ãmsgflgå¯ä»¥ä¸IPC_CREATåææä½ï¼è¡¨ç¤ºå½keyæå½åçæ¶æ¯éåä¸åå¨æ¶å建ä¸ä¸ªæ¶æ¯éåï¼å¦ækeyæå½åçæ¶æ¯éååå¨æ¶ï¼IPC_CREATæ å¿ä¼è¢«å¿½ç¥ï¼èåªè¿åä¸ä¸ªæ è¯ç¬¦ã
å®è¿åä¸ä¸ªä»¥keyå½åçæ¶æ¯éåçæ è¯ç¬¦ï¼éé¶æ´æ°ï¼ï¼å¤±è´¥æ¶è¿å-1.
2ãmsgsndå½æ°
该å½æ°ç¨æ¥ææ¶æ¯æ·»å å°æ¶æ¯éåä¸ãå®çåå为ï¼
int msgsend(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);
msgidæ¯ç±msggetå½æ°è¿åçæ¶æ¯éåæ è¯ç¬¦ã
msg_ptræ¯ä¸ä¸ªæååå¤åéæ¶æ¯çæéï¼ä½æ¯æ¶æ¯çæ°æ®ç»æå´æä¸å®çè¦æ±ï¼æémsg_ptrææåçæ¶æ¯ç»æä¸å®è¦æ¯ä»¥ä¸ä¸ªé¿æ´åæååéå¼å§çç»æä½ï¼æ¥æ¶å½æ°å°ç¨è¿ä¸ªæåæ¥ç¡®å®æ¶æ¯çç±»åãæ以æ¶æ¯ç»æè¦å®ä¹æè¿æ ·ï¼
struct my_message{
long int message_type;
/* The data you wish to transfer*/
};
msg_szæ¯msg_ptræåçæ¶æ¯çé¿åº¦ï¼æ³¨ææ¯æ¶æ¯çé¿åº¦ï¼èä¸æ¯æ´ä¸ªç»æä½çé¿åº¦ï¼ä¹å°±æ¯è¯´msg_szæ¯ä¸å
æ¬é¿æ´åæ¶æ¯ç±»åæååéçé¿åº¦ã
msgflgç¨äºæ§å¶å½åæ¶æ¯éå满æéåæ¶æ¯å°è¾¾ç³»ç»èå´çéå¶æ¶å°è¦åççäºæ
ã
å¦æè°ç¨æåï¼æ¶æ¯æ°æ®çä¸åå¯æ¬å°è¢«æ¾å°æ¶æ¯éåä¸ï¼å¹¶è¿å0ï¼å¤±è´¥æ¶è¿å-1.
3ãmsgrcvå½æ°
该å½æ°ç¨æ¥ä»ä¸ä¸ªæ¶æ¯éåè·åæ¶æ¯ï¼å®çåå为
int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);
msgid, msg_ptr, msg_stçä½ç¨ä¹å½æ°msgsndå½æ°çä¸æ ·ã
msgtypeå¯ä»¥å®ç°ä¸ç§ç®åçæ¥æ¶ä¼å
级ãå¦æmsgtype为0ï¼å°±è·åéåä¸ç第ä¸ä¸ªæ¶æ¯ãå¦æå®çå¼å¤§äºé¶ï¼å°è·åå
·æç¸åæ¶æ¯ç±»åç第ä¸ä¸ªä¿¡æ¯ãå¦æå®å°äºé¶ï¼å°±è·åç±»åçäºæå°äºmsgtypeçç»å¯¹å¼ç第ä¸ä¸ªæ¶æ¯ã
msgflgç¨äºæ§å¶å½éåä¸æ²¡æç¸åºç±»åçæ¶æ¯å¯ä»¥æ¥æ¶æ¶å°åççäºæ
ã
è°ç¨æåæ¶ï¼è¯¥å½æ°è¿åæ¾å°æ¥æ¶ç¼ååºä¸çåèæ°ï¼æ¶æ¯è¢«å¤å¶å°ç±msg_ptræåçç¨æ·åé
çç¼ååºä¸ï¼ç¶åå é¤æ¶æ¯éåä¸ç对åºæ¶æ¯ã失败æ¶è¿å-1.
4ãmsgctlå½æ°
该å½æ°ç¨æ¥æ§å¶æ¶æ¯éåï¼å®ä¸å
±äº«å
åçshmctlå½æ°ç¸ä¼¼ï¼å®çåå为ï¼
int msgctl(int msgid, int command, struct msgid_ds *buf);
commandæ¯å°è¦éåçå¨ä½ï¼å®å¯ä»¥å3个å¼ï¼
IPC_STATï¼æmsgid_dsç»æä¸çæ°æ®è®¾ç½®ä¸ºæ¶æ¯éåçå½åå
³èå¼ï¼å³ç¨æ¶æ¯éåçå½åå
³èå¼è¦çmsgid_dsçå¼ã
IPC_SETï¼å¦æè¿ç¨æ足å¤çæéï¼å°±ææ¶æ¯åéçå½åå
³èå¼è®¾ç½®ä¸ºmsgid_dsç»æä¸ç»åºçå¼
IPC_RMIDï¼å é¤æ¶æ¯éå
bufæ¯æåmsgid_dsç»æçæéï¼å®æåæ¶æ¯éå模å¼å访é®æéçç»æãmsgid_dsç»æè³å°å
æ¬ä»¥ä¸æåï¼
struct msgid_ds
{
uid_t shm_perm.uid;
uid_t shm_perm.gid;
mode_t shm_perm.mode;
};
æåæ¶è¿å0ï¼å¤±è´¥æ¶è¿å-1.
ä¸ã使ç¨æ¶æ¯éåè¿è¡è¿ç¨é´éä¿¡
马ä¸åè¹ï¼ä»ç»å®æ¶æ¯éåçå®ä¹åå¯ä½¿ç¨çæ¥å£ä¹åï¼æ们æ¥ççå®æ¯æä¹è®©è¿ç¨è¿è¡éä¿¡çãç±äºå¯ä»¥è®©ä¸ç¸å
³çè¿ç¨è¿è¡è¡éä¿¡ï¼æ以æ们å¨è¿éå°ä¼ç¼å两个ç¨åºï¼msgreceiveåmsgsnedæ¥è¡¨ç¤ºæ¥æ¶ååéä¿¡æ¯ãæ ¹æ®æ£å¸¸çæ
åµï¼æ们å
许两个ç¨åºé½å¯ä»¥å建æ¶æ¯ï¼ä½åªææ¥æ¶è
å¨æ¥æ¶å®æåä¸ä¸ªæ¶æ¯ä¹åï¼å®ææå®å é¤ã
æ¥æ¶ä¿¡æ¯çç¨åºæºæ件为msgreceive.cçæºä»£ç 为ï¼
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/msg.h>
struct msg_st
{
long int msg_type;
char text[BUFSIZ];
};
int main()
{
int running = 1;
int msgid = -1;
struct msg_st data;
long int msgtype = 0; //注æ1
//建ç«æ¶æ¯éå
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
//ä»éåä¸è·åæ¶æ¯ï¼ç´å°éå°endæ¶æ¯ä¸ºæ¢
while(running)
{
if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1)
{
fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("You wrote: %s\n",data.text);
//éå°endç»æ
if(strncmp(data.text, "end", 3) == 0)
running = 0;
}
//å é¤æ¶æ¯éå
if(msgctl(msgid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "msgctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
åéä¿¡æ¯çç¨åºçæºæ件msgsend.cçæºä»£ç 为ï¼
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <errno.h>
#define MAX_TEXT 512
struct msg_st
{
long int msg_type;
char text[MAX_TEXT];
};
int main()
{
int running = 1;
struct msg_st data;
char buffer[BUFSIZ];
int msgid = -1;
//建ç«æ¶æ¯éå
msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
if(msgid == -1)
{
fprintf(stderr, "msgget failed with error: %d\n", errno);
exit(EXIT_FAILURE);
}
//åæ¶æ¯éåä¸åæ¶æ¯ï¼ç´å°åå
¥end
while(running)
{
//è¾å
¥æ°æ®
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
data.msg_type = 1; //注æ2
strcpy(data.text, buffer);
//åéååéæ°æ®
if(msgsnd(msgid, (void*)&data, MAX_TEXT, 0) == -1)
{
fprintf(stderr, "msgsnd failed\n");
exit(EXIT_FAILURE);
}
//è¾å
¥endç»æè¾å
¥
if(strncmp(buffer, "end", 3) == 0)
running = 0;
sleep(1);
}
exit(EXIT_SUCCESS);
}
转载ä»
ä¾åèï¼çæå±äºåä½è
ãç¥ä½ æå¿«ï¼æ»¡æ请é纳å¦
温馨提示:内容为网友见解,仅供参考