The producer process will generate the Catalan sequence and write it to a shared
ID: 3829120 • Letter: T
Question
The producer process will generate the Catalan sequence and write it to a shared memory object. The consumer process will then read and output the sequence from shared memory.
In this instance, the producer process will be passed an integer parameter on the command line specifying the number of Catalan numbers to produce, i.e. providing 5 on the command line means the producer process will generate the first 5 Catalan numbers.
------------------------------
here I put both code in. correct data print on console , my issue is that n variable in memory sharing location does not work will. on another word , consumer does not pull any information from producer and temp.txt file is empty
----------------------------
producer
#include <Windows.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
int n;
int i,j;
int value;
HANDLE hFile, hMapFile;
LPVOID lpMapAddress;
char message[100] = "shared memory message";
hFile = CreateFile("temp.txt",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
hMapFile = CreateFileMapping(hFile,
NULL,
PAGE_READWRITE,
0,
0,
TEXT("SharedObject"));
lpMapAddress = MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
printf("Enter number :");
scanf("%d",&n);
memcpy(lpMapAddress,(void *)&n,sizeof(n));
lpMapAddress = lpMapAddress+sizeof(n);
for(i=1; i <= n; i++)
{
value = 1;
for(j=1; j <= (2*i); j++)
{
value *= j;
}
for(j=1; j <= i; j++)
value /= j;
for(j=1; j <= (i+1); j++)
value /= j;
//printf("%d ",value);
memcpy(lpMapAddress,(void *)&value,sizeof(value));
lpMapAddress = lpMapAddress+sizeof(value);
}
//memcpy(lpMapAddress,message,strlen(message)+1);
UnmapViewOfFile(lpMapAddress);
CloseHandle(hFile);
CloseHandle(hMapFile);
}
Comsumer
#include <Windows.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
int n;
int i,j;
int value;
HANDLE hMapFile;
LPVOID lpMapAddress;
char message[100] = "shared memory message";
hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,
FALSE,
TEXT("SharedObject"));
lpMapAddress = MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
memcpy((void *)&n,lpMapAddress,,sizeof(n));
lpMapAddress = lpMapAddress+sizeof(n);
for(i=1; i <= n; i++)
{
memcpy((void *)&value,lpMapAddress,sizeof(value));
lpMapAddress = lpMapAddress+sizeof(value);
printf("%d ",value);
}
//memcpy(lpMapAddress,message,strlen(message)+1);
UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);
}
Explanation / Answer
You can you thus code it definitly works out.
#include<windows.h>
#include<stdio.h>
int main()
{
HANDLE hFile,hMapFile;
LPVOID MapAddress;
hFile=createFile("temp.text",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
fPrintf(stderr,"coulld not open file temp.text(%d)" GetLastError());
return-1;
}
hMaapFile=create File Mapping(hFile,NULL,PAGE_READWRITE,0,0,TEXT("shared object"));
if(hMapFile==NULL)
{
fprintf(stderr,'could not create mapping(%d)",GetLastError());
return-1;
}
MapAddress==MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);
if(MapAddress==Null)
{
printf("could not map view of file(%d)",GetLastError());
return-1;
}
sprintf(mapAddres,"%s",shared memory message");
while(1);
Unmap ViewOfFile(MapAddress);
closeHandle(hMapFile);
closeHandle(hFile);
}
//consumer code
#include<windows.h>
#include<stdio.h>
int main()
{
HANDLE hMapFile;
LPVOID IpMapAddress;
hMapFile=openFileMapping(FILE_MAP_ALL_ACCESS,FALSE,TEXT("sharred object"));
if(hMapFile==NULL)
{
printf(could not open file mapping object(%d)",GetLastError());
return-1;
}
IpMapAddress=MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);
if(IpMapAddress==NULL)
{
printf("could not map view of file(%d)", Get LastError());
return-1;
}
printf("%s",IpMapAddress);
UnMapViewOfFile(IpMapAddress);
closeHandle(hMapFile);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.