操作系统线程同步机制实验报告一、实验名称:线程同步机制二、实验内容及目的:2.1、通过观察共享数据资源但不受控制的两个线程的并发运行输出结果,体会同步机制的必要性和重要性;2.2、利用现有操作系统提供的同步机制编程实现两线程的有序控制;2.3、根据同步机制的Peterson软件解决方案编程实现同步机制对于同一问题的解决;2.4、基于程序运行时间长短比较两种同步机制。
三、实验步骤:3.1、编程实现不设置任何线程同步机制的多线程银行转账程序,观察输出结果。
3.1.1、主要代码(完整代码见附录)://nAccount1与nAccount2为全局变量,也是此线程同步机制里的临界变量。
do{nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop++;} while ((nAccount1 + nAccount2) == 0);3.1.2、输出结果:当没有任何线程同步机制时,程序循环不了多少次便跳了出来,某一次的输出结果如下图:图一无线程同步机制时的程序运行结果3.2、编程实现调用系统Mutex的多线程银行转账程序,观察输出结果。
3.2.1、:主要代码(完整代码见附录):do{::WaitForSingleObject(m1,INFINITE);nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;if((nAccount1 + nAccount2) != 0)break;::ReleaseMutex(m1);nLoop++;} while (nLoop <= 1000000);为了防止程序陷入死循环,在while条件里加上了nloop<=1000000,当循环执行到第1000001时将跳出。
3.2.2、输出结果:调用Mutex时的程序运行结果如图二:图二调用Mutex时的程序运行结果3.3、编程实现利用Peterson算法,实现线程同步的多线程银行转账程序,观察输出结果:3.3.1、主要代码(完整代码见附录):do{flagi = true;turn = j;while(flagj && turn == j);nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;if((nAccount1 + nAccount2) != 0)break;flagi = false;nLoop++;} while (nLoop <= 1000000);为了防止程序陷入死循环,在while条件里加上了nloop<=1000000,当循环执行到第1000001时将跳出。
3.3.2、输出结果:使用Peterson法时的输出结果如图三:图三使用Peterson算法时的输出结果3.4、对以上三步所得结果的分析:3.4.1、由步骤1可知不采取任何线程同步措施的多线程银行转账程序执行不了多少次就会出问题。
其原因在于共享资源nAccount1和nAccount2没有实现互斥访问,当两线程同时对其修改,导致数据不一致时,将跳出循环。
3.4.2、比较步骤2与步骤3的结果,可知:实现的线程同步时,用Peterson 法的效率要比调用系统Mutex互斥变量的效率高很多。
循环1000000次的时间,前者仅为26ms,而后者需4917ms。
其原因在于Peterson法只用一些本地变量来实现,这个存取速度是相当快的,而调用系统Mutex互斥变量涉及到好几个Windows API(应用程序编程接口)的调用,必然比较慢。
四、实验心得:通过这次实验,体会到线程同步机制的重要性,同时,在选用线程同步机制时也应该根据具体案例的要求选择合适的线程同步机制,对效率要求很高的程序就应该自己编写效率比较高的线程同步算法,而不是调用Windows API。
附录:实验源代码及注释附录一、无任何线程同步机制的代码:os_test2_none.cpp#include <stdio.h>#include <windows.h>#include <ctime>int nAccount1 = 0, nAccount2 = 0; //主线程创建的全局变量DWORD WINAPI Thread1Executive(LPVOID lpParameter);long startTime, endTime;int status = 2;void main(){printf("无线程同步机制:\n\n");HANDLE h1,h2;startTime = clock();h1 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);h2 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);::CloseHandle(h1);::CloseHandle(h2);while(1){}}DWORD WINAPI Thread1Executive(LPVOID lpParameter){int nLoop = 0;int nTemp1, nTemp2, nRandom;do{nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;nLoop++;} while ((nAccount1 + nAccount2) == 0 );printf("循环1次数为%d\n", nLoop);printf("%d,%d\n\n",nAccount1,nAccount2);status --;if(status == 0){endTime = clock();int x = endTime - startTime;printf("所花费时间为:%dms\n",x);}return 0;}附录二、调用系统Mutex互斥变量的代码:os_test2_mutex.cpp#include <stdio.h>#include <windows.h>#include <ctime>int nAccount1 = 0, nAccount2 = 0; //主线程创建的全局变量DWORD WINAPI Thread1Executive(LPVOID lpParameter);HANDLE m1;long startTime, endTime;int status = 2;void main(){printf("解决方案一:调用mutex:\n\n");m1 = CreateMutex(NULL, FALSE, "Sample07");// 检查错误代码if (GetLastError() == ERROR_ALREADY_EXISTS) {CloseHandle(m1);// 如果已有互斥量存在则释放句柄并复位互斥量printf("互斥量m1存在\n");m1 = NULL;return; // 程序退出}HANDLE h1,h2;h1 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);h2 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);::CloseHandle(h1);::CloseHandle(h2);while(1){}}DWORD WINAPI Thread1Executive(LPVOID lpParameter){int nLoop = 0;int nTemp1, nTemp2, nRandom;startTime = clock();do{::WaitForSingleObject(m1,INFINITE);nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;if((nAccount1 + nAccount2) != 0)break;::ReleaseMutex(m1);nLoop++;} while (nLoop <= 1000000);printf("循环1次数为%d\n", nLoop);printf("%d,%d\n\n",nAccount1,nAccount2);status --;if(status == 0){endTime = clock();int x = endTime-startTime;printf("所花费时间为:%dms\n",x);}return 0;}附录三、Peterson法实现线程同步的代码:os_test2_peterson.cpp #include <stdio.h>#include <windows.h>#include <ctime>bool flagi = false, flagj = false;enum Turn {i,j};Turn turn = i;int nAccount1 = 0, nAccount2 = 0; //主线程创建的全局变量DWORD WINAPI Thread1Executive(LPVOID lpParameter);HANDLE m1;long startTime, endTime;int status = 2;void main(){printf("解决方案二:peterson法:\n\n");HANDLE h1,h2;startTime = clock();h1 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);h2 = ::CreateThread(NULL,0,Thread1Executive,NULL,0,NULL);::CloseHandle(h1);::CloseHandle(h2);while(1){}}DWORD WINAPI Thread1Executive(LPVOID lpParameter){int nLoop = 0;int nTemp1, nTemp2, nRandom;do{flagi = true;turn = j;while(flagj && turn == j);nTemp1 = nAccount1;nTemp2 = nAccount2;nRandom = rand();nAccount1 = nTemp1 + nRandom;nAccount2 = nTemp2 - nRandom;if((nAccount1 + nAccount2) != 0)break;flagi = false;nLoop++;} while (nLoop <= 1000000);//printf("循环1次数为%d\n", nLoop);printf("%d,%d\n\n",nAccount1,nAccount2);status --;if(status == 0){endTime = clock();int x = endTime - startTime;printf("所花费时间为:%dms\n",x);}return 0;}。