读写者问题(4)

2019-02-20 20:34

山东科技大学学生课程设计

// the array of the thread object

HANDLE h_Thread[MAX_THREAD_NUM]; ThreadInfo thread_info[MAX_THREAD_NUM];

readcount = 0; // Initialize readcount InitializeCriticalSection(&RP_Write); ifstream inFile; inFile.open(file);

printf(\ while(inFile) {

// read every writer,reader's information inFile>>thread_info[n_thread].serial; inFile>>thread_info[n_thread].entity; inFile>>thread_info[n_thread].delay; inFile>>thread_info[n_thread++].persist; inFile.get(); }

for(int i = 0; i < (int)(n_thread); i++) {

if(thread_info[i].entity == READER || thread_info[i].entity == 'r') {

// create reader thread

h_Thread[i] = CreateThread(NULL, 0,

(LPTHREAD_START_ROUTINE)(RP_ReaderThread), &thread_info[i], 0, &thread_ID); } else {

h_Thread[i] = CreateThread(NULL, 0,

(LPTHREAD_START_ROUTINE)(RP_WriterThread), &thread_info[i], 0, &thread_ID); } }

// wait for all thread to terminate.

wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1);

printf(\}

山东科技大学学生课程设计

void WP_ReaderThread(void *p) {

// execlusive object HANDLE h_Mutex1;

h_Mutex1 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, \

HANDLE h_Mutex2;

h_Mutex2 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, \

DWORD wait_for_mutex1; // wait for the mutex1 DWORD wait_for_mutex2;

DWORD m_delay; // latency time

DWORD m_persist; // the time it used for reading the file int m_serial; // the serial number of the thread // get information from the parameter 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); // wait for a while

printf(\ wait_for_mutex1 = WaitForSingleObject(h_Mutex1, -1); // enter the reader's critical section EnterCriticalSection(&cs_Read);

// block execlusive object mutex2, ensure the access,modify to readcount is // execlusive.

wait_for_mutex2 = WaitForSingleObject(h_Mutex2, -1); // modify the reader's number readcount++; if(readcount == 1) {

// if it is the first reader, wait for the writer finish EnterCriticalSection(&cs_Write); }

ReleaseMutex(h_Mutex2); // release the execlusive signal mutex2 // let other reader enter the critical section

山东科技大学学生课程设计

LeaveCriticalSection(&cs_Read); ReleaseMutex(h_Mutex1); // read file

printf(\ // block execlusive object mutex2, ensure the access, modify to readcount

// is execlusive.

wait_for_mutex2 = WaitForSingleObject(h_Mutex2, -1); readcount--;

if(readcount == 0) {

// the last reader, wake up writer LeaveCriticalSection(&cs_Write); }

ReleaseMutex(h_Mutex2); // release execlusive signal }

void WP_WriterThread(void *p) {

DWORD m_delay; DWORD m_persist; int m_serial;

DWORD wait_for_mutex3; HANDLE h_Mutex3;

h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, \

// get information from the parameter 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); // latency wait

printf(\ // block execlusive object mutex3, ensure the access,modify to writecount

// is execlusive.

wait_for_mutex3 = WaitForSingleObject(h_Mutex3, -1); writecount++; // modify the number of writer if(writecount == 1)

山东科技大学学生课程设计

{

// the first writer, wait for the reader finish. EnterCriticalSection(&cs_Read); }

ReleaseMutex(h_Mutex3); // enter the writer critical section EnterCriticalSection(&cs_Write);

// write the file

printf(\ Sleep(m_persist);

// exit the thread.

printf(\ // leave the critical section

LeaveCriticalSection(&cs_Write);

wait_for_mutex3 = WaitForSingleObject(h_Mutex3, -1); writecount--;

if(writecount == 0) {

// writer finished, reader can read. LeaveCriticalSection(&cs_Read); }

ReleaseMutex(h_Mutex3); }

void WriterPriority(char * file) {

DWORD n_thread = 0; DWORD thread_ID; DWORD wait_for_all;

// execlusive object HANDLE h_Mutex1;

h_Mutex1 = CreateMutex(NULL, FALSE, \ HANDLE h_Mutex2;

h_Mutex2 = CreateMutex(NULL, FALSE, \ HANDLE h_Mutex3;

h_Mutex3 = CreateMutex(NULL, FALSE, \

山东科技大学学生课程设计

// thread object

HANDLE h_Thread[MAX_THREAD_NUM]; ThreadInfo thread_info[MAX_THREAD_NUM];

readcount = 0; writecount = 0;

InitializeCriticalSection(&cs_Write); InitializeCriticalSection(&cs_Read); ifstream inFile; inFile.open(file);

printf(\ while(inFile) {

inFile>>thread_info[n_thread].serial; inFile>>thread_info[n_thread].entity; inFile>>thread_info[n_thread].delay; inFile>>thread_info[n_thread++].persist; inFile.get(); }

for(int i = 0; i < (int)(n_thread); i++) {

if(thread_info[i].entity == READER || thread_info[i].entity == 'r') {

// create reader thread

h_Thread[i] = CreateThread(NULL, 0,

(LPTHREAD_START_ROUTINE)(WP_ReaderThread), &thread_info[i], 0, &thread_ID); } else {

// create writer thread

h_Thread[i] = CreateThread(NULL, 0,

(LPTHREAD_START_ROUTINE)(WP_WriterThread), &thread_info[i], 0, &thread_ID); } }

wait_for_all = WaitForMultipleObjects(n_thread, h_Thread, TRUE, -1);


读写者问题(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:(北师大版)五年级数学上册知识点归纳讲解

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: