ɽ¶«¿Æ¼¼´óѧѧÉú¿Î³ÌÉè¼Æ
µ±È»£¬ÕâЩÊý¾Ý»¹ÐèÒª¸ü¶àµÄ²âÊÔ²Å׼ȷ£¬µ«ÊÇÕûÌå¿´À´»¹ÊÇʹÓøßËÙ»º´æÊ±ËÙ¶È×î¿ì°¡¡£²»¹ýÎÒÃÇ»¹ÊÇÓ¦¸Ã¸ù¾ÝÇé¿ö²ÉÓò»Í¬µÄÎļþ²Ù×÷ģʽ¡£
ɽ¶«¿Æ¼¼´óѧѧÉú¿Î³ÌÉè¼Æ
¸½¼þ
Ò»¡¢¶ÁÕßдÕßÔ´³ÌÐò
#include \#include
#define READER 'R' // reader #define WRITER 'W' // writer
#define INTE_PER_SEC 1000 // interrupt number per second #define MAX_THREAD_NUM 64 // max thread number int readcount = 0; int writecount = 0;
CRITICAL_SECTION RP_Write; // Critical Section CRITICAL_SECTION cs_Write; CRITICAL_SECTION cs_Read;
struct ThreadInfo {
int serial; // the serial number of the thread. char entity; // type of thread(reader or writer). double delay; // delay of thread.
double persist; // time fo thread's read and write operation. };
void RP_ReaderThread(void *p); void RP_WriterThread(void *p); void ReaderPriority(char * file); void WP_ReaderThread(void *p); void WP_WriterThread(void *p); void WriterPriority(char * file); // the main function
int main(int argc, char *argv[]) {
char ch; while(true)
ɽ¶«¿Æ¼¼´óѧѧÉú¿Î³ÌÉè¼Æ
{
printf(\*/n\
printf(\ printf(\ printf(\
printf(\*/n\
printf(\ // input is incorrect. do{
ch = (char)_getch();
}while(ch != '1' && ch != '2' && ch != '3'); // clear the screen. system(\ // choose 3, return. if(ch == '3') return 0;
// choose 1, reader first. else if(ch == '1')
ReaderPriority(\ // choose 2, writer first. else
WriterPriority(\ // end.
printf(\ _getch();
system(\ }
return 0; }
void RP_ReaderThread(void *p) {
// execlusive object HANDLE h_Mutex;
h_Mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, \
ɽ¶«¿Æ¼¼´óѧѧÉú¿Î³ÌÉè¼Æ
DWORD wait_for_mutex; // wait for the execlusive object DWORD m_delay; // delay time
DWORD m_persist; // the time of read file operation int m_serial; // serial number of the thread // get the 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 little while.
printf(\ // wait for execlusive signal,
wait_for_mutex = WaitForSingleObject(h_Mutex, -1); // add the reader's number readcount++; if(readcount == 1) {
// the first reader,wait for resource. EnterCriticalSection(&RP_Write); }
ReleaseMutex(h_Mutex); // release execlusive signal // read the file.
printf(\ Sleep(m_persist);
// exit the thread
printf(\ wait_for_mutex = WaitForSingleObject(h_Mutex, -1); // decrement the reader's number readcount--;
if(readcount == 0) {
// if all readers finished their operation, wake up the writer LeaveCriticalSection(&RP_Write);
printf(\ }
ɽ¶«¿Æ¼¼´óѧѧÉú¿Î³ÌÉè¼Æ
ReleaseMutex(h_Mutex); // release the execlusive signal }
void RP_WriterThread(void *p) {
DWORD m_delay; DWORD m_persist; int m_serial;
// get infomation 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
printf(\ // wait for the resource
EnterCriticalSection(&RP_Write);
// write the file
printf(\ Sleep(m_persist);
// exit the thread
printf(\m_serial);
// release the resource
LeaveCriticalSection(&RP_Write); }
void ReaderPriority(char * file) {
DWORD n_thread = 0; // number of threads DWORD thread_ID; // thread ID
DWORD wait_for_all; // wait for all threads // execlusive object HANDLE h_Mutex;
h_Mutex = CreateMutex(NULL, FALSE, \