Process Synchronization-Writer Priority

Source: Internet
Author: User
In Windows 2000, create a console process that contains n threads. Use the n threads to represent n
Readers or writers. Each thread performs read/write operations according to the requirements of the corresponding test data file. Use the semaphores mechanism to separate
Implement reader-writer problems with reader priority and Writer Priority.
Reader-writer restrictions:
1) Write-write mutex;
2) read-write mutex;
3) read-read permission;
Additional restrictions on reader priority: If one reader applies for a read operation and another reader is reading,
The reader can directly start the read operation.
Additional restrictions on Writer Priority: If a reader applies for a read operation, another writer is waiting to access shared resources.
The reader must wait until no writer is in the waiting status to start reading.
Running result display requirements: requires creation in each thread, sending a read/write operation request, starting read/write operations, and ending read
Each row of prompt information is displayed during write operations to ensure that all processes comply with the corresponding read/write operation restrictions.
Test data file format
The test data file contains N rows of test data, which respectively describe whether the created n threads are readers or writers, and read/write
The start time and duration of the operation. Each row of test data includes four fields, separated by spaces. Field 1
Is a positive integer that indicates the thread sequence number. The second field indicates the corresponding thread role, r indicates the reader is, W indicates the writer.
The third field is a positive number, indicating the start time of the read/write operation. After the thread is created, the corresponding delay time (in seconds)
And then send a read/write application for the shared resources. The fourth field is a positive number, indicating the duration of the read/write operation. When the thread reads
After the write application is successful, the read and write operations on the shared resources are started. The operation ends after the corresponding time and the shared resources are released.
The following is an example of a test data file:
1 R 3 5
2 W 4 5
3 R 5 2
4 R 6 5

The source code is as follows:

/*************************************** *********************************/
/* Operating System practices-reader issues
/* Writer-First Algorithm Implementation
/* Time: 2004-12.10
/* Author: Tang Liang
/*************************************** *********************************/
# Include <windows. h>
# Include <ctype. h>
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <malloc. h>

# 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;
} Person;

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;
Int g_numofwriterequest = 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 );

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 person list
}
}

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

// Wait for The Write Request
While (g_numofwriterequest! = 0 ){}

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 (g_currenttime! = Pperson-> m_nstarttime ){}

Printf ("writer % d is requesting.../N", pperson-> m_nid );
G_numofwriterequest ++;
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
}
Printf ("writer % d is exit.../N", pperson-> m_nid );
G_numofwriterequest --;
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 = 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.