|
操作系统实验进程同步--读者优先(3) { int i=0; int *pList = pPersonLists; bool Ret; while(pList[0] != END) { switch(pList[1]) { case R: Ret = CreateReader(pList[2],pList[3],pList[0]); break; case W: Ret = CreateWriter(pList[2],pList[3],pList[0]); break; } if(!Ret) printf("Create Person %d is wrong\n",pList[0]); pList += 4; // move to next person list } } DWORD WINAPI ReaderProc(LPVOID lpParam) { Person *pPerson = (Person*)lpParam; // wait for the start time while(g_CurrentTime != pPerson->m_nStartTime) { } printf("Reader %d is Requesting ...\n",pPerson->m_nID); WaitForSingleObject(g_hReadSemaphore,INFINITE); if(g_NumOfReading ==0) { WaitForSingleObject(g_hWriteSemaphore,INFINITE); } g_NumOfReading++; ReleaseSemaphore(g_hReadSemaphore,1,NULL); // modify the reader's real start time pPerson->m_nStartTime = g_CurrentTime; printf("Reader %d is Reading the Shared Buffer...\n",pPerson->m_nID); while(g_CurrentTime <= pPerson->m_nStartTime + pPerson->m_nWorkTime) { // .. perform read operations } printf("Reader %d is Exit...\n",pPerson->m_nID); WaitForSingleObject(g_hReadSemaphore,INFINITE); g_NumOfReading--; if(g_NumOfReading == 0) { ReleaseSemaphore(g_hWriteSemaphore,1,NULL); } ReleaseSemaphore(g_hReadSemaphore,1,NULL); ExitThread(0);
|