-
June 1st, 2012, 07:41 AM
#1
Memory Mapping in Visual C++ programming
Hi,
I am doing the memory mapping in between two processes. I used the example from Creating Named and Shared memory from MSDN. I can write and read the data in between two processes, but the problem i am facing is, in middle of this process after some time, some values are read as zero while reading from Process 2 which are written by Process 1, resulting in error in my process. Also tell me whether I have to maintain any timing in between the processes when doing Memory mapping. How to solve this memory reset problem.
Please help me in solving this problem. Will be waiting for reply.
Thank You in advance.
-
June 1st, 2012, 08:19 AM
#2
Re: Memory Mapping in Visual C++ programming
This may seem obvious, but I have to ask before continuing: are you using a mutex or semaphore to protect access to the memory? Because yes, you do indeed have to maintain timing between the processes. The OS is going to schedule your processes completely randomly (at least to you) and if something needs to happen in a certain order, you need to make sure you do that.
-
June 2nd, 2012, 01:17 AM
#3
Re: Memory Mapping in Visual C++ programming
Thank You for your reply.
Actually what i am doing is, i have Process-1 which need hardware details and Process-2 which consists of hardware details. And these details are shared in between these two processes which are available in memory using the memory mapping example from Create Named and Shared memory from MSDN library. I am not using any threads in those two processess and no mutex and no semaphore. So during this I read zero in middle after sometime which results in errors. Please let me know how to proceed further.
Thank You.
-
June 2nd, 2012, 01:29 AM
#4
Re: Memory Mapping in Visual C++ programming
Originally Posted by afroza
Please let me know how to proceed further.
We don't have your code. The error can be anything -- maybe your program has a simple bug and has nothing to do with threads or even memory mapped files.
Until we have your program, compile the code, and duplicate the error, you're the only one right now who should know how to proceed. All I can tell you is to debug your code more thoroughly.
Regards,
Paul McKenzie
-
June 2nd, 2012, 01:35 AM
#5
Re: Memory Mapping in Visual C++ programming
I have gone through the code, and i am taking log of the data where i am writing continuously 1 and reading continuously 1. In log file writes remain 1 but reading side log changing in middle from 1 to 0 and again 0 to 1.
-
June 2nd, 2012, 08:53 AM
#6
Re: Memory Mapping in Visual C++ programming
Originally Posted by afroza
I have gone through the code, and i am taking log of the data where i am writing continuously 1 and reading continuously 1. In log file writes remain 1 but reading side log changing in middle from 1 to 0 and again 0 to 1.
So how does that change the fact that we do not have your code to see what you're doing? You're asking us to magically guess what the issue is, and no one can do that without the code in hand.
Again, you need to debug further -- you have the program, code, etc. You're in a much better position to see what the problem is then anyone here.
Regards,
Paul McKenzie
-
June 3rd, 2012, 11:05 PM
#7
Re: Memory Mapping in Visual C++ programming
Hello Sir,
I have gone through the code for debugging it as you said. I found that the pointer which i am using to point the memory is pointing to the wrong location after some time. And hence the wrong data i am reading. Now i am working on it to solve.
Thank You for your support.
Sorry for the inconvenience.
-
June 4th, 2012, 07:43 AM
#8
Re: Memory Mapping in Visual C++ programming
Please help me with the below issue, where i am going wrong.
I am doing the memory mapping in between two process. Process 1 writing into memory and Process 2 reading from memory.My system is getting hanged when I execute the below Processess. But when I run these two process in Debugging mode, writes and reads of memory is correct and system won't hang. Please tell me where i am going wrong.
Thank You .
Will be waiting for the reply.
Code:
WRITE PROCESS
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include <stdio.h>
#include <windows.h>
#include <conio.h>
/****************************************************************************/
//Memeory Mapped
#define BUF_SIZE 1024
TCHAR szName[] = TEXT("Global\\ContinousValues");
LPWORD pBuf;
LPWORD SavedPointer;
HANDLE handleMappedFile;
int WriteIntoGUIMemory();
FILE *log1;
unsigned int TimerCount;
WORD Data_Buffer[20];
unsigned int Data0 = 144;
unsigned int Data1 = 1234;
unsigned int Data2 = 2566;
unsigned int Data3 = 3;
unsigned int Data4 = 4;
unsigned int Data5 = 5;
unsigned int loop;
unsigned char onetime = 0;
unsigned int CloseCounter = 0;
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
//void main()//int argc, char **argv)
UINT Communicate ( LPVOID param )
{
// log1 = fopen("D:\\WritingSide.xls","w");
while(CloseCounter<50)
{
if(TimerCount >= 10)
{
WriteIntoGUIMemory();
TimerCount = 0;
CloseCounter++;
}
}
UnmapViewOfFile(SavedPointer);
CloseHandle(handleMappedFile);
CloseCounter = 0;
onetime = 0;
// fclose(log1);
return 1;
}
/******************************************************************/
/**********************************************************************/
int WriteIntoGUIMemory()
{
if(!onetime)
{
handleMappedFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(BUF_SIZE), LOWORD(BUF_SIZE), szName);
if (handleMappedFile == NULL)
{
printf("Could not create file mapping object.\n");
return 1;
}
pBuf = (LPWORD)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
onetime = 1;
}
SavedPointer = pBuf;
if (pBuf == NULL)
{
printf("Could not map view of file.\n");
CloseHandle(handleMappedFile);
return 1;
}
memset((LPWORD)pBuf, 0, sizeof(char) * BUF_SIZE);
Data_Buffer[0] = Data0;
Data_Buffer[1] = Data1;
Data_Buffer[2] = Data2;
Data_Buffer[3] = Data3;
Data_Buffer[4] = Data4;
for(loop=0;loop<5;loop++)
{
*pBuf = Data_Buffer[loop];
// fprintf(log1,"value[%d] = %d\n",loop,*pBuf);
pBuf++;
}
pBuf = SavedPointer;
Data0+=10; //@@
return 1;
}
/**********************************************************************/
Code:
READ PROCESS
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include <stdio.h>
#include <windows.h>
#include <conio.h>
/****************************************************************************/
//Memeory Mapped
#define BUF_SIZE 1024
TCHAR szName[] = TEXT("Global\\ContinousValues");
LPWORD pBuf;
LPWORD SavedPointer;
int ReadFromGUIMemory();
FILE *log1;
unsigned int TimerCount;
WORD Data_Buffer[20];
unsigned int Data0 = 144;
unsigned int Data1 = 1234;
unsigned int Data2 = 2566;
unsigned int Data3 = 3;
unsigned int Data4 = 4;
unsigned int Data5 = 5;
unsigned int loop;
HANDLE handleMappedFile;
unsigned char onetime = 0;
unsigned int CloseCounter = 0;
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
//void main()//int argc, char **argv)
UINT Communicate ( LPVOID param )
{
// log1 = fopen("D:\\ReadingSide.xls","w");
while(CloseCounter<50)
{
if(TimerCount >= 10)
{
ReadFromGUIMemory();
TimerCount = 0;
CloseCounter++;
}
}
UnmapViewOfFile(SavedPointer);
CloseHandle(handleMappedFile);
CloseCounter = 0;
onetime = 0;
// fclose(log1);
return 1;
}
/******************************************************************/
/**********************************************************************/
int ReadFromGUIMemory()
{
if(onetime == 0)
{
handleMappedFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, szName);
if (handleMappedFile == NULL)
{
printf("Could not open file mapping object (%d).\n",GetLastError());
return 1;
}
pBuf = (LPWORD)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
onetime = 1;
}
SavedPointer = pBuf;
if (pBuf == NULL)
{
printf("Could not map view of file.\n");
CloseHandle(handleMappedFile);
return 1;
}
for(loop=0;loop<5;loop++)
{
Data_Buffer[loop] = *pBuf;
// fprintf(log1,"value[%d] = %d\n",loop,*pBuf);
pBuf++;
}
pBuf = SavedPointer;
return 1;
}
/**********************************************************************/
Last edited by afroza; June 4th, 2012 at 11:03 PM.
-
June 4th, 2012, 09:57 AM
#9
Re: Memory Mapping in Visual C++ programming
Please edit your post. Add code tags [code]Your code here[/code] and re-indent it to make it readable.
-
June 4th, 2012, 10:19 AM
#10
Re: Memory Mapping in Visual C++ programming
Originally Posted by afroza
Hello Sir,
Please help me with the below issue, where i am going wrong.
Until you use code tags no one is going to read that code.
The code is unformatted and almost unreadable. Please edit your post to use code tags.
Regards,
Paul McKenzie
-
June 4th, 2012, 11:10 PM
#11
Re: Memory Mapping in Visual C++ programming
Hello Sir,
Problem am facing is my system hangs, when I run the two procesess.
Sorry Sir, Now I have added the code tags. Forgive me for my mistake. Actually am the new user of VC++ and also looks only on the interfacing part of hardware with GUI, and doing first time the Memory mapping and also I have debugged the code using my knowledge. So please tell me my mistake.
Thank You.
-
June 5th, 2012, 03:19 AM
#12
Re: Memory Mapping in Visual C++ programming
Originally Posted by afroza
Hello Sir,
Problem am facing is my system hangs, when I run the two procesess.
The "system hangs" or your processes go into infinite (while) loops?
Did you try to debug your processes?
Your variable
Code:
unsigned int TimerCount;
is not initialized. So are you using it?
Victor Nijegorodov
-
June 5th, 2012, 03:35 AM
#13
Re: Memory Mapping in Visual C++ programming
Yes I have initialized it in other cpp file. TimerCount is not a problem.
The System only hangs and not the processess. Is my way of using memory is correct? Can you please tell me how to do the same thing.
-
June 5th, 2012, 04:07 AM
#14
Re: Memory Mapping in Visual C++ programming
Again:
Originally Posted by VictorN
Did you try to debug your processes?
Note that until you'll have learnt how to debug your code you won't be able to write any software (well, perhaps with the one exception: "Hello World!" application)
Victor Nijegorodov
-
June 5th, 2012, 04:14 AM
#15
Re: Memory Mapping in Visual C++ programming
Originally Posted by afroza
Sorry Sir, Now I have added the code tags.
Where is the main() program? The flow of the program is just as important, if not more so, than just posting two non-main() functions. Please post a main() program for both programs, so that we know the exact flow of the program.
Also, saying that "you initialized a variable in another function" means nothing to us unless we see for ourselves everything you're doing. Too many times we have posters saying "I did this and did that", but when we finally see the code, they didn't do what they said they did (or they did it incorrectly).
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|