``我正在尝试做作业,但我不能继续,需要你的帮助。 我有两个使用相同文件(log.txt)的进程。当我想在同一时间写东西时,它会覆盖并且只有一个进程写入txt。
这是我的创作过程,
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
答案 0 :(得分:0)
你遇到了很多问题:)
首先,在Windows下,内存映射文件无法扩展 - 这使得它们不适合编写日志文件。
同一个文件的多个视图应该是一致的,只要它们不参考网络位置并且你不要混合内存映射和普通文件I / O,所以它应该是可能的做一些有用的东西......但你需要一些方法来协调你的写作。
我担心在某些进程间通信中无法安全地执行此操作。对于标准的ASCII日志文件肯定没有,对于自定义二进制格式,你可能会对长度前缀字符串和原子数据访问做一些诡计(虽然我不确定在写入时是否可以保证原子性记忆映射文件),但要正确地做到这一点是很棘手的 - 成熟的竞争条件的可能性。