Internship 1: Process Synchronization
Internship requirements
In the Windows 2000 environment, create a console process that contains N threads. Use this n thread to represent n
A reader or writer. Each thread performs read and write operations according to the requirements of the corresponding test data file. Please use the semaphore mechanism
Realize the reader's priority and writer priority reader - writer problem.
Reader - The read and write operation limit for writer issues:
1) Write - write mutual exclusion;
2) Read - write mutual exclusion;
3) Read - read permission;
Reader's preferred additional restrictions: Another reader is being read, if a reader application is read,
The reader can directly start reading.
Predictive additional restrictions: If a reader applies for reading operation, there is another writer waiting for access to sharing
Source, then the reader must wait until the writer is waiting to be waiting.
Operation results display requirements: Requirements to create, read and write operations, start reading and writing, and end reading at each thread
A line prompt information is displayed at the time of writing, so that all processing complies with the corresponding read and write operations.
Test data file format
Test data files include N-line test data, and the N threads created separately are readers or writers, and read and write.
The start time and duration of the operation. Each line test data consists of four fields, and each field is separated by spaces. First field
For a positive integer, represent 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)
After issuing reading and writing applications for shared resources. The fourth field is a positive number, indicating the duration of the read and write operation. Transceiver
After the 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.
Below is an example of a test data file:
1 r 3 5
2 W 4 5
3 r 5 2
4 r 6 5
5 W 5.1 3
Introduction to the experimental related API
The APIs that may involve this experiment are:
Thread control:
CreateThread completes thread creation, create a thread on the address space of the call process to perform the specified letter
Number; its return value is the handle of the created thread.
Handle CreateThread
LPSecurity_attributes lpthreadattributes, // sd
DWORD DWSTACKSIZE, // Initial Stack Size
LPTHREAD_START_ROUTINE LPSTARTADDRESS, // THREAD
Function
LPVOID LPPARETER, // Thread Argument
DWORD dwcreationFlags, // creeation option
LPDWORD LPTHREADID // THREAD IDENTIFIER
);
ExitThread is used to end current threads.
Void exitthread
DWORD DWEXITCODE / / EXIXT CODE for this Thread
);
Sleep can hang the current thread within the specified time.
Void Sleep
DWORD dwmilliseconds // Sleep Time
);
Signal control:
CreateMutex creates a mutex, return to an object handle;
Handle Createmutex
LPSecurity_attributes lpmutexattributes, // sd
Bool Binitialowner, // Initial Owner
LPCTSTR LPNAME // Object Name);
OpenMutex opens and returns an existing mutual exclusive object handle for subsequent access;
Handle OpenMuteX
DWORD DWDESIREDACCESS, // Access
Bool BinheritHandle, // Inheritance Option
LPCTSTR LPNAME // Object Name
);
ReleaseMutex releases the occupation of mutually exclusive objects, making it available.
Bool ReleaseMutex (
Handle Hmutex // Handle To Mutex
);
WaitForsingleObject can wait for the specified object to be available in the specified time;
DWORD WAITFORSINGLEOBJECT
Handle Hhandle, // Handle To Object
DWORD dwmilliseconds // time-out interval
);
Experimental program code
#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_NWORKTIME;
INT m_nid;
} In Person;
Person g_Persons [max_person];
INT G_NUMPERSON = 0;
Long g_currenttime = 0;
INT g_PersonLists [] = {
1, R, 5, 5,
2, W, 4, 5,
3, R, 2, 2,
4, R, 1, 5,
End,
}
INT g_numofreaming = 0;
Handle g_hreadmutex;
Handle g_hwritemutex;
Void CheckPersonList (int * ppersonlist);
Bool CreateReader (int StartTime);
Bool CreateWriter (int starttime, int worktime);
DWORD WINAPI ReaderProc (LPVOID LPPARAM);
DWORD WINAPI WRITERPROC (LPVOID LPPARAM);
int main ()
{
g_hreadmutex = Createmutex (NULL, FALSE, NULL);
g_hwritemutex = Createmutex (Null, false, null);
g_currenttime = 0;
While (True)
{
CheckPersonList (g_personlists);
g_currenttime ;
SLEEP (300);
Printf ("CurrentTime =% D / N", g_currenttime);
Return 0;
}
Void CheckpersonList (int * ppersonlists)
{
INT i = 0;
INT * PLIST = PPERSONLISTS;
Bool ret;
While (PLIST [0]! = END)
{
IF (PLIST [2] == g_currenttime)
{
Switch (PLIST [1])
{
Case R:
RET = CreateReader (PLIST [2], PLIST [3]);
Break;
Case W:
Ret = CreateWriter (PLIST [2], PLIST [3]);
Break;
}
IF (! RET)
Printf ("CREATE PERSON% D Is Wrong / N", PLIST [0]);
}
PLIST = 4; // Move to Next Person List
}
}
DWORD WINAPI ReaderProc (LPVOID LPPARAM)
{
Person * pperson = (person *) LPPARAM;
Printf ("Reader% D Is Request The Shared Buffer ... / N", PPERSON-> M_NID);
WaitforsingleObject (g_hreadmutex, infinite);
IF (g_numofreaming == 0)
{
WaitforsingleObject (g_hwritemutex, infinite);
}
g_numofreaming ;
ReleaseMutex (g_hreadmutex);
Printf ("Reader% D Is Request OK / N", PPERSON-> M_NID);
// 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_hreadmutex, infinite);
g_numofreaming -;
IF (g_numofreaming == 0)
ReleaseMutex (g_hwritemutex);
ReleaseMutex (g_hreadmutex);
EXITTHREAD (0);
Return 0;
}
DWORD WINAPI WriterProc (LPVOID LPPARAM)
{
WaitforsingleObject (g_hwritemutex, infinite);
Person * pperson = (person *) LPPARAM;
//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
}
Printf ("Writer% D Is Exit ... / N", PPERSON-> M_NID);
ReleaseMutex (g_hwritemutex);
EXITTHREAD (0);
Return 0;
}
Bool CreateReader (int StartTime, Int Worktime)
{
DWORD DWTHREADID;
IF (g_numperson> = max_person)
Return False;
Person * pPerson = & g_Persons [g_numperson];
PPERSON-> M_NID = g_numperson;
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)
{
DWORD DWTHREADID;
IF (g_numperson> = max_person)
Return False;
Person * pPerson = & g_Persons [g_numperson];
PPERSON-> M_NID = g_numperson;
PPERSON-> M_NStartTime = StartTime;
pPerson-> m_nworktime = Worktime;
PPERSON-> M_NTYPE = Writer;
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;
}
Discussion on the API of synchronous mutual exclusion
There are two ways in the Windows API to achieve synchronous mutual exclusion, one is CreateMutex creates a mutually exclusive signal, and the other is the amount of CreateSemaphore creates a general sector. The above program is the synchronous mutual exclusion problem of a general reader writer, and use it to use it. Mutex is a comparison selection, but the results of the actual program run find that after the thread of the Mutex is created, its MUTEX is automatically released (unwind), even if there is no usemutex function, then the teacher told me to use createMaphore. , Test hair, use semaphore without this problem. I don't know if this problem is not intentionally designed in the Windows system.
For more information on the synchronous mutual exclusion of Windows implementation, please see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/USING_SYNCHRONIZATION.ASP