16)msgCnt[i]=msgmaxcount[i];
17)//先创建文件映像
18)m_hMutex=CreateMutex(NULL,false,strname[nowProcess][MutexEnum]);
19)
20)m_hFileMap=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,
21)PAGE_READWRITE,0,sizeof(_MSGQ_HEADER)+
22)sizeof(char)*msgCnt[nowProcess],strname[nowProcess][SpaceEnum]);
23)if(m_hFileMap==NULL)
24){
25)QMessageBox:
:
information(NULL,tr("提示"),
26)tr("创建共享内存失败"));
27)returnFALSE;
28)}
29)//映射文件映像
30)
31)m_hViewBuf=MapViewOfFile(m_hFileMap,FILE_MAP_ALL_ACCESS,0,0,0);//映射全部Buffer
32)if(m_hViewBuf==NULL)
33){
34)QMessageBox:
:
information(NULL,tr("提示"),
35)tr("读取共享内存失败"));
36)returnFALSE;
37)}
38)
39)//信号量创建
40)m_SemaphoreSend=CreateSemaphore(NULL,msgCnt[nowProcess],msgCnt[nowProcess],strname[nowProcess][SendEnum]);
41)m_SemaphoreReceive=CreateSemaphore(NULL,0,msgCnt[nowProcess],strname[nowProcess][ReceiveEnum]);
42)
43)//设置MsgQ头信息
44){
45)pMsgInfo=(_MSGQ_HEADER*)m_hViewBuf;
46)pMsgInfo->MsgMaxCount=msgCnt[nowProcess];
47)pMsgInfo->MsgSize=msgSize[nowProcess];
48)pMsgInfo->ReadIndex=0;
49)pMsgInfo->WriteIndex=0;//从索引0开始写
50)pMsgInfo->MsgNum=0;
51)
52)//ReleaseMutex(m_Semaphore);
53)}
54)returnTRUE;
55)}
5.4.3、Send()函数实现
1)BOOLCMsgQ:
:
Send(LPVOIDbuf,intwhichProcess,DWORDwaitTime)
2){
3)LPCVOIDori;
4)char*lpchar;//地址指针
5)BOOLbRet=FALSE;//返回值判断
6)_MSGQ_HEADER*potherMsgInfo;//目的地的队列索引
7)
8)//打开目的地的邮箱
9)HANDLEother_hFileMap=OpenFileMapping(FILE_MAP_WRITE,FALSE,strName[whichProcess][SpaceEnum]);
10)if(other_hFileMap==NULL)
11){
12)QMessageBox:
:
information(NULL,tr("提示"),
13)tr("打开共享内存失败"));
14)returnFALSE;
15)}
16)
17)//打开目的地邮箱的发送信号量
18)HANDLEother_SemaphoreSend=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,strName[whichProcess][SendEnum]);
19)//打开目的地邮箱的接受信号量
20)HANDLEother_SemaphoreReceive=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,strName[whichProcess][ReceiveEnum]);
21)
22)
23)//ReleaseSemaphore(other_Semaphore,1,NULL);
24)//打开目的地邮箱的空间
25)QProcessgzip;
26)gzip.start("clock.exe",QStringList()<<"28");
27)DWORDdRet=WaitForSingleObject(other_SemaphoreSend,waitTime);
28)
29)ori=MapViewOfFile(other_hFileMap,FILE_MAP_WRITE,0,0,0);
30)lpchar=(char*)ori;
31)potherMsgInfo=(_MSGQ_HEADER*)lpchar;
32)
33)HANDLEopenmutex=OpenMutex(MUTEX_ALL_ACCESS,false,strName[whichProcess][MutexEnum]);
34)//P操作
35)
36)
37)if(dRet==WAIT_OBJECT_0)
38){
39)gzip.close();
40)if(potherMsgInfo->MsgNum!
=potherMsgInfo->MsgMaxCount)//再次判断是否可写
41){
42)WaitForSingleObject(openmutex,waitTime);
43)potherMsgInfo->MsgNum++;
44)lpchar+=sizeof(_MSGQ_HEADER)+sizeof(char)*potherMsgInfo->MsgSize*potherMsgInfo->WriteIndex;
45)strcpy(lpchar,(char*)buf);
46)
47)if(++potherMsgInfo->WriteIndex==potherMsgInfo->MsgMaxCount)//环形Queue
48)potherMsgInfo->WriteIndex=0;
49)ReleaseMutex(openmutex);
50)bRet=TRUE;
51)}
52)//V操作
53)ReleaseSemaphore(other_SemaphoreReceive,1,NULL);
54)}elseif(dRet==WAIT_TIMEOUT)
55){
56)QMessageBox:
:
information(NULL,tr("提示"),
57)tr("对方邮箱已满,发送失败"));
58)}
59)//CloseHandle(other_hViewBuf);
60)UnmapViewOfFile(ori);
61)CloseHandle(openmutex);
62)CloseHandle(other_hFileMap);
63)CloseHandle(other_SemaphoreSend);
64)CloseHandle(other_SemaphoreReceive);
65)CloseHandle(other_SemaphoreReceive);
66)returnbRet;
67)}
5.4.4、Receive()函数实现
1)BOOLCMsgQ:
:
Receive(LPVOIDbuf,DWORDwaitTime)
2){
3)LPCVOIDori;
4)char*lpchar;
5)BOOLbRet=FALSE;
6)//打开本地邮箱的发送信号量
7)HANDLEmy_SemaphoreSend=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,strName[nowProcess][SendEnum]);
8)//打开本地地邮箱的接受信号量
9)HANDLEmy_SemaphoreReceive=OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,strName[nowProcess][ReceiveEnum]);
10)
11)DWORDdRet=WaitForSingleObject(my_SemaphoreReceive,waitTime);
12)
13)if(dRet==WAIT_OBJECT_0)
14){
15)if(pMsgInfo->MsgNum!
=0)//再次判断是否可读
16){
17)ori=MapViewOfFile(m_hFileMap,FILE_MAP_ALL_ACCESS,0,0,0);
18)lpchar=(char*)ori;
19)lpchar+=sizeof(_MSGQ_HEADER)+sizeof(char)*msgSize[nowProcess]*pMsgInfo->ReadIndex;
20)strcpy((char*)buf,lpchar);
21)lpchar[0]='\0';
22)pMsgInfo->MsgNum--;
23)if(++pMsgInfo->ReadIndex==pMsgInfo->MsgMaxCount)//环形Queue
24)pMsgInfo->ReadIndex=0;
25)
26)bRet=TRUE;
27)}
28)ReleaseSemaphore(my_SemaphoreSend,1,NULL);
29)}elseif(dRet==WAIT_TIMEOUT)
30){
31)QMessageBox:
:
information(NULL,tr("提示"),
32)tr("本进程邮箱已空,不可接收信息"));
33)}
34)UnmapViewOfFile(ori);
35)CloseHandle(my_SemaphoreSend);
36)CloseHandle(my_SemaphoreReceive);
37)returnbRet;
38)}
5.4.5、实时显示函数
1)intCMsgQ:
:
Read(char**ReadMailBox)
2){
3)LPCVOIDori;
4)char*lpchar;
5)inti;
6)for(i=0;iMsgMaxCount;i++)
7)ReadMailBox[i][0]='\0';
8)i=0;
9)while(i!
=pMsgInfo->MsgMaxCount)
10){
11)//ori=MapViewOfFile(m_hFileMap,FILE_MAP_READ,0,0,0);
12)ori=m_hViewBuf;
13)lpchar=(char*)ori;
14)lpchar+=sizeof(_MSGQ_HEADER)+sizeof(char)*msgSize[nowProcess]
15)*((pMsgInfo->ReadIndex+i)%pMsgInfo->MsgMaxCount);
16)strcpy(ReadMailBox[i++],lpchar);
17)if((pMsgInfo->ReadIndex+i)%pMsgInfo->MsgMaxCount==pMsgInfo->WriteIndex)//环形Queue
18)break;
19)//UnmapViewOfFile(ori);
20)}
21)returni;
22)}
6、演示效果图