`` Я пытаюсь сделать домашнее задание, но я не могу продолжить, нужна ваша помощь. У меня есть два процесса, которые используют тот же файл (log.txt). когда я пишу что-то в одно и то же время, он перезаписывает и ТОЛЬКО ОДИН процесс записывает в txt.MapViewOfFile-OpenFileMapping (нужна помощь)
Это мой процесс созидатель,
enter code here
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "..\\def.h"
int main(){
HANDLE hReadPipe;
HANDLE hWritePipe;
HANDLE hMMap;
DWORD bytesWritten;
HANDLE hFile;
STARTUPINFO si_e;
PROCESS_INFORMATION pi_e;
STARTUPINFO si_d;
PROCESS_INFORMATION pi_d;
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
if(!CreatePipe(&hReadPipe,&hWritePipe, &sa,0))
fprintf(stderr,"Unable to create pipe.\n");
ZeroMemory(&si_e, sizeof(STARTUPINFO));
ZeroMemory(&si_d, sizeof(STARTUPINFO));
if((hFile =CreateFile( FILE_NAME,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL))
== INVALID_HANDLE_VALUE){
fprintf(stderr,"PARENT: Unable to open file %s: %d\n",FILE_NAME,GetLastError());
exitPrompt();
}
if((hMMap = CreateFileMapping( hFile,
NULL,
PAGE_READWRITE,
0,
BUFF_SIZE*2,
"Global\\file_log_txt"))
== NULL){
fprintf(stderr,"PARENT: Unable to create memory mapping: %d\n",GetLastError());
}
si_e.cb = sizeof(si_e);
si_e.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si_e.hStdOutput =hWritePipe;
si_e.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si_e.dwFlags = STARTF_USESTDHANDLES;
if(!CreateProcess( NULL,
"..\\Debug\\Encrypter.exe",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si_e,
&pi_e))
fprintf(stderr,"Unable to create child process.\n");
si_d.cb = sizeof(si_d);
si_d.hStdInput= hReadPipe;
si_d.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si_d.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si_d.dwFlags = STARTF_USESTDHANDLES;
if(!CreateProcess(NULL,
"..\\Debug\\Decrypter.exe",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si_d,
&pi_d))
fprintf(stderr,"Unable to create child process.\n");
Sleep(5000000);
return 0;
}
1.Process:
enter code here
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include "..\\def.h"
int main()
{
int i;
HANDLE writeHandle;
HANDLE hFile, hMMap;
DWORD bytesToWrite,bytesWritten,a=50;
char * pFile, *start;
char message[50][15];
char code[]=" ";
int index;
writeHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
}
if((pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
{
fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
}
//student number =160201007(0+0+7=7) +7encrypter
while(1){
gets(code);
for( i=0;i<strlen(code);i++){
code[i]=code[i]+7;
}
bytesToWrite = strlen(code);
bytesToWrite++;
WriteFile(writeHandle,code,bytesToWrite,&bytesWritten,NULL);
}
start = pFile;
*(pFile)=*(pFile)+50;
while(pFile < start+ 100){
*(pFile=pFile+80);
*(pFile++) = 'A';
break;
}
system("pause");
return 0;
}
2.Process:
enter code here
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include "..\\def.h"
int main()
{
char message[80];
int index,i;
HANDLE hFile, hMMap;
char * pFile, *start;
if((hMMap = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Global\\file_log_txt")) == NULL){
fprintf(stderr,"Unable to create memory mapping: %d\n",GetLastError());
}
if((pFile = (char *) MapViewOfFile(hMMap,FILE_MAP_ALL_ACCESS,0,0,BUFF_SIZE)) == NULL)
{
fprintf(stderr,"Unable to create map view: %d\n",GetLastError());
}
for(index = 0;(message[index]=getchar()) != 0;index++);
for( i=0;i<strlen(message);i++){
message[i]=message[i]-7;
}
printf("Decrypter : %s\n",message);
start = pFile;
while(pFile < start + 50){
*(pFile++) = 'A';
break;
}
system("pause");
return 0;
}
».. \ def.h "
enter code here
#ifndef __DEF_H
#define __DEF_H
#include <Windows.h>
#include <stdio.h>
//#include <stdlib.h>
#define BUFF_SIZE 1024 *64
#define FILE_NAME "log.txt"
void exitPrompt(){
system("pause");
exit(0);
}
#endif
Можете ли вы указать (1), кто создает сопоставление файлов, (2) как он создан; (3), какая из четырех строк выше, и (4) с кодом ошибки. –