




免费预览已结束,剩余48页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课 程 设 计 说 明 书设计题目: 操作系统课程设计 专 业: 数学与应用数学 班级: 08级 学 号: 200801050735 姓名: 郑帅 山 东 科 技 大 学2011 年 07 月 08 日课 程 设 计 任 务 书学院 信息科学与工程 专业 数学与应用数学 班级 08级 姓名 郑帅 一、课程设计题目: 操作系统课程设计 二、课程设计主要参考资料(1)Abraham Silberschatz & Peter Baer Galvin & Greg Gagne. Operating System Concepts(第七版 影印版). 高等教育出版社. 2007.3. (2)陈向群,等.Windows内核实验教程.机械工业出版社.2004.7. (3) 三、课程设计应解决的主要问题:(1) 读者写者问题 (2) 内存管理 (3) 软盘I/0 (4) 四、课程设计相关附件(如:图纸、软件等):(1) 程序源代码 (2) 五、任务发出日期: 2011-05-10 课程设计完成日期: 2011-07-08 指导教师签字: 系主任签字: 指导教师对课程设计的评语成绩: 指导教师签字: 年 月 日山东科技大学学生课程设计设计1 读者写者问题一、设计目的1、在Windows 环境下,创建一个控制台进程,此进程包括n个线程。用这n个线程来表示n个读者或写者。每个线程按相应测试数据文件的要求进行读写操作。2、用信号量机制分别实现读者优先和写者优先的的读者-写者问题。3、理解并能够灵活使用同步互斥机制和进程间通信机制。二、设计要求在Windows 2000/XP环境下,使用多线程和信号量机制实现经典的读者写者问题,每个线程代表一个读者或一个写者。每个线程按相应测试数据文件的要求,进行读写操作。请用信号量机制分别实现读者优先和写者优先的读者-写者问题。读者-写者问题的读写操作限制:(1)写-写互斥,即不能有两个写者同时进行写操作(2)读-写互斥,即不能同时有一个读者在读,同时却有一个写者在写(3)读-读允许,即可以有二个以上的读者同时读读者优先的附加限制:如果一个读者申请进行读操作时已有另一读者正在进行读操作,则该读者可直接开始读操作。写者优先的附加限制:如果一个读者申请进行读操作时已有另一写者在等待访问共享资源,则该读者必须等到没有写者处于等待状态后才能开始读操作。运行结果显示要求:要求在每个线程创建、发出读写操作申请、开始读写操作和结束读写操作时分别显示一行提示信息,以确信所有处理都遵守相应的读写操作限制。三、设计说明1概要设计1)读者优先:读者优先指的是除非有写者在写文件,否则读者不需要等待。所以可以用一个整数变量ReadCount记录当前的读者数目,用于确定是否需要释放正在等待的写者进程当ReadCount=0时,表明所有的读者读完,需要释放写者等待队列中的一个写者。每当一个读者开始读文件时,必须修改ReadCount变量。因此需要一个互斥对象rmutex来实现对全局变量ReadCount修改时的互斥。2)写者优先:写者优先和读者优先有相同之处,不同的地方在:一旦有一个写者到来时,应该尽快让写者进行写,如果有一个写者在等待,则新到的读者操作不能读操作,为此添加一个整型变量writecount,记录写者的数目,当writecount=0时才可以释放读者进行读操作! 为了实现对全局变量writecount的互斥访问,设置了一个互斥对象Mutex3。为了实现写者优先,设置一个临界区对象read,当有写者在写或等待时,读者必须阻塞在临界区对象read上。 读者除了要一个全局变量readcount实现操作上的互斥外,还需要一个互斥对象对阻塞在read这一个过程实现互斥,这两个互斥对象分别为mutex1和mutex2。2 程序结构3 详细代码#include windows.h#include #include #include #include #include #include #define READER R /读者#define WRITER w /写者#define INTE_PER_SEC 1000 /每秒时钟中断数目#define MAX_THREAD_NUM 64 /最大线程数目#define MAX_FILE_NUM 32 /最大数据文件数目#define MAX_STR_LEN 32 /字符串长度int readcount=0; /读者数目int writecount=0; /写者数目CRITICAL_SECTION RP_Write; /临界区CRITICAL_SECTION cs_Write;CRITICAL_SECTION cs_Read;struct ThreadInfoint serial; /线程序号char entity; /线程类别(判断是读者线程还是写者线程double delay; /线程延迟double persist; /线程读写操作持续时间;/读者优先-读者线程/p:读者线程信息void RP_ReaderThread(void* p)HANDLE h_Mutex; /互拆变量h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex_for_readcount);DWORD wait_for_mutex; /等待互斥变量所有权 DWORD m_delay; /延迟时间DWORD m_persist; /读文件持续时间int m_serial; /线程序号 m_serial=(ThreadInfo*)(p)-serial; m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC); Sleep(m_delay); /延迟等待 printf(Reader thread %d sent the reading require .n,m_serial);/等待互斥信号,保证readcount的访问,修改互斥 wait_for_mutex=WaitForSingleObject(h_Mutex,-1);/读者数目增加readcount+;if(readcount=1)/第一个读者,等待资源EnterCriticalSection(&RP_Write);ReleaseMutex(h_Mutex); /释放互斥信号/读文件 printf(Reader thread %d begins to read file .n,m_serial);Sleep(m_persist);/退出线程 printf(Reader thread %d finished reading file .n,m_serial);/等待互斥信号,保证readcount的访问,修改互斥 wait_for_mutex=WaitForSingleObject(h_Mutex,-1); /读者数目减少readcount-;if(readcount=0)/如果所有读者读完,唤醒读者 LeaveCriticalSection(&RP_Write);ReleaseMutex(h_Mutex); /释放互斥信号/读者优先-写者线程/p:写者线程信息void RP_WriterThread(void *p) DWORD m_delay; /延迟时间DWORD m_persist; /写文件持续时间int m_serial; /线程序号/从参数中获取信息 m_serial=(ThreadInfo*)(p)-serial; m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC;m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC;Sleep(m_delay); /延迟等待 printf(Writer thread %d sents the writting require .n,m_serial);/等待资源EnterCriticalSection(&RP_Write);/写文件 printf(Writer thread %d begins to write to the file .n,m_serial);Sleep(m_persist);/退出线程 printf(Writer thread %d finishing writing to the file .n,m_serial);LeaveCriticalSection(&RP_Write); /释放资源 /读者优先处理函数/file:文件名void ReaderPriority(char* file)DWORD n_thread=0; /线程数目DWORD thread_ID; /线程IDDWORD wait_for_all;/等待所有线程结束/互斥对象HANDLE h_Mutex;h_Mutex=CreateMutex(NULL,FALSE,mutex_for_readcount);/线程对象的数组HANDLE h_ThreadMAX_THREAD_NUM;ThreadInfo thread_infoMAX_THREAD_NUM;readcount=0; /初始化readcountInitializeCriticalSection(&RP_Write); /初始化临界区ifstream inFile; inFile.open(file); /打开文件printf(Reader Priority :nn);while(inFile)/读入每一个读者、写者的信息inFilethread_infon_thread.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread+.persist;inFile.get(); for(int i=0;iserial;m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);Sleep(m_delay); /延迟等待printf(Reader thread %d sents the reading require n,m_serial); /进入读者临界区EnterCriticalSection(&cs_Read); /读文件 printf(Reader thread %d begins to read file n,m_serial); Sleep(m_persist); /退出线程 printf(Reader thread %d finishing writing to the file.n,m_serial); /阻塞互斥对象mutex2,保证对writecount的访问、修改互斥 LeaveCriticalSection(&cs_Read);/写者优先-写者线程/p:写者线程信息void WP_WriterThread(void* p)DWORD wait_for_mutex;DWORD m_delay;/延迟时间DWORDm_persist;/读文件持续时间 int m_serial; /线程序号 HANDLE h_Mutex; /互斥对象h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,mutex_for_writecount); /从参数中获得信息 m_serial=(ThreadInfo*)(p)-serial;m_delay=(DWORD)(ThreadInfo*)(p)-delay*INTE_PER_SEC);m_persist=(DWORD)(ThreadInfo*)(p)-persist*INTE_PER_SEC);Sleep(m_delay); /延迟等待printf(Writer thread %d sents the writing require n,m_serial); /阻塞互斥对象mutex.保证对readcount的访问、修改互斥wait_for_mutex=WaitForSingleObject(h_Mutex,-1);writecount+; /修改读者数目 if(writecount=1)/如果是第一个写者,等待读者读完EnterCriticalSection(&cs_Read); ReleaseMutex(h_Mutex);/释放互斥信号mutex3 /读文件 printf(Writer thread %d begins to read file n,m_serial); Sleep(m_persist); /退出线程 printf(Writer thread %d finishing writing to the file.n,m_serial); /阻塞互斥对象mutex,保证对writecount的访问、修改互斥 wait_for_mutex=WaitForSingleObject(h_Mutex,-1); writecount-; if(writecount=0) /写者写完,读者可以读 LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex);/写着优先处理函数/file:文件名void writerPriority(char *file)DWORD n_thread=0; /线程数目DWORD thread_ID; /线程IDDWORD wait_for_all; /等待所有线程结束 HANDLE h_Mutex; /互斥对象h_Mutex=CreateMutex(NULL,FALSE,mutex_for_writecount);HANDLE h_threadMAX_THREAD_NUM; /线程对象ThreadInfo thread_infoMAX_THREAD_NUM;readcount=0; /初始化readcountwritecount=0; /初始化writecountInitializeCriticalSection(&cs_Write); /初始化临界区InitializeCriticalSection(&cs_Read);ifstream inFile;inFile.open(file); /打开文件printf(Writer Priority:nn);while(inFile)/读入每一个读者、写着的信息 inFilethread_infon_thread.serial; inFilethread_infon_thread.entity; inFilethread_infon_thread.delay; inFilethread_infon_thread.persist;n_thread+; inFile.get(); for(int i=0;i(int)(n_thread);i+)if(thread_infoi.entity=READER|thread_infoi.entity=r) /创建读者线程 h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&thread_infoi,0,&thread_ID); else /创建写着线程h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&thread_infoi,0,&thread_ID);/等待所有线程结束wait_for_all=WaitForMultipleObjects(n_thread,h_thread,TRUE,-1);printf(All reader and writer have finished operating.n);/主函数int main(int argc,char* argv)char ch;FILE *file;file=fopen(thread.dat,r);while(true)/打印提示信息printf(*n);printf( 1: Reader Priority n);printf( 2: Writer Priority n);printf( 3: Exit to Windows n); printf(*n);printf(Enter your choice(1,2 or 3):);/如果输入不正确,继续输入doch=(char)_getch();while(ch!=1&ch!=2&ch!=3);system(cls);if(ch=3) /选择3,返回return 0;else if(ch=1) /选择1,读者优先ReaderPriority(thread.dat);else /选择2,写着优先writerPriority(thread.dat);printf(nPress Any Key To Continue:); /结束_getch();system(cls);return 0;四、运行结果及分析1、输入:1 R 3 52 W 4 53 R 5 24 R 6 55 W 5.1 32、读者优先结果:3、分析如下:写者2发出写申请时,因为读者1在读文件,所以写者2阻塞。读者3发出读申请后,立即得到满足。写者5发出写申请时,因为读者1和3在读文件,所以写者5阻塞。读者4发出读申请后,立即得到满足。等到所有读者完成读操作后,写者2和5开始执行写操作。五、总结通过这次课程设计,加深了我对操作系统进程互斥与同步的了解,加强了我们动手、思考和解决问题的能力。学会了读者写者问题和PV操作同时掌握了WINAPI的一些知识。同时对实现线程同步的对象有了一定的理解: 对于互斥对象 首先,调用CreateMutex创建互斥对象;然后,调用等待函数,可以的话利用关键资源;最后,调用RealseMutex释放互斥对象。互斥对象可以在进程间使用,但关键段对象只能用于同一进程的线程之间。等待函数分两种情况:1.等待单个对象的(FOR SINGLE OBJECT): WaitForSingleObject,函数参数包括同步对象的句柄和等待时间等。如果等待时间不限制(Infinite),则只有同步对象获得信号才返回;如果等待时间为0,则在测试了同步对象的状态之后马上返回。2.等待多个对象的(FOR MULTIPLE OBJECTS) WaitForMultipleObjects,函数参数包括同步对象的句柄,等待时间,是等待一个还是多个同步对象等等,如果等待时间不限制(Infinite),则只有同步对象获得信号才返回;如果等待时间为0,则在测试了同步对象的状态之后马上返回。设计2 内存管理一、设计目的1、通过从不同侧面了解Window XP对用户进程的虚拟内存空间的个管理,分配方法。2、理解跟踪程序的编写方法(与被跟踪程序保持同步,使用Windows提供的信号量);3、熟悉掌握对Windows分配虚拟内存,改变内存状态,以及对物理内存和页面文件状态查询的API函数功能、参数限制、使用规则。二、设计要求1、.使用Widows XP的API函数,编写一个包含两个线程的进程。一个线程用于模拟内存分配活动、一个线程用于跟踪第一个线程的内存行为,而且要求两个线程之间通过信号量实现同步。2、模拟内存活动的线程可以从一个文件中读出摇摇进行的内存操作,每个内存操作包括如下内容:l 时间:操作等待时间。l 块数:分配内存的粒度。l 操作:包括保留(reserve)一个区域、提交(commit)一个区域,释放(release)一个区域、回收(decommit)一个区域和加锁(lock)与解锁(unlock)一个区域,可以将这些操作编号存放于文件。l 大小:块的大小。l 访问权限:共五种,可以将这些权限编号存放在文件中跟踪线程将页面大小、已使用的地址范围、物理内存总量,以及虚拟内存总量等信息显示出来。三、设计说明1 系统流程图2 详细代码1)makefile.cpp#include #include #include #include struct operationint time; /起始时间int block; /内存页数int oper; /操作int protection; /权限;int main()FILE* file;file=fopen(opfile,wb); /openfile为二进制用以确定内存操作operation op;for(int j=0;j6;j+)for(int i=0;i5;i+) /0-保留;1-提交;2-锁;3-解锁;4-回收;5-释放op.time=rand()%1000; /随机生成等待时间op.block=rand()%5+1; /随机生成块大小op.oper=j;tection=i;fwrite(&op,sizeof(operation),1,file); /将生成的结构写入文件return 0;2)memory.cpp/内存管理实习/该文件从文件读入每次的操作,并将结果输出到out.txt文件中#include #include #include #include #include struct operationint time; /起始时间int block; /内存页数int oper; /操作int protection; /权限;struct trace /跟踪每一次分配活动的数据结构LPVOID start; /起始地址long size; /分配大小; HANDLE allo,trac; /信号量句柄DWORD Tracker(LPDWORD lpdwparm) /跟踪allocator线程的存行为,并输出必要信息ofstream outfile; /输出文件outfile.open(out.txt);for(int i=0;i=30;i+)WaitForSingleObject(trac,INFINITE); /等待allocatot一次内存分配活动结束outfileiendl; /打印内存状况和系统状况/以下一段显示系统信息,每次执行操作后系统信息不变 /*SYSTEM_INFO info; /系统信息 GetSystemInfo(&info);outfiledwActiveProcessorMasktinfo.dwActiveProcessorMaskendl;outfiledwAllocationGranularitytinfo.dwAllocationGranularityendl;outfiledwNumberOfProcessorstinfo.dwNumberOfProcessorsendl;outfiledwOemIdtinfo.dwOemIdendl;outfiledwPageSizetinfo.dwPageSizeendl;outfiledwProcessorTypetinfo.dwProcessorTypeendl;outfilelpMaximumApplicationAddresstinfo.lpMaximunApplicationAddressendl;outfilelpMinimumApplocationAddresstinfo.lpMinimumApplicationAddressendl;outfilewProcessorArchitecturetinfo.wProcessorArchitectureendl;outfilewProcessorLeveltinfo.wProcessorLevelendl;outfilewProcessorRevisiontinfo.wProcessorRevisionendloutfilewReservedtinfo.wReservedendl;outfile*endl;*/MEMORYSTATUS status;GlobalMemoryStatus(&status);outfiledwAvailPageFiletstatus.dwAvailPageFileendl;outfiledwAvailPhyststatus.dwAvailPhysendl;outfiledwAvailVirtualtstatus.dwAvailVirtualendl;outfiledwLengthtstatus.dwLengthendl;outfiledwMemoryLoadtstatus.dwMemoryLoadendl;outfiledwTotalPageFiletstatus.dwTotalPageFileendl;outfiledwTotalPhytstatus.dwTotalPhysendl;outfiledwTotalVirtualtstatus.dwTotalVirtualendl;outfile&endl;/以下一段显示系统信息,每次执行操作后系统信息不变 /* MEMORY_BASIC_INFORMATION mem;VirtualQuery(info.lpMinimumApplicationAddress,&mem,sizeof(MEMORY_BASIC_INFORMATION);outfileAllocationBasetmem.AllocationBaseendl;outfileAllocationProtecttmem.AllocationProtectendl;outfileBaseAddresstmem.BaseAddressendl;outfileProtecttmem.Protectendl;outfileRegionSizetmem.RegionSizeendl;outfileStatetmem.Stateendl;outfileTypetmem.Typeendl;outfileendl;*/释放信号量通知allocator可以执行下一次内存分配活动ReleaseSemaphore(allo,1,NULL);return 0;void Allocator() /模拟内存分配活动线程trace traceArray5;int index=0;FILE* file;file=fopen(opfile,rb); /读入文件operation op;SYSTEM_INFO info;DWORD temp;GetSystemInfo(&info);for(int i=0;i30;i+)WaitForSingleObject(allo,INFINITE); /等待tracker打印结束的信号量couti:;fread(&op,sizeof(operation),1,file);Sleep(op.time); GetSystemInfo(&info);switch(tection) /根据文件内容确定权限case 0:index=0;temp=PAGE_READONLY;break;case 1:temp=PAGE_READWRITE;break;case 2:temp=PAGE_EXECUTE;break;case 3:temp=PAGE_EXECUTE_READ;break;case 4:temp=PAGE_EXECUTE_READWRITE;break;default:temp=PAGE_READONLY;switch(op.oper)case 0: /保留一个区域coutreserve now endl; traceArrayindex.start=VirtualAlloc(NULL,op.block*info.dwPageSize,MEM_RESERVE,PAGE_NOACCESS);traceArrayindex+.size=op.block*info.dwPageSize;coutstarting address:traceArrayindex-1.starttsize:traceArrayindex-1.sizeendl;break;case 1: /提交一个区域coutcommit now endl;traceArrayindex.start=VirtualAlloc(traceArrayindex.start,traceArrayindex.size,MEM_COMMIT,temp);index+;coutstarting address:traceArrayindex-1.starttsize:traceArrayindex-1.sizeendl;break;case 2: /锁一个区域coutlock nowendl;coutstarting address:traceArrayindex.starttsize:traceArrayindex.sizeendl;if(!VirtualLock(traceArrayindex.start,traceArrayindex+.size)coutGetLastError()endl;break;case 3: /解锁一个区域coutunlock nowendl;coutstarting address:traceArrayindex.starttsize:traceArrayindex.sizeendl; if(!VirtualLock(traceArrayindex.start,traceArrayindex+.size)coutGetLastError()endl;break;case 4: /后手一个区域coutdecommit nowendl;coutstarting address:traceArrayindex.starttsize:traceArrayindex.sizeendl; if(!VirtualFree(traceArrayindex.start,traceArrayindex+.size,MEM_DECOMMIT)coutGetLastError()endl;break;case 5: /释放一个区域coutrelease nowendl;coutstarting address:traceArrayi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 西宁市重点中学2025年化学高二第二学期期末联考试题含解析
- 新疆乌鲁木齐七十中2025届数学高二下期末联考试题含解析
- 浙江省宁波效实中学2024-2025学年物理高二第二学期期末达标检测模拟试题含解析
- 知名餐饮品牌连锁店转让及经营管理合同
- 餐饮配送与物流配送中心建设合同
- 车辆物流运输合同车辆安全检查标准
- 住宅小区集中供暖设施建设与运营合同
- 中国煤炭地质总局第二勘探局招聘考试真题2024
- 遂宁市市属事业单位招聘工作人员考试真题2024
- 吉安县教育体育局工作人员招聘考试真题2024
- 联合协议书模板
- 厂房租赁合同2
- 人教版高中生物必修二复习提纲
- 社区矫正人员心理健康讲座模板课件
- 江苏苏州昆山2022-2023学年小升初考试数学试卷含答案
- 掘进机行走部减速器设计毕业设计论文
- 学校少先队队前知识闯关活动方案
- 西方合唱发展史课件
- 基本安全之个人求生新版课件
- 自然资源保护法案例分析
- 产品质量法企业培训讲座课件
评论
0/150
提交评论