C language for beginners, recently began to learn the API system programming, wrote a producer consumer synchronization problems, compiled, but the results is wrong, according to the debug step by step I use the print function, found that the program runs to the CreateProcess statements dropped out, struggle, for bosses to help debug (poor students, no points, who see the mood to help), the code is as follows:
# include & lt; stdio.h>
H # include "Windows."
# include & lt; String. H>
# include & lt; stdlib.h>
# include & lt; Time. H>
HANDLE MakeSharedFile ();
PROCESS_INFORMATION CreateMember (const char * string);
Void PutProduct (char * ProducerID, int * pnData, HANDLE hMutex, HANDLE hPutProductSemaphore, HANDLE hGetProductSemaphore);
Void GetProduct (char * ConsumerID, int * pnData, HANDLE hMutex, HANDLE hPutProductSemaphore, HANDLE hGetProductSemaphore);
Int main (int arg c, char * argv [])
{
If (arg c & gt; 1 & amp; & (STRCMP (argv [1], "Producer1")==0 | | STRCMP (argv [1], "Producer2")==0)) {
Printf (" getin ");
HANDLE MutexMapping=CreateMutex (NULL, FALSE, "Mutex");
HANDLE hMapping=OpenFileMapping (FILE_MAP_ALL_ACCESS, FALSE, "MappingFile");
HANDLE hPutProductSemaphore=OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE, "PutProduct");
HANDLE hGetProductSemaphore=OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE, "GetProduct");
LPVOID pFile=MapViewOfFile (hMapping FILE_MAP_ALL_ACCESS, 0, 0, 0).
for (int i=0; I & lt; 6; I++) PutProduct (argv [1], (int *) pFile, MutexMapping, hPutProductSemaphore, hGetProductSemaphore);
The CloseHandle (MutexMapping);
The CloseHandle (hMapping);
The CloseHandle (hPutProductSemaphore);
The CloseHandle (hGetProductSemaphore);
return 0;
}
Else if (arg c & gt; 1 & amp; & (STRCMP (argv [1], "Consumer1")==0 | | STRCMP (argv [1], "Consumer2")==0 | | STRCMP (argv [1], "Consumer3")==0)) {
Printf (" getin ");
HANDLE MutexMapping=CreateMutex (NULL, FALSE, "Mutex");
HANDLE hMapping=OpenFileMapping (FILE_MAP_ALL_ACCESS, FALSE, "MappingFile");
HANDLE hGetProductSemaphore=OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE, "GetProduct");
HANDLE hPutProductSemaphore=OpenSemaphore (SEMAPHORE_ALL_ACCESS, FALSE, "PutProduct");
LPVOID pFile=MapViewOfFile (hMapping FILE_MAP_ALL_ACCESS, 0, 0, 0).
While (1) the GetProduct (argv [1], (int *) pFile, MutexMapping, hPutProductSemaphore, hGetProductSemaphore);
The CloseHandle (MutexMapping);
The CloseHandle (hMapping);
The CloseHandle (hPutProductSemaphore);
The CloseHandle (hGetProductSemaphore);
return 0;
}
The else {
HANDLE hChildProcess [5].
PROCESS_INFORMATION Producer1 Producer2, Consumer1 Consumer2, Consumer3;
HANDLE hMapping=MakeSharedFile ();
HANDLE MutexMapping=CreateMutex (NULL, FALSE, "Mutex");
HANDLE hPutProductSemaphore=CreateSemaphore (
NULL,
3,
3,
"PutProduct");
HANDLE hGetProductSemaphore=CreateSemaphore (
NULL,
0,
3,
"GetProduct");
Producer1 Producer1=CreateMember (" ");
Producer2=CreateMember (" Producer2 ");
Consumer1 Consumer1=CreateMember (" ");
Consumer2 Consumer2=CreateMember (" ");
Consumer3 Consumer3=CreateMember (" ");
HChildProcess [0]=Producer1. HProcess;
HChildProcess [1]=Producer2. HProcess;
HChildProcess [2]=Consumer1. HProcess;
HChildProcess [3]=Consumer2. HProcess;
HChildProcess [4]=Consumer3. HProcess;
WaitForMultipleObjects (5, hChildProcess, TRUE, INFINITE).
return 0;
}
}
//create the Shared memory
HANDLE MakeSharedFile ()
{
HANDLE hMapping=CreateFileMapping (
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
Sizeof (int),
"MappingFile");
LPVOID pData=https://bbs.csdn.net/topics/MapViewOfFile (
HMapping,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
ZeroMemory (pData, sizeof (int));
UnmapViewOfFile (pData);
Return (hMapping);
}
//create the consumers and producers
PROCESS_INFORMATION CreateMember (const char * string)
{
PROCESS_INFORMATION PI;
SzCmdLine char szFilename [100], [100].
Memset (szFilename, 0, 100);
Memset (szCmdLine, 0, 100);
GetModuleFileName (NULL, szFilename MAX_PATH);
Sprintf (szCmdLine, "% s % s", szFilename, string);
CreateProcess (
SzFilename,
SzCmdLine,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
NULL,
& PI);
Return the PI;
}
//from the critical section products
Void GetProduct (char * ConsumerID, int * pnData, HANDLE hMutex, HANDLE hPutProductSemaphore, HANDLE hGetProductSemaphore)
{
SYSTEMTIME Time;
Srand ((unsigned) time (NULL));
Int a=rand () % 1000;
Sleep (a);
The WaitForSingleObject (hGetProductSemaphore, INFINITE);
The WaitForSingleObject (hMutex, INFINITE);
- (* pnData);
ReleaseSemaphore (hPutProductSemaphore, 1, NULL);
//print buffer state and operating time
GetLocalTime (& amp; Time);
Dsecond dmin dh printf (" % % % % dmilliseconds a product has removed from the buffer by % s \ n ", Time. WHour, Time. WMinute, Time. WSecond, Time. WMilliseconds, ConsumerID);
Printf (" The buffer is filled with % d products ", * pnData);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull