Get Instant Help From 5000+ Experts For
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing:Proofread your work by experts and improve grade at Lowest cost

And Improve Your Grades
myassignmenthelp.com
loader
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Guaranteed Higher Grade!
Free Quote
wave
Using Semaphores and Mutex Locks in a Multi-Threaded Program
Answered

1 Overview

1 Overview While multi-threaded applications offer a number of benefits over multi-process designs, they have one important limitation: Synchronizing threads’ access to shared data and preventing race con- ditions is the responsibility of the programmer. With processes, the kernel provides that synchro- nization. With threads, synchronization must be written into the program. In this programming assignment, you will practice using two synchronization primitives, semaphores and mutex locks, in a multi-threaded program using the pthread API.

2 Requirements Refer to the program code given in the file thread sync.c. Although the program does not perform any particularly useful tasks, it demonstrates some important concepts regarding threading. The program involves a large number of concurrent threads that will each write to and read from portions of a file at specific locations within it, effectively serving as a shared buffer to be synchronized. In the main program a total of 20,000 threads are created: 10,000 for writing; and 10,000 for reading. Note: if you find your computer too resource limited to manage that many threads, feel free to reduce that number appropriately. The threads that write to the file execute first, and the main program thread blocks in the mean- time. The threads that read from the file execute following all writer threads. Your job is to modify the two thread functions to implement synchronized reading and writing. Each writer thread will write a portion of the file at a designated offset into it using the pwrite system call. The reader threads will each read from the file at a similar file offset using the pread system call.

3 Synchronization This section describes the requirements for the thread functions for the reader and writer threads.

3.1 Writer Threads This is the function called by each writer thread.  Each thread will take the following steps:

1. Place the thread’s ID number into the char array named buffer, that is already declared. The resulting character string must be exactly five characters in length. For this, consider using sprintf with a format specifier. If you place a number after the % in the format specifier, that indicates the minimum field width. If the resulting string happens to be less than the width, it will be filled with blank spaces. Refer to the sample file contents in Section 3.3.

2. Call wait on the semaphore. Refer to the man page for sem wait.

3. Acquire the mutex lock. Refer to the man page for pthread mutex lock.

4. Do a pwrite of the char array buffer on the file at the current file offset. Write exactly DATA SIZE, i.e., 5, bytes. Refer to the man page for pwrite for parameters and return type.

5. Increment the file offset value by DATA SIZE number of bytes.

6. Release the mutex lock. Refer to the man page for pthread mutex lock.

7. Call signal on the semaphore. Refer to the man page for sem post. Steps 4 and 5 constitute the critical section of the thread. The mutex lock (and therefore exclusive write access) is needed to write to the file and modify the variable representing the total offset into the file shared by all threads. However, the semaphore, initialized to 5, allows up to five threads at a time to contend for the mutex lock. All others will block.

3.2 Reader Threads This is the function called by each reader thread. Each thread will take the following steps:

1. Call wait on the semaphore. Refer to the man page for sem wait.

2. Acquire the mutex lock. Refer to the man page for pthread mutex lock.

3. Do a pread of the file at the current file offset. Read exactly DATA SIZE, i.e., 5, bytes. Refer to the man page for pread for parameters and return type.

4. Decrement the file offset value by DATA SIZE number of bytes.

5. Release the mutex lock. Refer to the man page for pthread mutex lock.

6. Call signal on the semaphore. Refer to the man page for sem post.

7. Output the result of the pread. Steps 3 and 4 constitute the critical section of the reader thread. The mutex lock is needed to read from the file and modify the shared variable representing the total offset into the file, as with the writer threads. Note that all writer threads execute first, followed by the reader threads. Your program should not have both reader and writer threads executing concurrently.

3.3 Program Output Once the program terminates

support
Whatsapp
callback
sales
sales chat
Whatsapp
callback
sales chat
close