Reader - writer problem

zhaozj2021-02-16  42

#include

#include

#include "ready-write.h"

#include "semaphore.h"

// This is the P operation of multi-threaded work under Windows

#define p (s) WaitforsingleObject (s, Infinite)

// This is a V operation for multi-threaded work under Windows.

#define v (s) ReleaseSemaphore (S, 1, NULL)

Const int RN = 5; // Total number of readers

Const int wn = 3; // Total number of writers

Handle SDOC; // Document Seematic - Mutual Exclusion

Handle SR; // reader semaphore - generalized semaphore

Handle SCNT; / / Protect G_CNTReader's mutual exclusion

INT g_cntreader = 0; // reader count counter

// ############. Hj [2003-9-26 20:10:49]. ##########

// | FuncName: Justwait ()

// NOTE: Show some information, wait

// |

// --------------------------------------------- ------------------

// | RET VAL: VOID

// |

// parameter:

// | [int] - NReader reader (writer) number, reader> 0, writer <0

// | [int] - minimum time waiting for MIN operation

// | [int] - max operation waiting for the longest time, the actual waiting time is between the two

// | [lpcstr] - INFO to display information

Void Justwait (int Nreader, int Min, int max, lpcstr info)

// --------------------------------------------- ------------------

{

// The basic amount of waiting time is indicated in milliseconds

Const int base = 1000;

// actually wait for time

INT WAIT_TIME = 0;

If (max == min) // is determined to avoid% 0 errors, pay attention to the random value

WAIT_TIME = min * basetime;

Else

WAIT_TIME = Rand ()% (MAX * Basetime-min * Basetime) min * basetime;

// Finally displayed information buffer

Char S_out [128];

// reader is greater than 0, the writer is less than 0

IF (NReader> 0)

Sprintf (S_OUT, "Reader [% D]:% S / N", NREADER, INFO);

Else

Sprintf (S_out, "/ Twriter [% D]:% S / N", -NReader, Info;

// Print

Printf (S_OUT);

// then wait

SLEEP (wait_time);

}

// This is the main function

Void tryreaderandwriter ()

{

// Creating a selection This is the initial value - ---- this is the maximum signal value // | | |

SDOC = CREATESEMAPHORE (NULL, 1, 1, "Document");

// A maximum of 3 readers to read once

SR = CreateSemaphore (NULL, 3, 3, "ReaderNumber");

// He is also a mutually exclusive quantity, the initial value is 1

SCNT = CreateSemaphore (NULL, 1, 1, "ReaderCounterProtect");

// thread handle

Handle Threads [RN WN];

// Create a reader thread, a total of RN readers

For (int i = 0; i

Threads [i] = CreateThread (0, 0, Reader, 0, 0, 0);

// Create a writer thread, a total of WN writers

For (int J = 0; j

Threads [J RN] = CreateThread (0, 0, Writer, 0, 0, 0);

WaitFormultiPleObjects (RN WN, Threads, True, Infinite);

}

// reader thread

DWORD WINAPI Reader (LPVOID LPPAR)

{

// Note that it is static variable, you can increase each reader

Static int ready_num = 1;

INT i = reader_num ;

While (1)

{

Justwait (I, 1, 2, "I Want to Read");

// reader is not full

P (sr);

// Lock the reader counter

P (SCNT);

Printf ("//:% d readers in, [% d] in / n", g_cntreader, i);

g_cntreader ;

// If it is the first reader

IF (g_cntreader == 1)

{

Justwait (I, 1, 2, "I am Number-One!");

// Lock the document

P (SDOC);

Printf ("# ---------- [% D] <== DOC busy / n", i);

Justwait (i, 1, 2, "i have get the document");

}

// Unlock the reader counter

V (SCNT);

// Read ing ............

Justwait (I, 2, 5, "I am Reading ...");

Justwait (i, 1, 2, "i want to get out");

// Lock the reader counter

P (SCNT);

g_cntreader -;

// If it is the last one

IF (g_cntreader == 0)

{

Justwait (i, 1, 2, "i am the last-one!");

Printf ("---------- # [% D] ==> DOC Free / N", i);

// Unlock the document

V (sdoc);

}

Printf ("//:% d readers Left, [% D] is out / n", g_cntreader, i);

// Unlock the reader counter

V (SCNT);

// go away

V (sr);

Justwait (i, 5, 3, "rest ^^^^^^^^^^^^");

Return 0;

}

DWORD WINAPI WRITER (LPVOID LPPAR)

{

// Note that it is a static variable, you can minus each writer, pay attention to the initial value is negative

Static int g_cnt = -1;

INT j = g_cnt -;

While (1)

{

Justwait (J, 2, 4, "I Want WRITE ...");

// Lock the document

P (SDOC);

Printf ("/ t # =========== [% D] <== DOC busy / n", -j);

// Write ing ...

Justwait (J, 4, 3, "Writing ...");

Justwait (J, 1, 2, "Write over! Out");

Printf ("/ t =========== # [% d] ==> DOC free / n", -j);

// Unlock the document

V (sdoc);

Justwait (J, 8, 4, "REST ~~~~~~~~~~~~~");

}

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-27687.html

New Post(0)