POSIX Pthreads
Credits
The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e-mail to: perrone[at]bucknell[dot]edu
Set Up
Follow the Github Classroom instructions to accept and set up the repo for the lab. Name the directory for this pre-lab ~/csci315/Labs/Lab3Prelab. You will will write a few simple programs for this pre-lab, all of which should be committed to your git repository. Remember, that you should be committing files to your local repo incrementally; that is, as you develop each program. This will help you recover files you might accidentally corrupt. Students have run into situations in which small modifications made to code that “nearly” works end up introducing some hard to track down error. Think of committing intermediate work that you feel brings you one step closer to the final solution. And, it goes without saying, that you must always commit and push the final solution, as well.
Read sections 1 through 5 in the POSIX Threads Programming tutorial, by Blaise Barney, from Lawrence Livermore National Laboratory. Read the source code examples you find and make a strong effort to understand how they work. Pay particular attention to the topics of thread creation and thread termination, how one can pass parameters to a thread, and how one can synchronize the termination of threads (join).
Problem 1 [20 points]
Write a program called char_threads.c, which creates three threads that never terminate. The general shape of all three threads is similar, what is particular to each one is the character they print. Here is the general structure:
while (1) {
- Print one character; the character to print depends on which thread it is:
- Thread 1 prints a digit followed by a newline character. In each iteration of the loop, the thread should print a digit in [0:9], starting with 0, then 1, 2, 3, …, and 9, repeating the sequence all over again.
- Thread 2 prints a letter followed by a newline character. The thread should cycle through the lowercase letters of the alphabet a to z, and cycle back to ‘a‘, repeating the sequence all over again.
- Thread 3 prints always the same character ‘#‘ followed by a newline character.
- After printing the one character, the thread computes the sum of all integers from 1 to 1,000,000. (You might need to adjust this number to make the results visible.) Do nothing with the result; we just want to the thread to spend some time doing CPU-bound work.
}
Create (or update) your Makefile to make sure the program compiles with the Makefile.
Answer the following questions using this Google Form.
- Do the threads’ order of execution match the order in which they were created?
- Looking at the characters that are printed to the standard output, do the threads always follow the same order of execution? That is, we want to know whether if you always see thread 1, then thread 2, then thread 3, etc., repeating in a predictable order.
When the above while loop works correctly, change the infinite loop to a limited loop so the autograding can review the output properly. Make it loop 10 times, i.e., change while(1) to while (i<10) and make sure you define the variable i and increment it by 1 each time through the loop.
Autograding note: Make note there should be *NO OTHER OUTPUT* beyond the threads’ digits, letters, and hashes (#).
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab3Prelab
- git pull
- git add char_threads.c
- git commit -m “Pre-lab 3.1 completed”
- git push
Problem 2 [10 points]
Write a program called mytime.c, which calls gettimeofday(2), passes the results to ctime(3), and then prints to the screen the resulting string. Whether you have used these functions before or not, take the time to read their man pages to understand what they do and how to use them.
$ ./mytime Thu Sep 7 21:49:33 2023
Update your Makefile to make sure the program compiles with the Makefile.
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab3Prelab
- git pull
- git add mytime.c
- git commit -m “Pre-lab 3.2 completed”
- git push
Note: In order to earn full credit in this pre-lab assignment, you must have pushed your solutions to your remote git repo before the start of you actual Lab 3 session.
Grading Rubric
Problem 1 [10 points total]
- [5 points] The char_threads.c program compiles correctly without any errors or warnings;
- [5 points] Reasonable output is generated by the program.
Problem 2 [10 points total]
- [5 points] The mytime.c program compiles correctly without any errors or warnings;
- [5 points] Correct output is generated by the program.
Observations [10 points total]
- [10 points] Describe your observations for Problem 1 and Problem 2 in the given Google Form (see link in the Problem 1 description).