logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

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.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

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, the contents of the file sharedfile.txt should look like this: 0000 0016 0032 0048 0064 0080 ... 9968 9984 4 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030 0031 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0065 0066 0067 0068 0069 0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 0081 0082 0083 0084 0085 0086 0087 0088 0089 0090 0091 0092 0093 0094 0095 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 Building Programs with Pthreads When building the program, be sure to add the linker option -lpthread so that the Pthread library is correctly linked to the executable program, i.e., gcc -o thread_sync thread_sync.c -lpthread 5 Deliverables Submit the C code source file for your program through the WorldClass assignment dropbox no later than the due date specified in the weekly assignment. Be sure to add your name to the header block of comments.

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

895 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

840 Answers

Hire Me
expert
Husnain SaeedComputer science

562 Answers

Hire Me
expert
Atharva PatilComputer science

653 Answers

Hire Me

Get Free Quote!

371 Experts Online