⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise_1.txt

📁 操作系统原理模拟 操作系统实验及课程设计,如果你正在做操作系统实验或课程设计,我保证肯定有用的.
💻 TXT
字号:
------------------------------------------------------------------------

*实验一   读者-写者问题*

一.*实验要求*

在Windows 2000/XP平台上,用VC++调用相关的API创建一个包含n个线程的控制台
进程程序。用这n 个线程来表示n个读者或写者。每个线程按相应数据文件的要
求,进行读写操作。请用信号量机制分别实现读者优先和写者优先的读者-写者问题。

读者-写者问题的读写操作限制(包括读者优先和写者优先):

1)写-写互斥;

2)读-写互斥;

3)读-读允许;

读者优先的附加限制:如果一个读者申请进行读操作时已有另一读者正在进行读操
作,则该读者可直接开始读操作。

写者优先的附加限制:如果一个读者申请进行读操作时已有另一写者在等待访问共
享资源,则该读者必须等到没有写者处于等待状态后才能开始读操作。

运行结果显示要求:要求在每个线程创建、发出读写操作申请、开始读写操作和结
束读写操作时分别显示一行提示信息,以确信所有处理都遵守相应的读写操作限制。

 

*二.实验分析*

可以将所有读者和所有写者分别存于一个读者等待队列和一个写者等待队列中,每
当读允许时,就从读者队列中释放一个或多个读者线程进行读操作;每当写允许
时,就从写者队列中释放一个写者进行写操作。

 

1)读者优先

指除非有写者在写文件,否则读者不需要等待。所以可以用一个整形变量
read_count记录当前的读者数目,用于确定是否需要释放正在等待的写者线程(当
read_count=0时,表明所有的读者读完,需要释放写者等待队列中的一个写者)。
每一个读者开始读文件时,必须修改read_count变量。因此需要一个互斥对象
mutex来实现对全局变量read_count修改时的互斥。

另外,为了实现写写互斥,需要增加一个临界区对象write。当写者发出写请求
时,必须社请临界区对象的所有权。通过这种方法,也可以实现读写互斥,当
read_count=1时(即第一个读者到来时),读者线程也必须申请临界区对象的所有权。

当读者拥有临界区的所有权时,写者阻塞在临界区对象write上。当写者拥有临界
区的所有权时,第一个读者判断完“read_count=1”后阻塞在write上,其余的读者
由于等待对read_count的判断,阻塞在mutex上。

 

2)写者优先

与读者优先类似。不同之处在于一旦一个写者到来,它应尽快对文件进行写操作,
如果有一个写者在等待,则新到来的读者不允许进行读操作。

 

测试数据文件格式:测试数据文件包括n 行测试数据,分别描述创建的n 个线程是
读者还是写者,以及读写操作的开始时间和持续时间。每行测试数据包括四个字
段,各字段间用空格分隔。第一字段为一个正整数,表示线程序号。第一字段表示
相应线程角色,R 表示读者是,W 表示写者。第二字段为一个正数,表示读写操作
的开始时间。线程创建后,延时相应时间(单位为秒)后发出对共享资源的读写申
请。第三字段为一个正数,表示读写操作的持续时间。当线程读写申请成功后,开
始对共享资源的读写操作,该操作持续相应时间后结束,并释放共享资源。

下面是一个测试数据文件的例子:

1 R 3 5

2 W 4 5

3 R 5 2

4 R 6 5

5 W 5.1 3

注意:为确保格式正确,应在记事本中手工逐个敲入数据,不要整体粘贴。

运行结果显示要求:要求在每个线程创建、发出读写操作申请、开始读写操作和结束读

写操作时分别显示一行提示信息,以确信所有处理都遵守相应的读写操作限制。

 

下面是针对上面测试数据文件的实验结果的一种表示方法,同学们编的程序应使输
出结果格式与此类似:

读者优先结果:

Reader thread 1 sents the reading require.

Reader thread 1 begins to read file.

Writer thread 2 sents the writing require.

Reader thread 3 sents the reading require.

Reader thread 3 begins to read file.

Writer thread 5 sents the writing require.

Reader thread 4 sents the reading require.

Reader thread 4 begins to read file.

Reader thread 3 finished reading file.

Reader thread 1 finished reading file.

Reader thread 4 finished reading file.

Writer thread 2 begins to write to the file.

Writer thread 2 finished writing to the file.

Writer thread 5 begins to write to the file.

Writer thread 5 finished writing to the file.

All reader and writer have finished operating.

写者优先结果:

Reader thread 1 sents the reading require.

Reader thread 1 begins to read file.

Writer thread 2 sents the writing require.

Reader thread 3 sents the reading require.

Writer thread 5 sents the writing require.

Reader thread 4 sents the reading require.

Reader thread 1 finished reading file.

Writer thread 2 begins to write to the file.

Writer thread 2 finished writing to the file.

Writer thread 5 begins to write to the file.

Writer thread 5 finished writing to the file.

Reader thread 3 begins to read file.

Reader thread 4 begins to read file.

Reader thread 3 finished reading file.

Reader thread 4 finished reading file.

All reader and writer have finished operating.

 

*三.相关**API**介绍*

 

线程控制:

*CreateThread* 完成线程创建,在调用进程的地址空间上创建一个线程,以执行
指定的函

数;它的返回值为所创建线程的句柄。

HANDLE CreateThread(

LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD

DWORD dwStackSize, // initial stack size

LPTHREAD_START_ROUTINE lpStartAddress, // thread function

LPVOID lpParameter, // thread argument

DWORD dwCreationFlags, // creation option

LPDWORD lpThreadId // thread identifier

);

*ExitThread* 用于结束当前线程。

VOID ExitThread(

DWORD dwExitCode // exit code for this thread

);

*Sleep* 可在指定的时间内挂起当前线程。

VOID Sleep(

DWORD dwMilliseconds // sleep time

);

 

信号量控制:

*CreateMutex* 创建一个互斥对象,返回对象句柄;

HANDLE CreateMutex(

LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD

BOOL bInitialOwner, // initial owner

LPCTSTR lpName // object name

);

*OpenMutex* 打开并返回一个已存在的互斥对象句柄,用于后续访问;

HANDLE OpenMutex(

DWORD dwDesiredAccess, // access

BOOL bInheritHandle, // inheritance option

LPCTSTR lpName // object name

);

R*eleaseMutex* 释放对互斥对象的占用,使之成为可用。

BOOL ReleaseMutex(

HANDLE hMutex // handle to mutex

);

*WaitForSingleObject* 可在指定的时间内等待指定对象为可用状态;

DWORD WaitForSingleObject(

HANDLE hHandle, // handle to object

DWORD dwMilliseconds // time-out interval

);

------------------------------------------------------------------------
   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -