Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 5-1: Working with Pthreads - Strict Alternation c++ Assignment Instructions: You must use the Cloud9 environment for this assignment. Be sure to compile and

Assignment 5-1: Working with Pthreads - Strict Alternation c++

Assignment Instructions:

  • You must use the Cloud9 environment for this assignment.
  • Be sure to compile and test each program to be certain it works as expected. If you aren't sure how to compile and run a C++ program, refer to Slides 20 & 21 of the Cloud9SetUpSlides.
  • When a program is complete, refer to Slide 23 of the Cloud9SetUpSlides to see how to download your .cpp file.

Important notes:

  • At the top of your .cpp file, please include a comment with your full name. If your section uses Lightweight Teams, add the names of the teammates whom you worked with to the same comment.
  • Add your own individual comment for each function / major portion of code that you add, briefly explaining what that part does.
  • Make sure to organize and format your code in a consistent way.
  • If you refer to any online resource to understand a concept, see examples of the use of a particular syntax, etc., add a comment citing that resource (i.e., specify website name and link).
  • You must only submit .cpp files. If you have multiple .cpp files, upload them individually and not as a zip / compressed file.

Assignment Objectives:

  • Understand the functionality of the pthread_join function
  • Practice the use of the pthread_join function in a program
  • Apply the concept of mutual exclusion in the context of a C++ program
  • Better understand solutions to the mutual exclusion problem
  • Apply strict alternation to address the problem of mutual exclusion

Assignment Tasks:

The goal of the assignment is for you to practice and use functions related to POSIX threads or Pthreads creation & handling in Unix based OSs. An online version of the Linux manual can be found here:http://linux.die.net/man/.

For this activity, you will need to refer to the pthreads section of the Linux manual, available here:https://linux.die.net/man/7/pthreads

Another useful resource is the POSIX Threads Programming page at Lawrence Livermore National Laboratory by Blaise Barney [URL: https://computing.llnl.gov/tutorials/pthreads/]

If you need help with navigating the file system through a command line terminal, refer to this: http://linuxcommand.org/lc3_lts0020.php

Assignment Setup (0 points)

Note:

  • You need to use the bash shell to compile and run the program. Do not use the Cloud 9 GUI.

  1. You will need to download, compile, and execute a small program using your Cloud9 C++ environment.
  2. Type the following command into the terminal window to pull the project repository from GitLab:
  3. git clone https://cci-git.uncc.edu/jbahamon/ITSC_3146_A_5_1.git

  1. Change directory into the newly created directory (folder) named ITSC_3146_A_5_1
  2. Issue the following command to compile one of the programs:
  3. g++ pthread_join.cpp -o pthread_join -lpthread
  4. Issue the following command to execute the program:
  5. ./pthread_join

Note:

  • When compiling each of the programs in this assignment if you get the following error:

error: 'rand' is not declared in this scope

Simply add #include to the top of the file

Part 1: Waiting for a Thread to Terminate (5 points)

  1. Execute the pthread_join program several times.
  2. Examine the output carefully. You should notice a problem in the implementation. Make sure to follow the logic in main() and to read the comments carefully.
  3. Read the Linux manual page that describes the pthread_join function
  4. [URL: https://linux.die.net/man/3/pthread_join] and make sure that you understand the functionality that pthread_join provides and how to call (invoke) this function.
  5. Correct the problem. Look for the // TODO comment and address it (i.e., implement the functionality described in the comment). Hint: The solution requires only one (1) line of code.
  6. Build and run your program and make sure that it works correctly.
  7. Expected Output:
  8. Your program should produce the following output

Part 2: Pthread Data Sharing (5 points)

  1. A file named pthread-data-sharing-mutex.cpp
  2. has been provided to you in the same project.
  3. Compile the program and execute it several times, at least 10. Make sure to pay close attention to the output that the program produces.
  4. Create a Word or Google Docs document.
  5. In this document, answer the following questions about the program's behavior:
  6. What does it do?
  7. What output does it produce?
  8. Examine the program code carefully. Is the program functioning correctly?
  9. If you do not think that the program is working correctly,describe why?
  10. Include one screenshot of the program's output. Your choice.

Part 3: Strict Alternation (10 points)

  1. A file named pthread-data-sharing-mutex-strict-alternation.cpp has been provided to you in the same project.
  2. Compile the program and make sure that it executes.
  3. Examine the program code. Note that except for some minor changes, this program is identical to the one you used in Part 2 of this assignment.
  4. Modify the program to implement the strict alternation solution to achieve mutual exclusion (refer back to the relevant prep work video/slides if you need to; IMPORTANT NOTE: the outer, infinite while loop in the prep work video/slides is just an example and is not part of the strict alternation solution; only the empty while loop before the call to the critical region and the line immediately following the call to the critical region are part of the strict alternation solution).

Build and execute the updated program several times.

Please post code & output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions