I'm programming in C using win32 api.
My program starts at void main , I do some action that create mutex with specific name
and then launching waitForSingleObject function on it with INFINITE time parameter.
Then I'm launching an EXE process by createProcess function.
The EXE process has a similar code that is now doing createMutex with the same name I did before with the parent process.
As I understand I should get a handle to the same mutex I created in the parent program.. because it has the same name. So after that, the EXE code is also doing again WaitForSingleObject on the mutex handle with same parameters.
I have expected this to stop and wait now but it continued as it was signaled somehow, but there is no way I did a signal anywhere at this stage.
I tried to replace the INFINITE parameter with 0 and to see if I get WAIT_TIMEOUT but this didn't work also.
Why my mutex don't work?
Thanks
relevant code added: (I tried to put only things that are relevant) * please note the the EXE file Process1 contains a code that does openfileInDirectory with the same file name I did in void main and for write. ** My mutex to follow is that is called writeMutex. FileSystemMutex is another one that has nothing to do with my current problem.
// Global variables definition from library header
void* viewVec;
HANDLE fileHandle;
int directoryLastBlockIndex;
FilesInProcessUse processFiles;
HANDLE fileSystemMutex;
HANDLE filesAccessSemaphore;
void main()
{
FileDescriptor* fd, *fd2;
int message,message2,message3;
int processId = GetCurrentProcessId();
char* input= NULL;
/* print out our process ID */
printf("Process %d reporting for duty\n",processId);
fileSystemMutex = CreateMutex(NULL,FALSE,FILE_SYSTEM_MUTEX_NAME);
printf("Process %d: After creating fileSystem mutex\n",processId);
filesAccessSemaphore = CreateSemaphore(NULL,MAX_ACCESSORS_FOR_ALL_FILES,MAX_ACCESSORS_FOR_ALL_FILES,FILE_SYSTEM_SEMAPHORE_NAME);
printf("Process %d: After creating filesAccessSemaphore\n",processId);
initfs("C",NUM_OF_BLOCKS_IN_DISK,NUM_OF_BLOCKS_IN_DISK);
viewVec = attachfs("C"); // Saving the address of the vector in global pointer.
if(viewVec!=NULL)
{
printf("Process %d: After AttachFS which succeded\n",processId);
fd = (FileDescriptor*) createFileInDirectory("FileX",2,&message);
if (fd!=NULL)
{
printf("Process %d:successfuly created the file: FileX in Drive C\n",processId);
}
else
{
printErrMessage(message);
}
}
else
{
printf("Process %d: After AttachFS, which failed\n",processId);
}
fd = (FileDescriptor*) openFileInDirectory("FileX",READ_PERMISSION,&message);
if(fd!=NULL)
{
printf("Process %d: opened FileXfile for read succefully",processId);
}
else
{
printf("Process %d:",processId);
printErrMessage(message);
}
closeFileInDirectory(fd);
fd = (FileDescriptor*) openFileInDirectory("FileX",WRITE_PERMISSION,&message);
if(fd!=NULL)
{
printf("Process %d: opened FileXfile for write succefully",processId);
}
else
{
printf("Process %d:",processId);
printErrMessage(message);
}
fd2 = (FileDescriptor*) openFileInDirectory("FileX",WRITE_PERMISSION,&message);
if(fd!=NULL)
{
printf("Process %d: opened FileX file for write succefully",processId);
}
else
{
printf("Process %d:",processId);
printErrMessage(message);
}
}
}
void* openFileInDirectory(char* fileName, int ReadWriteFlag, int* out_ErrMessage)
{
SystemInfoSection* sysInfo = readSystemInformationFromBeginingOfVector((char*)viewVec);
DirectoryEntry* fileEntryInDirOffset;
FileDescriptor* openfileDescriptor = NULL;
int fileIndexInOpenFiles = 0;
int writeRV;
//Mark that another file is being processed
WaitForSingleObject(filesAccessSemaphore,INFINITE);
//Check if the file exists else return error
if(isFileAlreadyExisting(fileName, sysInfo->directoryStartBlockIndex, &fileEntryInDirOffset))
{
fileIndexInOpenFiles = getFileIndexInOpenFileDirectory(fileName);
processFiles.allFilesVector[fileIndexInOpenFiles].accessSemaphore = CreateSemaphore(NULL,MAX_FILE_ACCESSORS,MAX_FILE_ACCESSORS,fileName);
WaitForSingleObject(processFiles.allFilesVector[fileIndexInOpenFiles].accessSemaphore,INFINITE);
if (ReadWriteFlag == WRITE_PERMISSION)
{
char writeMutexName[15];
strcpy(writeMutexName, WRITE_MUTEX_PREFIX);
strcat(writeMutexName, fileName);
processFiles.allFilesVector[fileIndexInOpenFiles].writeMutex = CreateMutex(NULL,FALSE,writeMutexName);
WaitForSingleObject(processFiles.allFilesVector[fileIndexInOpenFiles].writeMutex,INFINITE);
//writeRV = WaitForSingleObject(processFiles.allFilesVector[fileIndexInOpenFiles].writeMutex,MAX_WAIT_TIMEOUT_IN_FS);
//if(writeRV == WAIT_TIMEOUT)
//{
// ReleaseSemaphore(processFiles.allFilesVector[fileIndexInOpenFiles].accessSemaphore,1,NULL);
// //return error indicating that another process is already writing to the file AND RETURN FROM THE FUNCTION
// *out_ErrMessage = ERR_FILE_IS_ALREADY_OPEN_TO_A_WRITE_BY_SOME_PROCESS;
// return openfileDescriptor;
//}
}
processFiles.FDInProcessUseVector[fileIndexInOpenFiles].fileDirectoryEntry = fileEntryInDirOffset;
processFiles.FDInProcessUseVector[fileIndexInOpenFiles].readWriteFlag = ReadWriteFlag;
openfileDescriptor = &(processFiles.FDInProcessUseVector[fileIndexInOpenFiles]);
processFiles.numOfFilesInUse++;
}
else
{
openfileDescriptor = NULL;
*out_ErrMessage = ERR_FILE_NOT_FOUND;
}
free(sysInfo);
return openfileDescriptor;
}
ERROR_ALREADY_EXISTSas expected? - Ben Voigt