sdd-memsql

releasemutex
2023年3月31日发(作者:u盘启动大师)

操作系统读者写者问题代码实现

问题分析:

读者优先:

读者进程执⾏:

1.⽆其他读者写者,直接执⾏

2.有写者等,但其他读者在读,直接读

3.有写者写,等待

写者进程执⾏:

1.⽆其他读写者,直接执⾏

2.有其他读写者,等待

写者优先:

读者进程执⾏:

1.如果此时没有写者等待,直接执⾏

2.如果有写者等待,那么等待

写者进程执⾏:

1.如果没有其他写者,那么执⾏

2.如果有其他读写者,那么等待

伪代码:

读者优先:

读者R:

Wait(rmutex);

rcount++;

if(rcount==1)

Wait(wmutex);

Signal(rmutex);

Read_Action();

Wait(rmutex);

rcount--;

if(rcount==0)

Signal(wmutex);

Signal(rmutex);

写者W:

Wait(wmutex);

Write_Action();

Signal(wmutex);

写者优先:

intwcount=0,rcount=0;

semaphorew=1,r=1,x=1;

读者R:

reader(){

P(r);

P(x);

rcount++;

if(rcount==1)

P(w);

V(x);

V(r);

//Read()

P(x);

rcount--;

if(rcount==0)

V(w);

V(x);

}

写者W:

writer(){

P(x);

wcount++;

if(wcount==1)

P(r);

V(x);

P(w);

//Write()

V(w);

P(x);

wcount--;

if(wcount==0)

V(r);

V(x);

}

源码C++实现:

#include

#include

#include

#include

#include

#include

#include

#defineMAX_THREAD_NUM64//最⼤线程数

#defineINTE_PER_SEC1000//每秒时钟中断的数⽬

#defineMAX_FILE_NUM32//最⼤⽂件数⽬数

#defineMAX_STR_LEN32//字符串的长度

usingnamespacestd;

intreadcount=0;//读者数⽬

intwritecount=0;//写者数⽬

CRITICAL_SECTIONRP_Write;//临界资源

CRITICAL_SECTIONcs_Write;

CRITICAL_SECTIONcs_Read;

structThreadInfo

{

intserial;//线程序号

charentity;//线程类别(判断是读者还是写者线程)

doubledelay;//线程延迟时间

doublepersist;//线程读写操作时间

};

};

//读者优先<--->读者线程//

voidRP_ReaderThread(void*p)//P:读者线程信息

{

//互斥变量

HANDLEh_Mutex;

h_Mutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex_for_readcount");

DWORDwait_for_mutex;//等待互斥变量所有权

DWORDm_delay;//延迟时间

DWORDm_persist;//读⽂件持续时间

intm_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);//延迟等待

cout<<"Readerthread"<

//等待互斥信号,保证对ReadCount的访问,修改互斥

wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

//读者数⽬增加

readcount++;

if(readcount==1)

{

//第⼀个读者,等待资源

EnterCriticalSection(&RP_Write);

}

ReleaseMutex(h_Mutex);//释放互斥信

//读⽂件

cout<<"Readerthread"<

Sleep(m_persist);

//退出线程

cout<<"Readerthread"<

//等待互斥信号,保证对ReadCount的访问,修改互斥

wait_for_mutex=WaitForSingleObject(h_Mutex,-1);

//读者数⽬减少

readcount--;

if(readcount==0)

{

//如果所有的读者读完,唤醒写者

LeaveCriticalSection(&RP_Write);

}

ReleaseMutex(h_Mutex);//释放互斥信号

}

//P:写者线程信息

voidRP_WriterThread(void*p)

{

DWORDm_delay;//延迟时间

DWORDm_persist;//写⽂件持续时间

intm_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);

cout<<"Writerthread"<

//等待资源

EnterCriticalSection(&RP_Write);

//写⽂件

cout<<"Writerthread"<

Sleep(m_persist);

//退出线程

cout<<"Writerthread"<

//释放资源

LeaveCriticalSection(&RP_Write);

}

//读者优先处理函数

voidReaderPriority(constchar*fileName)

{

DWORDn_thread=0;//线程数⽬

DWORDthread_ID;//线程ID

DWORDwait_for_all;//等待所有线程结束

//互斥对象

HANDLEh_Mutex;

h_Mutex=CreateMutex(NULL,FALSE,"mutex_for_readcount");

//线程对象的数组

HANDLEh_Thread[MAX_THREAD_NUM];

ThreadInfothread_info[MAX_THREAD_NUM];

readcount=0;//初始化readcount

InitializeCriticalSection(&RP_Write);//初始化临界区

ifstreaminFile;

(fileName);

if(!inFile)

{

cout<<"⽂件打开失败!"<

}

cout<<"读者优先:"<

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;

();

}

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

{

if(thread_info[i].entity=='R')

{

//创建读者进程

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=WaitForMultipleObjects(n_thread,h_Thread,TRUE,-1);

cout<<"Allreaderandwriterhavefinishedoperating!"<

}

//写者优先---读者线程

//P:读者线程信息

voidWP_ReaderThread(void*p)

{

HANDLEprint;

print=CreateMutex(NULL,FALSE,LPCTSTR("mutex_for_print"));

//互斥变量

HANDLEh_Mutex1;

h_Mutex1=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex1");

HANDLEh_Mutex2;

h_Mutex2=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex2");

DWORDwait_for_mutex1;//等待互斥变量所有权

DWORDwait_for_mutex2;

DWORDm_delay;//延迟时间

DWORDm_persist;//读⽂件持续时间

intm_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);//延迟等待

DWORDwait_to_print=WaitForSingleObject(print,-1);

cout<<"Readerthread"<

ReleaseMutex(print);

wait_for_mutex1=WaitForSingleObject(h_Mutex1,-1);

//读者进去临界区

EnterCriticalSection(&cs_Read);

//阻塞互斥对象Mutex2,保证对readCount的访问和修改互斥

wait_for_mutex2=WaitForSingleObject(h_Mutex2,-1);

//修改读者的数⽬

readcount++;

if(readcount==1)

{

//如果是第1个读者,等待写者写完

EnterCriticalSection(&cs_Write);

}

ReleaseMutex(h_Mutex2);//释放互斥信号Mutex2

//让其他读者进去临界区

LeaveCriticalSection(&cs_Read);

ReleaseMutex(h_Mutex1);

//读⽂件

wait_to_print=WaitForSingleObject(print,-1);

cout<<"Readerthread"<

ReleaseMutex(print);

Sleep(m_persist);

//退出线程

wait_to_print=WaitForSingleObject(print,-1);

cout<<"Readerthread"<

ReleaseMutex(print);

//阻塞互斥对象Mutex2,保证对readcount的访问,修改互斥

wait_for_mutex1=WaitForSingleObject(h_Mutex2,-1);

readcount--;

if(readcount==0)

{

//最后⼀个读者,唤醒写者

LeaveCriticalSection(&cs_Write);

}

ReleaseMutex(h_Mutex2);//释放互斥信号

}

///

//写者优先---写者线程

//P:写者线程信息

voidWP_WriterThread(void*p)

{

DWORDwait_for_mutex3;//互斥变量

DWORDm_delay;//延迟时间

DWORDm_persist;//读⽂件持续时间

intm_serial;//线程序号

HANDLEh_Mutex3;

h_Mutex3=OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3");

//从参数中获得信息

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);//延迟等待

cout<<"Writerthread"<

wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

writecount++;//修改写者数⽬

if(writecount==1)

{

EnterCriticalSection(&cs_Read);

}

ReleaseMutex(h_Mutex3);

EnterCriticalSection(&cs_Write);

cout<<"Writerthread"<

Sleep(m_persist);

cout<<"Writerthread"<

LeaveCriticalSection(&cs_Write);

wait_for_mutex3=WaitForSingleObject(h_Mutex3,-1);

writecount--;

if(writecount==0)

{

LeaveCriticalSection(&cs_Read);

}

ReleaseMutex(h_Mutex3);

}

/

//写者优先处理函数

//fileName:⽂件名

voidWriterPriority(constchar*fileName)

{

DWORDn_thread=0;

DWORDthread_ID;

DWORDwait_for_all;

HANDLEh_Mutex1;

h_Mutex1=CreateMutex(NULL,FALSE,"mutex1");

HANDLEh_Mutex2;

h_Mutex2=CreateMutex(NULL,FALSE,"mutex2");

HANDLEh_Mutex3;

h_Mutex3=CreateMutex(NULL,FALSE,"mutex3");

HANDLEh_Thread[MAX_THREAD_NUM];

ThreadInfothread_info[MAX_THREAD_NUM];

readcount=0;

writecount=0;

InitializeCriticalSection(&cs_Write);

InitializeCriticalSection(&cs_Read);

ifstreaminFile;

(fileName);

cout<<"WriterPriority:"<

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;

();

}

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

{

if(thread_info[i].entity=='R')

{

//创建读者进程

h_Thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReaderThread),&thread_info[i],0,&thread_ID);

}

elseif(thread_info[i].entity=='W')

{

//创建写线程

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);

cout<<"Allreaderandwriterhavefinishedoperating!"<

}

/

//主函数

intmain(void)

{

system("color3f");//改变颜⾊

constchar*file="";

charch;

while(1)

{

cout<<"--------------------------------读者与写者问题---------------------------------"<

cout<<"*******************************************************************************"<

cout<<"*1、读者优先*"<

cout<<"*2、写者优先*"<

cout<<"*3、退出...*"<

cout<<"*******************************************************************************"<

printf("请选择(1,2,3):");

do{

ch=(char)getchar();

}while(ch!='1'&&ch!='2'&&ch!='3');

system("cls");//执⾏清屏幕命令

switch(ch)

{

case'1':

ReaderPriority(file);

break;

case'2':

WriterPriority(file);

break;

case'3':

return0;

}

cout<

getchar();

system("cls");

}

return0;

}

内容:

1R35

2W45

3R52

4R65

5W5.13

结果:

更多推荐

releasemutex