Mutex Locking Pthread Threads Thread Data Code

Description

Objective: learn about multi-threaded programming and locking shared variables. For this exercise you could re-use some parts of your previous assignment implementations. You will modify your assignment 2 countnames_parallel code to employ multi-threading with two threads (thread1 is to be assigned the first file and thread2 the second file passed as arguments……), thus creating a new program countnames_threaded. The functionality is the same as A2, but we will simplify things by assuming you expect exactly two input files of names. Your assignment 2 implementation may be a good place to start, but you want to remove the multi-processing parts (fork, wait, etc). If the user gives one or three or four files your program will print a suitable error message and exit. Thus, you will have two threads that update the same counts of names in parallel. For simplicity, assume you are dealing with at most 100 names of length at most 30 characters (same as assignments 1&2). You can re-use the same test case files.Nevertheless, you must use a data structure like what you used in assignment #5 (either a linked list or hash table), since locking an entire array will be slow. Your program has two threads in which thread one reads the first file , thread two the second file. Then they update a shared name counts data structure with the counts of names. You may use the code/a6 template code.One thread creates an object of type THREADDATA (using malloc) and assigns it to pointer p. In the end the same thread will deallocate the THREADDATA object. This THREADDATA object stores the thread that created the object. The purpose of this THREADDATA object is so you can track which thread is which for allocation and deallocation.You need to protect as a critical section the allocation of memory to p. Checking if p is NULL should tell you if it has been malloc’ed yet. If thread 1 after checking the value of p being NULL, but before it calls malloc, gets interrupted by thread 2 (that also checked the value of p), then thread 2 would also find it to be NULL and hence allocates some memory to p. When thread 1 is back, it would allocate more memory to p and we have memory leaking as we didn’t assume that both threads would allocate memory and the allocation by thread 2 has been forgotten.You also need to protect as a critical section the deallocation of p. If the thread doing the deallocation is interrupted after the deallocation but before setting p=NULL, then the other thread could try to do work with a non-NULL p and cause a segmentation fault (crash the program).Thread 1 should read names from one file and update the counts (you can re-use any code you want from previous assignments). Thread 1 continues reading names until the end-of-file is reached.Thread 2 does the same for the second input file. You need to protect as a critical section the updating of the name counts, to ensure T1 and T2 only access valid values. Thread 2 exits when reading the input file is complete.Each thread should print a log message to stdout when it opens a new file for reading. Each message printed to stdout should be prefixed with a log index, thread index, PID, and date and time (you may check the sample time.c provided). The log index gets incremented for each message printed to stdout (for each new file that is opened). An example of a typical message: “Logindex 1, thread 2, PID 5435, 17/04/2023 09:23:25 pm: opened file names2.txt”. All stdout messages printed from threads should look like the log messages with the same format. Ideally you should have a logprint(char *message) function for printing out the logindex lines with messages. An example is shown at the bottom.You need to share the incrementing log index between the two threads, similar to how they shared THREADDATA. Thus, you also need to protect as a critical section the parts where the two threads read and update (increment) the log message index. The log message index reflects which file is assigned to each thread, since it gets incremented at each fileopen, thus it could be used to index argv. You can do this similar to the protection you implemented for allocating and deallocating p of type THREADDATA. You can assign either thread to each file.When you use shared memory (a shared object) it is important to use mutex locking, such that the two threads don’t write/increment the log index at the same time; as well as to prevent one thread reading before the other has completed writing (updating) the index.As explained above, you also need locking when accessing the name counts, and for accessing THREADDATA object through pointer p.Thus, you have three critical sections and you will need three mutex locks.After both threads are done, the process (main function) will print out the contents of the counts of all names (same as you did for previous assignments 1&2).To start with, you can look at the TODOs in the code/a6 Download code/a6template code.You can compile your code with:gcc -D_REENTRANT -pthread -o countnames_threaded countnames_threaded.c -Wall -WerrorYou can use valgrind to ensure there are no memory leaks in your code.

We offer the bestcustom writing paper services. We have done this question before, we can also do it for you.

Why Choose Us

  • 100% non-plagiarized Papers
  • 24/7 /365 Service Available
  • Affordable Prices
  • Any Paper, Urgency, and Subject
  • Will complete your papers in 6 hours
  • On-time Delivery
  • Money-back and Privacy guarantees
  • Unlimited Amendments upon request
  • Satisfaction guarantee

How it Works

  • Click on the “Place Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
  • Fill in your paper’s requirements in the "PAPER DETAILS" section.
  • Fill in your paper’s academic level, deadline, and the required number of pages from the drop-down menus.
  • Click “CREATE ACCOUNT & SIGN IN” to enter your registration details and get an account with us for record-keeping and then, click on “PROCEED TO CHECKOUT” at the bottom of the page.
  • From there, the payment sections will show, follow the guided payment process and your order will be available for our writing team to work on it.