Operating system experiment process synchronization - reader is preferred

xiaoxiao2021-03-06  51

Interns 1: Process Synchronization - Reader Priority Internship Requirements In Windows 2000 Environment, create a console process that contains N threads. Use this N thread to represent n readers or writers. Each thread performs read and write operations according to the requirements of the corresponding test data file. Please use the semaphore mechanism to achieve the reader's priority and write priority reader - writer problem. Reader - Write-write operation limit: 1) Write - write mutual exclusion; 2) Read - write mutual exclusion; 3) Read - read allowance; reader preferred additional restrictions: If a reader is applying for reading operation Another reader is performing reading, then the reader can directly start reading. Write a preferred additional restriction: If another writer has another writer waiting to access the shared resource when a reader application is read, the reader must wait until the writer is waiting to be waiting. Operation results Display requirements: Requires a row prompt information when the read / write operation request, start reading and writing operation, and end reading write operations, respectively, to give confident that all processing complies with corresponding read and write operations. Test data file format test data files include N row test data, and the N threads created separately are readers or writers, as well as the start time and duration of read and write operations. Each line test data consists of four fields, and each field is separated by spaces. The first field is a positive integer, indicating the line program number. The second field indicates the corresponding thread role, and R indicates the reader, W represents the write. The third field is a positive, indicating the start time of the read and write operation. After the thread is created, the delay corresponding time (in seconds) will issue a read and write application for shared resources. The fourth field is a positive number, indicating the duration of the read and write operation. When the thread read and write application is successful, the read and write operation of the shared resource is started, which continues to end after the corresponding time, and release the shared resource. Here is an example of test data files: 1 R 3 52 W 4 53 R 5 24 R 6 5

The source code is as follows:

/ ************************************************** ************************ * Operating System Practice - Reader Write Question / * Reader Priority Algorithm Implementation / * Time: 2004-12.10 / * author: Tang Liang / **************************** ************************************* / # include #include #include # Include #include #include

#define max_Person 100 # Define Reader 0 # Define Writer 1 # define end -1 # Define R Reader # Define W Writer

Typedef struct _person {handle m_hthread; int m_ntype; int m_nstarttime; int m_nid;}.

Person g_persons [max_person]; int g_numperson = 0; long g_currenttime = 0;

INT g_PersonLists [] = {1, R, 3, 5, 2, W, 4, 5, 3, R, 5, 2, 4, R, 6, 5, End,}

int g_NumOfReading = 0; HANDLE g_hReadSemaphore; HANDLE g_hWriteSemaphore; void CreatePersonList (int * pPersonList); bool CreateReader (int StartTime, int WorkTime, int ID); bool CreateWriter (int StartTime, int WorkTime, int ID); DWORD WINAPI ReaderProc (LPVOID LPPARAM; DWORD WINAPI WRITERPROC (LPVOID LPPARAM);

Int main () {g_hreadsemaphore = CreateSemaphore (NULL, 1,100, NULL); g_hwritesemaphore = CreateSemaphore (NULL, 1, 100, NULL); // CreateSemaphore (Null, false, null);

CreatePersonList (G_PersonLists); // Create All The Reader and Writers Printf ("Created All The Reader and Writer / N ... / n"); g_currenttime = 0; While (TRUE) {g_currenttime ; Sleep (300); // 300 ms printf ("currenttime =% d / n", g_currenttime);} return 0;}

Void createPersonList (int * ppersonLists) {INT i = 0; int * plist = pPersonLists; bool ret; while (PLIST [0]! = end) {switch (PLIST [1]) {Case R: Ret = CreateReader (PLIST [ 2], PLIST [3], PLIST [0]); Break; Case W: Ret = CreateWriter (PLIST [2], PLIST [3], PLIST [0]); Break;} if (! RET) Printf (" Create Person% D Is Wrong / N ", PLIST [0]); PLIST = 4; //move to next persourn}}

DWORD WINAPI ReaderProc (LPVOID LPPARAM) {Person * pPerson = (Person *) LPPARAM; // Wait for the start time While (g_currenttime! = PPerson-> m_nstarttime) {}

printf ( "Reader% d is Requesting ... / n", pPerson-> m_nID); WaitForSingleObject (g_hReadSemaphore, INFINITE); if (g_NumOfReading == 0) {WaitForSingleObject (g_hWriteSemaphore, INFINITE);} g_NumOfReading ; ReleaseSemaphore (g_hReadSemaphore, 1, NULL); // modify the reader's real start time pPerson-> m_nStartTime = g_CurrentTime; printf ( "Reader% d is Reading the Shared Buffer ... / n", pPerson-> m_nID); while (g_CurrentTime <= pPerson -> m_nStartTime pPerson-> m_nWorkTime) {// .. perform read operations} printf ( "Reader% d is Exit ... / n", pPerson-> m_nID); WaitForSingleObject (g_hReadSemaphore, INFINITE); g_NumOfReading--; IF (g_numofreading == 0) {ReleaseSemaphore (G_HWRITESEMAPHORE, 1, NULL);} ReleaseSemaphore (g_hreadsemaphore, 1, null); exitthread (0); return 0;}

DWORD WINAPI WRITERPROC (LPVOID LPPARAM) {Person * PPERSON = (Person *) LPPARAM;

// Wait for the start time while situation (g_currenttime! = pPerson-> m_nstarttime) {}

printf ( "Writer% d is Requesting ... / n", pPerson-> m_nID); WaitForSingleObject (g_hWriteSemaphore, INFINITE); // modify the writer's real start time pPerson-> m_nStartTime = g_CurrentTime; printf ( "Writer% d is Writting the shared buffer ... / n ", pPerson-> m_nid); while (g_currenttime <= pperson-> m_nstarttime pPerson-> m_nworktime) {// .. perform write operations}}. ../n" ,pperson -> m;; ReleaseSemaphore (G_HWRITESEMAPHORE, 1, NULL); EXITTHREAD (0); return 0;

bool CreateReader (int StartTime, int WorkTime, int ID) {DWORD dwThreadID; if (g_NumPerson> = MAX_PERSON) return false; Person * pPerson = & g_Persons [g_NumPerson]; pPerson-> m_nID = ID; pPerson-> m_nStartTime = StartTime; pPerson -> m_nworktime = Worktime; pPerson-> m_ntype = reader; g_numperson ;

// Create An New Thread Pperson-> m_hthread = CreateThread (Null, 0, ReaderProc, (LPVOID) PPERSON, 0, & DWTHREADID; if (pPerson-> m_hthread == null) Return False;

Return True;}

bool CreateWriter (int StartTime, int WorkTime, int ID) {DWORD dwThreadID; if (g_NumPerson> = MAX_PERSON) return false; Person * pPerson = & g_Persons [g_NumPerson]; pPerson-> m_nID = ID; pPerson-> m_nStartTime = StartTime; pPerson -> m_nworktime = Worktime; pPerson-> m_ntype = write;

g_numperson ;

// Create An New Thread Pperson-> m_hthread = CreateThread (Null, 0, WriterProc, (LPVOID) PPERSON, 0, & DWTHREADID; if (pPerson-> m_hthread == null) Return False; Return true;}

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

New Post(0)