操作系统 同步 司机与售票员

#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 threads aThread[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 threads aThread[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); } // ThreadDriver DWORD 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; } // ThreadConductor DWORD 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; }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值