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

本文通过一个具体的示例介绍了如何使用Windows API中的CreateSemaphore和CreateThread函数来实现两个线程之间的同步操作。该示例展示了司机线程与售票员线程之间通过信号量进行交互的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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; }
汽车司机售票员之间必须协同工作,一方面,只有售票员把车门关好了司机才能开车,因此,售票员关好车门应通知司机开车。另一方面,只有当司机已经停 下,售票员才能开门上下客,故司机停车后应通知售票员。假定某辆公共汽车上有两名售票员一名司机,汽车当前正在始发站停车上客,试设必要的信号灯及赋初值,写出他们的同步过程 分析: 司机停车,通知售票员开门,售票员关门,通知司机开车 使用到的函数和信号量 HANDLE mutex; HANDLE empty; HANDLE full; 创建信号量 HANDLE CreateSemaphore( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,// lpSemaphoreAttributes是信号量的安全属性 可为NULL __in LONG lInitialCount,// lInitialCount是初始化的信号量 __in LONG lMaximumCount,// lMaximumCount是允许信号量增加到最大值 __in_opt LPCWSTR lpName//lpName是信号量的名称 可为NULL ); 创建互斥信号量 HANDLE CreateMutex(  LPSECURITY_ATTRIBUTES lpMutexAttributes, // 指向安全属性的指针 可为NULL  BOOL bInitialOwner, // 初始化互斥对象的所有者  LPCTSTR lpName // 指向互斥对象名的指针 ); 申请一个资源 WaitForSingleObject(HANDLE full,INFINITE); 释放资源 ReleaseSemaphore( __in HANDLE hSemaphore,// hSemaphore是要增加的信号量句柄 __in LONG lReleaseCount,// lReleaseCount是增加的计数。 __out_opt LPLONG lpPreviousCount//lpPreviousCount是增加前的数值返回。 ); 释放互斥信号量 BOOL ReleaseMutex(HANDLE hMutex); DWORD WaitForMultipleObjects( DWORD nCount, // number of handles in array CONST HANDLE *lpHandles, // object-handle array BOOL bWaitAll, // wait option DWORD dwMilliseconds // time-out interval );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值