測試數據文件包括n行測試數據,分別描述建立的n個線程是讀者仍是寫者,以及讀寫操做的開始時間和持續時間。每行測試數據包括四個字段,各個字段間用空格分隔。第一字段爲一個正整數,表示線程序號。第二字段表示相應線程角色,R表示讀者,W表示寫者。第三字段爲一個正數,表示讀寫操做的開始時間:線程建立後,延遲相應時間(單位爲秒)後發出對共享資源的讀寫申請。第四字段爲一個正數,表示讀寫操做的持續時間。當線程讀寫申請成功後,開始對共享資源的讀寫操做,該操做持續相應時間後結束,並釋放共享資源。windows
下面是一個測試數據文件的例子:測試
2 W 4 5spa
3 R 5 2線程
4 R 6 5翻譯
5 W 5.1 3code
注意:在建立數據文件時,因爲涉及到文件格式問題,最好在記事本中手工逐個鍵入數據,而不要拷貝粘貼數據,不然,本示例程序運行時可能會出現不可預知的錯誤。blog
用信號量實現,可先寫出P/V操做的僞代碼,再根據僞代碼翻譯C代碼。進程
讀者優先:ip
//讀者寫者問題:讀者優先. g++/windows #include <windows.h> #include <stdio.h> #include <time.h> DWORD dwID; #define C(S) CreateSemaphore(NULL, 1, 3, (S)) #define P(S) WaitForSingleObject((S), INFINITE) #define V(S) ReleaseSemaphore((S), 1, NULL) #define CT(func, args) CreateThread(NULL, 0, (func), (args), 0, &dwID) #define PM(num, S) WaitForMultipleObjects((num), (S), true, INFINITE) struct TInfo { int id; char type; double s; double d; }; TInfo threads[100]; HANDLE hThread[100]; HANDLE s1, s2, s3; int c = 0; int Reader_Count = 0; void Read_TInfo(); void RP(); DWORD WINAPI Reader(LPVOID lpParam); DWORD WINAPI Writer(LPVOID lpParam); void Read_TInfo() { FILE* fp; fp = fopen("in.txt", "r"); while(fscanf(fp, "%d %c %lf %lf", &threads[c].id, &threads[c].type, &threads[c].s, &threads[c].d) != EOF) { c++; } fclose(fp); } void RP() { s1 = C("sig1"); s2 = C("sig2"); for(int i = 0; i < c; ++i) { if(threads[i].type == 'W') { hThread[i] = CT(Writer, &threads[i]); } else { hThread[i] = CT(Reader, &threads[i]); } } PM(c, hThread); //等待全部線程結束 } DWORD WINAPI Reader(LPVOID lpParam) { TInfo* arg = (TInfo*)lpParam; Sleep(arg->s * 1000); P(s1); if(Reader_Count == 0) { P(s2); } Reader_Count++; V(s1); printf("線程 %d 正在讀!\n", arg->id); Sleep(arg->d * 1000); printf("線程 %d 讀完了!\n", arg->id); P(s1); Reader_Count--; if(Reader_Count == 0) { V(s2); } V(s1); }
DWORD WINAPI Writer(LPVOID lpParam) { TInfo* arg = (TInfo*)lpParam; Sleep(arg->s * 1000); P(s2); printf("線程 %d 正在寫!\n", arg->id); Sleep(arg->d * 1000); printf("線程 %d 寫完了!\n", arg->id); V(s2); } int main() { Read_TInfo(); RP(); //Sleep(3000); return 0; }
寫者優先:資源
//讀者寫者問題:讀者優先. g++/windows #include <windows.h> #include <stdio.h> #include <time.h> DWORD dwID; #define C(S) CreateSemaphore(NULL, 1, 3, (S)) #define P(S) WaitForSingleObject((S), INFINITE) #define V(S) ReleaseSemaphore((S), 1, NULL) #define CT(func, args) CreateThread(NULL, 0, (func), (args), 0, &dwID) #define PM(num, S) WaitForMultipleObjects((num), (S), true, INFINITE) struct TInfo { int id; char type; double s; double d; }; TInfo threads[100]; HANDLE hThread[100]; HANDLE s1, s2, s3; int c = 0; int Reader_Count = 0; void Read_TInfo(); void RP(); DWORD WINAPI Reader(LPVOID lpParam); DWORD WINAPI Writer(LPVOID lpParam); void Read_TInfo() { FILE* fp; fp = fopen("in.txt", "r"); while(fscanf(fp, "%d %c %lf %lf", &threads[c].id, &threads[c].type, &threads[c].s, &threads[c].d) != EOF) { c++; } fclose(fp); } void RP() { s1 = C("sig1"); s2 = C("sig2"); s3 = C("sig3"); for(int i = 0; i < c; ++i) { if(threads[i].type == 'W') { hThread[i] = CT(Writer, &threads[i]); } else { hThread[i] = CT(Reader, &threads[i]); } } PM(c, hThread); //等待全部線程結束 } DWORD WINAPI Reader(LPVOID lpParam) { TInfo* arg = (TInfo*)lpParam; Sleep(arg->s * 1000); P(s3); P(s1); if(Reader_Count == 0) { P(s2); } Reader_Count++; V(s1); V(s3); printf("線程 %d 正在讀!\n", arg->id); Sleep(arg->d * 1000); printf("線程 %d 讀完了!\n", arg->id); P(s1); Reader_Count--; if(Reader_Count == 0) { V(s2); } V(s1); }
DWORD WINAPI Writer(LPVOID lpParam) { TInfo* arg = (TInfo*)lpParam; Sleep(arg->s * 1000); P(s3); P(s2); printf("線程 %d 正在寫!\n", arg->id); Sleep(arg->d * 1000); printf("線程 %d 寫完了!\n", arg->id); V(s2); V(s3); } int main() { Read_TInfo(); RP(); //Sleep(3000); return 0; }