#include <windows.h>#include <stdio.h>#define MAX_SEM_COUNT 1#define INI_SEM_COUNT 0#define THREADCOUNT 2 #define STATIONS 3 HANDLE ghSemaphore_driver;HANDLE ghSemaphore_conductor;DWORD WINAPI ThreadDriver( LPVOID );DWORD WINAPI ThreadConductor( LPVOID );void main(int argc, char* argv[]){HANDLE aThread[THREADCOUNT]; DWORD ThreadID; int i; // Create a semaphore with initial and max counts ghSemaphore_driver = CreateSemaphore( NULL, // default security attributes INI_SEM_COUNT, // initial count MAX_SEM_COUNT, // maximum count NULL); // unnamed semaphore if (ghSemaphore_driver == NULL) { printf("CreateSemaphore error: %d\n", GetLastError()); return; }ghSemaphore_conductor = CreateSemaphore( NULL, // default security attributes INI_SEM_COUNT, // initial count MAX_SEM_COUNT, // maximum count NULL); // unnamed semaphore if (ghSemaphore_conductor == NULL) { printf("CreateSemaphore error: %d\n", GetLastError()); return; } // Create driver threadsaThread[0] = CreateThread( NULL, // default security attributes 0, // default stack size (LPTHREAD_START_ROUTINE) ThreadDriver, NULL, // no thread function arguments 0, // default creation flags &ThreadID); // receive thread identifier if( aThread[0] == NULL ) { printf("CreateThread error: %d\n", GetLastError()); return; } // Create conductor threadsaThread[1] = CreateThread( NULL, // default security attributes 0, // default stack size (LPTHREAD_START_ROUTINE) ThreadConductor, NULL, // no thread function arguments 0, // default creation flags &ThreadID); // receive thread identifier if( aThread[1] == NULL ) { printf("CreateThread error: %d\n", GetLastError()); return; } // Wait for all threads to terminate WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE); // Close thread and semaphore handles for( i=0; i < THREADCOUNT; i++ ) CloseHandle(aThread[i]); CloseHandle(ghSemaphore_driver);CloseHandle(ghSemaphore_conductor);}// ThreadDriverDWORD WINAPI ThreadDriver( LPVOID lpParam ){ DWORD dwWaitResult; DWORD station; for(station=0; station<=STATIONS; station++) { // Try to enter the semaphore gate. printf("DRIVER:I want to run, waiting for closing the door\n"); dwWaitResult = WaitForSingleObject( ghSemaphore_conductor, // handle to semaphore INFINITE); // zero-second time-out interval switch (dwWaitResult) { // The semaphore object was signaled. case WAIT_OBJECT_0: // TODO: Perform task printf("DRIVER: Conductor has close the door\n"); // Simulate thread spending time on task printf("DRIVER: I am running\n"); // Relase the semaphore when task is finished break; // The semaphore was nonsignaled, so a time-out occurred. case WAIT_TIMEOUT: printf("Thread %d: wait timed out\n", GetCurrentThreadId()); break; }printf("DRIVER:I stop now\n");Sleep(5); if (!ReleaseSemaphore( ghSemaphore_driver, // handle to semaphore 1, // increase count by one NULL) ) // not interested in previous count { printf("DRIVER:CANNNOT STOP error: %d\n", GetLastError()); } } return TRUE;}// ThreadConductorDWORD WINAPI ThreadConductor( LPVOID lpParam ){ DWORD dwWaitResult; DWORD station; for(station=0; station<=STATIONS; station++) {printf("*************This is the %d station********************\n", station);printf("CONDUCTOR: I will open the door, Let passengers in\n"); // Try to enter the semaphore gate.printf("CONDUCTOR:I will close the door\n");if (!ReleaseSemaphore( ghSemaphore_conductor, // handle to semaphore 1, // increase count by one NULL) ) // not interested in previous count { printf("Condcutor cannot close the door error: %d\n", GetLastError()); }printf("CONDUCTOR:I am selling the tickets\n");//Sleep(5);printf("CONDUCTOR:I am waiting for next sattion\n"); dwWaitResult = WaitForSingleObject( ghSemaphore_driver, // handle to semaphore INFINITE); // zero-second time-out interval switch (dwWaitResult) { // The semaphore object was signaled. case WAIT_OBJECT_0: // TODO: Perform task printf("Condcutor: Dirver has stop the bus\n"); // Simulate thread spending time on task // Relase the semaphore when task is finished break; // The semaphore was nonsignaled, so a time-out occurred. case WAIT_TIMEOUT: printf("Thread %d: wait timed out\n", GetCurrentThreadId()); break; } } return TRUE;}